ttyd
ttyd copied to clipboard
How to paste+execute (simulate pressing Enter on Xterm.js)
I found it worth mentioning as I couldn't find an answer anywhere and it took me hours to figure it out.
This may be of interest to those who embed ttyd into their website (iframe etc.) and want to execute their command immediately, i.e. on button click.
So, if you want to pass the entire command and execute it immediately (without clicking on xterm and manually pressing enter), it's simple as :
term.paste("whoami");
document.querySelector("textarea.xterm-helper-textarea").dispatchEvent(new KeyboardEvent('keypress', {charCode: 13}))
If you are using an iframe, you must first access its contentWindow and contentDocument just like :
document.getElementById('youriframeid').contentWindow.term.paste("whoami");
document.getElementById('youriframeid').contentDocument.querySelector("textarea.xterm-helper-textarea").dispatchEvent(new KeyboardEvent('keypress', {charCode: 13}))