Input handler.ts rework#4225
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
💤 Files with no reviewable changes (1)
WalkthroughInputHandler now uses a registry for keyboard shortcuts. It stores keybind handlers with conditions, matches keys with real or synthesized events, tracks Alt keys in activeKeys, runs handlers from the registry-based keydown flow, and exposes the Alt-key modifier as a user-configurable setting for graphics refresh. ChangesKeyboard event data-driven refactoring and Alt-key configuration
Sequence Diagram(s)sequenceDiagram
participant Browser as Browser
participant InputHandler as InputHandler.keydown
participant Registry as keybindAndEvent Array
participant Matcher as keybindMatchesEvent
participant Active as activeKeys Set
participant Handler as registered handler
Browser->>InputHandler: keydown (KeyboardEvent)
InputHandler->>Active: add configured Alt code when pressed
InputHandler->>Registry: iterate entries
Registry->>Matcher: match entry against event or synthesized object
Matcher->>Active: consult activeKeys for modifier predicates
InputHandler->>Registry: evaluate predicates, call preventDefault()
Registry->>Handler: invoke handler when predicates pass
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 3❌ Failed checks (2 warnings, 1 inconclusive)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. 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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/client/InputHandler.ts (1)
1-1:⚠️ Potential issue | 🟡 Minor | ⚡ Quick winPrettier formatting issues reported by CI.
Run
npx prettier --write src/client/InputHandler.tsto fix the code style issues flagged by the pipeline.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/client/InputHandler.ts` at line 1, This file has Prettier style violations; run the formatter and commit the changes by executing npx prettier --write src/client/InputHandler.ts (or run the project's format script) to update formatting for the InputHandler module; after formatting, verify that imports like EventBus and GameEvent and any exported class/function in InputHandler.ts still compile and then re-run CI.Source: Pipeline failures
🧹 Nitpick comments (1)
src/client/InputHandler.ts (1)
929-941: ⚡ Quick winTypo in JSDoc and consider a typed structure.
Line 932:
"exectued"→"executed"The array-based convention
[action, ...conditions]works but requires knowing the index contract. A typed object would be clearer and catch misuse at compile time:interface KeybindEntry { action: (e: KeyboardEvent) => void; conditions?: Array<(e: KeyboardEvent) => boolean>; } private keybindAndEvent = new Map<string, KeybindEntry>();This makes the structure explicit and gives proper types for actions (void) vs conditions (boolean).
🔤 Minimal fix for typo
- * `@param` event The code to be exectued when this keybind is pressed + * `@param` event The code to be executed when this keybind is pressed🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/client/InputHandler.ts` around lines 929 - 941, Fix the JSDoc typo ("exectued" → "executed") and replace the fragile array convention used by keybindAndEvent with a typed object: define a KeybindEntry (with action: (e: KeyboardEvent) => void and optional conditions: Array<(e: KeyboardEvent) => boolean>), change the Map type to Map<string, KeybindEntry>, and update addKeybindAndEvent to accept an action and conditions with those explicit types (use the symbol addKeybindAndEvent and keybindAndEvent to locate the code); also update any code that reads from keybindAndEvent to use entry.action and entry.conditions and adjust return/usage expectations accordingly.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/client/InputHandler.ts`:
- Around line 580-592: Rename the misspelled local variable
allConditionsFullfiled to allConditionsFulfilled in the loop inside the
InputHandler logic that iterates over this.keybindAndEvent (the block that uses
keybindMatchesEvent and item[1].slice(1)), updating every reference (assignment,
checks, and the continue branch) to the new name to keep readability consistent;
ensure the same corrected name is used throughout that function scope and no
other behavior changes are introduced.
---
Outside diff comments:
In `@src/client/InputHandler.ts`:
- Line 1: This file has Prettier style violations; run the formatter and commit
the changes by executing npx prettier --write src/client/InputHandler.ts (or run
the project's format script) to update formatting for the InputHandler module;
after formatting, verify that imports like EventBus and GameEvent and any
exported class/function in InputHandler.ts still compile and then re-run CI.
---
Nitpick comments:
In `@src/client/InputHandler.ts`:
- Around line 929-941: Fix the JSDoc typo ("exectued" → "executed") and replace
the fragile array convention used by keybindAndEvent with a typed object: define
a KeybindEntry (with action: (e: KeyboardEvent) => void and optional conditions:
Array<(e: KeyboardEvent) => boolean>), change the Map type to Map<string,
KeybindEntry>, and update addKeybindAndEvent to accept an action and conditions
with those explicit types (use the symbol addKeybindAndEvent and keybindAndEvent
to locate the code); also update any code that reads from keybindAndEvent to use
entry.action and entry.conditions and adjust return/usage expectations
accordingly.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 09de9720-a581-40ca-9829-091e215d1987
📒 Files selected for processing (1)
src/client/InputHandler.ts
|
@coderabbitai help |
ChatThere are 3 ways to chat with CodeRabbit:
CodeRabbit commands
Other keywords and placeholders
CodeRabbit configuration file (
|
|
@coderabbitai full review |
✅ Action performedFull review finished. |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/client/InputHandler.ts`:
- Around line 317-333: Prettier formatting errors are caused by the block that
defines and transforms buildKeybinds in InputHandler.ts; reformat this section
(variable buildKeybinds, the .map using this.keybinds, the push of numpad keys,
and the for-loop) to match the project's Prettier rules by running the formatter
(run `prettier --write src/client/InputHandler.ts`) or apply equivalent
formatting changes so the file passes CI.
- Around line 317-333: The build keybind registration only adds Numpad0..Numpad9
extras so pressing Digit1 when the keybind is set to Numpad1 bypasses the
fallback; update the registration logic in InputHandler (the buildKeybinds array
mapping and the subsequent push of extras) to also include the Digit0..Digit9
counterparts for each Numpad key (or add a small helper that, for any "NumpadX"
adds both "NumpadX" and "DigitX") so resolveBuildKeybind() will be reached on
both Numpad and Digit keyups; apply the same dual-registration change to the
analogous block around lines 335-347 to cover all build-related keybinds.
- Around line 299-301: Remove the debug console.log calls from the input hot
paths: delete the console.log("TogglePerformanceOverlayEvent") inside the
addKeybindAndEvent handler that emits TogglePerformanceOverlayEvent (and any
other console.log added in the same file during init, e.g., the one around line
334-335). Locate the usages of addKeybindAndEvent and the init/startup code in
InputHandler (search for the string "TogglePerformanceOverlayEvent" and
"console.log") and simply remove the console.log statements so the handler only
calls this.eventBus.emit(new TogglePerformanceOverlayEvent()) and initialization
code has no noisy logs.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: d0ddfca9-2f39-45ab-94f4-fd62e41f7d9b
📒 Files selected for processing (1)
src/client/InputHandler.ts
|
@coderabbitai resume |
✅ Action performedReviews resumed. |
|
NVM |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
src/client/InputHandler.ts (1)
976-986: ⚡ Quick winTighten callback types to match
KeybindEntryinterface.The
eventparameter returnsanybut the interface expectsvoid. Theconditionsreturnanybut should returnboolean. This weakens type checking and can hide bugs.♻️ Proposed fix
private addKeybindAndEvent( keybind: string, - event: (type: KeyboardEvent) => any, - ...conditions: ((type: KeyboardEvent) => any)[] + event: (type: KeyboardEvent) => void, + ...conditions: ((type: KeyboardEvent) => boolean)[] ) {🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/client/InputHandler.ts` around lines 976 - 986, Tighten the callback signatures in InputHandler.addKeybindAndEvent so they match KeybindEntry: change the event handler type to return void and the conditions callbacks to return boolean. Update the addKeybindAndEvent parameter types and the KeybindEntry assignment in InputHandler to preserve type safety without altering the push logic.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/client/InputHandler.ts`:
- Line 230: The `keybindAndEvent` field in the InputHandler class is declared
but never initialized, causing it to be undefined when `addKeybindAndEvent()`
attempts to push entries to it. Additionally, the type annotation `[[string,
KeybindEntry]]` incorrectly represents a fixed-size tuple with one element
rather than a growable array. Initialize the `keybindAndEvent` field with an
empty array at the point of declaration, and correct the type annotation to
properly represent an array of tuples by using `[string, KeybindEntry][]`
instead.
- Around line 365-367: In the buildKeybinds filter operation, replace the loose
equality operator with strict equality. Change the comparison `v != null` to use
`v !== null` in the type guard predicate to comply with the project's strict
equality enforcement rule.
---
Nitpick comments:
In `@src/client/InputHandler.ts`:
- Around line 976-986: Tighten the callback signatures in
InputHandler.addKeybindAndEvent so they match KeybindEntry: change the event
handler type to return void and the conditions callbacks to return boolean.
Update the addKeybindAndEvent parameter types and the KeybindEntry assignment in
InputHandler to preserve type safety without altering the push logic.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 3c4e6e10-ae82-4f71-9031-5e9aad74de9d
📒 Files selected for processing (1)
src/client/InputHandler.ts
|
@Celant also how should I keep the remap support for altKey? |
|
@coderabbitai resume |
✅ Action performedReviews resumed. |
|
@TKTK123456 (via Discord): I do want this to be implemented by v32 or early v33 |
|
@TKTK123456 (via Discord): And think it should be |
Add approved & assigned issue number here:
Resolves #4193
Description:
Use activeKeys set in places where it is checking if a key is being pressed in a different way, and it makes more sense to use the activeKeys set. Make the overall code of the InputHandler.ts file more consistent and to make it easier to add new keybinds in the future.
Please complete the following:
Please put your Discord username so you can be contacted if a bug or regression is found:
tktk1234567