|
13 | 13 | />
|
14 | 14 | </head>
|
15 | 15 | <body>
|
16 |
| - <span style="font-size: 1.4em">pyxterm.js</span> |
| 16 | + <a href="https://github.com/cs01/pyxtermjs" target="_blank" style="font-size: 1.4em; text-decoration: none; color:black">pyxterm.js</a |
| 17 | + > |
| 18 | + </a> |
17 | 19 | <span style="font-size: small"
|
18 | 20 | >status:
|
19 | 21 | <span style="font-size: small" id="status">connecting...</span></span
|
|
39 | 41 | macOptionIsMeta: true,
|
40 | 42 | scrollback: true,
|
41 | 43 | });
|
| 44 | + term.attachCustomKeyEventHandler(customKeyEventHandler); |
42 | 45 | // https://github.com/xtermjs/xterm.js/issues/2941
|
43 | 46 | const fit = new FitAddon.FitAddon();
|
44 | 47 | term.loadAddon(fit);
|
|
52 | 55 | fit.fit();
|
53 | 56 | term.writeln("Welcome to pyxterm.js!");
|
54 | 57 | term.writeln("https://github.com/cs01/pyxterm.js");
|
| 58 | + term.writeln('') |
| 59 | + term.writeln("You can copy with ctrl+shift+x"); |
| 60 | + term.writeln("You can paste with ctrl+shift+v"); |
| 61 | + term.writeln('') |
55 | 62 | term.onData((data) => {
|
56 |
| - console.log("key pressed in browser:", data); |
| 63 | + console.log("browser terminal received new data:", data); |
57 | 64 | socket.emit("pty-input", { input: data });
|
58 | 65 | });
|
59 | 66 |
|
|
92 | 99 | };
|
93 | 100 | }
|
94 | 101 |
|
| 102 | + /** |
| 103 | + * Handle copy and paste events |
| 104 | + */ |
| 105 | + function customKeyEventHandler(e) { |
| 106 | + if (e.type !== "keydown") { |
| 107 | + return true; |
| 108 | + } |
| 109 | + if (e.ctrlKey && e.shiftKey) { |
| 110 | + const key = e.key.toLowerCase(); |
| 111 | + if (key === "v") { |
| 112 | + // ctrl+shift+v: paste whatever is in the clipboard |
| 113 | + navigator.clipboard.readText().then((toPaste) => { |
| 114 | + term.writeText(toPaste); |
| 115 | + }); |
| 116 | + return false; |
| 117 | + } else if (key === "c" || key === "x") { |
| 118 | + // ctrl+shift+x: copy whatever is highlighted to clipboard |
| 119 | + |
| 120 | + // 'x' is used as an alternate to 'c' because ctrl+c is taken |
| 121 | + // by the terminal (SIGINT) and ctrl+shift+c is taken by the browser |
| 122 | + // (open devtools). |
| 123 | + // I'm not aware of ctrl+shift+x being used by anything in the terminal |
| 124 | + // or browser |
| 125 | + const toCopy = term.getSelection(); |
| 126 | + navigator.clipboard.writeText(toCopy); |
| 127 | + term.focus(); |
| 128 | + return false; |
| 129 | + } |
| 130 | + } |
| 131 | + return true; |
| 132 | + } |
| 133 | + |
95 | 134 | const wait_ms = 50;
|
96 | 135 | window.onresize = debounce(fitToscreen, wait_ms);
|
97 | 136 | </script>
|
|
0 commit comments