Swiftpack.co - Package - Clipy/Magnet

Magnet

CI Release version License: MIT Carthage compatible Version Platform SPM supported

Customize global hotkeys in macOS. Supports usual hotkey and double tap hotkey like Alfred.app.

Also supports sandbox application.

Usage

CocoaPods

pod 'Magnet'

Carthage

github "Clipy/Magnet"

Upgrading from Magnet v2.x to v3.x

See Upgrading from Magnet v2.x

Example

Register Normal hotkey

Add ⌘ + Control + B hotkey.

if let keyCombo = KeyCombo(key: .b, cocoaModifiers: [.command, .control]]) {
   let hotKey = HotKey(identifier: "CommandControlB", keyCombo: keyCombo, target: self, action: #selector())
   hotKey.register() // or HotKeyCenter.shared.register(with: hotKey)
}

Or you can use closures.

if let keyCombo = KeyCombo(key: .b, cocoaModifiers: [.command, .control]) {
    let hotKey = HotKey(identifier: "CommandControlB", keyCombo: keyCombo) { hotKey in
        // Called when ⌘ + Control + B is pressed
    }
    hotKey.register()
}        

Register Double tap hotkey

Add ⌘ double tap hotkey.

if let keyCombo = KeyCombo(doubledCocoaModifiers: .command) {
   let hotKey = HotKey(identifier: "CommandDoubleTap", keyCombo: keyCombo, target: self, action: #selector())
   hotKey.register() // or HotKeyCenter.shared.register(with: hotKey)
}

Add Control double tap hotkey.

if let keyCombo = KeyCombo(doubledCarbonModifiers: controlKey) {
   let hotKey = HotKey(identifier: "ControlDoubleTap", keyCombo: keyCombo, target: self, action: #selector())
   hotKey.register() // or HotKeyCenter.shared.register(with: hotKey)
}

Support modifiers

Double tap hotkey only support following modifiers.

  • Command Key
    • NSEventModifierFlags.command or cmdKey
  • Shift Key
    • NSEventModifierFlags.shift or shiftKey
  • Option Key
    • NSEventModifierFlags.option or optionKey
  • Control Key
    • NSEventModifierFlags.control or controlKey

Unregister hotkeys

HotKeyCenter.shared.unregisterAll()

or

HotKeyCenter.shared.unregisterHotKey(with: "identifier")

or

let hotKey = HotKey(identifier: "identifier", keyCombo: KeyCombo, target: self, action: #selector())
hotKey.unregister() // or HotKeyCenter.shared.unregister(with: hotKey)

Dependencies

How to Build

  1. Move to the project root directory
  2. Install dependency library with carthage or git submodule
  3. carthage checkout --use-submodules or git submodule update --init --recursive
  4. Open Magnet.xcworkspace on Xcode.
  5. build.

Github

link
Stars: 275

Dependencies

Used By

Total: 0

Releases

v3.2.0 - 2020-05-23 15:35:10

Breaking Changes

  • None.

v3.x Migration Guide

Enhancements

  • Update Sauce.framework v2.1.0. ( #57 )
  • Change the save to KeyCode or Key to support special keys. ( #59 )

Bugfixes

  • Change ModifierEventHandler access control. ( #55 )
  • Fix so that modifiers are not reflected in non-alphabetic keys. ( #58 )

v3.1.0 - 2020-05-14 08:20:02

Breaking Changes

  • None.

v3.x Migration Guide

Enhancements

  • Publicated of Key and fix the decoding ( #51 )
  • Separated modifier event handling ( #52 )
  • Added more tests.

Bugfixes

  • None.

v3.0.4 - 2020-05-06 08:15:56

Enhancements

  • Added keyEquivalent property to KeyCombo. ( #50 )
    • KeyCombo.characters returns the string that is entered when the modifier is actually pressed. (e.g.: ⌘Vv, ⇧VV, ⌥V )
    • KeyCombo.keyEquivalent returns a string which takes into account only the Shift key. (e.g.: ⌘Vv, ⇧VV, ⌥Vv, ⌥⇧VV)

To make keyEquivalent case-sensitive, use keyEquivalent.uppercased() if you want to use it in uppercase.

Bugfixes

  • Renamed convertSupportCococaModifiers to convertSupportCocoaModifiers. ( #50 )

v3.0.3 - 2020-05-02 06:34:18

Enhancements

  • Added keyEquivalentModifierMask, keyEquivalentModifierMaskString properties to KeyCombo for NSMenuItem. ( #48 )

Bugfixes

  • None.

v3.0.2 - 2020-04-30 14:09:50

Enhancements

  • None.

Bugfixes

  • Fix dependency framework settings.

v3.0.1 - 2020-04-30 13:58:33

Enhancements

  • Change Int extension access control. ( #46 )

Bugfixes

  • None.

v3.0.0 - 2020-04-30 13:54:09

Breaking Changes

  • Added Sauce.framework to dependency library. ( #42 )
  • Added KeyCombo(key:carbonModifiers:) KeyCombo(key:cocoaModifiers:). ( #44 )
  • Renamed KeyCombo(keyCode:carbonModifiers:) KeyCombo(keyCode:cocoaModifiers:) to KeyCombo(QWERTYKeyCode:carbonModifiers:) KeyCombo(QWERTYKeyCode:cocoaModifiers:). ( #44 )
  • Renamed KeyCombo.keyCode to KeyCombo.QWERTYKeyCode. ( #44 )
  • Removed KeyTransformer.swift ( #44 )
  • KeyCombo.characters now returns a string with a modifier. (e.g.: ⌘Vv, ⇧VV, ⌥V )
  • Drop support macOS 10.9 Mavericks.

v3.x Migration Guide

Enhancements

  • Supported for keyboard layouts other than the QWERTY keyboard. ( #44 )
  • Supported Key enum case initializer. ( #44 ) ( #16 )

Bugfixes

  • Fixed a problem where characters could not be generated with modifiers correctly. ( #44 ) ( #10 )
  • Fixed double-tap shortcuts being disabled if accessibility is not enabled since macOS 10.14 Mojave. ( #45 )

v2.3.1 - 2018-10-30 08:26:47

Enhancements

  • HotKey inherits NSObject. ( #9 )

Bugfixes

  • None.

v2.3.0 - 2018-09-21 16:05:05

Breaking Changes

  • None.

Enhancements

  • None.

Bugfixes

  • Fix crash swift4.2 hashValue ( #24 )

v2.2.1 - 2018-09-11 17:02:06

Enhancements

  • Conform KeyCombo to Codable ( #15 )

Bugfixes

  • None.

v2.2.0 - 2018-08-08 06:54:05

Breaking Changes

  • target and action properties of HotKey initializer are now non-Optional. ( #20 )

Enhancements

  • Publicize KeyCodeTransformer.shared. ( #8 )
  • Support closure style callback handling ( #20 )
  • Auto unregister all hotkeys when application terminate ( #21 )

Bugfixes

  • Remove duplicate CGEventFlags.contains method. ( #13 )
  • Fix CGEvent memory leaks. ( #19 )

v2.1.0 - 2017-11-25 18:42:22

Breaking Changes

  • None.

Enhancements

  • Support Swift4 and Xcode9.

Bugfixes

  • None.

v2.0.1 - 2017-05-12 13:37:28

Enhancements

  • Add callback action queue setting.

Bugfixes

  • None.

v2.0.0 - 2016-09-28 13:25:20

Breaking Changes

  • None.

Enhancements

  • Support Swift3 and Xcode8.

Bugfixes

  • None.

v1.0.1 - 2016-09-28 10:20:00

Enhancements

  • Carthage supports.

Bugfixes

  • None.

v1.0.0 - 2016-06-26 10:23:53

Breaking Changes

  • Remove key code enum.

Enahncements

  • Support multi language keyboards.
  • Add character property in KeyCombo.swift

Bugfixes

  • None.

v0.1.1 - 2016-06-20 08:44:35

Enhancements

  • None.

Bugfixes

  • Fix thread handling.

v0.1.0 - 2016-06-20 07:50:14

Enhancements

  • Support NSCoding and NSObject protocol.
  • Add key code enum.
  • Support sandbox application.
  • Remove key code transform method.

Bugfixes

  • None.

v0.0.2 - 2016-06-18 16:01:39

:rocket: