gohook/examples/main.go

81 lines
1.4 KiB
Go
Raw Normal View History

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-12-03 01:01:20 +08: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-03 01:01:20 +08:00
fmt.Println("--- Please press w ---")
hook.Register(hook.KeyDown, []string{"w"}, func(e hook.Event) {
2020-09-07 22:27:44 +08:00
fmt.Println("w-")
})
s := hook.Start()
<-hook.Process(s)
}
2022-02-10 01: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-11 01:11:32 +08:00
// hook listen and return values using detailed examples
2019-02-19 23:32:48 +08:00
func add() {
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() {
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)
2022-02-10 01:26:46 +08:00
if ev.Keychar == 'q' {
break
}
2019-02-19 23:32:48 +08:00
}
}
func main() {
2020-12-03 01:01:20 +08:00
registerEvent()
2019-02-19 23:32:48 +08:00
base()
add()
2022-02-10 01:26:46 +08:00
addMouse()
2019-02-19 23:32:48 +08:00
}