Skip to content

Commit

Permalink
Fixed issue with keyboardWindows
Browse files Browse the repository at this point in the history
Fixed an issue with keyboardWindows that could cause old events to be
processed due to a missing check for ctx.Done()
  • Loading branch information
nathan-fiscaletti committed Nov 1, 2024
1 parent 9d31210 commit 36a2fa7
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions pkg/keyboard/keyboard_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,25 +67,31 @@ func (k keyboardWindows) Events(ctx context.Context) (chan Event, error) {
k.eventChan = make(chan Event)
go func() {
hookCallback := syscall.NewCallback(func(nCode int, wParam, lParam uintptr) uintptr {
if nCode >= 0 {
kbStruct := (*kbDLLHOOKSTRUCT)(unsafe.Pointer(lParam))
select {
case <-ctx.Done():
return 0

var eventType EventType = KeyboardEventTypeDown
if kbStruct.Flags == flagKeyUp {
eventType = KeyboardEventTypeUp
}
default:
if nCode >= 0 {
kbStruct := (*kbDLLHOOKSTRUCT)(unsafe.Pointer(lParam))

event := Event{
Key: key.FindKeyCode(kbStruct.VKCode),
EventType: eventType,
Timestamp: parseTime(kbStruct.Time),
var eventType EventType = KeyboardEventTypeDown
if kbStruct.Flags == flagKeyUp {
eventType = KeyboardEventTypeUp
}

event := Event{
Key: key.FindKeyCode(kbStruct.VKCode),
EventType: eventType,
Timestamp: parseTime(kbStruct.Time),
}

k.eventChan <- event
}

k.eventChan <- event
r1, _, _ := procCallNextHookEx.Call(0, uintptr(nCode), wParam, lParam)
return r1
}

r1, _, _ := procCallNextHookEx.Call(0, uintptr(nCode), wParam, lParam)
return r1
})

h, _, _ := procSetWindowsHookEx.Call(uintptr(keyboardHook), hookCallback, 0, 0)
Expand Down

0 comments on commit 36a2fa7

Please sign in to comment.