xterm.js
xterm.js copied to clipboard
Support DECBKM to remap backspace <-> delete
DECBKM to remap Backspace, Escape and Delete to their respective codes is not working at all. Quickly looking through code DECBKM is set to reset by default according to...
https://vt100.net/docs/vt510-rm/DECBKM.html
DEL issues an ESC keycode 27... which I now realise is not issuing the keycode but an ANSI sequence ESC issues keycode 27 Backspace issues keycode 127
This is troubling when you are trying to isolate what key is actually being pressed. And other odd behaviour when trying to parse things.
Code Sample
https://repl.it/@KelvinVerhey/decbkm-issue
Details
- Chrome/Firefox/IE
- Windows 10 Pro
- xterm.js version: dunno... whatever repl.it is using
Question
https://stackoverflow.com/questions/63405913/xterm-js-decbkm-support-for-del-and-backspace
Looks like this is missing from the VT feature wiki page https://github.com/xtermjs/xterm.js/wiki/VT-features#dec-private-reset-mode
Code pointer:
https://github.com/xtermjs/xterm.js/blob/cece3db0cb01bfb403d468010b89fb3161fe0b5d/src/common/InputHandler.ts#L1934-L1938
Solution is here: https://github.com/xtermjs/xterm.js/issues/4278#issuecomment-1332092361
My adapted version:
const keymap = [
{ key: "Backspace", shiftKey: false, mapSeq: "\b \b" } as const, // mapCode: 8
{ key: "Backspace", shiftKey: true, mapSeq: "\b \b" } as const, // mapCode: 127
] as { key: string; shiftKey: boolean; mapCode?: number; mapSeq?: string }[];
//...
term.attachCustomKeyEventHandler((e) => handleKey(e, term));
// ...
function handleKey(ev: KeyboardEvent, term: Terminal) {
if (ev.type == "keydown") {
for (let i in keymap) {
if (keymap[i].key == ev.key && keymap[i].shiftKey == ev.shiftKey) {
const string = keymap[i].mapCode ? String.fromCharCode(keymap[i].mapCode as number) : keymap[i].mapSeq;
if (string) term.write(string);
return false;
}
}
}
return true;
}
Hmm thinking again about DECBKM I come to the conclusion, that we should implement it as supported VT sequence.
Reason - While those custom overloads work, it is still a static solution for app side missing the configuration aspect of a sequence, means app side still cannot change the behavior on its on behalf/needs.
@Tyriar What do you think? Would it be possible to hook into Keyboard.ts
and simply return a different value based on a dec private mode value (seems to be a small code addition)? Or would that negatively interfere with the custom key bindings?
This is a bit of a weird one. I think if you ran some app that set it and it crashed, the mode would be sticky and break backspace? I guess it's the same as something like DECNKM
and we should trust that it's implemented properly? Adding to core seems fine, we can always rip it out or add an option for it later if needed.