forked from ThioJoe/F-Key-Sender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
449 lines (377 loc) · 14.9 KB
/
MainForm.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace F_Key_Sender
{
public partial class MainForm : Form
{
// Dictionary to store virtual key codes and scan codes. Will want to use wscan codes for SendInput
private static readonly Dictionary<string, (ushort vk, ushort scan)> keyCodes = new Dictionary<string, (ushort, ushort)>
{
{"F13", (0x7C, 100)},
{"F14", (0x7D, 101)},
{"F15", (0x7E, 102)},
{"F16", (0x7F, 103)},
{"F17", (0x80, 104)},
{"F18", (0x81, 105)},
{"F19", (0x82, 106)},
{"F20", (0x83, 107)},
{"F21", (0x84, 108)},
{"F22", (0x85, 109)},
{"F23", (0x86, 110)},
{"F24", (0x87, 118)},
{"LCTRL", (0x11, 29)},
{"LSHIFT", (0x10, 42)},
{"LALT", (0x12, 56)},
{"X", (0x58, 45)}
};
// Flags for keybd_event
const uint INPUT_KEYBOARD = 1;
const uint KEYEVENTF_KEYDOWN = 0x0000;
const uint KEYEVENTF_KEYUP = 0x0002;
const uint KEYEVENTF_SCANCODE = 0x0008;
private readonly Stopwatch stopwatch = new Stopwatch();
public MainForm()
{
InitializeComponent();
this.TopMost = true;
stopwatch.Start();
dropdownMethod.SelectedIndex = 0;
}
// Using WaitHandle instead of Thread.Sleep to avoid wasting system resources and also more accurate
private static readonly AutoResetEvent waitHandle = new AutoResetEvent(false);
private CancellationTokenSource _cts;
private async void SendKeyCombo(string key)
{
bool ctrl = checkBoxCtrl.Checked;
bool shift = checkBoxShift.Checked;
bool alt = checkBoxAlt.Checked;
// Enable cancel button even though not visible yet
btnCancel.Enabled = true;
_cts = new CancellationTokenSource();
try
{
if (dropdownMethod.SelectedIndex == 0) // SendInput
{
await SendKey_Method_SendInputAsync(key, ctrl, shift, alt, _cts.Token);
}
else if (dropdownMethod.SelectedIndex == 1) // keybd_event
{
await SendKey_keybd_eventAsync(key, ctrl, shift, alt, _cts.Token);
}
}
catch (OperationCanceledException)
{
// Handle cancellation
labelToolstripStatus.Text = "Status: Ready (Operation Cancelled)";
labelToolstripStatus.ForeColor = Color.SaddleBrown;
}
finally
{
All_Buttons_Enabler();
btnCancel.Visible = false;
_cts.Dispose();
_cts = null;
}
}
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
private async Task SendKey_keybd_eventAsync(string key, bool ctrl, bool shift, bool alt, CancellationToken ct)
{
byte virtualKeyCode = BitConverter.GetBytes(keyCodes[key].vk)[0];
await Task.Run(async () =>
{
// Start delay, disable buttons, and update status text
this.Invoke((MethodInvoker)delegate
{
All_Buttons_Disabler();
labelToolstripStatus.Text = "Status: Waiting Before Sending...";
labelToolstripStatus.ForeColor = Color.Purple;
btnCancel.Visible = true;
});
await Task.Delay((int)nudDelay.Value * 1000, ct);
ct.ThrowIfCancellationRequested();
// Press modifier keys
if (ctrl) keybd_event(0x11, 0, KEYEVENTF_KEYDOWN, UIntPtr.Zero);
if (shift) keybd_event(0x10, 0, KEYEVENTF_KEYDOWN, UIntPtr.Zero);
if (alt) keybd_event(0x12, 0, KEYEVENTF_KEYDOWN, UIntPtr.Zero);
// Press F-key
keybd_event(virtualKeyCode, 0, KEYEVENTF_KEYDOWN, UIntPtr.Zero);
// Hold the key for specified duration, update status text
this.Invoke((MethodInvoker)delegate
{
labelToolstripStatus.Text = "Status: Holding Key...";
labelToolstripStatus.ForeColor = Color.Green;
});
await Task.Delay((int)nudDuration.Value, ct);
ct.ThrowIfCancellationRequested();
// Release F-key
keybd_event(virtualKeyCode, 0, KEYEVENTF_KEYUP, UIntPtr.Zero);
// Release modifier keys
if (alt) keybd_event(0x12, 0, KEYEVENTF_KEYUP, UIntPtr.Zero);
if (shift) keybd_event(0x10, 0, KEYEVENTF_KEYUP, UIntPtr.Zero);
if (ctrl) keybd_event(0x11, 0, KEYEVENTF_KEYUP, UIntPtr.Zero);
// Re-enable all buttons after keys are released
this.Invoke((MethodInvoker)delegate
{
All_Buttons_Enabler();
labelToolstripStatus.Text = "Status: Ready";
labelToolstripStatus.ForeColor = Color.Black;
btnCancel.Visible = false;
});
}, ct);
}
[DllImport("user32.dll", SetLastError = true)]
static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
[StructLayout(LayoutKind.Sequential)]
struct INPUT
{
public uint type;
public InputUnion u;
}
[StructLayout(LayoutKind.Explicit)]
struct InputUnion
{
[FieldOffset(0)]
public MOUSEINPUT mi;
[FieldOffset(0)]
public KEYBDINPUT ki;
[FieldOffset(0)]
public HARDWAREINPUT hi;
}
[StructLayout(LayoutKind.Sequential)]
struct KEYBDINPUT
{
public ushort wVk;
public ushort wScan;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
struct MOUSEINPUT
{
public int dx;
public int dy;
public uint mouseData;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
struct HARDWAREINPUT
{
public uint uMsg;
public ushort wParamL;
public ushort wParamH;
}
// Bypassing SendKeys and directly using SendInput due to limitations of F17 through F24 in .NET's SendKeys
private async Task SendKey_Method_SendInputAsync(string key, bool ctrl, bool shift, bool alt, CancellationToken ct)
{
if (!keyCodes.TryGetValue(key.ToUpper(), out var codes))
{
return; // Invalid key
}
await Task.Run(async () =>
{
// Helper function to create a single input
INPUT CreateInput(ushort vk, ushort scan, bool isKeyUp = false)
{
return new INPUT
{
type = INPUT_KEYBOARD,
u = new InputUnion
{
ki = new KEYBDINPUT
{
wVk = vk,
wScan = scan,
dwFlags = isKeyUp ? KEYEVENTF_KEYUP : 0,
time = (uint)stopwatch.ElapsedMilliseconds,
dwExtraInfo = IntPtr.Zero
}
}
};
}
// Delay before sending keys. Disable all buttons while keys are virtually held down
this.Invoke((MethodInvoker)delegate
{
All_Buttons_Disabler();
labelToolstripStatus.Text = "Status: Waiting Before Sending...";
labelToolstripStatus.ForeColor = Color.Purple;
btnCancel.Visible = true;
});
await Task.Delay((int)nudDelay.Value * 1000, ct);
ct.ThrowIfCancellationRequested();
// Create lists to hold key down and key up inputs
List<INPUT> keyDownInputs = new List<INPUT>();
List<INPUT> keyUpInputs = new List<INPUT>();
// Add key down events for modifiers
if (ctrl) keyDownInputs.Add(CreateInput(keyCodes["LCTRL"].vk, keyCodes["LCTRL"].scan));
if (shift) keyDownInputs.Add(CreateInput(keyCodes["LSHIFT"].vk, keyCodes["LSHIFT"].scan));
if (alt) keyDownInputs.Add(CreateInput(keyCodes["LALT"].vk, keyCodes["LALT"].scan));
// Add key down event for the main key
keyDownInputs.Add(CreateInput(codes.vk, codes.scan));
// Add key up events in reverse order
keyUpInputs.Add(CreateInput(codes.vk, codes.scan, true));
if (alt) keyUpInputs.Add(CreateInput(keyCodes["LALT"].vk, keyCodes["LALT"].scan, true));
if (shift) keyUpInputs.Add(CreateInput(keyCodes["LSHIFT"].vk, keyCodes["LSHIFT"].scan, true));
if (ctrl) keyUpInputs.Add(CreateInput(keyCodes["LCTRL"].vk, keyCodes["LCTRL"].scan, true));
// Send key down inputs
SendInput((uint)keyDownInputs.Count, keyDownInputs.ToArray(), Marshal.SizeOf(typeof(INPUT)));
// Wait for the key press duration using Task.Delay
this.Invoke((MethodInvoker)delegate
{
labelToolstripStatus.Text = "Status: Holding Key...";
labelToolstripStatus.ForeColor = Color.Green;
});
await Task.Delay((int)nudDuration.Value, ct);
ct.ThrowIfCancellationRequested();
// Send key up inputs
SendInput((uint)keyUpInputs.Count, keyUpInputs.ToArray(), Marshal.SizeOf(typeof(INPUT)));
// Re-enable all buttons after keys are released
this.Invoke((MethodInvoker)delegate
{
All_Buttons_Enabler();
labelToolstripStatus.Text = "Status: Ready";
labelToolstripStatus.ForeColor = Color.Black;
btnCancel.Visible = false;
});
}, ct);
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
_cts?.Cancel();
base.OnFormClosing(e);
}
private void All_Buttons_Disabler()
{
// Disable all buttons
btnF13.Enabled = false;
btnF14.Enabled = false;
btnF15.Enabled = false;
btnF16.Enabled = false;
btnF17.Enabled = false;
btnF18.Enabled = false;
btnF19.Enabled = false;
btnF20.Enabled = false;
btnF21.Enabled = false;
btnF22.Enabled = false;
btnF23.Enabled = false;
btnF24.Enabled = false;
btnTestX.Enabled = false;
// Disable checkboxes
checkBoxAlt.Enabled = false;
checkBoxCtrl.Enabled = false;
checkBoxShift.Enabled = false;
// Disable numeric updowns
nudDelay.Enabled = false;
nudDuration.Enabled = false;
// Disable dropdown
dropdownMethod.Enabled = false;
}
private void All_Buttons_Enabler()
{
// Enable all buttons
btnF13.Enabled = true;
btnF14.Enabled = true;
btnF15.Enabled = true;
btnF16.Enabled = true;
btnF17.Enabled = true;
btnF18.Enabled = true;
btnF19.Enabled = true;
btnF20.Enabled = true;
btnF21.Enabled = true;
btnF22.Enabled = true;
btnF23.Enabled = true;
btnF24.Enabled = true;
btnTestX.Enabled = true;
// Enable checkboxes
checkBoxAlt.Enabled = true;
checkBoxCtrl.Enabled = true;
checkBoxShift.Enabled = true;
// Enable numeric updowns
nudDelay.Enabled = true;
nudDuration.Enabled = true;
// Enable dropdown
dropdownMethod.Enabled = true;
}
private void chkAlwaysOnTop_CheckedChanged(object sender, EventArgs e)
{
this.TopMost = chkAlwaysOnTop.Checked;
}
// Define button clicks to send F-key presses based on the button clicked
private void btnF13_Click(object sender, EventArgs e)
{
// Send F13 key press to function, it will handle which combos to send
SendKeyCombo("F13");
}
private void btnF14_Click(object sender, EventArgs e)
{
SendKeyCombo("F14");
}
private void btnF15_Click(object sender, EventArgs e)
{
SendKeyCombo("F15");
}
private void btnF16_Click(object sender, EventArgs e)
{
SendKeyCombo("F16");
}
private void btnF17_Click(object sender, EventArgs e)
{
SendKeyCombo("F17");
}
private void btnF18_Click(object sender, EventArgs e)
{
SendKeyCombo("F18");
}
private void btnF19_Click(object sender, EventArgs e)
{
SendKeyCombo("F19");
}
private void btnF20_Click(object sender, EventArgs e)
{
SendKeyCombo("F20");
}
private void btnF21_Click(object sender, EventArgs e)
{
SendKeyCombo("F21");
}
private void btnF22_Click(object sender, EventArgs e)
{
SendKeyCombo("F22");
}
private void btnF23_Click(object sender, EventArgs e)
{
SendKeyCombo("F23");
}
private void btnF24_Click(object sender, EventArgs e)
{
SendKeyCombo("F24");
}
private void btnTestX_Click(object sender, EventArgs e)
{
SendKeyCombo("X");
}
private void btnCancel_Click(object sender, EventArgs e)
{
if (_cts != null && !_cts.IsCancellationRequested)
{
_cts.Cancel();
labelToolstripStatus.Text = "Status: Cancelling...";
labelToolstripStatus.ForeColor = Color.Orange;
btnCancel.Enabled = false;
}
}
}
}