How to use
Feel free to download and decode this project in order to implement it in your own projects. Or follow these loose instructions to configure it.
Instructions for other implementations
Configure your project as necessary this project is using ARC and sandboxed with no entitlements.
Import Carbon.framework into your project if it's not already.
Import the PTHotKey files into your project. I like to group them to stay organized.
If you are using ARC be sure to add
-fno-objc-arcunder the compiler flags for each of the PTHotKey m files. (This is found by clicking on your Target's settings -> Build Phases -> Compile Sources)
Import the PTHotKey header files into your controlling header file.
#import "PTKeyComboPanel.h" #import "PTKeyCombo.h" #import "PTHotKeyCenter.h" #import "PTHotKey.h"
I found that the project worked correctly with these import statements. Your implementation may vary.
- Create another view named
PTKeyComboPanel.xib(the naming matters) file with a single window and change the File Owner's custom class to
- Add the rest of the controls to the window dependent on what you want. See the view in this project for the "typical" implementation. I would recommend copying the window in this project and pasting it into yours. If you choose to configure it yourself there is a hidden element that needs to be bound to the File's Owner.
- Bind your controls to the File Owner.
- Implement the controlling code in your m file. See this project for other details. The key is configuring the
@selectorargument being passed to
registerAppActivationKeystrokeWithTargetwith the action you want the shortcut the execute when pressed. In this example it toggles the main window.
This configuration is not perfectly coded there are some things I would change it before implementing this method to be more efficient. Use it wisely.
This configuration saves the hotkey information to
[NSUserDefaults standardUserDefaults]if you'd like to save it to a different controller or plist you'll need to change those settings.
The statements in
applicationDidFinishLaunchingregister the hotkeys when the application is launched. Be sure to edit that statement's
@selectoras you did before.
I have yet to configure this example to work with multiple hotkey instances. It would require some sort of configuration to use separate sets of shortcuts for different functions.
If you would like to configure 1 shortcut to be user customizable for different options use a NSPopupButton with something like this configuration. (This can also be done with an NSArrayController)
SEL customSelector = NSSelectorFromString([[[button selectedItem] title] stringByAppendingString:@":"]);
customSelectorin place of the
@selectorBe sure to append the colon so it can function correctly.
This implementation came from Brett Terpstra's fork nvALT when searching for a dynamic global shortcut solution. Thank's for being awesome Brett.