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

Multiple shortcuts #19

Open
albert-beatz opened this issue Nov 13, 2019 · 1 comment
Open

Multiple shortcuts #19

albert-beatz opened this issue Nov 13, 2019 · 1 comment

Comments

@albert-beatz
Copy link

First of all I'd like to thank you for the useful and convenient library!

Though I find it difficult to use with the multiple shortcuts, if you specify same callbacks. The callback can't distinct from which hotkey it is called (using 'identifier' and 'carbonHotKeyID' fields of HotKeyBox is not very convenient in this case)

I've just added two more handlers to HotKey class, and the reference variable:

public typealias RefHandler = (Int?) -> Void

public var keyDownRefHandler: RefHandler?
public var keyUpRefHandler: RefHandler?
public var refCon: Int?

Added slightly modified constructors:

    public init(keyCombo: KeyCombo, refCon: Int? = nil, keyDownRefHandler: RefHandler? = nil, keyUpRefHandler: RefHandler? = nil) {
        self.keyCombo = keyCombo
        self.keyDownRefHandler = keyDownRefHandler
        self.keyUpRefHandler = keyUpRefHandler
        self.refCon = refCon
        HotKeysController.register(self)
    }
    
    public convenience init(carbonKeyCode: UInt32, carbonModifiers: UInt32, refCon: Int? = nil, keyDownRefHandler: RefHandler? = nil, keyUpRefHandler: RefHandler? = nil) {
        let keyCombo = KeyCombo(carbonKeyCode: carbonKeyCode, carbonModifiers: carbonModifiers)
        self.init(keyCombo: keyCombo, refCon: refCon, keyDownRefHandler: keyDownRefHandler, keyUpRefHandler: keyUpRefHandler)
    }
    
    public convenience init(key: Key, modifiers: NSEvent.ModifierFlags, refCon: Int? = nil, keyDownRefHandler: RefHandler? = nil, keyUpRefHandler: RefHandler? = nil) {
        let keyCombo = KeyCombo(key: key, modifiers: modifiers)
        self.init(keyCombo: keyCombo, refCon: refCon, keyDownRefHandler: keyDownRefHandler, keyUpRefHandler: keyUpRefHandler)
    }

Modified the 'Call the handler' part of handleCarbonEvent()

        // Call the handler
        if hotKey.isPaused {
            return noErr
        }
        switch GetEventKind(event) {
        case UInt32(kEventHotKeyPressed):
            if let handler = hotKey.keyDownHandler {
                handler()
                return noErr
            }
            if let handler = hotKey.keyDownRefHandler {
                handler(hotKey.refCon)
                return noErr
            }
        case UInt32(kEventHotKeyReleased):
            if let handler = hotKey.keyUpHandler {
                handler()
                return noErr
            }
            if let handler = hotKey.keyUpRefHandler {
                handler(hotKey.refCon)
                return noErr
            }
        default:
            break
        }

And now I'm able to register the multiple hotkeys with the same callbacks!

I'm not very familiar with git so I posted it here in case you want to merge it.

Best regards.
Albert

@tlk
Copy link

tlk commented Dec 30, 2019

I would like to echo the appreciations for this library. Thank you!

An alternative approach to the suggestion here is to use a closure (as a generic handler) that captures which hotkey it has been attached to. You can see a working example behind the two links below:

One thing I like about this approach is that the switch over enums provides an assurance that all commands are supported.

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

2 participants