xterm.js icon indicating copy to clipboard operation
xterm.js copied to clipboard

Support DECBKM to remap backspace <-> delete

Open PDrifting opened this issue 4 years ago • 5 comments

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

PDrifting avatar Aug 14 '20 04:08 PDrifting

Looks like this is missing from the VT feature wiki page https://github.com/xtermjs/xterm.js/wiki/VT-features#dec-private-reset-mode

Tyriar avatar Aug 14 '20 15:08 Tyriar

Code pointer:

https://github.com/xtermjs/xterm.js/blob/cece3db0cb01bfb403d468010b89fb3161fe0b5d/src/common/InputHandler.ts#L1934-L1938

Tyriar avatar Oct 19 '21 17:10 Tyriar

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;
}

a-x- avatar Apr 16 '23 09:04 a-x-

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?

jerch avatar Apr 16 '23 10:04 jerch

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.

Tyriar avatar May 24 '23 13:05 Tyriar