Skip to content

Commit

Permalink
fix(Terminal): Accept input from Chrome on Android
Browse files Browse the repository at this point in the history
Xterm.js produces two kinds of events: onKey and onData. On a desktop,
these are effectively the same, but on mobile, IME inputs produce data
but not key presses. By listening to onData instead of onKey, we get
that input.

With some experimentation, I also found that we don't need the code to
handle enter, home, end, or Ctrl-Shift-V. All of these function as
expected without that code, so we can remove it and simplify this
further. :^)
  • Loading branch information
AtkinsSJ committed May 14, 2024
1 parent 2656b47 commit 4ef3e53
Showing 1 changed file with 3 additions and 38 deletions.
41 changes: 3 additions & 38 deletions packages/terminal/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ class XTermIO {
}

bind () {
this.term.onKey(this.handleKey.bind(this));

this.term.attachCustomKeyEventHandler(this.handleKeyBeforeProcess.bind(this));
this.term.onData(this.handleData.bind(this));

(async () => {
for ( ;; ) {
Expand All @@ -40,41 +38,8 @@ class XTermIO {
})();
}

async handleKeyBeforeProcess (evt) {
if ( evt.key === 'V' && evt.ctrlKey && evt.shiftKey && evt.type === 'keydown' ) {
const clipboard = navigator.clipboard;
const text = await clipboard.readText();
this.pty.out.write(text);
}
}

handleKey ({ key, domEvent }) {
const pty = this.pty;

const handlers = {
Enter: () => {
pty.out.write('\n');
},
// Backspace: () => {
// pty.out.write('\x08');
// },
// Delete: () => {
// pty.out.write('\x1B[3~');
// },
Home: () => {
pty.out.write('\x1B[H');
},
End: () => {
pty.out.write('\x1B[F');
}
}

if ( handlers.hasOwnProperty(domEvent.key) ) {
const writeKey = handlers[domEvent.key]();
if ( ! writeKey ) return;
}

pty.out.write(key);
handleData ( data ) {
this.pty.out.write(data);
}
}

Expand Down

0 comments on commit 4ef3e53

Please sign in to comment.