fix(desktop): prevent browser default behavior for terminal shortcuts#988
Conversation
Add event.preventDefault() for Cmd+Left, Cmd+Right, Cmd+Backspace, and Shift+Enter to prevent browser default actions from interfering with xterm's hidden textarea. This fixes a bug where IME input would malfunction after using these shortcuts for cursor navigation.
📝 WalkthroughWalkthroughThis PR adds Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
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 |
|
Thanks for creating the PR, sorry I must've missed this entirely |
Summary
Fix IME (Input Method Editor) input corruption after using
Cmd+Left,Cmd+Right,Cmd+Backspace, orShift+Entershortcuts in the terminal.Problem
When using IME to input CJK characters (Chinese, Japanese, Korean) in the terminal:
Cmd+Leftto move cursor to the beginning of the lineAs is
2026-01-27.12.43.52.mov
To be
2026-01-27.12.44.45.mov
More detail
This issue does not occur when:
Ctrl+Adirectly (instead ofCmd+Left)Root Cause
This bug was introduced in #926 (
feat(terminal): add Cmd+Arrow for cursor line navigation), which remapped shortcuts to match macOS conventions:Cmd+Left/RightCmd+Option+Left/RightCmd+Option+Left/RightCmd+Shift+Left/RightCmd+Left/Rightxterm.js uses a hidden
<textarea>element to capture keyboard input and IME composition. When handling custom shortcuts viaattachCustomKeyEventHandler:falsetells xterm to skip its default key handlingevent.preventDefault(), so browser default behavior still occursCmd+Leftin browsers moves the textarea cursor to the beginning of the textSolution
Add
event.preventDefault()before handling the following shortcuts:Cmd+Left(move to line beginning)Cmd+Right(move to line end)Cmd+Backspace(delete to line beginning)Shift+Enter(line continuation)This prevents the browser from manipulating the hidden textarea's cursor position while we send the appropriate control sequences to the PTY.
Testing
Cmd+Leftto jump to line beginningAdditional Notes
A video demonstration can be provided if needed to illustrate the bug and the fix.