I am running into problems trying to use KeyListener in java.
I am trying to write a program that runs until a key is pressed, and then outputs that key.
This is a stepping stone to more elaborate code, so the use of a method to print the key pressed is just being used as a kind of prototype.
Here is the code:
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class keylistener implements KeyListener{
public keylistener(){
addKeyListener(this);
}
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
keylistener output = new keylistener();
output.print(key);
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public void print(int key){
System.out.println(key);
}
public static void main(String[] args){
}
}
The program runs and then ends directly after.
I have never used KeyListener before, and I cannot figure out how to make the program wait for the key to be pressed.