0

I am trying to make it so that instead of hitting the enter key in my java program to make the enter action occur, I can instead hit the enter key while a custom method is running. At this point, I have looked into Hashmaps but am pretty confused if this going to do what I want. Any help would be greatly appreciated. It seems like this should be an easy thing to do, but for some reason I am just not getting a solution to it.

So, I am thinking my code will be something like this. Essentially, I am making a game of Hot Potato and I need to have the players take turn entering a character (e, d, and c for team 1 and o, k, and n for team 2). This will be involving a GUI interface as well. A have a while loop that will end once a timer reaches zero. What I would like is for the players to be able to put in a letter into a JTextField in the GUI and then simply press spacebar (as they will be sharing a keyboard). Once they enter the right letter (I made it randomized), they can hit spacebar and they will have "tossed the potato" to the other player.

public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_SPACE)
        {
            KeyEvent.VK_ENTER;
        }
    }

I am working all of this out of the actionPerformed(ActionEvent ev) method. The fuller code looks like this. The if statement is commented out because I would like the game to start when the main button is pressed, but right now I get a ton of compilation errors when I uncomment the JButton and have it working with the String text that is the dialogue from the text you enter into a JTextField.

public void actionPerformed(ActionEvent ev) {
    Object eventSource = ev.getSource();
    String text = entryText.getText(); // text entered into JText
    //JButton eventButton = (JButton) eventSource;


    System.out.println(text);

    //if (eventButton.equals(main))
    {
        int totalTime = 1000*(HotPotato.randNum());
        long startTime = System.currentTimeMillis();
        System.out.println(totalTime/1000);
        Boolean p1Start = false;
        String input = "";

            while (System.currentTimeMillis() - startTime <= totalTime)
            {
                p1Start = !p1Start; 
                char a = HotPotato.randLetterBoth(p1Start);
                String aString = String.valueOf(a);
                while (!input.equals(aString))
                {
                    System.out.print(aString); // temporary, shows test letter
                    input = theKeyboard.next();

                    public void keyPressed(KeyEvent e) {
                        if (e.getKeyCode() == KeyEvent.VK_SPACE)
                        {
                            KeyEvent.VK_ENTER;
                        }
                    }


                }
            }   

            if (p1Start)
            {
                System.out.print("Team 2 Wins!");
            }
            else
            {
                System.out.print("Team 1 Wins!");
            }

            setLabels();
            myPicture.makeHotPotatoOn(myHotPotato.state());
            myPicture.repaint();

    }
}
jakob
  • 103
  • 4
  • Welcome to SO! In order to help you, we need a little more information about how you are accepting input. Please read: [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) Further, It seems like you have an [XY problem](http://meta.stackexchange.com/a/66378/340590). Why do you want to map the spacebar to another input? Why not check for a spacebar input? A hashmap is not what you're looking for, it refers to a different type of "mapping" than you mean. – ti7 Dec 09 '16 at 07:57
  • Thank you for the welcome! I added my code and more description of the problem, including my reasons for why I wanted the spacebar to act as the enter key when inside a While loop. – jakob Dec 11 '16 at 01:40
  • You may use Robot class. Refer to this answer. http://stackoverflow.com/questions/18169598/how-can-i-programmatically-generate-keypress-events – Azman K. Dec 11 '16 at 02:21
  • Thank, I do see how this is helpful, but unfortunately I am not sure how I can implement this. I have made it so that I can access those keys now, but I need a way to be able to type in a letter into a TextField (lets say the letter "a"), but then I need the keyboard to always be on alert so that when you hit "a" and then "spacebar", the letter "a" is entered into the game. – jakob Dec 11 '16 at 03:40

0 Answers0