Pregunta

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

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

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

How do I test for only the optionkey and nothing else?

¿Fue útil?

Solución

Like this:

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

Otros consejos

You can also try

NSUInteger modFlags = [theEvent modifierFlags];
if ((modFlags & NSCommandKeyMask) && !(modFlags & ~NSCommandKeyMask))
{
    // Only alt was pressed
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top