开发者

get modifierFlags in keyDown event without pressing non-modifier key along with it!

I have subclassed NSWindow in a NSDocument application in order to receive keyDown events.

I have used the fol开发者_StackOverflowlowing code in my subclasss...

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

    NSLog(@"keyDown!");

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

I'm receiving key events when non-modifier keys are pressed! I'm also receiving "Alt Key is Down" when I press alt+z for example (alt+non-modifierkey).

The issue here is that I want to handle the event when just the alt/option key is pressed alone, without in combination with other keys and -keyDown: does not get called! What am I missing ?

Thanks...


You could catch the Alt/Option key alone in -flagsChanged: instead of -keyDown:.

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


You can do like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