robotgo
robotgo copied to clipboard
Hotkey monitoring implementation
For the hotkey monitoring implementation I want to use goroutine+AddEvents/AddEvent
Example code:
package main
import (
"fmt"
"github.com/go-vgo/robotgo"
"time"
)
func main() {
go addKeysListen("q", "ctrl")
go addKeyListen("k")
for {
// do something
time.Sleep(time.Millisecond * 50)
}
}
func addKeysListen(key string, arr ...string) {
for {
if ok := robotgo.AddEvents(key, arr...); ok {
fmt.Println("pressed Ctrl + q")
}
}
}
func addKeyListen(key string) {
for {
if ok := robotgo.AddEvent(key); ok {
fmt.Println("Pressed k")
}
}
}
If I only listen to a hotkey, it is ok, but more than one will fail. Can you change the underlying code to support this type of writing? Or has a more elegant solution?
I already have an another solution, and it worked, but it will overwrite previous shortcuts. Example code:
package main
import (
"fmt"
"github.com/wuyueCreator/cliphistory/hotkey"
)
var (
keys = map[int16]*hotkey.HotkeyAndEvent{
1: {&hotkey.Hotkey{1, hotkey.ModAlt + hotkey.ModCtrl, 'O'}, test},
2: {&hotkey.Hotkey{2, hotkey.ModCtrl, 'V'}, test2},
}
)
func main() {
hotkey.KeysEventMap = keys
hotkey.Run()
}
func test() {
fmt.Println("hello world")
}
func test2() {
fmt.Println("hello world 2")
}
The hook itself is asynchronous, you can consider calling the hook code. I will update the code or add a simplified api.