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 inNSText.h
as0x08
) and for the Forward Delete key, useNSDeleteCharacter
(defined inNSText.h
as0x7F
). Note that these are not the same characters you get from anNSEvent
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)!))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With