← Back to Pull requests

Fix _VirtualKeyFromCharacter returning 65535 on missing layout mapping #20800

Merged
shrikargs7-cloud merged 1 files (6 lines) on Oct 19, 2026 16:01 UTC
View on GitHub
shrikargs7-cloud shrikargs7-cloud commented on Oct 13, 2026 16:27 UTC
Author

Summary of the Pull Request


Fixes Terminal::_VirtualKeyFromCharacter incorrectly returning 65535 when 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


  • VkKeyScanW returns a signed SHORT (-1 on failure when the character does not exist in the active keyboard layout). Wrapping this in LOWORD(...) cast the value to an unsigned 16-bit WORD (65535). Because vkey was unsigned, vkey == -1 evaluated to false due to integer promotion, preventing 0 from ever being returned and instead propagating 65535.

  • Additionally, for characters that require modifier states (e.g. Shift for uppercase 'A'), LOWORD preserved the modifier flags in the high byte (0x0141 = 327), exceeding the single-byte limit for virtual key codes.

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

  • This change keeps vkey as a signed SHORT, checks for -1 to return 0, and masks valid mappings with LOBYTE(vkey) to ensure the returned virtual key code is strictly within 0..255.

  • Added unit tests in InputTest.cpp validating both unmapped character handling and single-byte key code return.
  • Validation Steps Performed


  • Added unit test VirtualKeyFromCharacter to InputTest.cpp verifying Terminal::_VirtualKeyFromCharacter(L'\xffff') == 0 and Terminal::_VirtualKeyFromCharacter(L'A') == 'A'.
  • PR Checklist


  • Closes #20797

  • Tests added/passed

  • Documentation updated

  • Schema updated (if necessary)
  • Diff: 6 lines (+3 / -3) across 1 files Reviewer: DHowett Review duration: 8614m
    All Vouch review confidence checks passed
    Residual risk 0.04 = Change Risk (0.15) × (1 − Review Confidence (0.72)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.15 · 6 lines diff across 1 files
    0.15
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.46 · 5 substantive comment(s)
    0.46
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    71%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.116
    Real PR Conversation Timeline
    10 items
    azure-pipelines[bot] azure-pipelines[bot] commented on Oct 13, 2026 16:27 UTC
    Bot

    <!-- AzurePipelinesStatusComment -->
    <samp>
    Azure Pipelines:<br>
    There may be pipelines that require an authorized user to comment /azp run to run.<br>

    </samp>

    DHowett DHowett commented on code on Oct 13, 2026 16:31 UTC
    Code Review
    src/cascadia/UnitTests_TerminalCore/InputTest.cpp:1
    @@ -30,6 +30,7 @@ namespace TerminalCoreUnitTests
     
             TEST_METHOD(AltShiftKey);
             TEST_METHOD(InvalidKeyEvent);
    +        TEST_METHOD(VirtualKeyFromCharacter);
    

    I'd say we can do without the test, because it creates a false dependency on what is effectively an implementation detail.

    DHowett DHowett requested changes on Oct 13, 2026 16:32 UTC
    Changes Requested Reviewer

    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?

    lhecker lhecker commented on Oct 19, 2026 15:15 UTC
    Reviewer

    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.

    lhecker lhecker commented on Oct 19, 2026 15:17 UTC
    Contributor

    @DHowett I have reverted the tests.

    @shrikargs7-cloud We can merge this PR only after you agreed to the Contributor License Agreement above.

    DHowett DHowett approved these changes on Oct 19, 2026 15:18 UTC
    Approved Reviewer

    Approved these changes.

    DHowett DHowett commented on Oct 19, 2026 15:18 UTC
    Contributor

    /azp run

    azure-pipelines[bot] azure-pipelines[bot] commented on Oct 19, 2026 15:18 UTC
    Bot

    <samp>
    Azure Pipelines:<br>
    Successfully started running 1 pipeline(s).<br>
    1 pipeline(s) were filtered out due to trigger conditions.<br>

    </samp>

    DHowett DHowett commented on Oct 19, 2026 15:21 UTC
    Contributor

    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
    

    Reviewers
    Assignees
    shrikargs7-cloud shrikargs7-cloud
    Labels
    risk: low confidence: 71.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 14.6% (LOW) · Higher = larger blast radius
    14.6%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 71.8% · Lower = rushed or shallow review
    71.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 4.1% · Safe to merge
    4.1%