Xterm.js: altKey is not defined.

Created on 11 Apr 2020  路  3Comments  路  Source: xtermjs/xterm.js

2655

                const term = new xterm.default.Terminal({ cursorBlink: true });
        const fitAddon = new xtermAddonFit.FitAddon();
        term.loadAddon(fitAddon);
        term.open(document.getElementById('term'));
        term.writeln('Terminal: ');
        term.writeln('');
        term.prompt = function () {
            term.write('\r\n' + '$: ');
        };
        term.prompt();
        term.onKey(function (key, ev) {
            console.log(ev)
            var printable = (
                !ev.altKey && !ev.altGraphKey && !ev.ctrlKey && !ev.metaKey
            );

            if (ev.keyCode == 13) {
                term.prompt();
            } else if (ev.keyCode == 8) {
                // Do not delete the prompt
                if (term.x > 2) {
                    term.write('\b \b');
                }
            } else if (printable) {
                term.write(key);
            }
        });

        term.paste('paste', function (data, ev) {
            term.write(data);
        });
        const socket = new WebSocket('ws://localhost:8000/')
        term.loadAddon(attachAddon);
        const attachAddon = new attachAddon(socket)
        fitAddon.fit();

output when I press any button:
Sn铆mka obrazovky z 2020-04-11 14-12-17

I'm not sure, what is problem.

typquestion

All 3 comments

onKey's has an object as the first param, not 2 params.

https://github.com/xtermjs/xterm.js/blob/1913e9512c048e3cf56bb5f5df51bfff6899c184/typings/xterm.d.ts#L641

Do this instead:

term.onKey(function (ev) {
            console.log(ev.key);
            console.log(ev.domEvent)

my onKey function look now like this:

term.onKey(function (ev) {
            var printable = (
                !ev.altKey && !ev.altGraphKey && !ev.ctrlKey && !ev.metaKey
            );

            if (ev.keyCode == 13) {
                term.prompt();
            } else if (ev.keyCode == 8) {
                // Do not delete the prompt
                if (term.x > 2) {
                    term.write('\b \b');
                }
            } else if (printable) {
                term.write(ev.key);
            }
        });

but when I write command and press enter, it doesn't create new line in output.
I tried to add term.writeln(''); to if (ev.Keycode == 13)

same with backspace (keyCode 8)..

other things work fine.

If you want to write things to the terminal, the recommended way it to listening to onData instead which gives you a string containing serialized keystrokes. Stack Overflow would be a better place for these sorts of questions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kolbe picture kolbe  路  3Comments

albinekb picture albinekb  路  4Comments

chris-tse picture chris-tse  路  4Comments

LB-J picture LB-J  路  3Comments

travisobregon picture travisobregon  路  3Comments