Fix _VirtualKeyFromCharacter returning 65535 on missing layout mapping #20800
<!-- AzurePipelinesStatusComment -->
<samp>
Azure Pipelines:<br>
There may be pipelines that require an authorized user to comment /azp run to run.<br>
</samp>
I'd say we can do without the test, because it creates a false dependency on what is effectively an implementation detail.
this does not seem incorrect. your general name, presentation and more suggest that you are some sort of robot, however, so I must ask: have you followed our AI contribution requirements?
I concur with @DHowett. This is an excellent issue you found and fixed, but the test tests existing internals, not relevant "externals", if that makes sense.
@DHowett I have reverted the tests.
@shrikargs7-cloud We can merge this PR only after you agreed to the Contributor License Agreement above.
Approved these changes.
/azp run
<samp>
Azure Pipelines:<br>
Successfully started running 1 pipeline(s).<br>
1 pipeline(s) were filtered out due to trigger conditions.<br>
</samp>
Rewrote the commit body and title,
Prevents us from incorrectly encoding `65535` into an input record when
a character has no virtual key mapping in the active keyboard layout.
- `VkKeyScanW` returns a signed `SHORT` `-1` on failure when the
character does not exist in the active keyboard layout).
- When injected characters without key-down/scan-code information (e.g.
via VNC or remote inputs) passed through `SendCharEvent`, this caused
.NET `Console.ReadKey()` to throw an unhandled
`System.ArgumentOutOfRangeException: Console key values must be
between 0 and 255 inclusive.` and crash PowerShell.
Closes #20797
DHowett
shrikargs7-cloud
Summary of the Pull Request
Fixes
Terminal::_VirtualKeyFromCharacterincorrectly returning65535when a character has no virtual key mapping in the active keyboard layout, and ensures returned virtual key codes are masked to a single byte (0..255).References and Relevant Issues
Closes #20797
Detailed Description of the Pull Request / Additional comments
VkKeyScanWreturns a signedSHORT(-1on failure when the character does not exist in the active keyboard layout). Wrapping this inLOWORD(...)cast the value to an unsigned 16-bitWORD(65535). Becausevkeywas unsigned,vkey == -1evaluated tofalsedue to integer promotion, preventing0from ever being returned and instead propagating65535.'A'),LOWORDpreserved the modifier flags in the high byte (0x0141=327), exceeding the single-byte limit for virtual key codes.SendCharEvent, this caused .NETConsole.ReadKey()to throw an unhandledSystem.ArgumentOutOfRangeException: Console key values must be between 0 and 255 inclusive.and crash PowerShell.vkeyas a signedSHORT, checks for-1to return0, and masks valid mappings withLOBYTE(vkey)to ensure the returned virtual key code is strictly within0..255.InputTest.cppvalidating both unmapped character handling and single-byte key code return.Validation Steps Performed
VirtualKeyFromCharactertoInputTest.cppverifyingTerminal::_VirtualKeyFromCharacter(L'\xffff') == 0andTerminal::_VirtualKeyFromCharacter(L'A') == 'A'.PR Checklist