cocoa: menu bar item with backspace as key equivalent
Asked Answered
A

3

7

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.

Aesculapian answered 26/4, 2012 at 4:13 Comment(2)
This was 3 years ago. Relax.Aesculapian
I have the exact same problem - more than six years after the OP. No conflicts, and all other key equivalents work - just not backspace (neither in IB, nor setting it programmatically.) Which is weird, since NSBackspaceCharacter is explicitly mentioned in the docs.Lavelle
G
14

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?

Grannia answered 26/4, 2012 at 4:39 Comment(1)
I am doing this in interface builder, and I did try to set the key equivalent there, but it did not work. Does anyone know what could be preventing it from binding?Aesculapian
S
4

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)!))
Sinistral answered 2/8, 2020 at 7:56 Comment(0)
D
2

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)
}
Decomposition answered 12/4, 2016 at 19:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.