gohook/examples/main.go

81 lines
1.4 KiB
Go
Raw Normal View History

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
)
2020-12-02 13:01:20 -04:00
func registerEvent() {
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 ---")
hook.Register(hook.KeyDown, []string{"w"}, func(e hook.Event) {
2020-09-07 10:27:44 -04:00
fmt.Println("w-")
})
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() {
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() {
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
}
}
func main() {
2020-12-02 13:01:20 -04:00
registerEvent()
2019-02-19 11:32:48 -04:00
base()
add()
2022-02-09 09:26:46 -08:00
addMouse()
2019-02-19 11:32:48 -04:00
}