Adam Meyer
Adam Meyer

Reputation: 1525

cocoa global watch for modifier key press

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?

Upvotes: 1

Views: 830

Answers (1)

pkamb
pkamb

Reputation: 34983

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!");
    }
}

Upvotes: 0

Related Questions