mirror of
https://github.com/robotn/gohook.git
synced 2025-04-28 19:27:17 +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)
|
[](https://godoc.org/github.com/cauefcr/gohook)
|
||||||
<!-- This is a work in progress. -->
|
<!-- This is a work in progress. -->
|
||||||
|
|
||||||
|
Based on [libuiohook](https://github.com/kwhat/libuiohook)
|
||||||
|
|
||||||
```Go
|
```Go
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
@ -17,9 +17,10 @@ func go_send(s *C.char) {
|
|||||||
str := []byte(C.GoString(s))
|
str := []byte(C.GoString(s))
|
||||||
out := Event{}
|
out := Event{}
|
||||||
err := json.Unmarshal(str, &out)
|
err := json.Unmarshal(str, &out)
|
||||||
if err != nil{
|
if err != nil {
|
||||||
log.Fatal(err)
|
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
|
out.When = time.Now() //at least it's consistent
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
37
hook.go
37
hook.go
@ -29,29 +29,36 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
//todo: add enums
|
|
||||||
const (
|
const (
|
||||||
HOOK_ENABLED = 1 //iota
|
HookEnabled = 1 //iota
|
||||||
HOOK_DISABLED = 2
|
HookDisabled = 2
|
||||||
KEY_TYPED = 3
|
KeyDown = 3
|
||||||
KEY_PRESSED = 4
|
KeyHold = 4
|
||||||
KEY_RELEASED = 5
|
KeyUp = 5
|
||||||
MOUSE_CLICKED = 6
|
MouseUp = 6
|
||||||
MOUSE_PRESSED = 7
|
MouseHold = 7
|
||||||
MOUSE_RELEASED = 8
|
MouseDown = 8
|
||||||
MOUSE_MOVED = 9
|
MouseMove = 9
|
||||||
MOUSE_DRAGGED = 10
|
MouseDrag = 10
|
||||||
MOUSE_WHEEL = 11
|
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 {
|
type Event struct {
|
||||||
Kind uint8 `json:"id"`
|
Kind uint8 `json:"id"`
|
||||||
When time.Time
|
When time.Time
|
||||||
Mask uint16 `json:"mask"`
|
Mask uint16 `json:"mask"`
|
||||||
Reserved uint16 `json:"reserved"`
|
Reserved uint16 `json:"reserved"`
|
||||||
Keycode uint16 `json:"keycode"`
|
Keycode uint16 `json:"keycode"`
|
||||||
Rawcode uint16 `json:"rawcode"`
|
Rawcode uint16 `json:"rawcode"`
|
||||||
Keychar uint16 `json:"keychar"`
|
Keychar rune `json:"keychar"`
|
||||||
Button uint16 `json:"button"`
|
Button uint16 `json:"button"`
|
||||||
Clicks uint16 `json:"clicks"`
|
Clicks uint16 `json:"clicks"`
|
||||||
X int16 `json:"x"`
|
X int16 `json:"x"`
|
||||||
@ -76,7 +83,7 @@ func Start() chan Event {
|
|||||||
C.pollEv()
|
C.pollEv()
|
||||||
time.Sleep(time.Millisecond * 50)
|
time.Sleep(time.Millisecond * 50)
|
||||||
//todo: find smallest time that does not destroy the cpu utilization
|
//todo: find smallest time that does not destroy the cpu utilization
|
||||||
if ! asyncon {
|
if !asyncon {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -79,7 +79,7 @@ typedef struct _screen_data {
|
|||||||
typedef struct _keyboard_event_data {
|
typedef struct _keyboard_event_data {
|
||||||
uint16_t keycode;
|
uint16_t keycode;
|
||||||
uint16_t rawcode;
|
uint16_t rawcode;
|
||||||
uint16_t keychar;
|
uint32_t keychar;
|
||||||
// char *keychar;
|
// char *keychar;
|
||||||
} keyboard_event_data,
|
} keyboard_event_data,
|
||||||
key_pressed_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];
|
scancode = keycode_scancode_table[vk_code][0];
|
||||||
|
|
||||||
if (flags & LLKHF_EXTENDED) {
|
if (flags & LLKHF_EXTENDED) {
|
||||||
logger(LOG_LEVEL_WARN, "%s [%u]: EXTD2, vk_code %li\n",
|
// logger(LOG_LEVEL_WARN, "%s [%u]: EXTD2, vk_code %li\n",
|
||||||
__FUNCTION__, __LINE__, vk_code);
|
// __FUNCTION__, __LINE__, vk_code);
|
||||||
|
|
||||||
switch (vk_code) {
|
switch (vk_code) {
|
||||||
case VK_PRIOR:
|
case VK_PRIOR:
|
||||||
|
18
test/main.go
18
test/main.go
@ -9,21 +9,29 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := hook.Start()
|
s := hook.Start()
|
||||||
|
defer hook.End()
|
||||||
tout := time.After(time.Second * 10)
|
tout := time.After(time.Second * 10)
|
||||||
done := false
|
done := false
|
||||||
for !done {
|
for !done {
|
||||||
select {
|
select {
|
||||||
case i := <-s:
|
case i := <-s:
|
||||||
if i.Keychar == uint16('q') {
|
if i.Kind == hook.KeyDown {
|
||||||
tout = time.After(1 * time.Millisecond)
|
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:
|
case <-tout:
|
||||||
fmt.Print("Done.")
|
fmt.Print("Done.")
|
||||||
done = true
|
done = true
|
||||||
break;
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
hook.End()
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user