2
votes

Is it possible to add hotkeys only when you're editing a certain NSTextField? For example, when you're in this text field I would like it if you pressed Command + 1 it inserted one string, and Command + 2 to insert another text string, etc, but only when you're editing that NSText field.

I tried adding a keyDown method to my subclassed NSTextField, but that didn't seem to ever get fired. If I changed it to keyUp it gets fired, but not if the command key is held down.

It seems when I'm searching for hotkeys the only information I find is on global hotkeys (ones that activate even when the app isn't visible) which isn't what I'm looking for.

3

3 Answers

2
votes

If you had a menu item with that keyboard shortcut, you could implement validateUserInterfaceItem: to enable it when your field is in the responder chain. What I'm not sure about is whether that would work for a hidden menu item.

1
votes

NSTextField's to not deal with keyDown: themselves. They instead seem to rely on intermediaries called Field Editors (found this info from a similar question here http://www.cocoabuilder.com/archive/cocoa/51299-custom-nstextfield-keydown.html).

Apple has some more info on the subject - http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/TextEditing/Tasks/FieldEditor.html

0
votes

I ended up subclassing my window and overriding sendEvent when my text field is the first responder.