Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

fix: release held modifier keys before SendInput paste on Windows

Gabriel Stein5f7c0a57c3e6
diff --git a/resources/windows-fast-paste.c b/resources/windows-fast-paste.c
index 20894c7ef..bf464e3bf 100644
--- a/resources/windows-fast-paste.c
+++ b/resources/windows-fast-paste.c
@@ -91,14 +91,54 @@ static void SetKey(INPUT* input, WORD vk, DWORD flags) {
input->ki.dwFlags = flags;
}
+/* Modifier keys that may interfere with SendInput if held by the user's
+ hotkey. We check left/right-specific variants since GetAsyncKeyState
+ reports physical key state per side. */
+static const WORD MODIFIER_VKS[] = {
+ VK_LCONTROL, VK_RCONTROL,
+ VK_LSHIFT, VK_RSHIFT,
+ VK_LMENU, VK_RMENU,
+ VK_LWIN, VK_RWIN,
+};
+#define NUM_MODIFIERS (sizeof(MODIFIER_VKS) / sizeof(MODIFIER_VKS[0]))
+
+/* Release any modifier keys that are currently held down so they don't
+ contaminate the paste keystroke. Returns the count of keys released;
+ the caller must pass the same arrays back to RestoreModifiers(). */
+static int ReleaseModifiers(INPUT* released, WORD* releasedVKs) {
+ int count = 0;
+ for (int i = 0; i < (int)NUM_MODIFIERS; i++) {
+ if (GetAsyncKeyState(MODIFIER_VKS[i]) & 0x8000) {
+ releasedVKs[count] = MODIFIER_VKS[i];
+ SetKey(&released[count], MODIFIER_VKS[i], KEYEVENTF_KEYUP);
+ count++;
+ }
+ }
+ if (count > 0) {
+ SendInput((UINT)count, released, sizeof(INPUT));
+ }
+ return count;
+}
+
+/* Re-press modifier keys that were released by ReleaseModifiers(). */
+static void RestoreModifiers(WORD* releasedVKs, int count) {
+ if (count == 0) return;
+ INPUT restore[NUM_MODIFIERS];
+ ZeroMemory(restore, sizeof(restore));
+ for (int i = 0; i < count; i++) {
+ SetKey(&restore[i], releasedVKs[i], 0);
+ }
+ SendInput((UINT)count, restore, sizeof(INPUT));
+}
+
static int SendPasteNormal(void) {
INPUT inputs[4];
ZeroMemory(inputs, sizeof(inputs));
- SetKey(&inputs[0], VK_CONTROL, 0);
+ SetKey(&inputs[0], VK_LCONTROL, 0);
SetKey(&inputs[1], 'V', 0);
SetKey(&inputs[2], 'V', KEYEVENTF_KEYUP);
- SetKey(&inputs[3], VK_CONTROL, KEYEVENTF_KEYUP);
+ SetKey(&inputs[3], VK_LCONTROL, KEYEVENTF_KEYUP);
UINT sent = SendInput(4, inputs, sizeof(INPUT));
return (sent == 4) ? 0 : 1;
@@ -108,12 +148,12 @@ static int SendPasteTerminal(void) {
INPUT inputs[6];
ZeroMemory(inputs, sizeof(inputs));
- SetKey(&inputs[0], VK_CONTROL, 0);
- SetKey(&inputs[1], VK_SHIFT, 0);
+ SetKey(&inputs[0], VK_LCONTROL, 0);
+ SetKey(&inputs[1], VK_LSHIFT, 0);
SetKey(&inputs[2], 'V', 0);
SetKey(&inputs[3], 'V', KEYEVENTF_KEYUP);
- SetKey(&inputs[4], VK_SHIFT, KEYEVENTF_KEYUP);
- SetKey(&inputs[5], VK_CONTROL, KEYEVENTF_KEYUP);
+ SetKey(&inputs[4], VK_LSHIFT, KEYEVENTF_KEYUP);
+ SetKey(&inputs[5], VK_LCONTROL, KEYEVENTF_KEYUP);
UINT sent = SendInput(6, inputs, sizeof(INPUT));
return (sent == 6) ? 0 : 1;
@@ -160,7 +200,14 @@ int main(int argc, char* argv[]) {
return 0;
}
- Sleep(5);
+ Sleep(10);
+
+ /* Release any modifier keys held by the user's hotkey so they don't
+ contaminate the paste keystroke (e.g. Ctrl+Win held → Ctrl+Win+V). */
+ INPUT releasedInputs[NUM_MODIFIERS];
+ WORD releasedVKs[NUM_MODIFIERS];
+ ZeroMemory(releasedInputs, sizeof(releasedInputs));
+ int releasedCount = ReleaseModifiers(releasedInputs, releasedVKs);
int result;
if (isTerminal) {
@@ -169,6 +216,8 @@ int main(int argc, char* argv[]) {
result = SendPasteNormal();
}
+ RestoreModifiers(releasedVKs, releasedCount);
+
if (result != 0) {
fprintf(stderr, "ERROR: SendInput failed (error %lu)\n", GetLastError());
return 1;