0

Not sure if I asked the question right but I am using keylistener for keyboard inputs in keyboard.java. I want to call the keyboard.java file in the display.java (main class) so that when I run the display class, I press a key and print something out to test it. Here is the code for the keyboard.java file:

public class Keyboard implements KeyListener {

    @Override
    public void keyTyped(KeyEvent event) {
        
    }

    @Override
    public void keyPressed(KeyEvent event) {
        int key = event.getKeyCode();
        if(key == KeyEvent.VK_E) {
            System.out.println("E pressed");
        }
    }

    @Override
    public void keyReleased(KeyEvent event) {

    }

}

I already typed in private Keyboard keyboard; into the display class but nothing is happening when I press the E key. I need help and I am using Eclipse (Im new to java).

Tobias
  • 2,547
  • 3
  • 14
  • 29
  • 3
    A KeyListener is meant to be used with the [Swing library](https://docs.oracle.com/javase/tutorial/uiswing/index.html). Something in your code would have to generate events to call the methods in the KeyListener. – Gilbert Le Blanc Feb 21 '21 at 07:32
  • Gilbert has provided a link to the `Swing` tutorial. That is (IMO) the best place about how to handle key events. If you are using JavaFX ... https://docs.oracle.com/javafx/2/events/handlers.htm. For a command line app it is [difficult](https://stackoverflow.com/a/17083212/139985). – Stephen C Feb 21 '21 at 07:49

0 Answers0