forked from go-vgo/robotgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhook.go
197 lines (164 loc) · 3.8 KB
/
hook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// 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 robotgo
import (
"strconv"
hook "github.com/robotn/gohook"
)
/*
___________ ____ _______ .__ __. .___________.
| ____\ \ / / | ____|| \ | | | |
| |__ \ \/ / | |__ | \| | `---| |----`
| __| \ / | __| | . ` | | |
| |____ \ / | |____ | |\ | | |
|_______| \__/ |_______||__| \__| |__|
*/
// EventStart start global event hook
// return event channel
func EventStart() chan hook.Event {
return hook.Start()
}
// EventEnd removes global event hook
func EventEnd() {
hook.End()
}
// EventProcess return go hook process
func EventProcess(Events chan hook.Event) chan bool {
return hook.Process(Events)
}
// EventHook register gohook event
func EventHook(When uint8, keysPressed []string, Callback func(hook.Event)) {
hook.Register(When, keysPressed, Callback)
return
}
// AddEvent add event listener,
//
// parameters for the string type,
// the keyboard corresponding key parameters,
//
// mouse arguments: mleft, center, mright, wheelDown, wheelUp,
// wheelLeft, wheelRight.
//
// Use "robotgo.AddEvents()" or "gohook" add asynchronous event listener
func AddEvent(key string) bool {
var (
// cs *C.char
mArr = []string{"mleft", "center", "mright", "wheelDown",
"wheelUp", "wheelLeft", "wheelRight"}
mouseBool bool
)
for i := 0; i < len(mArr); i++ {
if key == mArr[i] {
mouseBool = true
}
}
if len(key) > 1 && !mouseBool {
key = strconv.Itoa(int(Keycode[key]))
}
geve := hook.AddEvent(key)
// defer C.free(unsafe.Pointer(cs))
if geve == 0 {
return true
}
return false
}
// StopEvent stop event listener
func StopEvent() {
hook.StopEvent()
}
// Start start global event hook
// return event channel
func Start() chan hook.Event {
return hook.Start()
}
// End removes global event hook
func End() {
hook.End()
}
// AddEvents add global event hook
//
// robotgo.AddEvents("q")
// robotgo.AddEvents("q", "ctrl")
// robotgo.AddEvents("q", "ctrl", "shift")
func AddEvents(key string, arr ...string) bool {
s := hook.Start()
// defer hook.End()
ct := false
k := 0
for {
e := <-s
l := len(arr)
if l > 0 {
for i := 0; i < l; i++ {
ukey := Keycode[arr[i]]
if e.Kind == hook.KeyHold && e.Keycode == ukey {
k++
}
if k == l {
ct = true
}
if e.Kind == hook.KeyUp && e.Keycode == ukey {
if k > 0 {
k--
}
// time.Sleep(10 * time.Microsecond)
ct = false
}
}
} else {
ct = true
}
if ct && e.Kind == hook.KeyUp && e.Keycode == Keycode[key] {
hook.End()
// k = 0
break
}
}
return true
}
// AddMouse add mouse event hook
//
// mouse arguments: left, center, right, wheelDown, wheelUp,
// wheelLeft, wheelRight.
//
// robotgo.AddMouse("left")
// robotgo.AddMouse("left", 100, 100)
func AddMouse(btn string, x ...int16) bool {
s := hook.Start()
ukey := MouseMap[btn]
ct := false
for {
e := <-s
if len(x) > 1 {
if e.Kind == hook.MouseMove && e.X == x[0] && e.Y == x[1] {
ct = true
}
} else {
ct = true
}
if ct && e.Kind == hook.MouseDown && e.Button == ukey {
hook.End()
break
}
}
return true
}
// AddMousePos add listen mouse event pos hook
func AddMousePos(x, y int16) bool {
s := hook.Start()
for {
e := <-s
if e.Kind == hook.MouseMove && e.X == x && e.Y == y {
hook.End()
break
}
}
return true
}