Skip to content

HsiangHo/ShortcutsKit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ShortcutsKit

Travis Jenkins Contributions Platform Language

中文版

📦 ShortcutsKit is an easy way for developers to register/unregister/serialize/deserialize a hotkey and display hotkeys with customization.

Installation

Clone the rep, build the ShortcutsKit or copy all the source files into your project.

Features

  • Customization and Configuration to your needs
  • Easy to register/unregister hotkeys
  • Serialize/Deserialize keyCombo object

Example

To run the example project, clone the repo, build and run the target 'ShortcutsDemo'.

Getting started

  • 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];

Requirements

macOS 10.7 and above
Xcode 8.0+

Contributing

Contributions are very welcome 🙌 🤓

Screenshots

About

📦 ShortcutsKit for macOS

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published