mirror of
https://github.com/robotn/gohook.git
synced 2025-04-16 16:31:56 +08:00
Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
f049036b8c | ||
|
0d69fe6a36 | ||
|
b93f6ca661 | ||
|
c94ab299da |
4
.github/workflows/go.yml
vendored
4
.github/workflows/go.yml
vendored
@ -10,10 +10,10 @@ jobs:
|
|||||||
runs-on: ${{ matrix.os }}
|
runs-on: ${{ matrix.os }}
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Set up Go 1.21.0
|
- name: Set up Go 1.23.0
|
||||||
uses: actions/setup-go@v1
|
uses: actions/setup-go@v1
|
||||||
with:
|
with:
|
||||||
go-version: 1.21.0
|
go-version: 1.23.0
|
||||||
id: go
|
id: go
|
||||||
|
|
||||||
- name: Check out code into the Go module directory
|
- name: Check out code into the Go module directory
|
||||||
|
@ -45,7 +45,11 @@ func add() {
|
|||||||
|
|
||||||
fmt.Println("--- Please press w---")
|
fmt.Println("--- Please press w---")
|
||||||
hook.Register(hook.KeyDown, []string{"w"}, func(e hook.Event) {
|
hook.Register(hook.KeyDown, []string{"w"}, func(e hook.Event) {
|
||||||
fmt.Println("w")
|
fmt.Println("keyDown: ", "w")
|
||||||
|
})
|
||||||
|
|
||||||
|
hook.Register(hook.KeyUp, []string{"w"}, func(e hook.Event) {
|
||||||
|
fmt.Println("keyUp: ", "w")
|
||||||
})
|
})
|
||||||
|
|
||||||
s := hook.Start()
|
s := hook.Start()
|
||||||
|
@ -34,7 +34,7 @@ environment:
|
|||||||
PATH: C:\msys64\mingw32\bin\;C:\Program Files (x86)\NSIS\;%PATH%
|
PATH: C:\msys64\mingw32\bin\;C:\Program Files (x86)\NSIS\;%PATH%
|
||||||
# - COMPILER: MINGW_W64
|
# - COMPILER: MINGW_W64
|
||||||
# ARCHITECTURE: x64
|
# ARCHITECTURE: x64
|
||||||
GOVERSION: 1.21.0
|
GOVERSION: 1.23.0
|
||||||
# GOPATH: c:\gopath
|
# GOPATH: c:\gopath
|
||||||
|
|
||||||
# scripts that run after cloning repository
|
# scripts that run after cloning repository
|
||||||
|
@ -6,6 +6,15 @@ import (
|
|||||||
hook "github.com/robotn/gohook"
|
hook "github.com/robotn/gohook"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
registerEvent()
|
||||||
|
|
||||||
|
base()
|
||||||
|
|
||||||
|
add()
|
||||||
|
addMouse()
|
||||||
|
}
|
||||||
|
|
||||||
func registerEvent() {
|
func registerEvent() {
|
||||||
fmt.Println("--- Please press ctrl + shift + q to stop hook ---")
|
fmt.Println("--- Please press ctrl + shift + q to stop hook ---")
|
||||||
hook.Register(hook.KeyDown, []string{"q", "ctrl", "shift"}, func(e hook.Event) {
|
hook.Register(hook.KeyDown, []string{"q", "ctrl", "shift"}, func(e hook.Event) {
|
||||||
@ -15,7 +24,11 @@ func registerEvent() {
|
|||||||
|
|
||||||
fmt.Println("--- Please press w ---")
|
fmt.Println("--- Please press w ---")
|
||||||
hook.Register(hook.KeyDown, []string{"w"}, func(e hook.Event) {
|
hook.Register(hook.KeyDown, []string{"w"}, func(e hook.Event) {
|
||||||
fmt.Println("w-")
|
fmt.Println("KeyDown: ", "w-")
|
||||||
|
})
|
||||||
|
|
||||||
|
hook.Register(hook.KeyUp, []string{"w"}, func(e hook.Event) {
|
||||||
|
fmt.Println("KeyUp: ", "w")
|
||||||
})
|
})
|
||||||
|
|
||||||
s := hook.Start()
|
s := hook.Start()
|
||||||
@ -69,12 +82,3 @@ func base() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
|
||||||
registerEvent()
|
|
||||||
|
|
||||||
base()
|
|
||||||
|
|
||||||
add()
|
|
||||||
addMouse()
|
|
||||||
}
|
|
||||||
|
2
go.mod
2
go.mod
@ -4,5 +4,5 @@ go 1.17
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/vcaesar/keycode v0.10.1
|
github.com/vcaesar/keycode v0.10.1
|
||||||
github.com/vcaesar/tt v0.20.0
|
github.com/vcaesar/tt v0.20.1
|
||||||
)
|
)
|
||||||
|
4
go.sum
4
go.sum
@ -1,4 +1,4 @@
|
|||||||
github.com/vcaesar/keycode v0.10.1 h1:0DesGmMAPWpYTCYddOFiCMKCDKgNnwiQa2QXindVUHw=
|
github.com/vcaesar/keycode v0.10.1 h1:0DesGmMAPWpYTCYddOFiCMKCDKgNnwiQa2QXindVUHw=
|
||||||
github.com/vcaesar/keycode v0.10.1/go.mod h1:JNlY7xbKsh+LAGfY2j4M3znVrGEm5W1R8s/Uv6BJcfQ=
|
github.com/vcaesar/keycode v0.10.1/go.mod h1:JNlY7xbKsh+LAGfY2j4M3znVrGEm5W1R8s/Uv6BJcfQ=
|
||||||
github.com/vcaesar/tt v0.20.0 h1:9t2Ycb9RNHcP0WgQgIaRKJBB+FrRdejuaL6uWIHuoBA=
|
github.com/vcaesar/tt v0.20.1 h1:D/jUeeVCNbq3ad8M7hhtB3J9x5RZ6I1n1eZ0BJp7M+4=
|
||||||
github.com/vcaesar/tt v0.20.0/go.mod h1:GHPxQYhn+7OgKakRusH7KJ0M5MhywoeLb8Fcffs/Gtg=
|
github.com/vcaesar/tt v0.20.1/go.mod h1:cH2+AwGAJm19Wa6xvEa+0r+sXDJBT0QgNQey6mwqLeU=
|
||||||
|
15
hook.go
15
hook.go
@ -93,9 +93,11 @@ var (
|
|||||||
lck sync.RWMutex
|
lck sync.RWMutex
|
||||||
|
|
||||||
pressed = make(map[uint16]bool, 256)
|
pressed = make(map[uint16]bool, 256)
|
||||||
|
uppressed = make(map[uint16]bool, 256)
|
||||||
used = []int{}
|
used = []int{}
|
||||||
|
|
||||||
keys = map[int][]uint16{}
|
keys = map[int][]uint16{}
|
||||||
|
upkeys = map[int][]uint16{}
|
||||||
cbs = map[int]func(Event){}
|
cbs = map[int]func(Event){}
|
||||||
events = map[uint8][]int{}
|
events = map[uint8][]int{}
|
||||||
)
|
)
|
||||||
@ -116,12 +118,17 @@ func Register(when uint8, cmds []string, cb func(Event)) {
|
|||||||
key := len(used)
|
key := len(used)
|
||||||
used = append(used, key)
|
used = append(used, key)
|
||||||
tmp := []uint16{}
|
tmp := []uint16{}
|
||||||
|
uptmp := []uint16{}
|
||||||
|
|
||||||
for _, v := range cmds {
|
for _, v := range cmds {
|
||||||
|
if when == KeyUp {
|
||||||
|
uptmp = append(uptmp, Keycode[v])
|
||||||
|
}
|
||||||
tmp = append(tmp, Keycode[v])
|
tmp = append(tmp, Keycode[v])
|
||||||
}
|
}
|
||||||
|
|
||||||
keys[key] = tmp
|
keys[key] = tmp
|
||||||
|
upkeys[key] = uptmp
|
||||||
cbs[key] = cb
|
cbs[key] = cb
|
||||||
events[when] = append(events[when], key)
|
events[when] = append(events[when], key)
|
||||||
// return
|
// return
|
||||||
@ -134,6 +141,7 @@ func Process(evChan <-chan Event) (out chan bool) {
|
|||||||
for ev := range evChan {
|
for ev := range evChan {
|
||||||
if ev.Kind == KeyDown || ev.Kind == KeyHold {
|
if ev.Kind == KeyDown || ev.Kind == KeyHold {
|
||||||
pressed[ev.Keycode] = true
|
pressed[ev.Keycode] = true
|
||||||
|
uppressed[ev.Keycode] = true
|
||||||
} else if ev.Kind == KeyUp {
|
} else if ev.Kind == KeyUp {
|
||||||
pressed[ev.Keycode] = false
|
pressed[ev.Keycode] = false
|
||||||
}
|
}
|
||||||
@ -145,6 +153,12 @@ func Process(evChan <-chan Event) (out chan bool) {
|
|||||||
|
|
||||||
if allPressed(pressed, keys[v]...) {
|
if allPressed(pressed, keys[v]...) {
|
||||||
cbs[v](ev)
|
cbs[v](ev)
|
||||||
|
} else if ev.Kind == KeyUp {
|
||||||
|
//uppressed[ev.Keycode] = true
|
||||||
|
if allPressed(uppressed, upkeys[v]...) {
|
||||||
|
uppressed = make(map[uint16]bool, 256)
|
||||||
|
cbs[v](ev)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -253,6 +267,7 @@ func End() {
|
|||||||
close(ev)
|
close(ev)
|
||||||
|
|
||||||
pressed = make(map[uint16]bool, 256)
|
pressed = make(map[uint16]bool, 256)
|
||||||
|
uppressed = make(map[uint16]bool, 256)
|
||||||
used = []int{}
|
used = []int{}
|
||||||
|
|
||||||
keys = map[int][]uint16{}
|
keys = map[int][]uint16{}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user