Missing keyUp events on meaningful key combinations (e.g. “Select till beginning of line”)

后端 未结 3 490
面向向阳花
面向向阳花 2020-12-11 20:28

I have an NSTextField which uses as an extended NSTextFieldCell, which creates a custom field editor, that intercepts and records key events. (Knowing the key events is impo

相关标签:
3条回答
  • 2020-12-11 21:13

    This is just the way the event architecture is set up. Sending key equivalent messages is preferred to sending messages for the various keys that are part of them. See "Handling Key Events," in particular, "Handling Key Equivalents." It looks like you could subclass NSApplication and override -sendEvent: to dispatch these events however you'd like, but you'd likely break more functionality than you'd add.

    0 讨论(0)
  • 2020-12-11 21:17

    For others, this is the code to "fix" it:

    - (void)sendEvent:(NSEvent *)event {
        if ([event type] == NSKeyUp && ([event modifierFlags] & NSCommandKeyMask))
            [[self keyWindow] sendEvent:event];
        else
            [super sendEvent:event];
    }
    
    0 讨论(0)
  • 2020-12-11 21:31

    To keep the normal application behavior and add your own (minimising the chances of unanticipated side effects), notifications are are a possibility:

    - (void)sendEvent:(NSEvent *)theEvent
    {
        [super sendEvent:theEvent];
        if (theEvent.modifierFlags & NSCommandKeyMask) {
            if (theEvent.type == NSKeyUp) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"myKeyUp" object:theEvent];
            }
            if (theEvent.type == NSKeyDown) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"myKeyDown" object:theEvent];
            }
        }
    }
    

    Then add an the appropriate object(s) as observer(s) for the notifications.

    0 讨论(0)
提交回复
热议问题