cocoa: menu bar item with backspace as key equivalent - macos

I would like to set the key equivalent of a menu bar item in IB to the backspace key (delete left of the cursor), but for some reason this doesn't seem to work. I can assign all kinds of keys to it, such as CMD+Backspace, or fn+Backspace (delete key, ie delete from right), but when I assign the plain backspace key, it just doesn't register at all.
Do I have to do something different to use the backspace key?
PS: I do not want to handle the key any other way. It must be a menu item key equivalent.
EDIT: I set the key equivalent for the menu item using IB. I know I said that before, but some people can't read. I know how to set it in IB, and it works for everything but the backspace key.

Please refer to the description of setKeyEquivalent: method in the NSMenuItem Class Reference:
If you want to specify the Backspace key as the key equivalent for a menu item, use a single character string with NSBackspaceCharacter (defined in NSText.h as 0x08) and for the Forward Delete key, use NSDeleteCharacter (defined in NSText.h as 0x7F). Note that these are not the same characters you get from an NSEvent key-down event when pressing those keys.
By the way, I can normally set the Key Equivalent for menu items to backspace in Interface Builder by pressing it and it worked in my sample app (created from Xcode template, set key equivalent of File-Close to backspace). I am using Xcode 4.2 on Snow Leopard. Are there any key binding conflicts in your scenario?

This is old, but I had the same problem and it was killing me. Turns out when I was setting the keyDown handler, I was not calling super.keyDown(theEvent) at the end. Somehow multiple-key shortcuts (like cmd+delete) still fired the events, but the single-character backspace key did not.
This fixed it:
override func keyDown(theEvent: NSEvent) {
...
super.keyDown(theEvent)
}

Use "\u{08}" for key equivalent string, it works
menu=NSMenuItem(title: "Delete Note", action: nil,
keyEquivalent: "\u{08}")
I find this from
let menu=window!.menu
print(menu?.item(at: 1)?.submenu?.item(at: 1)?.keyEquivalent)//set Delete key from xib file
also NSBackspaceCharacter has keycode 8
menu=NSMenuItem(title: "Delete Note", action: nil,
keyEquivalent: String(Unicode.Scalar(NSBackspaceCharacter)!))

Related

How to remove key binding for Jump To Next/Previous Counterpart in Xcode?

In most key bindings a small minus sign appears on the right to remove the binding, but with Jump to Next/Previous Counterpart it seems it is not possible to clear that one.
I want to use that key binding for a different command and I can't do it without removing it first.
When trying to delete those a small error message appears that says:
Can't delete the keyboard shortcut because it has alternate menu items
Any idea on how to get rid of those shortcuts?

NSLeftTextMovement and NSRightTextMovement

On what action do the NSTextEndEditing notifications contain NSLeftTextMovement & NSRightTextMovement? I was trying to catch left/right/up/down key press inside a text field, but the NSTextMovement key value is always either Tab/BackTab or Return.
This will not work to catch arrow key presses in a text field.
The NSTextDidEndEditingNotification is only sent when focus leaves the field editor. So, NSLeftTextMovement would only be used if/when a press of the left arrow press actually caused focus to leave the field. That's not something that normally happens. I don't know if it might happen when using a matrix of text fields or an NSForm. One could arrange to use a custom field editor and have that end editing on an arrow key press, too, I suppose.

NSMenuItem Key Equivalent set to Enter key from Number Pad

I want to set the Key Equivalent on an NSMenuItem to be the number pad's Enter key. I don't have a 10-key number pad on my desktop, but I was told that Function+Return on the main keyboard would emulate the Number Pad Enter key. However, when I try to do that in Interface Builder in the Key Equivalent field, my cursor just jumps to the next field.
How can I set, in Interface Builder, the number pad's Enter key to the Key Equivalent on an NSMenuItem?
If you'd like to provide the programmatic way to do it in the comments, that's fine and appreciated, but for full Answers to this question, please restrict them to doing it in Interface Builder.
It turns out that using Function+Return on my normal keyboard DID work to put the Keypad Enter key as the key equivalent. I went and got an extended physical keyboard that had a keypad. When I hit the Keypad Enter key in the Key Equivalent field, I got the same behavior I got when I hit the Function+Return. I tested it within the app and using Function+Return to set the key equivalent DID successfully bind that Menu Item to the Keypad Enter key... it's just that Xcode didn't SHOW that any key was really bound.

How to dismiss an OS X modal sheet with *either* ENTER or ESC key

My application launches a modal sheet that has several buttons. One of them is the default button (key equivalent \r) and pressing the ENTER key on the keyboard dismisses the sheet, as expected. I would like to have the same effect to also be achieved if the user presses the ESC key. So either ENTER or ESC should be the key equivalent for the button. How can this be achieved?
See -[NSResponder cancelOperation:], which is automatically bound to the escape key:
This method is bound to the Escape and Command-. (period) keys. The key window first searches the view hierarchy for a view whose key equivalent is Escape or Command-., whichever was entered. If none of these views handles the key equivalent, the window sends a default action message of cancelOperation: to the first responder and from there the message travels up the responder chain.
In other words, you can handle the escape key by implementing -cancelOperation: somewhere in your app's responder chain, such as the window controller.

User assigned key equivalents

I'm working on a Status Bar App. I'd like to allow the user to modify the menu item key equivalents to their own preferences. I've seen this done before it's a pretty common feature.
A prefs window usually has an area with textfields where the user enters their keyboard shortcut for specific menu items.
How does one setup the textfield so that it displays the modifier key fonts?
The default NSTextfield ignores modifiers.
Also I have yet to find an example project showing this functionality, if anyone has a link that would be very helpful.
You may wish to take a look at Shortcut Recorder which allows the user to record key equivalents using modifiers and then for you to retrieve them and set them for the NSMenuItem.
Once the user has recorder their shortcut/key combination you can access the SRRecorderControl's objectValue property which has values for the key code and modifier flags.
https://github.com/Kentzo/ShortcutRecorder
The modifier keys all have Unicode values that you can see in the "Special Characters" palette and they are displayed by the normal system font. Most of them are in the "Technical Symbols" category of the palette but the up-arrows for the Shift and Caps Lock keys are in the "Arrows" section.
You can insert these values when editing label elements in Interface Builder or Xcode, or include their corresponding Unicode values when creating something like an NSString object.
For example, the Shift key's UTF-8 sequence (as shown in the palette) is E2 87 A7 so one way to set it programmatically is to add those bytes to an array and create an NSString from the UTF-8 array. I prefer the array approach because it's "bulletproof"; it will always be interpreted as UTF-8 and do what you expect. If you try to insert characters directly into #"" or CFSTR() strings, you then have to make sure your source file's encoding is correct (and older versions of Xcode wouldn't even allow this, they'd assume ASCII only).

Resources