Missing keyUp events on meaningful key combinations (e.g. "Select till beginning of line")
Asked Answered
W

3

5

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 important for the application, but the text field is still supposed to work as usual, by calling the [super ...] method). This is what the official documentation suggests for this problem.

I do receive most keyUp events while typing, but in certain cases I don't get one. It seems to happen, when pressing a key combination that has an action attached to it. E.g. Cmd-Shift-Left is not issuing an keyUp event. That input makes the whole line from point to the beginning appear selected, but already when the keyDown is received.

In those cases where it is missing, when looking at -performKeyEquivalent: by overriding it, I see this is called. Why is the keyUp not delivered?

Winterkill answered 22/10, 2010 at 23:5 Comment(0)
W
7

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.

Wondrous answered 23/10, 2010 at 8:20 Comment(1)
Thanks Jeremy, overriding NSApplication is exactly what I ended up using. It seems safe, since I only intercept and not change the default behaviour.Winterkill
H
11

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];
}
Hoick answered 8/3, 2013 at 12:39 Comment(0)
W
7

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.

Wondrous answered 23/10, 2010 at 8:20 Comment(1)
Thanks Jeremy, overriding NSApplication is exactly what I ended up using. It seems safe, since I only intercept and not change the default behaviour.Winterkill
D
1

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.

Discernible answered 31/10, 2014 at 0:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.