📦 ShortcutsKit is an easy way for developers to register/unregister/serialize/deserialize a hotkey and display hotkeys with customization.
Clone the rep, build the ShortcutsKit or copy all the source files into your project.
- Customization and Configuration to your needs
- Easy to register/unregister hotkeys
- Serialize/Deserialize keyCombo object
To run the example project, clone the repo, build and run the target 'ShortcutsDemo'.
- Create a keyCombo object
//Create a keyCombo object for "shift + option + control + space"
SCKeyCombo *keyCombo = [[SCKeyCombo alloc] initWithKeyCode:kVK_Space keyModifiers:shiftKey + optionKey + controlKey];
//Serialize keyCombo object
[[NSUserDefaults standardUserDefaults] setValue:[NSKeyedArchiver archivedDataWithRootObject:keyCombo] forKey:@"keyCombo"];
//Deserialization keyCombo object
SCKeyCombo *keyCombo2 = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] valueForKey:@"keyCombo"]];
NSLog(@"%@",[keyCombo2 stringForKeyCombo]);
- Create a hotkey object
...
//Create a hotkey object from a keyCombo object
//Using block
SCHotkey *shortcut = [[SCHotkey alloc] initWithKeyCombo:keyCombo2 identifier:@"shortcut" handler:^(SCHotkey * _Nonnull hotkey) {
NSLog(@"shortcut has been called");
}];
[shortcut register];
//Using selector
SCKeyCombo *keyCombo3 = [[SCKeyCombo alloc] initWithKeyCode: kVK_ANSI_B keyModifiers: optionKey + controlKey];
SCHotkey *shortcut2 = [[SCHotkey alloc]initWithKeyCombo:keyCombo3 identifier:@"shortcut2" target:self action:@selector(shortcut2Callback)];
- Register/Unregister a hotkey object
...
//Register hotkey object
[shortcut register];
//Invoke hotkey object action
[shortcut invoke];
//Unregister hotkey object
[shortcut unregister];
- Display a hotkey object
...
//Create a keyComboView to show the keyCombo
SCKeyComboView *keyComboView = [SCKeyComboView standardKeyComboView];
//Cunstomize keyComboView
[keyComboView setOnTintColor:[NSColor redColor]];
//Set delegate to handle keyconbo changed notification
[keyComboView setDelegate:(id<SCKeyComboViewDelegate>)self];
[keyComboView setKeyCombo:[shortcut keyCombo]];
- Using SCHotkeyManager
...
//Register with hotkey object
[[SCHotkeyManager sharedManager] registerWithHotkey:shortcut];
//Unregister with identifier
[[SCHotkeyManager sharedManager] unregisterWithIdentifier:@"shortcut"];
//Unregister with hotkey object
[[SCHotkeyManager sharedManager] unregisterWithHotkey:shortcut];
//Unregister all hotkey objects
[[SCHotkeyManager sharedManager] unregisterAllHotkeys];
macOS 10.7 and above
Xcode 8.0+
Contributions are very welcome 🙌 🤓