-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a constructor to create a HotKey from string #30
Comments
This code works. import AppKit
import HotKey
extension HotKey {
public convenience init?(keys: String, keyDownHandler: Handler? = nil, keyUpHandler: Handler? = nil) {
guard let keyCombo = KeyCombo(keys: keys) else {
return nil
}
self.init(keyCombo: keyCombo, keyDownHandler: keyDownHandler, keyUpHandler: keyUpHandler)
}
}
extension KeyCombo {
public init?(keys: String) {
var chosenKey: Key? = nil
var chosenModifiers: NSEvent.ModifierFlags = []
for key in keys.split(separator: "+") {
guard let parsedKey = Key(string: String(key)) else {
// Parse failed
return nil
}
switch parsedKey {
case .command: chosenModifiers = chosenModifiers.union(.command)
case .rightCommand: chosenModifiers = chosenModifiers.union(.command)
case .option: chosenModifiers = chosenModifiers.union(.option)
case .rightOption: chosenModifiers = chosenModifiers.union(.option)
case .control: chosenModifiers = chosenModifiers.union(.control)
case .rightControl: chosenModifiers = chosenModifiers.union(.control)
case .shift: chosenModifiers = chosenModifiers.union(.shift)
case .rightShift: chosenModifiers = chosenModifiers.union(.shift)
case .function: chosenModifiers = chosenModifiers.union(.function)
case .capsLock: chosenModifiers = chosenModifiers.union(.capsLock)
default:
chosenKey = parsedKey
}
}
if chosenKey == nil {
return nil
}
self.init(carbonKeyCode: chosenKey!.carbonKeyCode, carbonModifiers: chosenModifiers.carbonFlags)
}
} The conversion from If this is something you'd like to support, I'd happily open a PR. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To help aid hotkeys configurable by the user, it would be helpful to have a way of creating a HotKey (or KeyCombo) from a string such as
option+command+t
.I have done some limited research, but I was unable to find any existing solution for getting the key and modifiers from a string such as the above.
The text was updated successfully, but these errors were encountered: