KeyListener不响应键盘输入 [英] KeyListener not responding to keyboard input
问题描述
我一直在尝试自己学习更高级的Java(我的课程仅涵盖文本文件),而我对使用KeyListener感到困惑.我设法使其在另一个程序中工作,但在这里找不到问题.控制台上没有错误显示.该程序使用机械手在文本文件中键入预定义的字符串.这是主班.
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) {
}
}
推荐答案
- 要使KeyListener正常工作,必须首先通过
addKeyListener(...)
将其 添加 到组件中.您不这样做,除非有机会,否则它将不起作用. - 正如camickr所述,KeyListener要求其侦听的组件具有焦点.
- 通常最好不要在Swing应用程序中使用KeyListener,而应使用 Key绑定.
- 射门,您甚至根本没有任何可见的GUI,因此,您甚至需要考虑对教程进行更多研究,以首先使您的gui正常运行,甚至不考虑添加KeyListener或使用键绑定.
- 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:
如果在最小化程序窗口时想使用KeyListener怎么办?
我的意思是使用快捷键来暂停启动或停止程序
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本身无法执行此操作.为此,您需要使用JNI或JNA扩充Java或使用特定于操作系统的实用程序.我已经在Windows应用程序中使用了AutoIt.
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.
这篇关于KeyListener不响应键盘输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!