在CGEvent点击中获取修改键 [英] Obtaining modifier key pressed in CGEvent tap

查看:143
本文介绍了在CGEvent点击中获取修改键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置事件点击,我无法确定给定CGEvent的修饰符键。

  CGEventFlags flagsp指向; 

flagsP = CGEventGetFlags(event);

NSLog(@flags:0x%llX,flagsP);
NSLog(@stored:0x%llX,kCGEventFlagMaskCommand);

if(flagsP == kCGEventFlagMaskCommand){
NSLog(@command);
}

鉴于上述代码段,第一个NSLog从第二个NSLog返回一个不同的值。当命令修改键被按下时,条件不会被触发,不要惊讶。



我需要确定是否按照给定的命令,备用,选项,控制或移位CGEvent。首先,我需要帮助才能理解为什么上述不起作用。



谢谢!

解决方案

这些是位掩码,它们将被逐个或并入到从 CGEventGetFlags (或自己创建一个事件时传递)的值。



由于没有单个位掩码将不等于多个位掩码的组合,因此您无法测试此处的相等性。您需要测试单个位的相等性。



要从组合位掩码中提取单个位掩码的值,请使用按位AND( & )运算符。然后,与您感兴趣的单位掩码进行比较:

  BOOL commandKeyIsPressed =(flagsP& kCGEventFlagMaskCommand)== kCGEventFlagMaskCommand; 

为什么两个?



<$在这种情况下,c $ c>& 表达式的计算结果与其操作数相同,即 CGEventFlags ,这可能不适合大小一个 BOOL ,这是一个已签名的char == 表达式将其解析为1或0,这将适用于 BOOL



该问题的其他解决方案包括否定值两次( !! )并将变量声明为 bool _Bool 而不是布尔值 BOOL 。当你包含stdbool.h时,C99的 _Bool 类型(同义为 bool )强制其值为1或0,就像 == !! 解决方案一样。


Having setup an event tap, I'm not able to identify what modifier key was pressed given a CGEvent.

 CGEventFlags flagsP;

 flagsP=CGEventGetFlags(event);

 NSLog(@"flags: 0x%llX",flagsP);
 NSLog(@"stored: 0x%llX",kCGEventFlagMaskCommand);

 if (flagsP==kCGEventFlagMaskCommand) {
       NSLog(@"command pressed");
    }

Given the above snippet, the first NSLog returns a different value from the second NSLog. No surprise that the conditional is never triggered when the command modifier key is pressed.

I need to identify whether command, alternate, option, control or shift are pressed for a given CGEvent. First though, I need help to understand why the above isn't working.

Thanks!

解决方案

These are bit masks, which will be bitwise-ORed together into the value you receive from CGEventGetFlags (or pass when creating an event yourself).

You can't test equality here because no single bit mask will be equal to a combination of multiple bit masks. You need to test equality of a single bit.

To extract a single bit mask's value from a combined bit mask, use the bitwise-AND (&) operator. Then, compare that to the single bit mask you're interested in:

BOOL commandKeyIsPressed = (flagsP & kCGEventFlagMaskCommand) == kCGEventFlagMaskCommand;

Why both?

The & expression evaluates to the same type as its operands, which is CGEventFlags in this case, which may not fit in the size of a BOOL, which is a signed char. The == expression resolves that to 1 or 0, which is all that will fit in a BOOL.

Other solutions to that problem include negating the value twice (!!) and declaring the variable as bool or _Bool rather than Boolean or BOOL. C99's _Bool type (synonymized to bool when you include stdbool.h) forces its value to be either 1 or 0, just as the == and !! solutions do.

这篇关于在CGEvent点击中获取修改键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