robotgo icon indicating copy to clipboard operation
robotgo copied to clipboard

Hotkey monitoring implementation

Open essenwbb opened this issue 6 years ago • 2 comments

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?

github:clipboard recorder.

essenwbb avatar Apr 28 '19 14:04 essenwbb

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")
}

essenwbb avatar Apr 30 '19 22:04 essenwbb

The hook itself is asynchronous, you can consider calling the hook code. I will update the code or add a simplified api.

vcaesar avatar May 07 '19 18:05 vcaesar