nosedive25
nosedive25

Reputation: 2435

Cocoa check if a keyboard shortcut is in use

I have a user specified global hotkey and want to check and make sure it's not going to collide with other applications. Is there any API that can ask other applications for their shortcuts or am I stuck manually checking if the selected shortcut is a common one (Cmd+v, Cmd+C etc.)?

Thanks

Upvotes: 0

Views: 251

Answers (1)

Bink
Bink

Reputation: 171

You have to ask the responder chain, in particular [NSResponder tryToPerform:with:] method will return if anything handles your action. Don't worry about what other apps are doing, just check if the user's shortcut is already in use.

tryToPerform:with: Attempts to perform the action indicated method with a specified argument.

  • (BOOL)tryToPerform:(SEL)anAction with:(id)anObject Parameters anAction The selector identifying the action method. anObject The object to use as the sole argument of the action method. Return Value Returns NO if no responder is found that responds to anAction, YES otherwise.

Discussion If the receiver responds to anAction, it invokes the method with anObject as the argument and returns YES. If the receiver doesn’t respond, it sends this message to its next responder with the same selector and object.

Availability Available in OS X v10.0 and later. See Also – doCommandBySelector: sendAction:to:from: (NSApplication) Declared In NSResponder.h

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/nsresponder_Class/Reference/Reference.html#//apple_ref/occ/instm/NSResponder/tryToPerform:with:

Upvotes: 1

Related Questions