Question

I have implemented the - (void)flagsChanged:(NSEvent *)theEvent method in my application. It works fine when I hold down the [alt] key. The method only sends when the key is pressed, but not when I let it go again. The - (void)keyUp:(NSEvent *)theEvent won't be called. Is there anyway to see when the [alt] key is released?

Here is the code:

- (void)flagsChanged:(NSEvent *)theEvent
{
    if([theEvent modifierFlags] & NSAlternateKeyMask)
    {
        NSLog(@"alt is down!!!");
    }
}
Was it helpful?

Solution

Okay. I really need to test the code a bit more. After logging the NSEvent, I noticed that the keyUp method is already implemented (thanks Jonathan Grynspan for pointing it out in the comment).

So now my code is:

- (void)flagsChanged:(NSEvent *)theEvent
{
    if([theEvent modifierFlags] & NSAlternateKeyMask)
    {
        NSLog(@"alt is down!!!");
    }
    else if([theEvent keyCode] == 58)
    {
        NSLog(@"alt is up!!!");
    }
}

This now works fine!

OTHER TIPS

Your code is broken for right alt. NSAlternateKeyNask is deprecated.

- (void)flagsChanged:(NSEvent *)theEvent {
    if ([theEvent modifierFlags] & NSEventModifierFlagOption) {
        NSLog(@"alt is down!!!");
    } else if ([theEvent modifierFlags] | NSEventModifierFlagOption) {
        NSLog(@"alt is up!!!!!");
    }
}

Note that these events will fire on any mask change, for example if you press alt and then cmd there will be 2 "key down" alt events.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top