From 8b7ccb2a0e533739e88537fb5cb2212bbafe5c6d Mon Sep 17 00:00:00 2001 From: ThioJoe <12518330+ThioJoe@users.noreply.github.com> Date: Mon, 8 Jul 2024 10:51:26 -0700 Subject: [PATCH] Always send key-up even if cancel during press Fixed potential issue where if the cancel button was pressed while the key was virtually being held down, it would never be released --- MainForm.cs | 109 ++++++++++++++++++++----------------- Properties/AssemblyInfo.cs | 4 +- 2 files changed, 61 insertions(+), 52 deletions(-) diff --git a/MainForm.cs b/MainForm.cs index 31619cf..25f6620 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -117,41 +117,46 @@ await Task.Run(async () => 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 + try { - labelToolstripStatus.Text = "Status: Holding Key..."; - labelToolstripStatus.ForeColor = Color.Green; - }); + // 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); - await Task.Delay((int)nudDuration.Value, ct); + // Press F-key + keybd_event(virtualKeyCode, 0, KEYEVENTF_KEYDOWN, UIntPtr.Zero); - ct.ThrowIfCancellationRequested(); - - // Release F-key - keybd_event(virtualKeyCode, 0, KEYEVENTF_KEYUP, UIntPtr.Zero); + // Hold the key for specified duration, update status text + this.Invoke((MethodInvoker)delegate + { + labelToolstripStatus.Text = "Status: Holding Key..."; + labelToolstripStatus.ForeColor = Color.Green; + }); - // 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); + await Task.Delay((int)nudDuration.Value, ct); - // Re-enable all buttons after keys are released - this.Invoke((MethodInvoker)delegate + ct.ThrowIfCancellationRequested(); + } + finally { - All_Buttons_Enabler(); - labelToolstripStatus.Text = "Status: Ready"; - labelToolstripStatus.ForeColor = Color.Black; - btnCancel.Visible = false; - }); + // 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); } @@ -266,34 +271,38 @@ INPUT CreateInput(ushort vk, ushort scan, bool isKeyUp = false) 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 + try { - labelToolstripStatus.Text = "Status: Holding Key..."; - labelToolstripStatus.ForeColor = Color.Green; - }); - - await Task.Delay((int)nudDuration.Value, ct); + // Send key down inputs + SendInput((uint)keyDownInputs.Count, keyDownInputs.ToArray(), Marshal.SizeOf(typeof(INPUT))); - ct.ThrowIfCancellationRequested(); + // Wait for the key press duration using Task.Delay + this.Invoke((MethodInvoker)delegate + { + labelToolstripStatus.Text = "Status: Holding Key..."; + labelToolstripStatus.ForeColor = Color.Green; + }); - // Send key up inputs - SendInput((uint)keyUpInputs.Count, keyUpInputs.ToArray(), Marshal.SizeOf(typeof(INPUT))); + await Task.Delay((int)nudDuration.Value, ct); - // Re-enable all buttons after keys are released - this.Invoke((MethodInvoker)delegate + ct.ThrowIfCancellationRequested(); + } + finally { - All_Buttons_Enabler(); - labelToolstripStatus.Text = "Status: Ready"; - labelToolstripStatus.ForeColor = Color.Black; - btnCancel.Visible = false; - }); + // 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(); diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index c98368e..4b805ef 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.1.0")] -[assembly: AssemblyFileVersion("1.0.1.0")] +[assembly: AssemblyVersion("1.0.2.0")] +[assembly: AssemblyFileVersion("1.0.2.0")]