1
votes

Using addGlobalMonitorForEventsMatchingMask, I can check for keys being pressed. And when an event is fired, I can check the modifier flags to see if any modifiers were used in conjunction.

But I need to know if a modifier is pressed without another key being pressed.

How would I do this?

1
Did you try passing NSFlagsChangedMask to addGlobalMonitorForEventsMatchingMask:handler:?JWWalker

1 Answers

0
votes

You're looking for events of type kCGEventFlagsChanged:

CGEventMask eventMask = (1 << kCGEventFlagsChanged);
CFMachPortRef eventTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault, eventMask, cgEventCallback, NULL);

...


CGEventRef cgEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef cgEvent, void *refcon)
{
    NSEvent *event = [NSEvent eventWithCGEvent:cgEvent];
    if (event.type == kCGEventFlagsChanged) {
        NSLog(@"modifier key!");
    }
}