ShortcutRecorder

repository·master·Indexed 20 days ago

https://github.com/kentzo/shortcutrecorder

A macOS framework for recording, displaying, and monitoring local and global keyboard shortcuts. It integrates with Cocoa Bindings and Interface Builder, providing components such as RecorderControl for UI, GlobalShortcutMonitor for background actions, and SRShortcutValidator for shortcut validation.

Tokens
922
Snippets
2
Records
3
Agent score
20%

What's inside ShortcutRecorder

  1. Core components of ShortcutRecorder

    master

    The framework provides several specialized classes to handle different parts of the shortcut lifecycle:

    • UI & Capture: SRRecorderControl is used to render the recording interface and capture user input.
    • Styling: SRRecorderControlStyle allows for custom visual styles.
    • Data Model: SRShortcut represents the actual shortcut model.
    • Global Monitoring:
      • SRGlobalShortcutMonitor: Registers a global hotkey to turn a shortcut into an action.
      • SRAXGlobalShortcutMonitor: Uses macOS Accessibility to handle any kind of keyboard event.
    • Local Monitoring: SRLocalShortcutMonitor handles events manually within the responder chain or via NSEvent monitors.
    • Integration & Validation:
      • SRShortcutController: Provides smooth Cocoa Bindings and Interface Builder integration.
      • SRShortcutValidator: Validates shortcuts against Cocoa key equivalents and global hot keys.
    • Formatting: Includes NSValueTransformer and NSFormatter subclasses for custom data alterations.
  2. Install ShortcutRecorder via Swift Package Manager, CocoaPods, or Carthage

    master

    ShortcutRecorder can be integrated into your macOS project using several dependency managers. It supports module maps, so you only need to import ShortcutRecorder (Swift) or #import <ShortcutRecorder/ShortcutRecorder.h> (Objective-C) after installation.

    Swift Package Manager

    Add the following to your Package.swift:

    .package(url: "git://github.com/Kentzo/ShortcutRecorder.git", from: "3.4.0")

    CocoaPods

    Add this to your Podfile:

    pod 'ShortcutRecorder', '~> 3.4.0'

    Carthage

    Add this to your Cartfile:

    github "Kentzo/ShortcutRecorder" ~> 3.4.0

    Git Submodule

    Run the following command and then drag the folder into your Xcode workspace:

    git submodule add git://github.com/Kentzo/ShortcutRecorder.git
  3. Implement shortcut recording and global monitoring

    master

    To use ShortcutRecorder, you typically combine a RecorderControl (for UI) with a GlobalShortcutMonitor (for background actions).

    Swift Usage

    1. Define a key path and binding options.
    2. Create a ShortcutAction to execute code when the shortcut is triggered.
    3. Register the action with GlobalShortcutMonitor.shared.
    4. Bind a RecorderControl instance to your data model (e.g., NSUserDefaultsController).

    Objective-C Usage

    1. Define a key path and binding options.
    2. Create an SRShortcutAction with an action handler.
    3. Register the action with [SRGlobalShortcutMonitor sharedMonitor].
    4. Bind an SRRecorderControl instance to your data model.
    import ShortcutRecorder
    
    let defaults = NSUserDefaultsController.shared
    let keyPath = "values.shortcut"
    let options = [NSBindingOption.valueTransformerName: .keyedUnarchiveFromDataTransformerName]
    
    let beepAction = ShortcutAction(keyPath: keyPath, of: defaults) { _ in
        NSSound.beep()
        return true
    }
    GlobalShortcutMonitor.shared.addAction(beepAction, forKeyEvent: .down)
    
    let recorder = RecorderControl()
    recorder.bind(.value, to: defaults, withKeyPath: keyPath, options: options)
    
    recorder.objectValue = Shortcut(keyEquivalent: "⇧⌘A")