Skip to content

Commit

Permalink
Always send key-up even if cancel during press
Browse files Browse the repository at this point in the history
Fixed potential issue where if the cancel button was pressed while the key was virtually being held down, it would never be released
  • Loading branch information
ThioJoe committed Jul 8, 2024
1 parent a16f5b4 commit 8b7ccb2
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 52 deletions.
109 changes: 59 additions & 50 deletions MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]

0 comments on commit 8b7ccb2

Please sign in to comment.