mirror of
https://github.com/robotn/gohook.git
synced 2025-01-18 13:46:41 +08:00
Merge pull request #12 from robotn/new-pr
add modern and concurrent API, merged #10
This commit is contained in:
commit
ac130dbd90
83
hook.go
83
hook.go
@ -90,8 +90,71 @@ var (
|
||||
asyncon = false
|
||||
|
||||
lck sync.RWMutex
|
||||
|
||||
pressed = make(map[uint16]bool, 256)
|
||||
used = []int{}
|
||||
|
||||
keys = map[int][]uint16{}
|
||||
cbs = map[int]func(Event){}
|
||||
events = map[uint8][]int{}
|
||||
)
|
||||
|
||||
func allPressed(pressed map[uint16]bool, keys ...uint16) bool {
|
||||
for _, i := range keys {
|
||||
// fmt.Println(i)
|
||||
if pressed[i] == false {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// Register register gohook event
|
||||
func Register(when uint8, cmds []string, cb func(Event)) {
|
||||
key := len(used)
|
||||
used = append(used, key)
|
||||
tmp := []uint16{}
|
||||
|
||||
for _, v := range cmds {
|
||||
tmp = append(tmp, Keycode[v])
|
||||
}
|
||||
|
||||
keys[key] = tmp
|
||||
cbs[key] = cb
|
||||
events[when] = append(events[when], key)
|
||||
return
|
||||
}
|
||||
|
||||
// Process return go hook process
|
||||
func Process(EvChan <-chan Event) (out chan bool) {
|
||||
out = make(chan bool)
|
||||
go func() {
|
||||
for ev := range EvChan {
|
||||
if ev.Kind == KeyDown || ev.Kind == KeyHold {
|
||||
pressed[ev.Keycode] = true
|
||||
} else if ev.Kind == KeyUp {
|
||||
pressed[ev.Keycode] = false
|
||||
}
|
||||
|
||||
for _, v := range events[ev.Kind] {
|
||||
if !asyncon {
|
||||
break
|
||||
}
|
||||
|
||||
if allPressed(pressed, keys[v]...) {
|
||||
cbs[v](ev)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// fmt.Println("exiting after end (process)")
|
||||
out <- true
|
||||
}()
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
// String return hook kind string
|
||||
func (e Event) String() string {
|
||||
switch e.Kind {
|
||||
@ -157,18 +220,19 @@ func KeychartoRawcode(kc string) uint16 {
|
||||
// Start Adds global event hook to OS
|
||||
// returns event channel
|
||||
func Start() chan Event {
|
||||
asyncon = true
|
||||
ev = make(chan Event, 1024)
|
||||
go C.start_ev()
|
||||
|
||||
asyncon = true
|
||||
go func() {
|
||||
for {
|
||||
C.pollEv()
|
||||
time.Sleep(time.Millisecond * 50)
|
||||
|
||||
// todo: find smallest time that does not destroy the cpu utilization
|
||||
if !asyncon {
|
||||
return
|
||||
}
|
||||
|
||||
C.pollEv()
|
||||
time.Sleep(time.Millisecond * 50)
|
||||
//todo: find smallest time that does not destroy the cpu utilization
|
||||
}
|
||||
}()
|
||||
|
||||
@ -177,14 +241,21 @@ func Start() chan Event {
|
||||
|
||||
// End removes global event hook
|
||||
func End() {
|
||||
asyncon = false
|
||||
C.endPoll()
|
||||
C.stop_event()
|
||||
|
||||
for len(ev) != 0 {
|
||||
<-ev
|
||||
}
|
||||
close(ev)
|
||||
|
||||
asyncon = false
|
||||
pressed = make(map[uint16]bool, 256)
|
||||
used = []int{}
|
||||
|
||||
keys = map[int][]uint16{}
|
||||
cbs = map[int]func(Event){}
|
||||
events = map[uint8][]int{}
|
||||
}
|
||||
|
||||
// AddEvent add event listener
|
||||
|
112
keycode.go
Normal file
112
keycode.go
Normal file
@ -0,0 +1,112 @@
|
||||
// Copyright 2016 The go-vgo Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// https://github.com/go-vgo/robotgo/blob/master/LICENSE
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
package hook
|
||||
|
||||
type uMap map[string]uint16
|
||||
|
||||
// MouseMap robotgo hook mouse's code map
|
||||
var MouseMap = uMap{
|
||||
"left": 1,
|
||||
"right": 2,
|
||||
"center": 3,
|
||||
"wheelDown": 4,
|
||||
"wheelUp": 5,
|
||||
"wheelLeft": 6,
|
||||
"wheelRight": 7,
|
||||
}
|
||||
|
||||
// Keycode robotgo hook key's code map
|
||||
var Keycode = uMap{
|
||||
"`": 41,
|
||||
"1": 2,
|
||||
"2": 3,
|
||||
"3": 4,
|
||||
"4": 5,
|
||||
"5": 6,
|
||||
"6": 7,
|
||||
"7": 8,
|
||||
"8": 9,
|
||||
"9": 10,
|
||||
"0": 11,
|
||||
"-": 12,
|
||||
"+": 13,
|
||||
//
|
||||
"q": 16,
|
||||
"w": 17,
|
||||
"e": 18,
|
||||
"r": 19,
|
||||
"t": 20,
|
||||
"y": 21,
|
||||
"u": 22,
|
||||
"i": 23,
|
||||
"o": 24,
|
||||
"p": 25,
|
||||
"[": 26,
|
||||
"]": 27,
|
||||
"\\": 43,
|
||||
//
|
||||
"a": 30,
|
||||
"s": 31,
|
||||
"d": 32,
|
||||
"f": 33,
|
||||
"g": 34,
|
||||
"h": 35,
|
||||
"j": 36,
|
||||
"k": 37,
|
||||
"l": 38,
|
||||
";": 39,
|
||||
"'": 40,
|
||||
//
|
||||
"z": 44,
|
||||
"x": 45,
|
||||
"c": 46,
|
||||
"v": 47,
|
||||
"b": 48,
|
||||
"n": 49,
|
||||
"m": 50,
|
||||
",": 51,
|
||||
".": 52,
|
||||
"/": 53,
|
||||
//
|
||||
"f1": 59,
|
||||
"f2": 60,
|
||||
"f3": 61,
|
||||
"f4": 62,
|
||||
"f5": 63,
|
||||
"f6": 64,
|
||||
"f7": 65,
|
||||
"f8": 66,
|
||||
"f9": 67,
|
||||
"f10": 68,
|
||||
"f11": 69,
|
||||
"f12": 70,
|
||||
// more
|
||||
"esc": 1,
|
||||
"delete": 14,
|
||||
"tab": 15,
|
||||
"ctrl": 29,
|
||||
"control": 29,
|
||||
"alt": 56,
|
||||
"space": 57,
|
||||
"shift": 42,
|
||||
"rshift": 54,
|
||||
"enter": 28,
|
||||
//
|
||||
"cmd": 3675,
|
||||
"command": 3675,
|
||||
"rcmd": 3676,
|
||||
"ralt": 3640,
|
||||
//
|
||||
"up": 57416,
|
||||
"down": 57424,
|
||||
"left": 57419,
|
||||
"right": 57421,
|
||||
}
|
Loading…
Reference in New Issue
Block a user