Skip to content

refactor(input): Use KeyState and simplify keyboard state handling - #3233

Open
CryoTheRenegade wants to merge 3 commits into
TheSuperHackers:mainfrom
CryoTheRenegade:fix/modifier-key-release-fix
Open

CryoTheRenegade wants to merge 3 commits into
TheSuperHackers:mainfrom
CryoTheRenegade:fix/modifier-key-release-fix

Conversation

@CryoTheRenegade

@CryoTheRenegade CryoTheRenegade commented Aug 28, 2026 •

Copy link
Copy Markdown

This is the KeyState refactor, with no behavior change. The modifier key fixes from the earlier version of this pull request will follow in separate pull requests.

Summary

 KeyDefs.h
+  KEY_STATE_MODIFIERS = KEY_STATE_CONTROL | KEY_STATE_SHIFT | KEY_STATE_ALT
+  typedef UnsignedShort KeyState

 KeyboardIO::state, Keyboard::m_modifiers, key state accessors, meta events
-  UnsignedShort / Int / UnsignedByte
+  KeyState

 HotKeyTranslator on MSG_RAW_KEY_UP
-  build CTRL/SHIFT/ALT mask from key state, return if non-zero
+  return if keyState & KEY_STATE_MODIFIERS

 Keyboard::updateKeys
-  compare key against KEY_LCTRL ... KEY_RALT inline
+  isCtrlShiftAltKey(key)
  • Keyboard::setKeyStateData now takes a KeyState instead of an UnsignedByte. It has no callers, so nothing changes at runtime.
  • The MSG_RAW_KEY_* comments drop "uses Microsoft VK_ codes". KeyDefType holds DirectInput DIK_* codes.

Evidence

  • Before: modifier checks and key state types as above.
    After: same events and modifier capture, no behavior change.
  • Disassembly of Keyboard.cpp, HotKey.cpp, MetaEvent.cpp, Mouse.cpp, W3DDisplay.cpp and Win32DIKeyboard.cpp, main vs this branch, ClangCL and VC6 Release, both games:
    • Mouse, W3DDisplay and Win32DIKeyboard: identical.
    • getKeyStateBit, setKeyStateData, getKeyModState, onKeyPressed: only the mangled names change, from the new parameter types.
    • HotKeyTranslator::translateGameMessage: the three mask tests become one test 0x4fc, which covers the same bits.
    • Keyboard::updateKeys: VC6 compiles isCtrlShiftAltKey to a jump table and now calls two of the inlined emitRawKeyUpIfDown copies out of line. The calls and their order are the same.
    • No float arithmetic changed.
  • Not run: in-game test.

Council review

Rounds: 2

Delegate Round 1 Round 2
Claude Opus 5.5 7.5/10 9/10
GPT-6.1 Sol 9/10 10/10
Gemini 4 Carbon 9.5/10 10/10

🤖 Generated with Claude Code

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Summary by Qodo

Prevent stuck modifier combos on out-of-order key releases

🐞 Bug fix 🕐 20-40 Minutes

Grey Divider

AI Description

• Tracks modified presses so combo UP mappings fire regardless of release order.
• Suppresses plain GUI hotkeys when releasing keys previously used in modifier combos.
• Synthesizes all modifier releases after focus loss and clears stale combo tracking.
Diagram

sequenceDiagram
    actor User
    participant Keyboard
    participant Stream as Message Stream
    participant Meta as Meta Events
    participant Hotkey as Hotkey Translator
    participant Manager as Hotkey Manager
    User->>Keyboard: Press modified key
    Keyboard->>Stream: Raw key down
    Stream->>Meta: Track combo state
    Meta->>Manager: Suppress key up
    User->>Keyboard: Release keys
    Keyboard->>Stream: Raw key up
    Stream->>Hotkey: Check GUI hotkey
    Hotkey->>Manager: Consume suppression
    Stream->>Meta: Resolve combo up
    Keyboard->>Stream: Synthetic modifier ups
    Stream->>Meta: Flush reset state
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Unified chord state machine
  • ➕ Centralizes modifier tracking, UP mapping, and GUI suppression ownership.
  • ➕ Reduces coordination through global translator state.
  • ➖ Requires a broad input-pipeline refactor with substantially higher regression risk.
  • ➖ Touches mature event ordering and message-disposition behavior beyond this bug.

