Compare commits

...

4 Commits

Author SHA1 Message Date
vcaesar
f049036b8c Update: update examples and README.md 2025-02-08 15:44:55 -08:00
mfkgef
0d69fe6a36
Fix Issues #48 (#50)
* fix KeyUp event bug

* Update go.mod

---------

Co-authored-by: Evans <vzvway@gmail.com>
2025-02-08 15:36:18 -08:00
vcaesar
b93f6ca661 Update: update CI and go mod 2024-08-26 16:42:12 -07:00
vcaesar
c94ab299da Update: bump CI to the newest 2024-02-23 08:57:29 -08:00
7 changed files with 42 additions and 19 deletions

View File

@ -10,10 +10,10 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- name: Set up Go 1.21.0
- name: Set up Go 1.23.0
uses: actions/setup-go@v1
with:
go-version: 1.21.0
go-version: 1.23.0
id: go
- name: Check out code into the Go module directory

View File

@ -45,7 +45,11 @@ func add() {
fmt.Println("--- Please press w---")
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()

View File

@ -34,7 +34,7 @@ environment:
PATH: C:\msys64\mingw32\bin\;C:\Program Files (x86)\NSIS\;%PATH%
# - COMPILER: MINGW_W64
# ARCHITECTURE: x64
GOVERSION: 1.21.0
GOVERSION: 1.23.0
# GOPATH: c:\gopath
# scripts that run after cloning repository

View File

@ -6,6 +6,15 @@ import (
hook "github.com/robotn/gohook"
)
func main() {
registerEvent()
base()
add()
addMouse()
}
func registerEvent() {
fmt.Println("--- Please press ctrl + shift + q to stop hook ---")
hook.Register(hook.KeyDown, []string{"q", "ctrl", "shift"}, func(e hook.Event) {
@ -15,7 +24,11 @@ func registerEvent() {
fmt.Println("--- Please press w ---")
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()
@ -69,12 +82,3 @@ func base() {
}
}
}
func main() {
registerEvent()
base()
add()
addMouse()
}

2
go.mod
View File

@ -4,5 +4,5 @@ go 1.17
require (
github.com/vcaesar/keycode v0.10.1
github.com/vcaesar/tt v0.20.0
github.com/vcaesar/tt v0.20.1
)

4
go.sum
View File

@ -1,4 +1,4 @@
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/tt v0.20.0 h1:9t2Ycb9RNHcP0WgQgIaRKJBB+FrRdejuaL6uWIHuoBA=
github.com/vcaesar/tt v0.20.0/go.mod h1:GHPxQYhn+7OgKakRusH7KJ0M5MhywoeLb8Fcffs/Gtg=
github.com/vcaesar/tt v0.20.1 h1:D/jUeeVCNbq3ad8M7hhtB3J9x5RZ6I1n1eZ0BJp7M+4=
github.com/vcaesar/tt v0.20.1/go.mod h1:cH2+AwGAJm19Wa6xvEa+0r+sXDJBT0QgNQey6mwqLeU=

19
hook.go
View File

@ -92,10 +92,12 @@ var (
lck sync.RWMutex
pressed = make(map[uint16]bool, 256)
used = []int{}
pressed = make(map[uint16]bool, 256)
uppressed = make(map[uint16]bool, 256)
used = []int{}
keys = map[int][]uint16{}
upkeys = map[int][]uint16{}
cbs = map[int]func(Event){}
events = map[uint8][]int{}
)
@ -116,12 +118,17 @@ func Register(when uint8, cmds []string, cb func(Event)) {
key := len(used)
used = append(used, key)
tmp := []uint16{}
uptmp := []uint16{}
for _, v := range cmds {
if when == KeyUp {
uptmp = append(uptmp, Keycode[v])
}
tmp = append(tmp, Keycode[v])
}
keys[key] = tmp
upkeys[key] = uptmp
cbs[key] = cb
events[when] = append(events[when], key)
// return
@ -134,6 +141,7 @@ func Process(evChan <-chan Event) (out chan bool) {
for ev := range evChan {
if ev.Kind == KeyDown || ev.Kind == KeyHold {
pressed[ev.Keycode] = true
uppressed[ev.Keycode] = true
} else if ev.Kind == KeyUp {
pressed[ev.Keycode] = false
}
@ -145,6 +153,12 @@ func Process(evChan <-chan Event) (out chan bool) {
if allPressed(pressed, keys[v]...) {
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)
pressed = make(map[uint16]bool, 256)
uppressed = make(map[uint16]bool, 256)
used = []int{}
keys = map[int][]uint16{}