robotn / gohook

GoHook, Go global keyboard and mouse listener hook
MIT License
314 stars 44 forks source link

Framework doesn't stick to any key codes standard but defines own one #41

Open aleknasz opened 1 year ago

aleknasz commented 1 year ago

Hi All, I have used robotn/gohook in my app by registering hook and consuming events - key down, key hold and key up However I don't think I can write cross-platform application. Below is my explanation:


chanHook := hook.Start()
    defer hook.End()

    go captureEvents(chanHook)

func captureEvents(chanHook <-chan hook.Event) {

for ev := range chanHook {
if ev.Kind == hook.KeyUp {
            fmt.Printf("key up: rawcode=%d rawcode=0x%x keycode=%d keycode=0x%x keychar=%d keychar=0x%x\n\n",
                ev.Rawcode, ev.Rawcode, ev.Keycode, ev.Keycode, ev.Keychar, ev.Keychar)
        } else if ev.Kind == hook.KeyDown {
            fmt.Printf("-----")
            fmt.Printf("key down: rawcode=%d rawcode=0x%x keycode=%d keycode=0x%x keychar=%d keychar=0x%x\n\n",
                ev.Rawcode, ev.Rawcode, ev.Keycode, ev.Keycode, ev.Keychar, ev.Keychar)
        } else if ev.Kind == hook.KeyHold {
            fmt.Printf("key hold: rawcode=%d rawcode=0x%x keycode=%d keycode=0x%x keychar=%d keychar=0x%x\n\n",
                ev.Rawcode, ev.Rawcode, ev.Keycode, ev.Keycode, ev.Keychar, ev.Keychar)
        }
}

}

// The console output is

// when pressed spacebar key hold: rawcode=49 rawcode=0x31 keycode=57 keycode=0x39 keychar=65535 keychar=0xffff key down: rawcode=49 rawcode=0x31 keycode=0 keycode=0x0 keychar=32 keychar=0x20 key up: rawcode=49 rawcode=0x31 keycode=57 keycode=0x39 keychar=65535 keychar=0xffff

// when pressed key a key hold: rawcode=0 rawcode=0x0 keycode=30 keycode=0x1e keychar=65535 keychar=0xffff key down: rawcode=0 rawcode=0x0 keycode=0 keycode=0x0 keychar=97 keychar=0x61 key up: rawcode=0 rawcode=0x0 keycode=30 keycode=0x1e keychar=65535 keychar=0xffff

// when pressed left arrow key hold: rawcode=123 rawcode=0x7b keycode=57419 keycode=0xe04b keychar=65535 keychar=0xffff key down: rawcode=123 rawcode=0x7b keycode=0 keycode=0x0 keychar=28 keychar=0x1c key up: rawcode=123 rawcode=0x7b keycode=57419 keycode=0xe04b keychar=65535 keychar=0xffff

// when pressed right shift (note missing down event) key hold: rawcode=60 rawcode=0x3c keycode=54 keycode=0x36 keychar=65535 keychar=0xffff key up: rawcode=60 rawcode=0x3c keycode=54 keycode=0x36 keychar=65535 keychar=0xffff

// when left shit pressed

key hold: rawcode=56 rawcode=0x38 keycode=42 keycode=0x2a keychar=65535 keychar=0xffff key up: rawcode=56 rawcode=0x38 keycode=42 keycode=0x2a keychar=65535 keychar=0xffff

As you can see, the right and left shift has no down event. Also the order of events is strange. Why hold before down?. The "a" key can't be read as standard "a", since the code is not byte 65, the spacebar has hex code 0x20, which is almost ok, but only in hold event. The left arrow should have hex value 0x25. Actually there is no consistency in events and the values which are populated and attributes which are populated are misleading.

Since the gohook is suppose to be cross-platform, we should have also some cross-platform keystroke values.

Just stick to some standards defined by bigger companies like Mozilla - https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode. Why not Microsoft or Apple? Because Mozilla had always web browsers in mind and try to write cross-platform software.