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

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.

Related

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.

AppleScript: Can I send a keypress ignoring a pressed modifier?

I've run into a rather specific problem where I would like to send a key press while having a modifier pressed without the modifier pressed.
Basically, I press Option+F4 and want to send Command+F, not Command+Option+F.
Can it be done?

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.

Press keys then type message in Textbox?

I was wondering if Visual Basic could emulate the pressing of keys.
I was thinking of a program that did this:
When I press a button (Button1) it would start a timer (Timer1), then press the V key on the keyboard and then type what was entered in a textbox (Textbox1), then press the Enter key. After doing that, repeating that action a second or two later, but saying what is in (Textbox2).
Could this be possible? If yes, please respond.
This will tell you how:
http://msdn.microsoft.com/en-us/library/ms171548(v=vs.80).aspx?cs-save-lang=1&cs-lang=vb
And use:
http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send.aspx?cs-save-lang=1&cs-lang=vb
For special keys.

cocoa: menu bar item with backspace as key equivalent

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)!))

Resources