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

Programmatically sending input from the client #620

Closed
dpage-edb opened this issue Mar 3, 2021 · 5 comments
Closed

Programmatically sending input from the client #620

dpage-edb opened this issue Mar 3, 2021 · 5 comments
Labels

Comments

@dpage-edb
Copy link

Hi

How can I programmatically send input from the client? Doing something like:

term.writeln('ls -al');

will write the text to the terminal, but it doesn't send it to the server (even when hitting return afterwards).

Thanks!

@tsl0922
Copy link
Owner

tsl0922 commented Mar 3, 2021

You can access the javascript Terminal object via window.term.

@tsl0922 tsl0922 closed this as completed Mar 3, 2021
@dpage-edb
Copy link
Author

Thanks for the quick reply. My issue is not how to access the Terminal object; that seems to work fine - the problem is that using term.write() displays the text on the terminal UI, but never sends it to the server.

For example, in the screenshot below, I first sent the "select version()" command using the Javascript console. That never gets sent to the server. If I then type the same command into the terminal window, it works fine and I get the version info I expect.

Screenshot 2021-03-03 at 12 22 46

@tsl0922
Copy link
Owner

tsl0922 commented Mar 3, 2021

There isn't an api from ttyd or xterm that can do it, but you can simulate keyboard dom events to xterm's textarea, it should work.

@tsl0922
Copy link
Owner

tsl0922 commented Mar 3, 2021

example code to send ls to the terminal:

var script = document.createElement('script');
script.src = 'https://unpkg.com/keysim@latest/dist/keysim.js';
script.onload = function() {
	var input = document.querySelector('textarea');
	var keyboard = Keysim.Keyboard.US_ENGLISH;
	keyboard.dispatchEventsForInput("ls", input);
	keyboard.dispatchEventsForAction("enter", input);
};
document.body.appendChild(script);

@dpage-edb
Copy link
Author

Yes, that works. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants