WT programs do not respond to arrow keys sent from the SendKeys API #10921

Closed
opened 2026-01-31 02:33:48 +00:00 by claunia · 6 comments
Owner

Originally created by @outlandishwherewolf843 on GitHub (Oct 7, 2020).

Environment

Windows build number: 10.0.18362.0
Windows Terminal version 1.3.2651.0:

Any other software?

A C# program using the SendKeys API

Steps to reproduce

Write a program in C# using the SendKeys API to emulate keypresses. Use this program to attempt to send arrow keys to a program running in Windows Terminal.

Expected behavior

The program in Windows Terminal should respond as if an arrow key has been pressed. In Powershell or CMD or most shells, the cursor should move according to the arrow key that is pressed.

Actual behavior

The cursor blinks as if a key has been pressed, but will not move. The program in WT does not respond in any other way. The up and down arrow keys sent by SendKeys WILL work in the dropdown menu however. It seems like programs running in WT will respond to most other keys appropriately.

Originally created by @outlandishwherewolf843 on GitHub (Oct 7, 2020). <!-- 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 I ACKNOWLEDGE THE FOLLOWING BEFORE PROCEEDING: 1. If I delete this entire template and go my own path, the core team may close my issue without further explanation or engagement. 2. If I list multiple bugs/concerns in this one issue, the core team may close my issue without further explanation or engagement. 3. If I write an issue that has many duplicates, the core team may close my issue without further explanation or engagement (and without necessarily spending time to find the exact duplicate ID number). 4. If I leave the title incomplete when filing the issue, the core team may close my issue without further explanation or engagement. 5. If I file something completely blank in the body, the core team may close my issue without further explanation or engagement. All good? Then proceed! --> <!-- This bug tracker is monitored by Windows Terminal development team and other technical folks. **Important: When reporting BSODs or security issues, DO NOT attach memory dumps, logs, or traces to Github issues**. Instead, send dumps/traces to secure@microsoft.com, referencing this GitHub issue. If this is an application crash, please also provide a Feedback Hub submission link so we can find your diagnostic data on the backend. Use the category "Apps > Windows Terminal (Preview)" and choose "Share My Feedback" after submission to get the link. Please use this form and describe your issue, concisely but precisely, with as much detail as possible. --> # Environment ```none Windows build number: 10.0.18362.0 Windows Terminal version 1.3.2651.0: Any other software? A C# program using the SendKeys API ``` # Steps to reproduce <!-- A description of how to trigger this bug. --> Write a program in C# using the SendKeys API to emulate keypresses. Use this program to attempt to send arrow keys to a program running in Windows Terminal. # Expected behavior <!-- A description of what you're expecting, possibly containing screenshots or reference material. --> The program in Windows Terminal should respond as if an arrow key has been pressed. In Powershell or CMD or most shells, the cursor should move according to the arrow key that is pressed. # Actual behavior <!-- What's actually happening? --> The cursor blinks as if a key has been pressed, but will not move. The program in WT does not respond in any other way. The up and down arrow keys sent by SendKeys WILL work in the dropdown menu however. It seems like programs running in WT will respond to most other keys appropriately.
Author
Owner

@zadjii-msft commented on GitHub (Oct 7, 2020):

Do you happen to know if SendKeys sends the key events with the scan code set to 0? I bet we're filtering them out, because they have a 0 scancode.

There's also #7438 and #7495 that I'm guessing are all the same root cause.

/cc @lhecker

@zadjii-msft commented on GitHub (Oct 7, 2020): Do you happen to know if SendKeys sends the key events with the scan code set to 0? I bet we're filtering them out, because they have a 0 scancode. There's also #7438 and #7495 that I'm guessing are all the same root cause. /cc @lhecker
Author
Owner

@outlandishwherewolf843 commented on GitHub (Oct 10, 2020):

I don't know enough to be able to say. Sounds like a good hypothesis though. Is there an easy way to find out?

@outlandishwherewolf843 commented on GitHub (Oct 10, 2020): I don't know enough to be able to say. Sounds like a good hypothesis though. Is there an easy way to find out?
Author
Owner

@lhecker commented on GitHub (Oct 10, 2020):

It most certainly is the underlying reason. Currently all "improper" SendInput inputs are ignored, whose wVk are invalid (outside of the 0x01-0xff range), or whose wScan is zero. See here.
To be completely honest: When I submitted the offending PR, I didn't think of testing whether AutoHotkey and PowerTools still work with non-character key events. 😟

