Java-检查控制键是否被按下
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11659801/
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
Java-check if control key is being pressed
提问by Mika
I have a Java function in which I want to test if the control key is being held down. How can I do that?
我有一个 Java 函数,我想在其中测试控制键是否被按住。我怎样才能做到这一点?
Edit: I am using swing for gui.
编辑:我正在使用 gui 摆动。
回答by ChrisSannar
use the "isControlDown()" boolean:
使用“isControlDown()”布尔值:
public void keyPressed (KeyEvent e)
{
System.out.println(e.isControlDown());
}
回答by Scott Izu
The code above works only if the only thing pressed is the control key. If they have ctrl and some other button is pressed (perhaps) accidently, it won't capture.
上面的代码仅在按下的唯一东西是控制键时才有效。如果他们有 ctrl 并且不小心按下了其他按钮(可能),则不会捕获。
You can check exactly for just the ctrl key
您可以只检查 ctrl 键
// Are just the CTRL switches left on
if(evt.getModifiers() == InputEvent.CTRL_MASK) {
System.out.println("just the control key is pressed);
}
When simulating multiple keys pressed, you use the or bit operator. To simulate holding both the left button and ctrl key, look for this.
模拟按下的多个键时,您可以使用或位运算符。要模拟同时按住左按钮和 ctrl 键,请查找此内容。
// Turn on all leftButton and CTRL switches
int desiredKey = InputEvent.BUTTON1_MASK | InputEvent.CTRL_MASK;
When checking if the ctrl key is down you may do this
检查 ctrl 键是否按下时,您可以这样做
// If we turn off all switches not belonging to CTRL, are all the CTRL switches left on
if((evt.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK) {
System.out.println("Control Key is pressed and perhaps other keys as well");
}
You can also check if both the left button and ctrl mask are pressed
您还可以检查是否同时按下了左按钮和 ctrl 掩码
// If we turn off all switches not belonging to leftButton or CTRL, are all the leftButton and CTRL switches left on
if((evt.getModifiers() & desiredKey) == desiredKey) {
System.out.println("left button and control keys are pressed and perhaps others as well");
}
Suppose you have this:
假设你有这个:
A | B
You should think of it like this. A has a control panel with a bunch of switches on. B also has a control panel with a bunch of switches on. The job of "| B" is to do the minimum work necessary to make sure all B's switches get turned on.
你应该这样想。A 有一个控制面板,上面有一堆开关。B 也有一个控制面板,上面有一堆开关。"| B" 的工作是做最少的工作,以确保所有 B 的开关都打开。
Suppose you have this:
假设你有这个:
A & B
The job of "& B" is to do the minimum work necessary to turn off any switches which aren't B's.
“& B”的工作是做最少的工作来关闭所有不是 B 的开关。
回答by Charlie Martin
It depends on several things.
这取决于几件事。
If you're running the Java program as a console program (text based) you have to test for approriate bits in the received chatracter.
如果您将 Java 程序作为控制台程序(基于文本)运行,您必须测试接收到的字符中的适当位。
Otherwise, you should look at InputEvents for the appropriate GUI classes, eg, http://docs.oracle.com/javase/6/docs/api/java/awt/event/InputEvent.html.
否则,您应该查看 InputEvents 以获取适当的 GUI 类,例如http://docs.oracle.com/javase/6/docs/api/java/awt/event/InputEvent.html。
Have a look at this tutorial: http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html
看看这个教程:http: //docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html
回答by Mika
I found one solution which solves my problem: I declare a global variable
我找到了一个解决我的问题的解决方案:我声明了一个全局变量
boolean controlStatus=false;
Then in the event for keyPressed on the jTextField:
然后在 jTextField 上的 keyPressed 事件中:
if(evt.getKeyCode()==KeyEvent.VK_CONTROL)
controlStatus=true;
In the event for keyReleased:
在 keyReleased 事件中:
if(evt.getKeyCode()==KeyEvent.VK_CONTROL)
controlStatus=false;
Then I can access the global variable to check if the control key is being held down.
然后我可以访问全局变量来检查控制键是否被按住。