Skip to content

fix(term): correctly map Ctrl+[ to ESC on non-US keyboard layouts#3406

Open
Jason-Shen2 wants to merge 2 commits into
wavetermdev:mainfrom
Jason-Shen2:fix/ctrl-bracket-escape
Open

fix(term): correctly map Ctrl+[ to ESC on non-US keyboard layouts#3406
Jason-Shen2 wants to merge 2 commits into
wavetermdev:mainfrom
Jason-Shen2:fix/ctrl-bracket-escape

Conversation

@Jason-Shen2

Copy link
Copy Markdown

Summary

Fixes a bug where pressing Ctrl+[ was incorrectly sending Ctrl+] (GS, \x1d) instead of ESC (\x1b) on some Linux systems with non-US keyboard layouts.

Root Cause

The keyboardEventToASCII utility only handled Ctrl+A-Z letter keys, leaving other standard terminal control characters (Ctrl+[ → ESC, Ctrl+\ → FS, Ctrl+] → GS) to be processed by xterm.js. On non-US keyboard layouts, the browser may report an incorrect event.key value when Ctrl is held (e.g. reporting "]" when the physical "[" key is pressed), causing xterm.js to send the wrong control character.

Changes

  1. frontend/util/keyutil.ts:

    • Extend keyboardEventToASCII to handle all single-character Ctrl combos using the standard charCode & 0x1F formula (not just A-Z)
    • Add an event.code-based fallback map for bracket/backslash/slash keys to reliably identify physical keys regardless of keyboard layout
    • Prioritize code-based mapping over event.key when Ctrl is held for known physical keys
  2. frontend/app/view/term/term-model.ts:

    • Add a fallback handler in handleTerminalKeydown that uses the fixed keyboardEventToASCII to send correct ASCII control codes for Ctrl+key combos not already handled by app keybindings (paste, copy, tab switching, etc.)

Fixes #3334

On some Linux keyboard layouts, xterm.js may misidentify Ctrl+symbol
key combinations (e.g. reporting Ctrl+] when Ctrl+[ is pressed),
because event.key reports the wrong character when Ctrl is held.

- Extend keyboardEventToASCII to handle all single-char Ctrl combos
  using the standard charCode & 0x1F formula (not just A-Z)
- Add event.code-based fallback for bracket/backslash keys to
  correctly identify physical keys regardless of keyboard layout
- Add fallback handler in handleTerminalKeydown to send correct
  ASCII control codes for Ctrl combos not already handled by
  app keybindings

Fixes wavetermdev#3334
@coderabbitai

coderabbitai Bot commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 76e48d51-df1f-4331-b9c5-adb33e5670c2

📥 Commits

Reviewing files that changed from the base of the PR and between 86f0cf2 and 8038d25.

📒 Files selected for processing (1)
  • frontend/util/keyutil.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • frontend/util/keyutil.ts

Walkthrough

keyboardEventToASCII in keyutil.ts is extended with a ctrlCodeMap that maps physical key codes to base characters for Ctrl-combo translation on non-US keyboard layouts. The Ctrl branch now special-cases Ctrl+Space and Ctrl+?, then derives control characters via ctrlCodeMap lookup or event.key, using either A–Z subtraction or an & 0x1f bitmask. In term-model.ts, handleTerminalKeydown gains a fallback that invokes keyboardEventToASCII for Ctrl-only combos and forwards any non-empty result to the terminal controller.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the Ctrl+[ mapping fix on non-US keyboard layouts.
Description check ✅ Passed The description is directly related to the terminal Ctrl+[ bug fix and the implementation changes.
Linked Issues check ✅ Passed The changes address #3334 by correctly mapping Ctrl+[ to ESC and handling related Ctrl control codes.
Out of Scope Changes check ✅ Passed The added terminal and key mapping changes appear scoped to the reported Ctrl+[ keyboard-layout fix.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 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 `@frontend/util/keyutil.ts`:
- Around line 292-297: The ctrlCodeMap fallback in keyutil.ts is still missing
the Slash physical key, so update the key-to-byte mapping used by the Ctrl key
handling to include Slash alongside BracketLeft, Backslash, BracketRight, and
Space. Make the change in the ctrlCodeMap object so the code-based path in the
key processing logic recognizes Slash even when event.key is localized on non-US
layouts.
🪄 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: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 0d01705b-384e-48cb-8d25-4e7118c07f2f

📥 Commits

Reviewing files that changed from the base of the PR and between c99022c and 86f0cf2.

📒 Files selected for processing (2)
  • frontend/app/view/term/term-model.ts
  • frontend/util/keyutil.ts

Comment thread frontend/util/keyutil.ts
CodeRabbit review identified that the ctrlCodeMap was missing the Slash
physical key, which would cause non-US layouts that surface a localized
event.key for the slash key to compute the wrong Ctrl byte.
@Jason-Shen2

Copy link
Copy Markdown
Author

Fixed in 8038d25: added Slash: "/" to ctrlCodeMap to cover the remaining slash-key hole on non-US layouts. Thanks for catching this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: The Ctrl+[ key is being mistakenly recognised as Ctrl+].

1 participant