1 Programming interface
Shuanglei Tao edited this page 2022-11-03 10:34:34 +08:00
interface TtydTerminal extends Terminal {
    fit(): void;
}

declare global {
    interface Window {
        term: TtydTerminal;
    }
}

ttyd exposes the xterm.js Terminal instance via window.term, and a fit() function on it (see fit addon).

Use Cases

  • Programmatically sending input from the client (#620)
    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);
    
  • How to paste+execute (simulate pressing Enter on Xterm.js) (#979)
    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}))