Recommendation: Keep the PR's targeted coordination between existing translators because it preserves raw key-up propagation and limits risk. A unified chord state machine would be cleaner long term, but its larger scope is not justified for this compatibility-sensitive fix; add focused regression coverage if the input harness supports synthetic event sequences.

Files changed (6) +115 / -31

Bug fix (6) +115 / -31
HotKey.hAdd one-shot key-up suppression state +5/-0

Add one-shot key-up suppression state

• Extends HotKeyManager with per-key suppression storage and APIs to set, consume, and clear suppression. This lets modified releases bypass GUI hotkey execution exactly once.

Core/GameEngine/Include/GameClient/HotKey.h

Keyboard.hExpose keyboard reset generations +3/-1

Expose keyboard reset generations

• Adds a reset generation counter and generalizes the focus-recovery helper from ALT-only handling to all modifier keys. Translators can now detect that keyboard state was reset.

Core/GameEngine/Include/GameClient/Keyboard.h

MetaEvent.hTrack reset synchronization in meta events +2/-0

Track reset synchronization in meta events

• Adds the last observed keyboard reset generation and a helper for clearing tracked key-down combinations. These declarations support safe recovery after focus loss.

Core/GameEngine/Include/GameClient/MetaEvent.h

Keyboard.cppSynthesize releases for every held modifier +19/-12

Synthesize releases for every held modifier

• Initializes and increments the keyboard reset generation whenever key state is cleared. Focus recovery now emits raw key-up messages for held CTRL and SHIFT keys as well as ALT.

Core/GameEngine/Source/GameClient/Input/Keyboard.cpp

HotKey.cppSkip GUI hotkeys for modified releases +34/-5

Skip GUI hotkeys for modified releases

• Consumes one-shot suppression before translating a raw key-up into a plain GUI hotkey, while leaving the message available to later translators. Initializes and manages the per-key suppression array.

Core/GameEngine/Source/GameClient/MessageStream/HotKey.cpp

MetaEvent.cppPreserve combo state across release ordering +52/-13

Preserve combo state across release ordering

• Records modifier-only holds, suppresses GUI handling for modified key releases, and retains combo state through same-frame release ordering. It also detects keyboard resets, emits pending UP mappings, and clears stale tracking.

Core/GameEngine/Source/GameClient/MessageStream/MetaEvent.cpp

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider

Great, no issues found!

Qodo reviewed your code and found no material issues that require review

Grey Divider

Tip of the day
💡 Did you know, you can reply 'qodo' on any finding to push back, ask questions, or dig deeper

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@greptile-apps

greptile-apps Bot commented Aug 28, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Low impact] The PR appears safe to merge; no new actionable issues were found.

Summary

This refactor names keyboard state values with KeyState and simplifies modifier checks without changing input behavior.

  • Keyboard input uses one named type for key state.

CryoTheRenegade explicitly accepted the legacy modifier-release bug to preserve key-up behavior. The PR description defers modifier fixes to separate pull requests. The earlier overlapping-hotkey concern no longer applies because hotkeys run on release.

Reviews (18) · Last reviewed commit: "refactor(input): Add default case to isC..." · Reviewed by Greptile

@xezon xezon left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this was entirely generated by LLM? It looks like slop the way the new logic is laid out. It is incomprehensible and unmaintainable. It needs to be unsloppified.

@xezon

xezon commented Aug 31, 2026

Copy link
Copy Markdown

How do we know the new generated revision is no slop?

