-
Notifications
You must be signed in to change notification settings - Fork 57
/
keybd_windows.go
310 lines (298 loc) · 6.44 KB
/
keybd_windows.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package keybd_event
import (
"syscall"
)
var dll = syscall.NewLazyDLL("user32.dll")
var procKeyBd = dll.NewProc("keybd_event")
// Press key(s)
func (k *KeyBonding) Press() error {
if k.hasALT {
downKey(_VK_ALT)
}
if k.hasALTGR {
downKey(_VK_ALT)
downKey(_VK_CTRL)
}
if k.hasSHIFT {
downKey(_VK_SHIFT)
}
if k.hasCTRL {
downKey(_VK_CTRL)
}
if k.hasRSHIFT {
downKey(_VK_RSHIFT)
}
if k.hasRCTRL {
downKey(_VK_RCONTROL)
}
if k.hasSuper {
downKey(_VK_LWIN)
}
for _, key := range k.keys {
downKey(key)
}
return nil
}
//Release key(s)
func (k *KeyBonding) Release() error {
if k.hasALT {
upKey(_VK_ALT)
}
if k.hasALTGR {
upKey(_VK_ALT)
upKey(_VK_CTRL)
}
if k.hasSHIFT {
upKey(_VK_SHIFT)
}
if k.hasCTRL {
upKey(_VK_CTRL)
}
if k.hasRSHIFT {
upKey(_VK_RSHIFT)
}
if k.hasRCTRL {
upKey(_VK_RCONTROL)
}
for _, key := range k.keys {
upKey(key)
}
if k.hasSuper {
upKey(_VK_LWIN)
}
return nil
}
// Launch key bounding
func (k *KeyBonding) Launching() error {
err := k.Press()
if err != nil {
return err
}
err = k.Release()
return err
}
func downKey(key int) {
flag := 0
if key < 0xFFF { // Detect if the key code is virtual or no
flag |= _KEYEVENTF_SCANCODE
} else {
key -= 0xFFF
}
vkey := key + 0x80
procKeyBd.Call(uintptr(key), uintptr(vkey), uintptr(flag), 0)
}
func upKey(key int) {
flag := _KEYEVENTF_KEYUP
if key < 0xFFF {
flag |= _KEYEVENTF_SCANCODE
} else {
key -= 0xFFF
}
vkey := key + 0x80
procKeyBd.Call(uintptr(key), uintptr(vkey), uintptr(flag), 0)
}
func initKeyBD() error { return nil }
const (
// I add 0xFFF for all Virtual key
_VK_SHIFT = 0x10 + 0xFFF
_VK_CTRL = 0x11 + 0xFFF
_VK_ALT = 0x12 + 0xFFF
_VK_LSHIFT = 0xA0 + 0xFFF
_VK_RSHIFT = 0xA1 + 0xFFF
_VK_LCONTROL = 0xA2 + 0xFFF
_VK_RCONTROL = 0xA3 + 0xFFF
_VK_LWIN = 0x5B + 0xFFF
_VK_RWIN = 0x5C + 0xFFF
_KEYEVENTF_KEYUP = 0x0002
_KEYEVENTF_SCANCODE = 0x0008
)
const (
VK_SP1 = 41
VK_SP2 = 12
VK_SP3 = 13
VK_SP4 = 26
VK_SP5 = 27
VK_SP6 = 39
VK_SP7 = 40
VK_SP8 = 43
VK_SP9 = 51
VK_SP10 = 52
VK_SP11 = 53
VK_SP12 = 86
VK_ESC = 1
VK_1 = 2
VK_2 = 3
VK_3 = 4
VK_4 = 5
VK_5 = 6
VK_6 = 7
VK_7 = 8
VK_8 = 9
VK_9 = 10
VK_0 = 11
VK_Q = 16
VK_W = 17
VK_E = 18
VK_R = 19
VK_T = 20
VK_Y = 21
VK_U = 22
VK_I = 23
VK_O = 24
VK_P = 25
VK_A = 30
VK_S = 31
VK_D = 32
VK_F = 33
VK_G = 34
VK_H = 35
VK_J = 36
VK_K = 37
VK_L = 38
VK_Z = 44
VK_X = 45
VK_C = 46
VK_V = 47
VK_B = 48
VK_N = 49
VK_M = 50
VK_F1 = 59
VK_F2 = 60
VK_F3 = 61
VK_F4 = 62
VK_F5 = 63
VK_F6 = 64
VK_F7 = 65
VK_F8 = 66
VK_F9 = 67
VK_F10 = 68
VK_F11 = 87
VK_F12 = 88
VK_F13 = 0x7C + 0xFFF
VK_F14 = 0x7D + 0xFFF
VK_F15 = 0x7E + 0xFFF
VK_F16 = 0x7F + 0xFFF
VK_F17 = 0x80 + 0xFFF
VK_F18 = 0x81 + 0xFFF
VK_F19 = 0x82 + 0xFFF
VK_F20 = 0x83 + 0xFFF
VK_F21 = 0x84 + 0xFFF
VK_F22 = 0x85 + 0xFFF
VK_F23 = 0x86 + 0xFFF
VK_F24 = 0x87 + 0xFFF
VK_NUMLOCK = 69
VK_SCROLLLOCK = 70
VK_RESERVED = 0
VK_MINUS = 12
VK_EQUAL = 13
VK_BACKSPACE = 14
VK_TAB = 15
VK_LEFTBRACE = 26
VK_RIGHTBRACE = 27
VK_ENTER = 28
VK_SEMICOLON = 39
VK_APOSTROPHE = 40
VK_GRAVE = 41
VK_BACKSLASH = 43
VK_COMMA = 51
VK_DOT = 52
VK_SLASH = 53
VK_KPASTERISK = 55
VK_SPACE = 57
VK_CAPSLOCK = 58
VK_KP0 = 82
VK_KP1 = 79
VK_KP2 = 80
VK_KP3 = 81
VK_KP4 = 75
VK_KP5 = 76
VK_KP6 = 77
VK_KP7 = 71
VK_KP8 = 72
VK_KP9 = 73
VK_KPMINUS = 74
VK_KPPLUS = 78
VK_KPDOT = 83
// I add 0xFFF for all Virtual key
VK_LBUTTON = 0x01 + 0xFFF
VK_RBUTTON = 0x02 + 0xFFF
VK_CANCEL = 0x03 + 0xFFF
VK_MBUTTON = 0x04 + 0xFFF
VK_XBUTTON1 = 0x05 + 0xFFF
VK_XBUTTON2 = 0x06 + 0xFFF
VK_BACK = 0x08 + 0xFFF
VK_CLEAR = 0x0C + 0xFFF
VK_PAUSE = 0x13 + 0xFFF
VK_CAPITAL = 0x14 + 0xFFF
VK_KANA = 0x15 + 0xFFF
VK_HANGUEL = 0x15 + 0xFFF
VK_HANGUL = 0x15 + 0xFFF
VK_JUNJA = 0x17 + 0xFFF
VK_FINAL = 0x18 + 0xFFF
VK_HANJA = 0x19 + 0xFFF
VK_KANJI = 0x19 + 0xFFF
VK_CONVERT = 0x1C + 0xFFF
VK_NONCONVERT = 0x1D + 0xFFF
VK_ACCEPT = 0x1E + 0xFFF
VK_MODECHANGE = 0x1F + 0xFFF
VK_PAGEUP = 0x21 + 0xFFF
VK_PAGEDOWN = 0x22 + 0xFFF
VK_END = 0x23 + 0xFFF
VK_HOME = 0x24 + 0xFFF
VK_LEFT = 0x25 + 0xFFF
VK_UP = 0x26 + 0xFFF
VK_RIGHT = 0x27 + 0xFFF
VK_DOWN = 0x28 + 0xFFF
VK_SELECT = 0x29 + 0xFFF
VK_PRINT = 0x2A + 0xFFF
VK_EXECUTE = 0x2B + 0xFFF
VK_SNAPSHOT = 0x2C + 0xFFF
VK_INSERT = 0x2D + 0xFFF
VK_DELETE = 0x2E + 0xFFF
VK_HELP = 0x2F + 0xFFF
VK_SCROLL = 0x91 + 0xFFF
VK_LMENU = 0xA4 + 0xFFF
VK_RMENU = 0xA5 + 0xFFF
VK_BROWSER_BACK = 0xA6 + 0xFFF
VK_BROWSER_FORWARD = 0xA7 + 0xFFF
VK_BROWSER_REFRESH = 0xA8 + 0xFFF
VK_BROWSER_STOP = 0xA9 + 0xFFF
VK_BROWSER_SEARCH = 0x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