-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinput.go
More file actions
317 lines (278 loc) · 6.62 KB
/
input.go
File metadata and controls
317 lines (278 loc) · 6.62 KB
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
311
312
313
314
315
316
317
package main
import (
"fmt"
"syscall"
"time"
"unicode/utf16"
"unsafe"
)
// Windows API for Input
var (
user32DLL = syscall.NewLazyDLL("user32.dll")
procSendInput = user32DLL.NewProc("SendInput")
procSetCursorPos = user32DLL.NewProc("SetCursorPos")
)
const (
INPUT_MOUSE = 0
INPUT_KEYBOARD = 1
INPUT_HARDWARE = 2
// Mouse flags
MOUSEEVENTF_MOVE = 0x0001
MOUSEEVENTF_LEFTDOWN = 0x0002
MOUSEEVENTF_LEFTUP = 0x0004
MOUSEEVENTF_RIGHTDOWN = 0x0008
MOUSEEVENTF_RIGHTUP = 0x0010
MOUSEEVENTF_MIDDLEDOWN = 0x0020
MOUSEEVENTF_MIDDLEUP = 0x0040
MOUSEEVENTF_ABSOLUTE = 0x8000
MOUSEEVENTF_WHEEL = 0x0800
// Keyboard flags
KEYEVENTF_EXTENDEDKEY = 0x0001
KEYEVENTF_KEYUP = 0x0002
KEYEVENTF_UNICODE = 0x0004
KEYEVENTF_SCANCODE = 0x0008
)
type INPUT struct {
Type uint32
Mi MOUSEINPUT
}
type MOUSEINPUT struct {
Dx int32
Dy int32
MouseData uint32
DwFlags uint32
Time uint32
DwExtraInfo uintptr
}
type KEYBDINPUT struct {
WVk uint16
WScan uint16
DwFlags uint32
Time uint32
DwExtraInfo uintptr
}
// Helper to create keyboard input struct
func createKeyInput(vk uint16, flags uint32) INPUT {
var input INPUT
input.Type = INPUT_KEYBOARD
ki := (*KEYBDINPUT)(unsafe.Pointer(&input.Mi))
ki.WVk = vk
ki.DwFlags = flags
return input
}
func createUnicodeInput(r uint16, flags uint32) INPUT {
var input INPUT
input.Type = INPUT_KEYBOARD
ki := (*KEYBDINPUT)(unsafe.Pointer(&input.Mi))
ki.WVk = 0
ki.WScan = r
ki.DwFlags = flags | KEYEVENTF_UNICODE
return input
}
func sendInput(inputs []INPUT) {
if len(inputs) == 0 {
return
}
procSendInput.Call(
uintptr(len(inputs)),
uintptr(unsafe.Pointer(&inputs[0])),
uintptr(unsafe.Sizeof(INPUT{})),
)
}
func moveMouse(x, y int) {
procSetCursorPos.Call(uintptr(x), uintptr(y))
}
func clickMouse(x, y int, button string) {
// First move to position
moveMouse(x, y)
var inputs []INPUT
var down, up uint32
switch button {
case "left":
down = MOUSEEVENTF_LEFTDOWN
up = MOUSEEVENTF_LEFTUP
case "right":
down = MOUSEEVENTF_RIGHTDOWN
up = MOUSEEVENTF_RIGHTUP
case "middle":
down = MOUSEEVENTF_MIDDLEDOWN
up = MOUSEEVENTF_MIDDLEUP
default:
return
}
inputs = append(inputs, INPUT{
Type: INPUT_MOUSE,
Mi: MOUSEINPUT{
DwFlags: down,
},
})
inputs = append(inputs, INPUT{
Type: INPUT_MOUSE,
Mi: MOUSEINPUT{
DwFlags: up,
},
})
sendInput(inputs)
}
func scrollMouse(delta int) {
var inputs []INPUT
inputs = append(inputs, INPUT{
Type: INPUT_MOUSE,
Mi: MOUSEINPUT{
DwFlags: MOUSEEVENTF_WHEEL,
MouseData: uint32(delta),
},
})
sendInput(inputs)
}
// simulateText sends unicode characters
func simulateText(text string) {
// Convert string to UTF-16 to handle Emoji and complex chars correctly
u16 := utf16.Encode([]rune(text))
var inputs []INPUT
for _, c := range u16 {
// Down
inputs = append(inputs, createUnicodeInput(c, 0))
// Up
inputs = append(inputs, createUnicodeInput(c, KEYEVENTF_KEYUP))
}
sendInput(inputs)
}
func simulateKey(key string) {
vk := mapKeyToVk(key)
if vk == 0 {
return
}
inputs := []INPUT{
createKeyInput(vk, 0), // Down
createKeyInput(vk, KEYEVENTF_KEYUP), // Up
}
sendInput(inputs)
}
// simulateHotkey presses modifier+key combination (e.g., Win+S, Ctrl+V)
func simulateHotkey(modifier, key uint16) {
inputs := []INPUT{
createKeyInput(modifier, 0), // Modifier Down
createKeyInput(key, 0), // Key Down
createKeyInput(key, KEYEVENTF_KEYUP), // Key Up
createKeyInput(modifier, KEYEVENTF_KEYUP), // Modifier Up
}
sendInput(inputs)
}
// simulateKeyVk presses a key by virtual key code
func simulateKeyVk(vk uint16) {
inputs := []INPUT{
createKeyInput(vk, 0), // Down
createKeyInput(vk, KEYEVENTF_KEYUP), // Up
}
sendInput(inputs)
}
// Virtual Key Codes
const (
VK_BACK = 0x08
VK_TAB = 0x09
VK_RETURN = 0x0D
VK_SHIFT = 0x10
VK_CONTROL = 0x11
VK_MENU = 0x12 // Alt
VK_ESCAPE = 0x1B
VK_SPACE = 0x20
VK_LEFT = 0x25
VK_UP = 0x26
VK_RIGHT = 0x27
VK_DOWN = 0x28
VK_DELETE = 0x2E
VK_LWIN = 0x5B // Left Windows key
VK_S = 0x53 // S key
VK_V = 0x56 // V key
)
func mapKeyToVk(key string) uint16 {
// Basic mapping (handle both cases since client may send lowercase)
switch key {
case "Enter", "enter":
return VK_RETURN
case "Backspace", "backspace":
return VK_BACK
case "Space", "space", " ":
return VK_SPACE
case "Escape", "escape", "esc":
return VK_ESCAPE
case "Tab", "tab":
return VK_TAB
case "Delete", "delete":
return VK_DELETE
case "ArrowUp", "up", "Up":
return VK_UP
case "ArrowDown", "down", "Down":
return VK_DOWN
case "ArrowLeft", "left", "Left":
return VK_LEFT
case "ArrowRight", "right", "Right":
return VK_RIGHT
}
// Single characters
if len(key) == 1 {
b := key[0]
if b >= 'a' && b <= 'z' {
return uint16(b - 32) // To Upper
}
if b >= 'A' && b <= 'Z' {
return uint16(b)
}
if b >= '0' && b <= '9' {
return uint16(b)
}
}
return 0
}
// Safer setClipboard with local DLL loading to avoid init order issues
func setClipboard(text string) error {
user32 := syscall.NewLazyDLL("user32.dll")
kernel32 := syscall.NewLazyDLL("kernel32.dll")
procOpenClipboard := user32.NewProc("OpenClipboard")
procCloseClipboard := user32.NewProc("CloseClipboard")
procEmptyClipboard := user32.NewProc("EmptyClipboard")
procSetClipboardData := user32.NewProc("SetClipboardData")
procGlobalAlloc := kernel32.NewProc("GlobalAlloc")
procGlobalLock := kernel32.NewProc("GlobalLock")
procGlobalUnlock := kernel32.NewProc("GlobalUnlock")
const CF_UNICODETEXT = 13
const GMEM_MOVEABLE = 0x0002
// Convert to UTF-16
u16 := utf16.Encode([]rune(text + "\x00"))
size := len(u16) * 2
// GlobalAlloc
hmem, _, err := procGlobalAlloc.Call(GMEM_MOVEABLE, uintptr(size))
if hmem == 0 {
return fmt.Errorf("GlobalAlloc failed: %v", err)
}
// GlobalLock
ptr, _, err := procGlobalLock.Call(hmem)
if ptr == 0 {
return fmt.Errorf("GlobalLock failed: %v", err)
}
// Copy data
dst := unsafe.Slice((*uint16)(unsafe.Pointer(ptr)), len(u16))
copy(dst, u16)
// GlobalUnlock
procGlobalUnlock.Call(hmem)
// OpenClipboard
// Retries for OpenClipboard as it might be locked by another app
var openRet uintptr
for i := 0; i < 5; i++ {
openRet, _, _ = procOpenClipboard.Call(0)
if openRet != 0 {
break
}
time.Sleep(10 * time.Millisecond)
}
if openRet == 0 {
return fmt.Errorf("OpenClipboard failed")
}
defer procCloseClipboard.Call()
// EmptyClipboard
procEmptyClipboard.Call()
// SetClipboardData
procSetClipboardData.Call(CF_UNICODETEXT, hmem)
return nil
}