Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

altKey is not defined. #2832

Closed
ghost opened this issue Apr 11, 2020 · 3 comments
Closed

altKey is not defined. #2832

ghost opened this issue Apr 11, 2020 · 3 comments
Labels
type/question A question on how to use the library

Comments

@ghost
Copy link

ghost commented Apr 11, 2020

#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.

@Tyriar
Copy link
Member

Tyriar commented Apr 11, 2020

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

onKey: IEvent<{ key: string, domEvent: KeyboardEvent }>;

Do this instead:

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

@Tyriar Tyriar closed this as completed Apr 11, 2020
@Tyriar Tyriar added the type/question A question on how to use the library label Apr 11, 2020
@ghost
Copy link
Author

ghost commented Apr 11, 2020

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.

@Tyriar
Copy link
Member

Tyriar commented Apr 11, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/question A question on how to use the library
Projects
None yet
Development

No branches or pull requests

1 participant