GithubHelp home page GithubHelp logo

spicekey's Introduction

SpiceKey

Global Shortcuts for macOS written in Swift.

Requirements

  • Development with Xcode 15.0.1+
  • Written in Swift 5.9
  • swift-tools-version: 5.8
  • Compatible with macOS 11.0+

Demo

Demo App is in this Project.

demo_appkit

demo_swiftui

Usage

  • Register a shortcut

Set ⌘ + A shortcut.

let key = Key.a
let modifierFlags = ModifierFlags.cmd
let keyCombo = KeyCombination(key, modifierFlags)
let spiceKey = SpiceKey(keyCombo, keyDownHandler: {
    // process (key down)
}, keyUpHandler: {
    // process (key up)
})
spiceKey.register()

Set press both side of ⌘ shortcut.

let flag = ModifierFlag.command
let spiceKey = SpiceKey(flag, bothModifierKeysPressHandler: {
    // process (press)
}, releaseKeyHandler: {
    // process (release)
})
spiceKey.register()

Set long press ⌘ shortcut.

// run after 0.6 sec
let flags = ModifierFlags.cmd
let spiceKey = SpiceKey(flags, 0.6, modifierKeysLongPressHandler: {
    // process (press)
}, releaseKeyHandler: {
    // process (release)
})
spiceKey.register()
  • Create a Key and a ModifierFlags from NSEvent.
func example(event: NSEvent) {
    let key = Key(keyCode: event.keyCode)
    let flags = event.modifierFlags.intersection(.deviceIndependentFlagsMask)
    let modifierFlags = ModifierFlags(flags: flags)
}
  • Get a description of the shortcut
let description = modifierFlags.string + key.string
// or
let description = keyCombination.string
  • Unregister a shortcut
spiceKey.unregister()
  • Save shortcut

SpiceKeyData supports NSCoding & Codable.

let key = Key.a
let flags = ModifierFlags.optCmd
let spiceKey = SpiceKey(...)

let spiceKeyData = SpiceKeyData(
    _ primaryKey: String,
    _ keyCode: CGKeyCode,  // key.keyCode
    _ control: Bool,       // flags.containsControl
    _ option: Bool,        // flags.containsOption
    _ shift: Bool,         // flags.containsShift
    _ command: Bool,       // flags.containsCommand
    _ spiceKey: SpiceKey   // spiceKey
)
// OR
let spiceKeyData = SpiceKeyData(
    _ primaryKey: String,
    _ key: Key,
    _ modifierFlags: ModifierFlags,
    _ spiceKey: SpiceKey
)
// OR
let spiceKeyData = SpiceKeyData(
    _ primaryKey: String,
    _ keyCombination: KeyCombination,
    _ spiceKey: SpiceKey
)

// NSCoding
let data = try! NSKeyedArchiver.archivedData(withRootObject: spiceKeyData,
                                             requiringSecureCoding: false)
UserDefaults.standard.set(data, forKey: "spiceKeyData")

// Codable
let data = try! JSONEncoder().encode(spiceKeyData)
UserDefaults.standard.set(data, forKey: "spiceKeyData")
  • Load shortcut
// NSCoding
let data = UserDefaults.standard.data(forKey: "spiceKeyData")!
let spiceKeyData = try! NSKeyedUnarchiver
    .unarchiveTopLevelObjectWithData(data) as! SpiceKeyData

// Codable
let data = UserDefaults.standard.data(forKey: "spiceKeyData")!
let spiceKeyData = try! JSONDecoder().decode(SpiceKeyData.self, from: data)

SpiceKeyField & SKTextField

A special text field that can be used to register SpiceKey.

  • SpiceKeyField is for AppKit only.
  • SKTextField is a SpiceKeyField made available from SwiftUI.

Please see the Demo App for detailed instructions.

spicekey's People

Contributors

kyome22 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.