Terminal: WT programs do not respond to arrow keys sent from the SendKeys API

Created on 7 Oct 2020  路  6Comments  路  Source: microsoft/terminal

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.

Area-Input Area-TerminalControl Issue-Bug Priority-2 Product-Terminal Resolution-Fix-Committed

All 6 comments

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

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

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 馃檪

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.)

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

Handy links:

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

Handy links:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Ronkiro picture Ronkiro  路  65Comments

cinnamon-msft picture cinnamon-msft  路  62Comments

Byloth picture Byloth  路  56Comments

privacyguy123 picture privacyguy123  路  131Comments

patriksvensson picture patriksvensson  路  143Comments