mirror of
https://github.com/robotn/gohook.git
synced 2025-04-28 15:59:20 +08:00
Changed constants, keychar type and test function
now Keychar is a rune, so it can be used as a regular character.
This commit is contained in:
parent
e52a5e1e9e
commit
7412190046
@ -6,6 +6,8 @@
|
||||
[](https://godoc.org/github.com/cauefcr/gohook)
|
||||
<!-- This is a work in progress. -->
|
||||
|
||||
Based on [libuiohook](https://github.com/kwhat/libuiohook)
|
||||
|
||||
```Go
|
||||
package main
|
||||
|
||||
|
@ -17,9 +17,10 @@ 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)
|
||||
|
35
hook.go
35
hook.go
@ -29,21 +29,28 @@ 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"`
|
||||
When time.Time
|
||||
@ -51,7 +58,7 @@ type Event struct {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -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,
|
||||
|
@ -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:
|
||||
|
18
test/main.go
18
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()
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user