Assign a hotkey to NSWindow in Xcode/Interface Builder? - objective-c

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

Related

Correct way to make an NSView the firstresponder to all Undo/Redo actions irrespective of active/focused view/control

I have a standard document based application whose main window has two NSViews.
Is it possible to make NSView1 the firstresponder for Undo/Redo actions even when NSView2 is the focused view.
Is it possible to have the context for Edit menuitem permanently set to NSView1's context. E.g. if an NSTextField in NSView2 has focus and the Edit menu item is opened, by default it opens in the context of NSTexfield's current state, can this be overriden?
If either is possible, how does one go about achieving this?
C.
Ok, found the solution, there's a rather convenient method in NSResponder called validateProposedFirstResponder. With a little manoeuvring, one should be able to achieve the desired effect.

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.

How do i add a right-click option to a menulet in Xcode and Interface Builder?

I'm trying to have a menulet that acts different on left-click and on right-click (and maybe combinations in the future like control-click, option-click or command-click).
Right now I have a menulet with a statusMenu attached to it that opens on left- and on right-click. In the statusMenu are five options: one execution item, three preference-setting-items and one quit item.
I'd like to separate the behavior somewhat as the menulet is designed to be a one trick pony. Left-clicking it should execute the function, right-clicking should open the menu exactly as it is (including the execution).
Any ideas how to do this?
Right now I'm calling the statusMenu from the code as is usual with statusMenus
[statusItem setMenu:statusMenu];
To do this, you will need to display the menu manually. One way to do it is to create a custom view and place that in the status item, and then use the mouseUp: and rightMouseUp: event handlers to perform your actions. You can also get the modifier keys from the event to perform other actions. It may also be possible to use the built-in target-action mechanism of the status item, and get the information from the NSEvent class methods +pressedMouseButtons and +modifierFlags. Either way, you can display your menu using NSStatusItem's -popUpStatusItemMenu: method.
For an example of handling left and right clicks with a custom view, see my answer to this question.

NSTableView responding to first click in a panel

I have noticed in the Interface Builder if I want to click on or drag from the Library panel, I only have to click on it once, even if the Library panel does not have the current focus.
I am trying to build a panel that behaves similarly.
Is there any simple way to let the NSTableView accept the click, even if the window does not have the focus?
Thanks.
Ok, I found the answer. Inside from awakeFromNib I call this:
[self setBecomesKeyOnlyIfNeeded:YES];
It seems to do the trick. It's a little bit different from Interface Builder where the Panel actually gets the focus simultaneously with a single click, but doing it this way is just what I was looking for.
Your view should override -acceptsFirstMouse: to return YES (or evaluate the event passed to you to determine what to return). You'll have to subclass NSTableView to do that of course.

How do I dismiss a sheet with the escape key?

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