cocoanseventmodifier-key

NSEvent: test for only one modifier key


Ok, I understand how to determine from an NSEvent if a modifier key is pressed:

if ([theEvent modifierFlags] & NSAlternateKeyMask) {
   // The Option/Alt key was pressed
}

But this also captures the option key and another modifier key at the same time, eg Option+Shift, or any combination with the option key.

How do I test for only the option key and nothing else?


Solution

  • Like this:

    const NSUInteger kNotAlt = NSAlphaShiftKeyMask | NSShiftKeyMask | NSControlKeyMask | NSCommandKeyMask;
    NSUInteger modFlags = [theEvent modifierFlags];
    if (((modFlags & NSAlternateKeyMask) != 0) &&
        ((modFlags & kNotAlt) == 0))
    {
        // Only alt was pressed
    }