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:

I'm not sure, what is problem.
onKey's has an object as the first param, not 2 params.
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.