2019-02-19 23:32:48 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2019-10-11 01:11:32 +08:00
|
|
|
hook "github.com/robotn/gohook"
|
2019-02-19 23:32:48 +08:00
|
|
|
)
|
|
|
|
|
2020-05-22 00:18:10 +08:00
|
|
|
func addEvent() {
|
|
|
|
fmt.Println("--- Please press ctrl + shift + q to stop hook ---")
|
|
|
|
hook.Register(hook.KeyDown, []string{"q", "ctrl", "shift"}, func(e hook.Event) {
|
|
|
|
fmt.Println("ctrl-shift-q")
|
|
|
|
hook.End()
|
|
|
|
})
|
|
|
|
|
|
|
|
fmt.Println("--- Please press w---")
|
|
|
|
hook.Register(hook.KeyDown, []string{"w"}, func(e hook.Event) {
|
|
|
|
fmt.Println("w")
|
|
|
|
})
|
|
|
|
|
|
|
|
s := hook.Start()
|
|
|
|
<-hook.Process(s)
|
|
|
|
}
|
|
|
|
|
2019-10-11 01:11:32 +08:00
|
|
|
// hook listen and return values using detailed examples
|
2019-02-19 23:32:48 +08:00
|
|
|
func add() {
|
2020-05-22 00:18:10 +08:00
|
|
|
fmt.Println("hook add...")
|
2019-02-19 23:32:48 +08:00
|
|
|
s := hook.Start()
|
|
|
|
defer hook.End()
|
|
|
|
|
|
|
|
ct := false
|
|
|
|
for {
|
|
|
|
i := <-s
|
|
|
|
|
|
|
|
if i.Kind == hook.KeyHold && i.Rawcode == 59 {
|
|
|
|
ct = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if ct && i.Rawcode == 12 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 01:11:32 +08:00
|
|
|
// base hook example
|
2019-02-19 23:32:48 +08:00
|
|
|
func base() {
|
2020-05-22 00:18:10 +08:00
|
|
|
fmt.Println("hook start...")
|
2019-10-31 23:08:50 +08:00
|
|
|
evChan := hook.Start()
|
2019-02-19 23:32:48 +08:00
|
|
|
defer hook.End()
|
|
|
|
|
2019-10-31 23:08:50 +08:00
|
|
|
for ev := range evChan {
|
2019-07-29 02:12:01 +08:00
|
|
|
fmt.Println("hook: ", ev)
|
2019-02-19 23:32:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2020-05-22 00:18:10 +08:00
|
|
|
addEvent()
|
|
|
|
|
2019-02-19 23:32:48 +08:00
|
|
|
base()
|
|
|
|
|
|
|
|
add()
|
|
|
|
}
|