Skip to content
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

Open
AlexGustafsson opened this issue Dec 27, 2020 · 1 comment
Open

Add a constructor to create a HotKey from string #30

AlexGustafsson opened this issue Dec 27, 2020 · 1 comment

Comments

@AlexGustafsson
Copy link

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.

@AlexGustafsson
Copy link
Author

AlexGustafsson commented Dec 27, 2020

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 Key to carbonModifiers leaves some to be desired. Perhaps one could add a modifier: NSEvent.ModifierFlags or something to the Key class.

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant