I've been searching everywhere for how to take input from the keyboard. I can find a ton of resources for how to modify what keylistener does on key press, I can find how to add it to a swing text line, and plenty of other miscellaneous things about it. But I can't find out how to actually put it in a file and use it.
For now I just want to have a main class whose main function just has an endless loop, and everytime you press a key it prints "Key _ has been pressed". How do I do this?
I've made a MyKeyListener that extends KeyAdapter that includes the system.out.print of the string under keyPressed (I assume this is workable). But what do I put in the main class to actually use it? Including it and initializing isn't enough.
Here is my key listener file : `
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class GKeyListener extends KeyAdapter {
public void keyPressed(KeyEvent e){
System.out.println("Key Pressed: " + e.getKeyChar());
}
public void keyReleased(KeyEvent e){
System.out.println("Key Released: " + e.getKeyChar());
}
public void keyTyped(KeyEvent e){
System.out.println("Key Typed: " + e.getKeyChar());
}
Here is a main file im working with and don't know where to go from: import java.awt.KeyEventDispatcher; import java.awt.KeyboardFocusManager; import java.awt.event.KeyEvent;
public class tempMain {
public static void main(String[] args){
while(true){
GKeyListener listen =new GKeyListener();
//addKeyListener(listen);
}
}
The KeyListener
is a class of the Java AWT GUI framework. It is part of the AWT event handling system, eg used to capture key events of text input components (such as TextArea, TextField etc.).
Without a component, it doesn't make much sense to use the KeyListener.
If you want to do some experiments, you can capture key input without a text input component, using the KeyboardFocusManager
. This example shows both, binding a KeyListener
to a text component and using the global KeyboardFocusManager
.
public class KeyExample extends JFrame {
public KeyExample() {
JTextField jTextField = new JTextField(20);
jTextField.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
System.out.println("KeyListener: " + e.getKeyChar());
}
});
add(jTextField);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
@Override
public boolean dispatchKeyEvent(KeyEvent e) {
System.out.println("KeyEventDispatcher: " + e.getKeyChar());
return false;
}
});
new KeyExample().setVisible(true);
}
}
You should use the KeyboardFocusManager
only in rare situations. Normally, handling events using the KeyListener
should be enough.
What this code does:
public class tempMain {
public static void main(String[] args){
while(true){
GKeyListener listen =new GKeyListener();
//addKeyListener(listen);
}
}
is repeatedly create a KeyListener, thousands of times ! It does nothing with the KeyListener of use, and again creates thousands of KeyListeners when only one is necessary. Instead you should consider the following:
Edit
You state in comment,
but without adding a text field I'm not sure how to actually call the listener to actually exist and be working
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.