HotKey icon indicating copy to clipboard operation
HotKey copied to clipboard

Multiple shortcuts

Open albert-beatz opened this issue 4 years ago • 1 comments

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

albert-beatz avatar Nov 13 '19 12:11 albert-beatz

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.

tlk avatar Dec 30 '19 19:12 tlk