@CryoTheRenegade

Copy link
Copy Markdown
Author

Fair criticism. I used LLM assistance on the first pass, and I should have reviewed and simplified the result before asking you to review it. I own that.

I’ve since rewritten the fix. The hotkey translator is stateless now. Modifier press state lives in Keyboard, where buffered events are processed in order, and each release records whether its matching press used Ctrl, Shift, or Alt.

@xezon

xezon commented Sep 3, 2026

Copy link
Copy Markdown

I tried to understand this change but I was unable to. It is lacking context. What was the issue and how was it reproduced, how was it fixed and why is it fixed the way it was fixed.

@CryoTheRenegade CryoTheRenegade changed the title fix(input): Keep modifier combos from sticking when keys are released out of order fix(input): Prevent modified key releases from firing plain hotkeys Sep 4, 2026
@CryoTheRenegade

Copy link
Copy Markdown
Author

I've reworded the PR description with a simple example and an explanation of the cause, the fix, and why the state is stored in Keyboard.

@xezon xezon added Bug Something is not working right, typically is user facing Minor Severity: Minor < Major < Critical < Blocker Gen Relates to Generals ZH Relates to Zero Hour Input labels Sep 10, 2026
@@ -74,33 +73,15 @@ GameMessageDisposition HotKeyTranslator::translateGameMessage(const GameMessage

if ( t == GameMessage::MSG_RAW_KEY_UP)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would MSG_RAW_KEY_DOWN perhaps be an option for hotkeys? All the MetaEvents are key down events. Or is there a good reason why hotkey needs to be posted on down?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Key-down could work. I found no comment explaining why hotkeys use key-up. I kept the current timing for now.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest test MSG_RAW_KEY_DOWN. It would be the simpler option and get rid of the new stuff you added to accomodate this event. Players will also be happier if their key presses register faster.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bump.

@coderabbitai

coderabbitai Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

  • Last reviewed commit: e52d4a4fd98bd2fddeb0de2365fc7bd691900495
  • Latest commit, not reviewed: 20f0bd2

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review.

Walkthrough

The changes add a KeyState alias and a modifier mask. Keyboard updates include modifier bits in key states, including repeat states, and emit key-up messages for tracked modifier keys during reset. Hotkey translation processes eligible raw key-down messages before meta-event translation.

Changes

Keyboard input state and message handling

Layer / File(s) Summary
Key-state types and declarations
Generals/.../GameClient/KeyDefs.h, GeneralsMD/.../GameClient/KeyDefs.h, Core/GameEngine/Include/Common/MessageStream.h, Core/GameEngine/Include/GameClient/Keyboard.h, Core/GameEngine/Include/GameClient/MetaEvent.h
The key definitions add KeyState and KEY_STATE_MODIFIERS. Keyboard and message declarations use KeyState for key-state values.
Modifier tracking and reset messages
Core/GameEngine/Source/GameClient/Input/Keyboard.cpp
Keyboard updates apply modifier bits before repeat processing. Reset handling emits raw key-up messages for tracked Ctrl, Shift, Alt, and configured shift-2 keys.
Key message translation and ordering
Core/GameEngine/Source/GameClient/MessageStream/HotKey.cpp, Core/GameEngine/Source/GameClient/MessageStream/MetaEvent.cpp, Generals/.../GameClient/GameClient.cpp, GeneralsMD/.../GameClient/GameClient.cpp
Hotkey translation handles raw key-down messages and filters modifier and autorepeat states. Meta-event handling uses KeyState. Both game variants register HotKeyTranslator before MetaEventTranslator.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant Keyboard
  participant HotKeyTranslator
  participant MetaEventTranslator
  Keyboard->>HotKeyTranslator: Send raw key-down with key state
  HotKeyTranslator->>HotKeyTranslator: Filter modifier and autorepeat states
  HotKeyTranslator->>MetaEventTranslator: Continue message translation
Loading

Suggested reviewers: xezon

Merge Risk: Minimal · up to e52d4

The change filters modified and repeated key presses and gives GUI hotkeys precedence. No actionable merge-blocking issue was identified; merge after normal checks.

Security architecture risk: Low · up to e52d4

Keyboard commands now fire on press, and reset clears additional modifier states. Existing input restrictions and enabled-button checks remain in place. Risk is low, with remaining uncertainty about customized key-release bindings.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The traced effect is a change in precedence for local keyboard activation of existing GUI actions in both variants. Exact action exposure depends on registered command buttons and localized labels; downstream action authority was not exhaustively assessed.

Trust Boundaries and Controls

  • observed — WindowTranslator remains ahead of hotkey handling and retains its disabled-input consumption rule for non-Escape keys. Hotkey handling additionally rejects modifier-bearing and autorepeat presses and preserves visible/enabled window checks.

Resilience and Maintainability Implications

  • inferred — Reset queues UP messages for tracked Ctrl, Shift, Alt, and alternate-shift keys before clearing keyboard state. Repeated serial resets without intervening input therefore queue no duplicate modifier releases. When these messages reach MetaEvent, its existing modifier-removal path clears recorded combinations and emits applicable terminal UP events; delivery through every earlier consumer remains unverified.
Pre-merge checks: Passed 4

Check name Status Explanation
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Title check Passed The title clearly identifies the primary change: the input refactor that introduces KeyState and simplifies keyboard state handling.
Description check Passed The description states this is a KeyState refactor with no behavior change. It names the touched code: the KeyState typedef and KEY_STATE_MODIFIERS in KeyDefs.h, the KeyboardIO and Keyboard::m_modifie…
Full details: Description check

Explanation

The description states this is a KeyState refactor with no behavior change. It names the touched code: the KeyState typedef and KEY_STATE_MODIFIERS in KeyDefs.h, the KeyboardIO and Keyboard::m_modifiers type changes, HotKeyTranslator's MSG_RAW_KEY_UP mask check, and Keyboard::updateKeys using isCtrlShiftAltKey. It also explains setKeyStateData's signature change and the MSG_RAW_KEY_* comment edits. This matches the raw summary, so the description is related to the changeset and meets the lenient bar.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

ℹ️ Recent review info

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: e895f905-13d8-4e86-91da-268267be8fea

📥 Commits

Reviewing files that changed from the base of the PR and between b4ed7f37cf44676fc5f6b68e3fd5ea5ce0493fa6 and e52d4a4fd98bd2fddeb0de2365fc7bd691900495.

📒 Files selected for processing (3)

  • Core/GameEngine/Source/GameClient/MessageStream/HotKey.cpp
  • Generals/Code/GameEngine/Source/GameClient/GameClient.cpp
  • GeneralsMD/Code/GameEngine/Source/GameClient/GameClient.cpp

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@xezon xezon left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I lost a oversight on this over time. If i am not mistaken this change now does 3 or more things:

  1. a KeyState type refactor (which is nice)
  2. Hotkey press fix
  3. Alt Tab key fix

And maybe something else.

Can you split all of these things into clean individual commits in an order that makes sense?

@CryoTheRenegade
CryoTheRenegade force-pushed the fix/modifier-key-release-fix branch from 67b80bd to b4ed7f3 Compare September 27, 2026 16:04
@CryoTheRenegade

Copy link
Copy Markdown
Author

I've split the changes into four commits, in this order:

  1. KeyState type refactor.
  2. Save the modifier state for each keyboard event.
  3. Run hotkeys on key-down, including the translator order change.
  4. Release held modifiers after focus loss.

@xezon xezon left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks promising. The split makes not entirely clear what a refactor is and what not, as it seems some refactors are mingled with non-refactor commits.

@CryoTheRenegade
CryoTheRenegade force-pushed the fix/modifier-key-release-fix branch from b4ed7f3 to e52d4a4 Compare September 30, 2026 03:25
@@ -550,6 +550,16 @@ void MetaEventTranslator::onKeyEvent(const GameMessage *msg, GameMessageDisposit
{
onKeyPressed(disp, systemKeyState, keyType, keyModState);
}

// TheSuperHackers @fix Preserve overlapping GUI hotkeys without suppressing the meta action

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this changed? What is the practical benefit here? Are you just following what the LLM tells you?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This question is still open.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This question is still open.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The block keeps a GUI hotkey working when the same plain key also has a key-down meta mapping. For example, E selects matching units, and a button also uses hotkey E. MetaEventTranslator (20) destroys the E press before HotKeyTranslator (25) sees it. Before this PR, the button still fired on key-up. After hotkeys moved to key-down, it would never fire. A simpler fix is to attach HotKeyTranslator before MetaEventTranslator and always return KEEP_MESSAGE. Both actions still run, and I can remove MSG_GUI_HOTKEY. Your earlier objection was to a reorder that destroyed the message, and this version doesn't.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot think of a good reason why the game should explicitly allow double key mappings. Meta Events and Hot Keys need to be exclusive key mappings.

It is a fair observation that this changes input behavior which I have not considered previously. How about we add a PRESERVE_RETAIL_INPUT=1 define that keeps Hot Keys on Key Up, otherwise it uses Key Down and gets shadowed by Meta Events. This prevents double key mapping with PRESERVE_RETAIL_INPUT=0.

For original English key mappings the PRESERVE_RETAIL_INPUT=0 is not a concern. For other languages it is, because there are a number of key mapping conflicts. These are fixed in data.

Or instead of PRESERVE_RETAIL_INPUT=0, we can make it a new user option TriggerHotKeyOnDown=yes/no

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can make it a new user option TriggerHotKeyOnDown=yes/no

I like this better, I'll update this pr with this

@CryoTheRenegade
CryoTheRenegade force-pushed the fix/modifier-key-release-fix branch from a8f164a to a15efbf Compare October 3, 2026 06:08

@xezon xezon left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commit 5 looks like it should be folded in one of the earlier commits where isCtrlShiftAltKey was added.

@xezon

xezon commented Oct 19, 2026

Copy link
Copy Markdown

If there was an update, it appears to be not pushed

@CryoTheRenegade
CryoTheRenegade force-pushed the fix/modifier-key-release-fix branch from 9a103e7 to e6e01ce Compare October 19, 2026 18:55
@CryoTheRenegade

Copy link
Copy Markdown
Author

If there was an update, it appears to be not pushed

pushed

@xezon

xezon commented Nov 1, 2026

Copy link
Copy Markdown

May I suggest to split this 4 commit review into 4 pull requests so we can finalize each of them individually? I think this will get us quicker to get it done.

@CryoTheRenegade
CryoTheRenegade force-pushed the fix/modifier-key-release-fix branch from e6e01ce to 97fa3bc Compare November 1, 2026 17:05
@CryoTheRenegade CryoTheRenegade changed the title fix(input): Prevent modified key releases from firing plain hotkeys refactor(input): Use KeyState and simplify keyboard state handling Nov 1, 2026
Introduce KeyState and the combined modifier mask. Update the keyboard, meta-event, and hotkey types. Simplify the hotkey modifier check and name the buffered key and physical-modifier helper without changing event timing or modifier capture.
@CryoTheRenegade
CryoTheRenegade force-pushed the fix/modifier-key-release-fix branch from 97fa3bc to 1c6bbb0 Compare November 17, 2026 03:39
CryoTheRenegade and others added 2 commits November 16, 2026 20:51
…dateKeys

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…StateData comment

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Something is not working right, typically is user facing Gen Relates to Generals Input Minor Severity: Minor < Major < Critical < Blocker ZH Relates to Zero Hour

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants