在 Java 中处理 KeyEvents
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/684526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dealing with KeyEvents in java
提问by Azreal
Say I have a GUI, and I want the program to actually run when the spacebar is pressed, but if the spacebar is pressed again then I want the program to exit. Would something like this work?
假设我有一个 GUI,我希望程序在按下空格键时实际运行,但是如果再次按下空格键,我希望程序退出。这样的东西会起作用吗?
public class MouseClicker extends JApplet implements KeyListener{
int counter = 0;
MouseClicker m1 = new MouseClicker();
//all of the other methods
public void keyPressed( KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode==KeyEvent.VK_SPACE){
m1.click();
counter ++;
if(counter%2==0)
System.exit(0);
}
//other methods needed for KeyListener
}
回答by David Z
Try it and see ;-)
试试看;-)
Seriously though, what do you mean you want to program to run when the space bar is pressed? Unless the program is already running, how are you going to receive the KeyEvent?
说真的,你的意思是你想让程序在按下空格键时运行?除非程序已经在运行,否则您将如何接收KeyEvent?
As for the other half of your question, the code you have should, in general, make Java exit when the space bar is pressed. Note that there's no point in using a counter, since as soon as Java exits, the value of the counter is lost. Also note that JAppletis an exception to the "in general"... you normally can't call System.exitfrom an applet, because the applet runs under the control of a browser and Java is only supposed to exit when the user closes the browser, not whenever your applet is done. There could be other applets running in the same JVM and they might not be done with what they're doing yet.
至于问题的另一半,您拥有的代码通常应该在按下空格键时让 Java 退出。请注意,使用计数器没有意义,因为一旦 Java 退出,计数器的值就会丢失。另请注意,这JApplet是“一般情况”的一个例外……您通常无法System.exit从小程序调用,因为小程序在浏览器的控制下运行,而 Java 仅应在用户关闭浏览器时退出,而不是每当您的小程序完成时。可能有其他小程序在同一个 JVM 中运行,它们可能还没有完成它们正在做的事情。
回答by Bill K
Probably, I think the AWT code is correct but the rest is terribly cryptic (Not that I don't understand it, but it doesn't express what you are actually doing very well, does it?)
可能,我认为 AWT 代码是正确的,但其余部分非常神秘(不是我不明白,但它并没有很好地表达您实际上在做什么,是吗?)
How about:
怎么样:
boolean running=false;
...
if(running)
System.exit(0);
else
running=true;
instead?
反而?
Update: After reading your discussion on the other post, you are going to have a little strangeness.
更新:阅读您在另一篇文章中的讨论后,您会感到有些陌生。
There is this concept of the AWT thread. Everything done with the AWT must be done on that thread. So when the space bar is pressed, you will get that event on the AWT thread. You probably don't want to keep it though because as long as you hold onto it, it will prevent other graphics updates.
AWT 线程有这个概念。使用 AWT 完成的所有操作都必须在该线程上完成。因此,当按下空格键时,您将在 AWT 线程上获得该事件。你可能不想保留它,因为只要你坚持它,它就会阻止其他图形更新。
I suggest looking at SwingTimer Set a swing timer to notify you every few ms, and that notification will come in on the AWT thread so that you can use it to click your button, then just exit and wait for the next timer event.
我建议查看 SwingTimer 设置一个摆动计时器以每隔几毫秒通知您一次,该通知将进入 AWT 线程,以便您可以使用它来单击您的按钮,然后退出并等待下一个计时器事件。
When they press space bar again, system.exit() should work fine.
当他们再次按下空格键时,system.exit() 应该可以正常工作。

