How to properly handle <Return> + modifier keys in NSTextField?

I want to be able to handle three key combinations when the user edits NSTextField:

  • ⌘↩
  • ⇧↩

Each of these combinations will respond differently.

Ideally, I would like to achieve this only through IB. However, I understand that Cocoa only allows one submitted action for each control.

I can associate this dispatched action with IBActionand set NSTextFieldit to only be sent when the return key is pressed, but I cannot find a way to get which modifier keys are currently held (as the action accepts only (id)sender, not the event object). Is it possible?

As an alternative, I think it would be possible to subclass NSTextField, redefine keyDown:and manually start the corresponding actions. However, I can’t figure out how to connect it correctly in IB. (I could use it IBOutletfor a purpose, but I can't think of a way to connect a particular method.) I am also concerned about the effectiveness of this approach.

What is the normal way to handle certain key events on NSTextField?


The approach I am currently referring to is writing a user protocol and creating output in a subclass of a text field:

@property (weak, nonatomic) IBOutlet id<TextFieldReturnHandler> target

which can then be called from a keypress event. This seems really confusing, however.

+4
source share
1

:

- (IBAction)cellDidEndEditing:(id)sender;

, , , , , Action NSTextField IB, "Send On Enter Only" 'Send On End Editing'.

:

NSEvent *evt = [NSApp currentEvent];

, , -:

if (evt && evt.type == NSKeyDown) {
    evt.modifierFlags // NSShiftKeyMask, NSCommandKeyMas, NSAlternateKeyMask

    // Perform actions
}
+1

Source: https://habr.com/ru/post/1529500/


All Articles