Optimally AutoHotkey, PowerTools, etc. should fix their SendInput code and only send valid wVk and wScan values. But since we should strive to be as compatible as possible to "incorrect" software, I'll try to look into this in the coming days. @zadjii-msft 🙂

@lhecker commented on GitHub (Oct 10, 2020): It most certainly is the underlying reason. Currently all "improper" [`SendInput` inputs](https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-keybdinput) are ignored, whose `wVk` are invalid (outside of the `0x01-0xff` range), or whose `wScan` is zero. See [here](https://github.com/microsoft/terminal/blob/9dc38ad0f51ed72b0d0eb21de618ecbe65f04246/src/cascadia/TerminalCore/Terminal.cpp#L474-L485). To be completely honest: When I submitted the offending PR, I didn't think of testing whether AutoHotkey and PowerTools still work with non-character key events. 😟 Optimally AutoHotkey, PowerTools, etc. should fix their `SendInput` code and only send _valid_ `wVk` and `wScan` values. But since we should strive to be as compatible as possible to "incorrect" software, I'll try to look into this in the coming days. @zadjii-msft 🙂
Author
Owner

@lhecker commented on GitHub (Oct 10, 2020):

Oh and just for reference... This is a minimal example of how you use SendInput correctly:

#define NOMINMAX
#include <Windows.h>
#include <cstdio>

int main()
{
    printf("Waiting 5 seconds for you to switch to Windows Terminal...\n");
    Sleep(5000);
    printf("Sending arrow up key\n");

    INPUT ip{};
    ip.type = INPUT_KEYBOARD;
    ip.ki.wVk = VK_UP;
    ip.ki.wScan = LOWORD(MapVirtualKeyW(VK_UP, MAPVK_VK_TO_VSC));

    ip.ki.dwFlags = 0;
    SendInput(1, &ip, sizeof(INPUT));

    ip.ki.dwFlags = KEYEVENTF_KEYUP;
    SendInput(1, &ip, sizeof(INPUT));

    return 0;
}

(This example is only correct in the scope of the problem in this issue though.)

@lhecker commented on GitHub (Oct 10, 2020): Oh and just for reference... This is a minimal example of how you use `SendInput` correctly: ```cpp #define NOMINMAX #include <Windows.h> #include <cstdio> int main() { printf("Waiting 5 seconds for you to switch to Windows Terminal...\n"); Sleep(5000); printf("Sending arrow up key\n"); INPUT ip{}; ip.type = INPUT_KEYBOARD; ip.ki.wVk = VK_UP; ip.ki.wScan = LOWORD(MapVirtualKeyW(VK_UP, MAPVK_VK_TO_VSC)); ip.ki.dwFlags = 0; SendInput(1, &ip, sizeof(INPUT)); ip.ki.dwFlags = KEYEVENTF_KEYUP; SendInput(1, &ip, sizeof(INPUT)); return 0; } ``` (This example is only correct in the scope of the problem in this issue though.)
Author
Owner

@ghost commented on GitHub (Nov 11, 2020):

:tada:This issue was addressed in #7900, which has now been successfully released as Windows Terminal v1.4.3141.0.🎉

Handy links:

@ghost commented on GitHub (Nov 11, 2020): :tada:This issue was addressed in #7900, which has now been successfully released as `Windows Terminal v1.4.3141.0`.:tada: Handy links: * [Release Notes](https://github.com/microsoft/terminal/releases/tag/v1.4.3141.0) * [Store Download](https://www.microsoft.com/store/apps/9n8g5rfz9xk3?cid=storebadge&ocid=badge)
Author
Owner

@ghost commented on GitHub (Nov 11, 2020):

:tada:This issue was addressed in #7900, which has now been successfully released as Windows Terminal Preview v1.5.3142.0.🎉

Handy links:

@ghost commented on GitHub (Nov 11, 2020): :tada:This issue was addressed in #7900, which has now been successfully released as `Windows Terminal Preview v1.5.3142.0`.:tada: Handy links: * [Release Notes](https://github.com/microsoft/terminal/releases/tag/v1.5.3142.0) * [Store Download](https://www.microsoft.com/store/apps/9n8g5rfz9xk3?cid=storebadge&ocid=badge)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#10921