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的开关所需的最少工作。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值