Cannot type Shifted characters (e.g. #, ") or overlapping keys in Safari on macOS
Details
- Browser and browser version: Safari 18.5
- OS version: macOS 15.5
- xterm.js version: 5.5.0
Steps to reproduce
- Open an xterm.js terminal on Safari (macOS 15.5) with a Japanese (JP) keyboard layout.
- Try typing Shift + 3 to input
#— nothing appears. - While still holding Shift, press 3 again — now
#appears. - Similarly, try typing Shift + 2 to input
"— same behavior occurs. - Next, press and hold the
Akey, then press theSkey without releasingA. - Only
Aappears in the terminal —Sis ignored. - If you release
Abefore pressingS, both characters are input as expected.
This issue does not occur in Google Chrome on the same macOS system.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@xterm/[email protected]/css/xterm.min.css">
<script src="https://cdn.jsdelivr.net/npm/@xterm/[email protected]/lib/xterm.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', () => {
const terminal = new Terminal();
terminal.open(document.getElementById('terminal'));
terminal.focus();
terminal.onData((data) => {
terminal.write(data);
});
});
</script>
</head>
<body>
<div id="terminal"></div>
</body>
</html>
When executing the following in Safari, the CustomKeyEvent is triggered after onData. In other browsers, CustomKeyEvent is handled before onData.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@xterm/[email protected]/css/xterm.min.css">
<script src="https://cdn.jsdelivr.net/npm/@xterm/[email protected]/lib/xterm.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', () => {
const terminal = new Terminal();
console.log('Terminal initialized:', terminal);
terminal.open(document.getElementById('terminal'));
terminal.focus();
terminal.onData((data) => {
console.log('onDataData:', data);
terminal.write(data);
});
terminal.attachCustomKeyEventHandler( (e) => {
console.log('customKeyEvent:', e.type, e.key);
return true;
});
});
</script>
</head>
<body>
<div id="terminal"></div>
</body>
</html>
[Log] onDataData: – "a" (index.html, line 14)
[Log] customKeyEvent: – "keydown" – "a" (index.html, line 18)
[Log] customKeyEvent: – "keyup" – "a" (index.html, line 18)
[Log] onDataData: – "s" (index.html, line 14)
[Log] customKeyEvent: – "keydown" – "s" (index.html, line 18)
[Log] customKeyEvent: – "keyup" – "s" (index.html, line 18)
[Log] onDataData: – "d" (index.html, line 14)
[Log] customKeyEvent: – "keydown" – "d" (index.html, line 18)
[Log] customKeyEvent: – "keyup" – "d" (index.html, line 18)
[Log] onDataData: – "f" (index.html, line 14)
[Log] customKeyEvent: – "keydown" – "f" (index.html, line 18)
[Log] customKeyEvent: – "keyup" – "f" (index.html, line 18)
[Log] customKeyEvent: – "keydown" – "Shift" (index.html, line 18)
[Log] customKeyEvent: – "keydown" – "#" (index.html, line 18)
[Log] customKeyEvent: – "keyup" – "#" (index.html, line 18)
[Log] onDataData: – "#" (index.html, line 14)
[Log] customKeyEvent: – "keydown" – "#" (index.html, line 18)
[Log] customKeyEvent: – "keyup" – "#" (index.html, line 18)
[Log] customKeyEvent: – "keyup" – "Shift" (index.html, line 18)
For now, I was able to work around it with the following not-so-smart code, but there should be a better solution.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@xterm/[email protected]/css/xterm.min.css">
<script src="https://cdn.jsdelivr.net/npm/@xterm/[email protected]/lib/xterm.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', () => {
const terminal = new Terminal();
console.log('Terminal initialized:', terminal);
terminal.open(document.getElementById('terminal'));
terminal.focus();
let lastOnDataTime = 0;
terminal.onData((data) => {
console.log('onDataData:', data);
terminal.write(data);
lastOnDataTime = Date.now();
});
if (/Safari/.test(navigator.userAgent) ) {
terminal.attachCustomKeyEventHandler( (e) => {
if(e.keyCode!==16 && e.keyCode!==17 && e.keyCode!==18 && e.keyCode!==91 && e.keyCode!==93 && e.key.length==1) {
if (e.type === 'keydown' && Date.now() - lastOnDataTime > 50 ) {
console.log('customKeyEvent:', e.type, 'Ignored key:', e.key);
terminal.write(e.key);
} else {
console.log('customKeyEvent:', e.type, e.key);
}
}
return true;
});
}
});
</script>
</head>
<body>
<div id="terminal"></div>
</body>
</html>
I can confirm that this is still a problem as of Safari 26.0.1, and also affects touch typing in general as pressing the next key before the previous key is released results in a lost key. It's become pretty easy to find out what projects integrate Xterm.js based entirely on this behavior. I usually have to switch from Japanese Romaji input back to US to fix it temporarily, or just use another browser for the terminal session.
@Tyriar Hello, when can I send an MR for this question, please
Workaround for dropped input with Safari Chinese IME, Caps Lock and Shift I investigated an input issue in Safari when a Chinese IME is active.
Symptoms After enabling Caps Lock, the first letter is not sent. With the Chinese IME active, the first Shift+3 (#) input is not sent. Repeating the operation after the corresponding keyup may work. Observed event behavior In the affected cases:
keypress is not emitted. input may arrive before the character's keydown. The input event contains the expected character in event.data. event.composed is true. _keyDownSeen is still true, because it was set by the preceding Shift/Caps Lock keydown. This causes the input event to be rejected by the current _inputEvent condition:
if (
ev.data &&
ev.inputType === 'insertText' &&
(!ev.composed || !this._keyDownSeen) &&
!this.optionsService.rawOptions.screenReaderMode
) {
this.coreService.triggerDataEvent(ev.data, true);
}
At that moment:
ev.composed = true
_keyDownSeen = true
!ev.composed || !_keyDownSeen
= false || false
= false
Therefore, triggerDataEvent is not called.
The later character keydown does not recover the input:
An uppercase letter may be deferred by the existing A-Z keypress workaround. An IME-managed key such as Shift+3 may be reported as keyCode=229 and handled by the composition path. Safari does not emit the expected keypress. As a result, none of the available paths sends the character.
The first keyup resets _keyDownSeen to false, which also explains why the following input may work.
Application-level workaround I cannot patch xterm.js directly in the consuming project, but _core._keyDownSeen is still accessible at runtime.
I added a bubbling input listener after terminal.open():
import type { Terminal } from '@xterm/xterm';
export function installSafariImeInputFallback(
terminal: Terminal
): () => void {
const textarea = terminal.textarea;
const core = (terminal as any)._core;
if (!textarea || !core) {
return () => {};
}
const handleInput = (event: InputEvent): void => {
if (
event.data &&
event.inputType === 'insertText' &&
event.composed &&
core._keyDownSeen
) {
terminal.input(event.data);
}
};
textarea.addEventListener('input', handleInput);
return () => {
textarea.removeEventListener('input', handleInput);
};
}
- Users are reporting persistent input issues with xterm.js on Safari, specifically involving dropped characters.
- Long proposed a workaround for the Safari input bug involving a custom event listener.
On Wed, Aug 19, 2026 at 8:36 PM long @.***> wrote:
longshuaige left a comment (xtermjs/xterm.js#5374) https://github.com/xtermjs/xterm.js/issues/5374#issuecomment-5351040856
Workaround for dropped input with Safari Chinese IME, Caps Lock and Shift I investigated an input issue in Safari when a Chinese IME is active.
Symptoms After enabling Caps Lock, the first letter is not sent. With the Chinese IME active, the first Shift+3 (#) input is not sent. Repeating the operation after the corresponding keyup may work. Observed event behavior In the affected cases:
keypress is not emitted. input may arrive before the character's keydown. The input event contains the expected character in event.data. event.composed is true. _keyDownSeen is still true, because it was set by the preceding Shift/Caps Lock keydown. This causes the input event to be rejected by the current _inputEvent condition:
if ( ev.data && ev.inputType === 'insertText' && (!ev.composed || !this._keyDownSeen) && !this.optionsService.rawOptions.screenReaderMode ) { this.coreService.triggerDataEvent(ev.data, true); }
At that moment:
ev.composed = true _keyDownSeen = true
!ev.composed || !_keyDownSeen = false || false = false
Therefore, triggerDataEvent is not called.
The later character keydown does not recover the input:
An uppercase letter may be deferred by the existing A-Z keypress workaround. An IME-managed key such as Shift+3 may be reported as keyCode=229 and handled by the composition path. Safari does not emit the expected keypress. As a result, none of the available paths sends the character.
The first keyup resets _keyDownSeen to false, which also explains why the following input may work.
Application-level workaround I cannot patch xterm.js directly in the consuming project, but _core._keyDownSeen is still accessible at runtime.
I added a bubbling input listener after terminal.open():
import type { Terminal } from @.***/xterm';
export function installSafariImeInputFallback( terminal: Terminal ): () => void { const textarea = terminal.textarea; const core = (terminal as any)._core;
if (!textarea || !core) { return () => {}; }
const handleInput = (event: InputEvent): void => { if ( event.data && event.inputType === 'insertText' && event.composed && core._keyDownSeen ) { terminal.input(event.data); } };
textarea.addEventListener('input', handleInput);
return () => { textarea.removeEventListener('input', handleInput); }; }
— Reply to this email directly, view it on GitHub https://github.com/xtermjs/xterm.js/issues/5374?email_source=notifications&email_token=BRMAOF3WXILFPCVNAPJ4JO35KZW3FA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTKMZVGEYDIMBYGU3KM4TFMFZW63VHMNXW23LFNZ2KKZLWMVXHJLDGN5XXIZLSL5RWY2LDNM#issuecomment-5351040856, or unsubscribe https://github.com/notifications/unsubscribe-auth/BRMAOF5QBUUYHCYDPPY6OS35KZW3FAVCNFSNUABEKJSXA33TNF2G64TZHMYTQMBWHA2TIMR3JFZXG5LFHMZTEMRSGE4DIMBXGSQXMAQ . You are receiving this because you commented.Message ID: <xtermjs/xterm. @.***>
Root cause: On macOS, when Shift or Caps Lock is active, beforeinput / input may arrive before the character’s keydown. xterm.js still considers the preceding modifier key as pressed, leaving _keyDownSeen set to true, so it mistakenly drops the real input event as a duplicate.
Fix: In valid text beforeinput events on the macOS desktop client, reset _keyDownSeen to false so the following input can trigger xterm’s triggerDataEvent normally. The workaround is limited to composed insertText events outside IME composition to avoid affecting other input paths.
private setMacBeforeInputHandler() {
const textarea = this.term.textarea;
if (!textarea) return;
this.textareaBeforeInputHandler = (event: InputEvent) => {
if (
!event.composed ||
!event.data ||
event.inputType !== 'insertText' ||
event.isComposing
) {
return;
}
// macOS 客户端可能在字符自身 keydown 前触发 input。xterm 将修饰键也标记为
// _keyDownSeen,会让该 input 被当成重复事件而丢弃。仅处理 xterm 会用该标记
// 去重的 composed insertText,避免干预 IME 组合输入。
const core = (this.term as XtermTerminalInternals)._core;
if (core) {
core._keyDownSeen = false;
}
};
textarea.addEventListener('beforeinput', this.textareaBeforeInputHandler);
}
Duplicate of #5887
The dropped-character-on-key-rollover part of this is the same bug tracked there.