Skip to content

Commit 833856f

Browse files
committed
Move robotgo event code to event.go
1 parent 1dd095b commit 833856f

File tree

2 files changed

+151
-1
lines changed

2 files changed

+151
-1
lines changed

event.go

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
// Copyright 2016 The go-vgo Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// https://github.com/go-vgo/robotgo/blob/master/LICENSE
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
package hook
12+
13+
import "strconv"
14+
15+
/*
16+
___________ ____ _______ .__ __. .___________.
17+
| ____\ \ / / | ____|| \ | | | |
18+
| |__ \ \/ / | |__ | \| | `---| |----`
19+
| __| \ / | __| | . ` | | |
20+
| |____ \ / | |____ | |\ | | |
21+
|_______| \__/ |_______||__| \__| |__|
22+
*/
23+
24+
// AddEvent add event listener,
25+
//
26+
// parameters for the string type,
27+
// the keyboard corresponding key parameters,
28+
//
29+
// mouse arguments: mleft, center, mright, wheelDown, wheelUp,
30+
// wheelLeft, wheelRight.
31+
//
32+
// Use "hook.AddEvents()" or "gohook" add asynchronous event listener
33+
func AddEvent(key string) bool {
34+
var (
35+
// cs *C.char
36+
mArr = []string{"mleft", "center", "mright", "wheelDown",
37+
"wheelUp", "wheelLeft", "wheelRight"}
38+
mouseBool bool
39+
)
40+
41+
for i := 0; i < len(mArr); i++ {
42+
if key == mArr[i] {
43+
mouseBool = true
44+
}
45+
}
46+
47+
if len(key) > 1 && !mouseBool {
48+
key = strconv.Itoa(int(Keycode[key]))
49+
}
50+
51+
geve := addEvent(key)
52+
// defer C.free(unsafe.Pointer(cs))
53+
return geve == 0
54+
}
55+
56+
// AddEvents add global event hook
57+
//
58+
// hook.AddEvents("q")
59+
// hook.AddEvents("q", "ctrl")
60+
// hook.AddEvents("q", "ctrl", "shift")
61+
func AddEvents(key string, arr ...string) bool {
62+
s := Start()
63+
// defer End()
64+
65+
ct := false
66+
k := 0
67+
for {
68+
e := <-s
69+
70+
l := len(arr)
71+
if l > 0 {
72+
for i := 0; i < l; i++ {
73+
ukey := Keycode[arr[i]]
74+
75+
if e.Kind == KeyHold && e.Keycode == ukey {
76+
k++
77+
}
78+
79+
if k == l {
80+
ct = true
81+
}
82+
83+
if e.Kind == KeyUp && e.Keycode == ukey {
84+
if k > 0 {
85+
k--
86+
}
87+
// time.Sleep(10 * time.Microsecond)
88+
ct = false
89+
}
90+
}
91+
} else {
92+
ct = true
93+
}
94+
95+
if ct && e.Kind == KeyUp && e.Keycode == Keycode[key] {
96+
End()
97+
// k = 0
98+
break
99+
}
100+
}
101+
102+
return true
103+
}
104+
105+
// AddMouse add mouse event hook
106+
//
107+
// mouse arguments: left, center, right, wheelDown, wheelUp,
108+
// wheelLeft, wheelRight.
109+
//
110+
// hook.AddMouse("left")
111+
// hook.AddMouse("left", 100, 100)
112+
func AddMouse(btn string, x ...int16) bool {
113+
s := Start()
114+
ukey := MouseMap[btn]
115+
116+
ct := false
117+
for {
118+
e := <-s
119+
120+
if len(x) > 1 {
121+
if e.Kind == MouseMove && e.X == x[0] && e.Y == x[1] {
122+
ct = true
123+
}
124+
} else {
125+
ct = true
126+
}
127+
128+
if ct && e.Kind == MouseDown && e.Button == ukey {
129+
End()
130+
break
131+
}
132+
}
133+
134+
return true
135+
}
136+
137+
// AddMousePos add listen mouse event pos hook
138+
func AddMousePos(x, y int16) bool {
139+
s := Start()
140+
141+
for {
142+
e := <-s
143+
if e.Kind == MouseMove && e.X == x && e.Y == y {
144+
End()
145+
break
146+
}
147+
}
148+
149+
return true
150+
}

hook.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ func End() {
261261
}
262262

263263
// AddEvent add event listener
264-
func AddEvent(key string) int {
264+
func addEvent(key string) int {
265265
cs := C.CString(key)
266266
defer C.free(unsafe.Pointer(cs))
267267

0 commit comments

Comments
 (0)