diff --git a/README.md b/README.md index 0fa0dc7..80c632b 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ [![GoDoc](https://godoc.org/github.com/cauefcr/gohook?status.svg)](https://godoc.org/github.com/cauefcr/gohook) +Based on [libuiohook](https://github.com/kwhat/libuiohook) + ```Go package main diff --git a/extern.go b/extern.go index 2b0dfeb..76fda10 100644 --- a/extern.go +++ b/extern.go @@ -17,13 +17,14 @@ func go_send(s *C.char) { str := []byte(C.GoString(s)) out := Event{} err := json.Unmarshal(str, &out) - if err != nil{ + if err != nil { log.Fatal(err) } + //todo bury this deep into the C lib so that the time is correct out.When = time.Now() //at least it's consistent if err != nil { log.Fatal(err) } //todo: maybe make non-bloking ev <- out -} \ No newline at end of file +} diff --git a/hook.go b/hook.go index 4d181f0..6a2991c 100644 --- a/hook.go +++ b/hook.go @@ -29,29 +29,36 @@ import ( "time" ) -//todo: add enums const ( - HOOK_ENABLED = 1 //iota - HOOK_DISABLED = 2 - KEY_TYPED = 3 - KEY_PRESSED = 4 - KEY_RELEASED = 5 - MOUSE_CLICKED = 6 - MOUSE_PRESSED = 7 - MOUSE_RELEASED = 8 - MOUSE_MOVED = 9 - MOUSE_DRAGGED = 10 - MOUSE_WHEEL = 11 + HookEnabled = 1 //iota + HookDisabled = 2 + KeyDown = 3 + KeyHold = 4 + KeyUp = 5 + MouseUp = 6 + MouseHold = 7 + MouseDown = 8 + MouseMove = 9 + MouseDrag = 10 + MouseWheel = 11 + //Keychar could be v + CharUndefined = 0xFFFF + WheelUp = -1 + WheelDown = 1 ) +//Holds a system event +//If it's a Keyboard event the relevant fields are: Mask, Keycode, Rawcode, and Keychar, +//Keychar is probably what you want. If it's a Mouse event the relevant fields are: +//Button, Clicks, X, Y, Amount, Rotation and Direction type Event struct { - Kind uint8 `json:"id"` + Kind uint8 `json:"id"` When time.Time Mask uint16 `json:"mask"` Reserved uint16 `json:"reserved"` Keycode uint16 `json:"keycode"` Rawcode uint16 `json:"rawcode"` - Keychar uint16 `json:"keychar"` + Keychar rune `json:"keychar"` Button uint16 `json:"button"` Clicks uint16 `json:"clicks"` X int16 `json:"x"` @@ -76,7 +83,7 @@ func Start() chan Event { C.pollEv() time.Sleep(time.Millisecond * 50) //todo: find smallest time that does not destroy the cpu utilization - if ! asyncon { + if !asyncon { return } } diff --git a/hook/iohook.h b/hook/iohook.h index 022c2a1..34a1e94 100644 --- a/hook/iohook.h +++ b/hook/iohook.h @@ -79,7 +79,7 @@ typedef struct _screen_data { typedef struct _keyboard_event_data { uint16_t keycode; uint16_t rawcode; - uint16_t keychar; + uint32_t keychar; // char *keychar; } keyboard_event_data, key_pressed_event_data, diff --git a/hook/windows/input_c.h b/hook/windows/input_c.h index c261c87..995101a 100644 --- a/hook/windows/input_c.h +++ b/hook/windows/input_c.h @@ -293,8 +293,8 @@ unsigned short keycode_to_scancode(DWORD vk_code, DWORD flags) { scancode = keycode_scancode_table[vk_code][0]; if (flags & LLKHF_EXTENDED) { - logger(LOG_LEVEL_WARN, "%s [%u]: EXTD2, vk_code %li\n", - __FUNCTION__, __LINE__, vk_code); +// logger(LOG_LEVEL_WARN, "%s [%u]: EXTD2, vk_code %li\n", +// __FUNCTION__, __LINE__, vk_code); switch (vk_code) { case VK_PRIOR: diff --git a/test/main.go b/test/main.go index 590f714..9685bea 100644 --- a/test/main.go +++ b/test/main.go @@ -9,21 +9,29 @@ import ( func main() { s := hook.Start() + defer hook.End() tout := time.After(time.Second * 10) done := false for !done { select { case i := <-s: - if i.Keychar == uint16('q') { - tout = time.After(1 * time.Millisecond) + if i.Kind == hook.KeyDown { + if i.Keychar == 'q' { + tout = time.After(0) + } + fmt.Printf("key: %c\n", i.Keychar) + } else if i.Kind >= hook.MouseDown && i.Kind < hook.MouseWheel { + fmt.Printf("x: %v, y: %v, button: %v\n", i.X, i.Y, i.Button) + }else if i.Kind == hook.MouseWheel { + fmt.Printf("x: %v, y: %v, button: %v, wheel: %v, rotation: %v\n", i.X, i.Y, i.Button,i.Amount,i.Rotation) + } else { + fmt.Printf("%+v\n",i) } - fmt.Printf("%+v\n", i) case <-tout: fmt.Print("Done.") done = true - break; + break } } - hook.End() }