Skip to content

How to paste+execute (simulate pressing Enter on Xterm.js) #979

@arekdudys

Description

@arekdudys

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}))

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions