KeyListener not responding to keyboard input

怎甘沉沦 提交于 2019-12-11 12:29:36

问题


I've been trying to learn more advanced java on my own (My class is only covering text files) and I'm stumped on the using KeyListener. I managed to get it to work in another program, but I can't find the problem here. No errors show up on the console. This program uses a robot to type predefined Strings in a text file. Here's the main class.

    import java.awt.AWTException;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;

    import javax.swing.SwingUtilities;


    public class FileTyper implements KeyListener {

static Keyboard kb;
static Scanner infile;
static boolean on = false;
static Window window;

public static void main(String args[]) throws AWTException, FileNotFoundException{
    init();
    start();
}
private static void init() throws AWTException, FileNotFoundException{
    window = new Window();
    kb = new Keyboard();
    kb.setSpeed(50);
    infile = new Scanner(new File("C:/Users/Ali/Desktop/input.txt"));

}
private static void start(){
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {

            if(on && infile.hasNext()){
                String temp = infile.nextLine();
                kb.type(temp);
                kb.type("\n");
            }
        }
    });
}

@Override
public void keyPressed(KeyEvent e) {

}

@Override
public void keyReleased(KeyEvent e) {
    switch(e.getKeyCode()) {
    case KeyEvent.VK_F9:
        System.out.println("CONSOLE: Starting");
        on = true;
        break;
    case KeyEvent.VK_F10:
        System.out.println("CONSOLE: Stopping");
        on = false;
        break;
    }

}

@Override
public void keyTyped(KeyEvent e) {

}

}


回答1:


  • For a KeyListener to work, you must first add it to a component via addKeyListener(...). You don't do this, and it won't work unless it has a chance to.
  • As camickr notes, a KeyListener requires that the component it listens to has focus.
  • Usually it's better not to use KeyListeners in Swing apps but to use Key Bindings.
  • Shoot, you don't even have a visible GUI of any kind at all, so you really need to do more studying of the tutorials to first get your gui up and running before even considering adding a KeyListener or use Key Bindings.

Edit
You state:

What if I wanted to use KeyListener when the program window is minimized?
I mean use a shortcut key to pause the start or stop the program

Core Java by itself cannot do this. For this to work, you either need to augment Java with JNI or JNA or use an OS-specific utility program. I've used AutoIt for my Windows apps.




回答2:


Not related to your problem, but don't use static methods and variables. This indicates a poor design.

If the KeyListener doesn't work then your component probably doesn't have focus.

Also, you actually need to add the KeyListener to your component. Start by reading the Swing tutorial on How to Write a Key Listener. The example should help you out and also show you a better way to design your program so you don't use static everywhere.



来源:https://stackoverflow.com/questions/19470110/keylistener-not-responding-to-keyboard-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!