java 判断哪个键按下

时间: 2024-11-10 admin IT培训

java 判断哪个键按下

java 判断哪个键按下

上面的代码只有在按下的唯一内容是控制键时才有效。如果他们有ctrl和其他一些按钮(可能)意外按下,它将无法捕获。

您可以完全检查ctrl键

// Are just the CTRL switches left on

if(evt.getModifiers() == InputEvent.CTRL_MASK) {

System.out.println("just the control key is pressed);

}模拟按下的多个键时,使用或位运算符。要模拟同时按住左键和ctrl键,请查找此项。

// Turn on all leftButton and CTRL switches

int desiredKey = InputEvent.BUTTON1_MASK | InputEvent.CTRL_MASK;检查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");

}您还可以检查是否按下了左按钮和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");

}假设你有这个:

A | B你应该这样想。 A有一个控制面板,上面有一堆开关。 B还有一个控制面板,上面有一堆开关。 “| B”的工作是做必要的最小工作,以确保所有B的开关都打开。

假设你有这个:

A & B“& B”的工作是完成关闭任何不是B的开关所需的最少工作。