golang-design / hotkey

⌨️ cross-platform hotkey package
https://golang.design/x/hotkey
MIT License
212 stars 10 forks source link

Can multiple hotkeys be registered at the same time? #3

Closed ficapy closed 3 years ago

ficapy commented 3 years ago

For example, listening to Ctrl+1 and Ctrl+2 at the same time. my test failed on OSX, is this not allowed, or is it used incorrectly?

package main

import (
    "context"
    "golang.design/x/hotkey"
    "golang.design/x/mainthread"
)

func register(tag chan bool, mods []hotkey.Modifier, k hotkey.Key, output string) {
    go func() {
        hk, err := hotkey.Register(mods, k)
        if err != nil {
            panic("hotkey registration failed")
        }
        triggered := hk.Listen(context.Background())
        <-tag
        for range triggered {
            println("hotkey", output)
        }
    }()
}

func fn() {
    c := make(chan bool, 1)
    c <- true
    register(c, []hotkey.Modifier{hotkey.ModCtrl}, hotkey.Key1, "1")
    c <- true
    register(c, []hotkey.Modifier{hotkey.ModCtrl}, hotkey.Key2, "2")
}

func main() {
    mainthread.Init(fn)
}
changkun commented 3 years ago

Indeed, this is an implementation issue. When we start the call Listen, the handling must register a background application, making the current implementation not support registering multiple hotkeys. I will investigate this, and see if there are any workaround to realize it.

changkun commented 3 years ago

Moreover, this is also a platform-specific issue. I think it only occurs in macOS.

changkun commented 3 years ago

Sorry for the confusion. I think there is a flaw in your example. The current implementation already supports registering multiple hotkeys. See here is a complete example for registering multiple hotkeys:

https://github.com/golang-design/hotkey/blob/main/hotkey_darwin_test.go

changkun commented 3 years ago

Nothing needs to do here. Close.