Get modifierFlags in a keyDown event without pressing a key without a modifier with it!

I got a subclass of NSWindow in an NSDocument application to receive keyDown events.

I used the following code in my subclasses ...

- (void)keyDown:(NSEvent *)theEvent {

    NSLog(@"keyDown!");

    if ([theEvent modifierFlags] & NSAlternateKeyMask) {
        NSLog(@"Alt key Down!");
    }
    else
        [super keyDown:theEvent];
}

I get key events when keys are pressed without a modifier! I also get "Alt Key Down" when I press alt + z, for example (alt + non-modifierkey).

The problem is that I want to handle the event when only one alt / option key is pressed one, without a combination with other keys and -keyDown: is not called! What am I missing?

Thank...

+5
source share
2 answers

Alt/Option -flagsChanged: -keyDown:.

-(void)flagsChanged:(NSEvent*)theEvent {
    if ([theEvent modifierFlags] & NSAlternateKeyMask) {
        NSLog(@"Alt key Down (again)!");
    }
}
+7

:

-(void)flagsChanged:(NSEvent*)theEvent {
    [super flagsChanged:theEvent];
    if ((([theEvent modifierFlags] & NSDeviceIndependentModifierFlagsMask) & NSAlternateKeyMask) > 0) {
        NSLog(@"Alt key Down (again)!");
    }
}
0

All Articles