mirror of
https://github.com/robotn/gohook.git
synced 2024-11-22 02:56:54 +08:00
update godoc and code style
This commit is contained in:
parent
43543e0cff
commit
fe1bbb57e6
@ -2,32 +2,36 @@ package hook
|
||||
|
||||
/*
|
||||
|
||||
|
||||
// #include "event/hook_async.h"
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
//export go_send
|
||||
func go_send(s *C.char) {
|
||||
str := []byte(C.GoString(s))
|
||||
out := Event{}
|
||||
|
||||
err := json.Unmarshal(str, &out)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if out.Keychar != CharUndefined {
|
||||
raw2key[out.Rawcode] = string([]rune{out.Keychar})
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
26
hook.go
26
hook.go
@ -33,15 +33,18 @@ import (
|
||||
const (
|
||||
HookEnabled = 1 // iota
|
||||
HookDisabled = 2
|
||||
|
||||
KeyDown = 3
|
||||
KeyHold = 4
|
||||
KeyUp = 5
|
||||
|
||||
MouseUp = 6
|
||||
MouseHold = 7
|
||||
MouseDown = 8
|
||||
MouseMove = 9
|
||||
MouseDrag = 10
|
||||
MouseWheel = 11
|
||||
|
||||
FakeEvent = 12
|
||||
//Keychar could be v
|
||||
CharUndefined = 0xFFFF
|
||||
@ -49,22 +52,28 @@ const (
|
||||
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:
|
||||
// Event 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
|
||||
Mask uint16 `json:"mask"`
|
||||
Reserved uint16 `json:"reserved"`
|
||||
|
||||
Keycode uint16 `json:"keycode"`
|
||||
Rawcode uint16 `json:"rawcode"`
|
||||
Keychar rune `json:"keychar"`
|
||||
|
||||
Button uint16 `json:"button"`
|
||||
Clicks uint16 `json:"clicks"`
|
||||
|
||||
X int16 `json:"x"`
|
||||
Y int16 `json:"y"`
|
||||
|
||||
Amount uint16 `json:"amount"`
|
||||
Rotation int16 `json:"rotation"`
|
||||
Direction uint8 `json:"direction"`
|
||||
@ -75,6 +84,7 @@ var (
|
||||
asyncon = false
|
||||
)
|
||||
|
||||
// String return hook kind string
|
||||
func (e Event) String() string {
|
||||
switch e.Kind {
|
||||
case HookEnabled:
|
||||
@ -102,32 +112,38 @@ func (e Event) String() string {
|
||||
case FakeEvent:
|
||||
return fmt.Sprintf("%v - Event: {Kind: FakeEvent}", e.When)
|
||||
}
|
||||
|
||||
return "Unknown event, contact the mantainers"
|
||||
}
|
||||
|
||||
// RawcodetoKeychar rawcode to keychar
|
||||
func RawcodetoKeychar(r uint16) string {
|
||||
return raw2key[r]
|
||||
}
|
||||
|
||||
// KeychartoiRawcode key char to rawcode
|
||||
func KeychartoRawcode(kc string) uint16 {
|
||||
return keytoraw[kc]
|
||||
}
|
||||
|
||||
// Adds global event hook to OS
|
||||
// Start Adds global event hook to OS
|
||||
// returns event channel
|
||||
func Start() chan Event {
|
||||
asyncon = true
|
||||
go C.startev()
|
||||
|
||||
go func() {
|
||||
for {
|
||||
C.pollEv()
|
||||
time.Sleep(time.Millisecond * 50)
|
||||
|
||||
// todo: find smallest time that does not destroy the cpu utilization
|
||||
if !asyncon {
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return ev
|
||||
}
|
||||
|
||||
@ -135,9 +151,11 @@ func Start() chan Event {
|
||||
func End() {
|
||||
C.endPoll()
|
||||
C.stop_event()
|
||||
|
||||
for len(ev) != 0 {
|
||||
<-ev
|
||||
}
|
||||
|
||||
asyncon = false
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user