Search code examples
cocoaosx-snow-leopardosx-leopardnsevent

NSEvent modifier flags not updating until mouse moves


In Mac OS X 10.6, NSEvent has a +modifierFlags class method to determind the currently pressed modifier flags. In 10.5, using [[NSApp currentEvent] modifierFlags] only updates after mouse move. Is there any way to asynchronously get the modifier flags?


Solution

  • According to a comment on this blog post, you can use CGEventCreate() and CGEventGetFlags().

    CGEventRef event = CGEventCreate(NULL /*default event source*/);
    CGEventFlags mods = CGEventGetFlags(event);
    if (mods & kCGEventFlagMaskShift)
        NSLog(@"Shift key is being pressed");
    

    The modifier flags returned by CGEventGetFlags() (CGEventFlags) are the same as the NSEvent ones.