How do I dismiss a sheet with the escape key? - objective-c

My app displays an NSWindow as a sheet. The NSWindow has various controls for settings properties of an object (an NSTextfield, a NSDatepicker etc) and two NSButton's, 'Add' and 'Cancel' which are used to confirm or cancel the action.
I have set the key equivalent of the 'Add' button to enter and the key equivalent of Cancel to escape. This does not have the desired affect. I think this is due to the other controls handling the keypress events.
How do I configure my sheet so that the buttons behave as described?

The solution is to not just check your work, but to double check it!
The setup as described in the question works perfectly. (I have two similar sheets and I was using the wrong one.)

Related

Key Equivalent on NSButton Failing

I am attempting to set the key equivalent of a NSButton in a NSWindow. I am using the following code in my view controller's class:
someButton.keyEquivalent = "\r"
However, when I run the application, pressing the key will not perform the button's action but play the system funk sound.
The window which contains this NSButton is presented as a window (not a sheet) using a modal NSStoryboardSegue. If I use the same code and configuration in my initial window, the key equivalent performs correctly. Would the type of presentation cause this problem? Thanks.
EDIT
Turns out this may be a possible bug. Key equivalents will not work when a title bar is deselected in the IB. I am now attempting to find a way to keep this selected with the same window appearance. Keeping the option selected and using the titlebarAppearsTransparent property on NSWindow, I have successfully removed the title bar but now I am unsure how to remove the window border. Any ideas?
Do you happen to have the title bar disabled in your window? There's some unexpected behavior (hesitate to call it a bug) where keyboard events are ignored in a modal window if the title bar is disabled (even though the title bar isn't used in sheets).

UIActionSheet - add disabled button

Is there a way to add a disabled (non-clickable, greyed out) button to a UIActionSheet?
All I see is "addButtonWithTitle" which does not supply any properties to work with.
I believe there is no way to add disabled button in a UIActionSheet. From the class reference:
Use the UIActionSheet class to present the user with a set of
alternatives for how to proceed with a given task. You can also use
action sheets to prompt the user to confirm a potentially dangerous
action. The action sheet contains an optional title and one or more
buttons, each of which corresponds to an action to take.
If the button is disabled, it should not be added into the UIActionSheet in the first place since it is not an alternative on how to proceed with a given task.
UIActionSheet's interface doesn't really give you much control over appearance of the the whole view or the buttons.
You can use some other libraries. If you can't find one that gives such control, it would be simple to you to add that functionality. For example JLActionSheet or RDActionSheet.
You can also, try to retrieve the subviews of UIActionSheet by traversing the view stack recursively. self.view.subviews or by [[UIApplication sharedApplication].windows[0].subviews] "try both, I don't know which one is the right one". You can find the views using introspection, and find the button you want to disable.

Assign a hotkey to NSWindow in Xcode/Interface Builder?

I have an application with an NSMenuItem that launches a separate window and I was wondering if it would also be possible to add a hot-key combination so that it would be possible to also invoke the NSWindow via that as well.
I was thinking that it's not possible, because wouldn't the application have to be 'Active' for a hot-key to work?
So, where should I be looking? Because, honestly, I'm not sure where to start.
Thanks!
Edit: Try this Global Hotkeys
Your hot-key will only work when the app is active.
Click where the square is and press the combo of keys that you want to assign:
In case you mess up the combo of keys, you can clear it here (the "Clear" button at the bottom):

Add hotkey when editing a NSTextField

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.
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.
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
I ended up subclassing my window and overriding sendEvent when my text field is the first responder.

Capture key press events while NSMenu is open

I'm interested in capturing key presses while a NSMenu is open. For example, if the menu is open and the user presses "e", or "1" on the keyboard, send a particular message (preferably passing an event object which contains reference to which key was pressed).
I've looked into alternate menus, but I'm under the impression that can only be used to capture the option key.
Currently I'm not using any custom views, just NSStatusBar (where the menu spawns from) and NSMenu.
I'm new to Objective-C so my apologies if I'm wording anything incorrectly.
Really appreciate the help!
I assume you are searching for this: Cocoa NSStatusBar Global HotKey