robotn / gohook

GoHook, Go global keyboard and mouse listener hook
MIT License
324 stars 43 forks source link

Keyboard listener on windows #18

Closed sunshineplan closed 1 year ago

sunshineplan commented 4 years ago

I see some confusion or errors when listen keyboard on win10

Code:

package main

import (
    "fmt"

    hook "github.com/robotn/gohook"
)

func main() {
    kind := map[uint8]string{3: "KeyDown", 4: "KeyHold", 5: "KeyUp"}
    when := []uint8{hook.KeyDown, hook.KeyHold, hook.KeyUp}

    for _, w := range when {
        hook.Register(w, nil, func(e hook.Event) {
            char := hook.RawcodetoKeychar(e.Rawcode)
            fmt.Printf("Kind: %7v, Rawcode: %3v, Keychar: %5v, Keycode: %5v, Char: %v\n", kind[e.Kind], e.Rawcode, e.Keychar, e.Keycode, char)
        })
    }

    s := hook.Start()
    <-hook.Process(s)
}

When I press left shift, ctrl, alt and right alt, ctrl, shift, I get: (which I guess take a wrong rawcode)

Kind: KeyHold, Rawcode: 160, Keychar: 65535, Keycode:    42, Char: ^
Kind:   KeyUp, Rawcode: 160, Keychar: 65535, Keycode:    42, Char: ^
Kind: KeyHold, Rawcode: 162, Keychar: 65535, Keycode:    29, Char: ؛
Kind:   KeyUp, Rawcode: 162, Keychar: 65535, Keycode:    29, Char: ؛
Kind: KeyHold, Rawcode: 164, Keychar: 65535, Keycode:    56, Char: $
Kind:   KeyUp, Rawcode: 164, Keychar: 65535, Keycode:    56, Char: $
Kind: KeyHold, Rawcode: 165, Keychar: 65535, Keycode:  3640, Char: ù
Kind:   KeyUp, Rawcode: 165, Keychar: 65535, Keycode:  3640, Char: ù
Kind: KeyHold, Rawcode: 163, Keychar: 65535, Keycode:  3613, Char: #
Kind:   KeyUp, Rawcode: 163, Keychar: 65535, Keycode:  3613, Char: #
Kind: KeyHold, Rawcode: 161, Keychar: 65535, Keycode:    54, Char: !
Kind:   KeyUp, Rawcode: 161, Keychar: 65535, Keycode:    54, Char: !

When I press tab, space, enter, backspace, I get: (which rawcode cannot convert to keychar)

Kind: KeyHold, Rawcode:   9, Keychar: 65535, Keycode:    15, Char: 
Kind: KeyDown, Rawcode:   9, Keychar:     9, Keycode:     0, Char: 
Kind:   KeyUp, Rawcode:   9, Keychar: 65535, Keycode:    15, Char: 
Kind: KeyHold, Rawcode:  32, Keychar: 65535, Keycode:    57, Char:  
Kind: KeyDown, Rawcode:  32, Keychar:    32, Keycode:     0, Char:  
Kind:   KeyUp, Rawcode:  32, Keychar: 65535, Keycode:    57, Char:  
Kind: KeyHold, Rawcode:  13, Keychar: 65535, Keycode:    28, Char: 
Kind: KeyDown, Rawcode:  13, Keychar:    13, Keycode:     0, Char: 
Kind:   KeyUp, Rawcode:  13, Keychar: 65535, Keycode:    28, Char: 
Kind: KeyHold, Rawcode:   8, Keychar: 65535, Keycode:    14, Char: 
Kind: KeyDown, Rawcode:   8, Keychar:     8, Keycode:     0, Char: 
Kind:   KeyUp, Rawcode:   8, Keychar: 65535, Keycode:    14, Char: 

When I press something with esc, I get: (which rawcode cannot convert to keychar, and erase a char after it when printing)

Kind: KeyHold, Rawcode:  66, Keychar: 65535, Keycode:    48, Char: b
Kind: KeyDown, Rawcode:  66, Keychar:    98, Keycode:     0, Char: b
Kind:   KeyUp, Rawcode:  66, Keychar: 65535, Keycode:    48, Char: b
Kind: KeyHold, Rawcode:  65, Keychar: 65535, Keycode:    30, Char: a
Kind: KeyDown, Rawcode:  65, Keychar:    97, Keycode:     0, Char: a
Kind:   KeyUp, Rawcode:  65, Keychar: 65535, Keycode:    30, Char: a
Kind: KeyHold, Rawcode:  67, Keychar: 65535, Keycode:    46, Char: c
Kind: KeyDown, Rawcode:  67, Keychar:    99, Keycode:     0, Char: c
Kind:   KeyUp, Rawcode:  67, Keychar: 65535, Keycode:    46, Char: c
Kind: KeyHold, Rawcode:  27, Keychar: 65535, Keycode:     1, Char: 
ind: KeyDown, Rawcode:  27, Keychar:    27, Keycode:     0, Char: 
ind:   KeyUp, Rawcode:  27, Keychar: 65535, Keycode:     1, Char: 
ind: KeyHold, Rawcode:  65, Keychar: 65535, Keycode:    30, Char: a
Kind: KeyDown, Rawcode:  65, Keychar:    97, Keycode:     0, Char: a
Kind:   KeyUp, Rawcode:  65, Keychar: 65535, Keycode:    30, Char: a

Other keys display just perfectly

cauefcr commented 4 years ago

I haven't found a good cross-platform way to do conversion of arbitrary keycodes to chars yet, you will have to make your own hash table (map[uint16]string) on the computer it will run, if you find a better way please let me know.

danbai225 commented 2 years ago

I also had this problem, I mapped by map https://github.com/danbai225/down_tip/blob/main/module/keylog/keymap.go