Xterm.js: Unable to detect `ENTER` key event with => xterm[4.2.0] onKey() method

Created on 13 Nov 2019  路  3Comments  路  Source: xtermjs/xterm.js

Using the latest version of xterm.js 4.2.0 i want to be able to detect key events and pass them on the terminal , i am not able to detect when ENTER or ALT or SHIFT is pressed like i used to with older version 3.x.x

chrome_2019-11-13_14-49-21

Why Enter is returning "" ?

What i have done:

    term.onKey(key => {
      const char = key;
      console.log(key);
      console.log(char.key === "");
      if (char === "") {
        console.log("Enter pressed");
        prompt();
      } else {
        term.write(char);
      }
    });

  prompt = () => {
    var shellprompt = "$ ";
    term.write("\r\n" + shellprompt);
  };

I am a new user of the library :)

typquestion

Most helpful comment

It's there you'll find it's actually \r:

Screen Shot 2019-11-13 at 6 40 09 AM

That is what a terminal emulator would normally send to a shell. If key doesn't have what you're after we also expose the KeyboardEvent directly:

https://github.com/xtermjs/xterm.js/blob/414eea2a2c4cde4c8fb72bb98cd48650bb89c927/typings/xterm.d.ts#L439-L445

All 3 comments

@jerch @Tyriar @parisk

It's there you'll find it's actually \r:

Screen Shot 2019-11-13 at 6 40 09 AM

That is what a terminal emulator would normally send to a shell. If key doesn't have what you're after we also expose the KeyboardEvent directly:

https://github.com/xtermjs/xterm.js/blob/414eea2a2c4cde4c8fb72bb98cd48650bb89c927/typings/xterm.d.ts#L439-L445

@Tyriar You are amazing you know that :) ?

I ended up using KeyboardEvent => keyboardEvent.domEvent.key

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jestapinski picture jestapinski  路  3Comments

ghost picture ghost  路  4Comments

johnpoth picture johnpoth  路  3Comments

kolbe picture kolbe  路  3Comments

circuitry2 picture circuitry2  路  4Comments