HotKey Documentation

repository·main·Indexed 22 days ago

https://github.com/soffes/hotkey

A Swift wrapper for macOS Carbon APIs that allows developers to implement global keyboard shortcuts. It provides functionality to register hot keys with modifiers and handle keyDown and keyUp events via closures.

Tokens
412
Snippets
5
Records
5
Agent score
27%

What's inside HotKey

  1. Install HotKey via Swift Package Manager

    main

    To use HotKey in your Swift project via Swift Package Manager, add the following dependency to your Package.swift file:

    .package(url: "https://github.com/soffes/HotKey", from: "0.2.1")
  2. Register a global hot key in Swift

    main

    Initialize a HotKey object by specifying a key and a set of modifiers. The library provides a convenience initializer that accepts a key and an array of modifiers (e.g., .command, .option, .shift, .control).

    Registration and unregistration of the hot key are handled automatically based on the lifecycle of the HotKey instance.

    // Setup hot key for ⌥⌘R
    let hotKey = HotKey(key: .r, modifiers: [.command, .option])
  3. Handle hot key press and release events

    main

    Once a HotKey is initialized, you can assign closures to keyDownHandler and keyUpHandler to respond to user input. The keyDownHandler is triggered when the specified key combination is pressed.

    hotKey.keyDownHandler = {
      print("Pressed at \(Date())")
    }
    
    // You can also implement keyUpHandler
    hotKey.keyUpHandler = {
      print("Released")
    }