0

How would one, once they had set up a Keylistener or Keyadapter class and had overridden keyPressed and the other two, get the keyboard response in their program working? For example, say I have my Keyadapter class all set up:

public class KeyInput extends Keyadapter {

public void keyPressed (KeyEvent e) {

    int key = e.getKeyCode();

    if (key == VK_W) {
        System.out.println("You pressed W");

    }

}

}

How would I go from there, to make it so it would actually detect it and display the string whenever I would press W? There aren't any methods that I could call in the main method to make it start checking, are there? If not how does the JVM know to start checking if there isn't a method in main to tell it to?

Thank you for your time.

1 Answers1

0

KeyAdapter is a java awt class. Your KeyInput (keylistener) has to be registered with another awt component to start listening to key events.

An example of what you are looking for can be found here

JButton button = new JButton("Clear");
button.addActionListener(this);

typingArea = new JTextField(20);
typingArea.addKeyListener(new KeyInput());
moh ro
  • 86
  • 8