-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSendInputToWindow.cs
More file actions
467 lines (412 loc) · 16.5 KB
/
SendInputToWindow.cs
File metadata and controls
467 lines (412 loc) · 16.5 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Text;
public class CPHInline
{
// P/Invoke declarations
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport("user32.dll", SetLastError = true)]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId);
// Delegate for EnumWindows
private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
// Extended dictionary of all Windows Virtual-Key Codes
Dictionary<string, int> VirtualKeyCodes = new Dictionary<string, int>
{
// Mouse buttons
{ "LBUTTON", 0x01 }, // Left mouse button
{ "RBUTTON", 0x02 }, // Right mouse button
{ "MBUTTON", 0x04 }, // Middle mouse button
{ "XBUTTON1", 0x05 }, // X1 mouse button
{ "XBUTTON2", 0x06 }, // X2 mouse button
// Control keys
{ "BACK", 0x08 }, // Backspace key
{ "TAB", 0x09 }, // Tab key
{ "RETURN", 0x0D }, // Enter key
{ "ENTER", 0x0D }, // Enter key
{ "SHIFT", 0x10 }, // Shift key
{ "CONTROL", 0x11 }, // Ctrl key
{ "CTRL", 0x11 }, // Ctrl key
{ "ALT", 0x12 }, // Alt key
{ "PAUSE", 0x13 }, // Pause key
{ "CAPSLOCK", 0x14 }, // Caps Lock key
// Navigation and editing
{ "ESCAPE", 0x1B }, // Escape key
{ "SPACE", 0x20 }, // Spacebar
{ " ", 0x20 }, // Spacebar
{ "PAGEUP", 0x21 }, // Page Up key
{ "PAGEDOWN", 0x22 }, // Page Down key
{ "END", 0x23 }, // End key
{ "HOME", 0x24 }, // Home key
{ "LEFT", 0x25 }, // Left arrow key
{ "UP", 0x26 }, // Up arrow key
{ "RIGHT", 0x27 }, // Right arrow key
{ "DOWN", 0x28 }, // Down arrow key
{ "SELECT", 0x29 }, // Select key
{ "PRINT", 0x2A }, // Print key
{ "EXECUTE", 0x2B }, // Execute key
{ "PRINTSCREEN", 0x2C }, // Print Screen key
{ "INSERT", 0x2D }, // Insert key
{ "DELETE", 0x2E }, // Delete key
{ "HELP", 0x2F }, // Help key
// Number keys (0-9)
{ "0", 0x30 },
{ "1", 0x31 },
{ "2", 0x32 },
{ "3", 0x33 },
{ "4", 0x34 },
{ "5", 0x35 },
{ "6", 0x36 },
{ "7", 0x37 },
{ "8", 0x38 },
{ "9", 0x39 },
// Letter keys (A-Z)
{ "A", 0x41 },
{ "B", 0x42 },
{ "C", 0x43 },
{ "D", 0x44 },
{ "E", 0x45 },
{ "F", 0x46 },
{ "G", 0x47 },
{ "H", 0x48 },
{ "I", 0x49 },
{ "J", 0x4A },
{ "K", 0x4B },
{ "L", 0x4C },
{ "M", 0x4D },
{ "N", 0x4E },
{ "O", 0x4F },
{ "P", 0x50 },
{ "Q", 0x51 },
{ "R", 0x52 },
{ "S", 0x53 },
{ "T", 0x54 },
{ "U", 0x55 },
{ "V", 0x56 },
{ "W", 0x57 },
{ "X", 0x58 },
{ "Y", 0x59 },
{ "Z", 0x5A },
// Function keys (F1-F24)
{ "F1", 0x70 },
{ "F2", 0x71 },
{ "F3", 0x72 },
{ "F4", 0x73 },
{ "F5", 0x74 },
{ "F6", 0x75 },
{ "F7", 0x76 },
{ "F8", 0x77 },
{ "F9", 0x78 },
{ "F10", 0x79 },
{ "F11", 0x7A },
{ "F12", 0x7B },
{ "F13", 0x7C },
{ "F14", 0x7D },
{ "F15", 0x7E },
{ "F16", 0x7F },
{ "F17", 0x80 },
{ "F18", 0x81 },
{ "F19", 0x82 },
{ "F20", 0x83 },
{ "F21", 0x84 },
{ "F22", 0x85 },
{ "F23", 0x86 },
{ "F24", 0x87 },
// Numpad keys
{ "NUMLOCK", 0x90 },
{ "SCROLLLOCK", 0x91 },
{ "NUMPAD0", 0x60 },
{ "NUMPAD1", 0x61 },
{ "NUMPAD2", 0x62 },
{ "NUMPAD3", 0x63 },
{ "NUMPAD4", 0x64 },
{ "NUMPAD5", 0x65 },
{ "NUMPAD6", 0x66 },
{ "NUMPAD7", 0x67 },
{ "NUMPAD8", 0x68 },
{ "NUMPAD9", 0x69 },
{ "MULTIPLY", 0x6A },
{ "ADD", 0x6B },
{ "SEPARATOR", 0x6C },
{ "SUBTRACT", 0x6D },
{ "DECIMAL", 0x6E },
{ "DIVIDE", 0x6F },
// Left/Right variants, app-specific control keys
{ "LSHIFT", 0xA0 },
{ "RSHIFT", 0xA1 },
{ "LCONTROL", 0xA2 },
{ "LCTRL", 0xA2 },
{ "RCONTROL", 0xA3 },
{ "RCTRL", 0xA3 },
{ "LMENU", 0xA4 },
{ "RMENU", 0xA5 },
{ "BROWSER_BACK", 0xA6 },
{ "BROWSER_FORWARD", 0xA7 },
{ "BROWSER_REFRESH", 0xA8 },
{ "BROWSER_STOP", 0xA9 },
{ "BROWSER_SEARCH", 0xAA },
{ "BROWSER_FAVORITES", 0xAB },
{ "BROWSER_HOME", 0xAC },
{ "VOLUME_MUTE", 0xAD },
{ "VOLUME_DOWN", 0xAE },
{ "VOLUME_UP", 0xAF },
{ "MEDIA_NEXT_TRACK", 0xB0 },
{ "MEDIA_PREV_TRACK", 0xB1 },
{ "MEDIA_STOP", 0xB2 },
{ "MEDIA_PLAY_PAUSE", 0xB3 },
{ "LAUNCH_MAIL", 0xB4 },
{ "LAUNCH_MEDIA_SELECT", 0xB5 },
{ "LAUNCH_APP1", 0xB6 },
{ "LAUNCH_APP2", 0xB7 },
// EOM keys that can vary by manufacturer, but have common US mappings.
// Sorry world.
{ ";", 0xBA },
{ ":", 0xBA },
{ "+", 0XBB },
{ "=", 0XBB },
{ ",", 0xBC },
{ "<", 0xBC },
{ "-", 0xBD },
{ "_", 0xBD },
{ ".", 0xBE },
{ ">", 0xBE },
{ "/", 0xBF },
{ "?", 0xBF },
{ "~", 0xC0 },
{ "`", 0xC0 },
{ "[", 0xDB},
{ "\\", 0xDC},
{ "|", 0xDC},
{ "]", 0xDD},
{ "'", 0xDE},
{ "\"", 0xDE},
};
private const uint WM_KEYDOWN = 0x0100;
private const uint WM_KEYUP = 0x0101;
public bool Execute()
{
CPH.TryGetArg("verboseLogging", out verbose);
if (!CPH.TryGetArg("keypresses", out string keypresses))
{
ERROR("Misssing required argument \"keypresses\"");
return false;
}
CPH.TryGetArg("keypressDelay", out int keypressDelay);
CPH.TryGetArg("keypressHold", out int keypressHold);
IntPtr hwnd = MatchWindow();
List<Action> actions = GenerateKeyActions(keypresses, hwnd, keypressDelay, keypressHold);
foreach (Action action in actions)
{
action();
}
return true;
}
// Converts a string containing a sequence of KEYNAME,
// and converts it to an array of functions to call in sequence to
// execute the keypresses and other actions.
//
// Each KEYNAME is a case-insensitive name of a key, and can either be:
// * a single character denoting a regular key, e.g., "a" or "1"
// * OR "{NAME[:HOLD]}", where NAME is one of the named keys listed in the table above,
// and HOLD is an optional number of milliseconds to hold the key down.
private List<Action> GenerateKeyActions(string keypresses, IntPtr hwnd, int keypressDelay=0, int keypressHold=0)
{
string upkeys = keypresses.ToUpper();
// Extract all of the individual key characters and {...} directives.
MatchCollection matches = Regex.Matches(upkeys, "[^{]|{[^}]*}", RegexOptions.None);
// Decode each parsed element into actions
bool needsDelay = false;
List<Action> actions = new List<Action>();
foreach (Match match in matches)
{
// Strip off any surrounding {} and use the contents as the name of the key to be looked up.
string keyname = match.Value.TrimStart('{').TrimEnd('}');
int pos = keyname.IndexOf(':', 0);
// {:NUMBER} means to pause for NUMBER ms instead of default delay
if (pos == 0 && keyname.Length > 1) {
int.TryParse(keyname.Substring(pos+1), out int delay);
DEBUG($"Explicit pause for {delay}ms");
actions.Add( () => {CPH.Wait(delay);} );
needsDelay = false;
continue;
}
// If the immediately prior action wasn't an inter-key delay, then add the default.
if (needsDelay && keypressDelay > 0)
{
DEBUG($"Default pause for {keypressDelay}ms");
actions.Add(() => {CPH.Wait(keypressDelay);});
}
int hold = keypressHold;
// {KEY:NUMBER} means to hold the key down for NUMBER ms.
if (pos > 0)
{
int.TryParse(keyname.Substring(pos+1), out hold);
keyname = keyname.Substring(0, pos);
}
if (VirtualKeyCodes.TryGetValue(keyname, out int keycode))
{
DEBUG($"Keypress {keyname} for {hold}ms");
// Key down
actions.Add(() => {SendMessage(hwnd, WM_KEYDOWN, (IntPtr)keycode, IntPtr.Zero);});
// Optional key down hold time
if (keypressHold > 0) actions.Add(() => {CPH.Wait(hold);});
// Key up
actions.Add(() => {SendMessage(hwnd, WM_KEYUP, (IntPtr)keycode, IntPtr.Zero);});
needsDelay = true;
}
else
{
WARN($"\"{match.Value}\" is not a valid virtual key name.");
}
}
return actions;
}
// Finds a window matching various window & process criteria
private IntPtr MatchWindow()
{
bool anyCriteria = false;
anyCriteria |= CPH.TryGetArg("targetWindowTitle", out string targWinTitle);
anyCriteria |= CPH.TryGetArg("targetProcess", out string targProcName);
anyCriteria |= CPH.TryGetArg("targetExecutable", out string targExePath);
anyCriteria |= CPH.TryGetArg("targetWindowClass", out string targWinClass);
DEBUG($"Criteria: c:\"{targWinClass}\" pn:\"{targProcName}\" pp:\"{targExePath}\" t:\"{targWinTitle}\"");
if (!anyCriteria)
{
ERROR("You must supply at least one argument of targetWindowTitle, targetWindowClass, targetExecutable, or targetProcess");
return IntPtr.Zero;
}
IntPtr bestMatch = IntPtr.Zero;
// Store the results of matches
IntPtr matchedHandle = IntPtr.Zero;
int bestMatchScore = int.MinValue;
EnumWindows((hWnd, lParam) =>
{
// Get window title
StringBuilder windowTitle = new StringBuilder(256);
GetWindowText(hWnd, windowTitle, windowTitle.Capacity);
// Get class name
StringBuilder className = new StringBuilder(256);
GetClassName(hWnd, className, className.Capacity);
VERBOSE($"Matching {hWnd}: c:\"{className}\" t:\"{windowTitle}\"");
// Scoring for matching
int matchScore = 500;
// Window class must match exactly, or it is disqualified
if (!string.IsNullOrEmpty(targWinClass) && !className.ToString().Equals(targWinClass, StringComparison.OrdinalIgnoreCase))
{
VERBOSE(" Class doesn't match at all");
return true; // skip
}
// Window title must match at least part of title, score decreased by the edit distance.
if (!string.IsNullOrEmpty(targWinTitle))
{
if (windowTitle.ToString().IndexOf(targWinTitle, StringComparison.OrdinalIgnoreCase) >= 0)
{
matchScore += (targWinTitle.Length - windowTitle.Length);
}
else
{
VERBOSE(" Title doesn't match at all");
return true; // skip
}
}
// Fetching process info is relatively expensive, so only do it
// after filtering for window info, and if it's part of the match criteria
if (!string.IsNullOrEmpty(targExePath) || !string.IsNullOrEmpty(targProcName))
{
// Get process ID and executable
GetWindowThreadProcessId(hWnd, out uint processId);
(string processExecutable, string processName) = GetProcessInfo((int)processId);
VERBOSE($"{hWnd}: c:\"{className}\" t:\"{windowTitle}\" pn:\"{processName}\" pp:\"{processExecutable}\"");
// Exe and Path are absolute criteria. If it doesn't match those, then it's out of the running.
if (!string.IsNullOrEmpty(targExePath) && processExecutable != null)
{
// Full path
if (processExecutable.Equals(targExePath, StringComparison.OrdinalIgnoreCase))
{
matchScore += 1000;
}
// file name only
else if (Path.GetFileName(processExecutable).Equals(targExePath, StringComparison.OrdinalIgnoreCase))
{
matchScore += 500;
}
else
{
VERBOSE(" Path doesn't match at all");
return true; // skip to next window
}
}
// Process name must match at least partially, score decreased by the edit distance.
if (!string.IsNullOrEmpty(targProcName) && !string.IsNullOrEmpty(processName))
{
if (processName.IndexOf(targProcName, StringComparison.OrdinalIgnoreCase) >= 0)
{
matchScore += (targProcName.Length - processName.Length);
}
else
{
VERBOSE(" Process name doesn't match at all");
return true; // skip
}
}
}
VERBOSE($" Score: {matchScore} window: {windowTitle}");
// Check if this is the best match so far
if (matchScore > bestMatchScore)
{
DEBUG($"Best Match so far: score:{matchScore} w:{hWnd}: c:\"{className}\" t:\"{windowTitle}\"");
bestMatchScore = matchScore;
matchedHandle = hWnd;
}
return true; // Continue enumeration
}, IntPtr.Zero);
return matchedHandle;
}
private Dictionary<int, (string, string)> ProcessCache = new Dictionary<int, (string, string)>();
private (string executable, string name) GetProcessInfo(int processId)
{
if (!ProcessCache.ContainsKey(processId))
{
try
{
VERBOSE($"Fetching fresh process info for {processId}");
Process process = Process.GetProcessById(processId);
ProcessCache[processId] = (process.MainModule.FileName, process.ProcessName);
}
catch
{
ProcessCache[processId] = (null, null);
}
}
return ProcessCache[processId];
}
private void DEBUG(string msg)
{
CPH.LogDebug($"SENDAPPKEY: {msg}");
}
private bool verbose = false;
private void VERBOSE(string msg)
{
if (verbose) CPH.LogVerbose($"SENDAPPKEY: {msg}");
}
private void WARN(string msg)
{
CPH.LogWarn($"SENDAPPKEY: {msg}");
}
private void ERROR(string msg)
{
CPH.LogError($"SENDAPPKEY: {msg}");
}
}