Obtaining modifier key pressed in CGEvent tap

筅森魡賤 提交于 2019-12-05 05:36:44

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/4420995/obtaining-modifier-key-pressed-in-cgevent-tap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!