2019-02-19 11:32:48 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2019-10-10 13:11:32 -04:00
|
|
|
hook "github.com/robotn/gohook"
|
2019-02-19 11:32:48 -04:00
|
|
|
)
|
|
|
|
|
2025-02-08 15:44:55 -08:00
|
|
|
func main() {
|
|
|
|
registerEvent()
|
|
|
|
|
|
|
|
base()
|
|
|
|
|
|
|
|
add()
|
|
|
|
addMouse()
|
|
|
|
}
|
|
|
|
|
2020-12-02 13:01:20 -04:00
|
|
|
func registerEvent() {
|
2020-05-21 12:18:10 -04:00
|
|
|
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()
|
|
|
|
})
|
|
|
|
|
2020-12-02 13:01:20 -04:00
|
|
|
fmt.Println("--- Please press w ---")
|
2020-05-21 12:18:10 -04:00
|
|
|
hook.Register(hook.KeyDown, []string{"w"}, func(e hook.Event) {
|
2025-02-08 15:44:55 -08:00
|
|
|
fmt.Println("KeyDown: ", "w-")
|
|
|
|
})
|
|
|
|
|
|
|
|
hook.Register(hook.KeyUp, []string{"w"}, func(e hook.Event) {
|
|
|
|
fmt.Println("KeyUp: ", "w")
|
2020-05-21 12:18:10 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
s := hook.Start()
|
|
|
|
<-hook.Process(s)
|
|
|
|
}
|
|
|
|
|
2022-02-09 09:26:46 -08:00
|
|
|
func addMouse() {
|
|
|
|
fmt.Println("--- Please press left mouse button to see it's position and the right mouse button to exit ---")
|
|
|
|
hook.Register(hook.MouseDown, []string{}, func(e hook.Event) {
|
|
|
|
if e.Button == hook.MouseMap["left"] {
|
|
|
|
fmt.Printf("mouse left @ %v - %v\n", e.X, e.Y)
|
|
|
|
} else if e.Button == hook.MouseMap["right"] {
|
|
|
|
hook.End()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
s := hook.Start()
|
|
|
|
<-hook.Process(s)
|
|
|
|
}
|
|
|
|
|
2019-10-10 13:11:32 -04:00
|
|
|
// hook listen and return values using detailed examples
|
2019-02-19 11:32:48 -04:00
|
|
|
func add() {
|
2020-05-21 12:18:10 -04:00
|
|
|
fmt.Println("hook add...")
|
2019-02-19 11:32:48 -04: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-10 13:11:32 -04:00
|
|
|
// base hook example
|
2019-02-19 11:32:48 -04:00
|
|
|
func base() {
|
2020-05-21 12:18:10 -04:00
|
|
|
fmt.Println("hook start...")
|
2019-10-31 11:08:50 -04:00
|
|
|
evChan := hook.Start()
|
2019-02-19 11:32:48 -04:00
|
|
|
defer hook.End()
|
|
|
|
|
2019-10-31 11:08:50 -04:00
|
|
|
for ev := range evChan {
|
2019-07-28 14:12:01 -04:00
|
|
|
fmt.Println("hook: ", ev)
|
2022-02-09 09:26:46 -08:00
|
|
|
if ev.Keychar == 'q' {
|
|
|
|
break
|
|
|
|
}
|
2019-02-19 11:32:48 -04:00
|
|
|
}
|
|
|
|
}
|