Skip to content

Add on-demand on-screen keyboard for Monaco editor on touch devices - #8167

Open
elias-coderunner wants to merge 1 commit into
coder:mainfrom
elias-coderunner:feature/mobile-keyboard-suppression
Open

elias-coderunner wants to merge 1 commit into
coder:mainfrom
elias-coderunner:feature/mobile-keyboard-suppression

Conversation

@elias-coderunner

Copy link
Copy Markdown

Problem: hypersenstivie keyboard appearing after every screen interaction (Android)

I am an Android code-server user. I use my phone to read and edit files on my vps, code-server is my UI.

I ran into the problem of hypersensitive keyboard that pops up on any screentouch. It's annoying to me because i can't swipe to read text without having to close the keyboard after each swipe.

I built a patch to solve the problem for myself. Sharing it so that you can assess if it's valuable for the product.

Solution

[this pr attemts to fix the issue number 8166]

A standalone 292-line TypeScript module (zero dependencies) makes the on-screen keyboard on-demand: the editor starts in a read mode by default on touch devices, and the user opts into typing mode with a double-tap. It ships as a quilt patch under patches/mobile-on-demand-keyboard.diff — matching the existing convention for every other code-server change against vendored vscode — which adds mobileOnDemandKeyboard.ts next to workbench.ts and wires its initializer into the workbench bootstrap.

Monaco exposes two input shapes across versions — the legacy hidden textarea.inputarea and the modern EditContext-API div.native-edit-context — and both get the same treatment.

The module keeps one piece of user-facing state, typingMode (plus small closure vars for double-tap and viewport-resize detection), and installs three cooperating suppression layers so that whichever path Monaco (or the browser) uses to land focus on an input, that focus either doesn't take or lands without a keyboard:

  • .focus() method override on HTMLTextAreaElement.prototype and HTMLElement.prototype. Monaco calls el.focus() directly in a lot of places; the override silently drops those calls when they target a Monaco input and typing mode is off. Installed synchronously at module load, before the workbench boots.
  • Capture-phase focusin blur guard. If a focus does slip past the method override (e.g. via a browser-level path like autofocus), the guard blurs the Monaco input the same tick. Second line of defense.
  • inputmode="none" stamping. The OS honors inputmode="none" on a focused input to suppress the virtual keyboard entirely. The module monkey-patches Element.prototype.setAttribute, .setAttributeNS and the className setter so the attribute is set in the same synchronous block Monaco adds its class="inputarea" in — before Monaco's next line (the focus() call) runs. A MutationObserver stays in place as a safety net for paths the setter hooks don't cover.

On top of that, the module watches two signals to go back to read mode automatically:

  • Any tap outside .monaco-editor while in typing mode toggles typing mode off (and blurs the active element, which dismisses the keyboard).
  • A visualViewport resize with a large height growth indicates the OS keyboard just closed (user hit the down-arrow / back gesture). Typing mode flips off so the next editor tap doesn't just reopen it.

A double-tap inside the editor toggles typing mode. The threshold is 350ms between taps, 30px of drift.

On desktop typingMode initializes to true (IS_TOUCH is false), so every suppression branch — the .focus() override, the focusin blur guard, and the inputmode="none" stamp — short-circuits on its !typingMode check. The listeners and prototype wrappers are still installed, but their guard condition is never satisfied. On touch devices the module reacts to taps everywhere on the page: inside .monaco-editor a double-tap toggles typing mode, outside it a single tap in typing mode exits back to read mode. It also reacts to Monaco's two input element classes being set on or added to any element in the DOM, and to visualViewport.resize events.

Testing

Playwright e2e. test/e2e/mobileOnDemandKeyboard.test.ts enables touch per-test via test.use({ hasTouch: true, ... }) and asserts the module's observable DOM effects:

  • On load, inputmode="none" is stamped on the Monaco textarea.
  • el.focus() on the Monaco textarea does not take focus in read mode.
  • A double-tap at the editor center removes inputmode (typing mode on).
  • A subsequent tap outside the editor re-stamps inputmode="none" (typing mode off).

Playwright has no way to observe "the OS keyboard opened" directly, so the test targets the observable preconditions for suppression rather than the OS behavior itself.

On-device. An earlier JS prototype of this module has been my daily driver on a Pixel 6 over Tailscale for a few days alongside the pinch-zoom module. The TypeScript port submitted in this PR was compiled from the exact module in the patch and deployed to the same device today, and behaves identically: scrolling and caret-positioning work without the keyboard appearing, a double-tap reliably opens the keyboard, and the OS down-arrow / tap-outside paths both return to read mode.

Commits are signed.

Adds a quilt patch that ships a module (mobileOnDemandKeyboard.ts)
which puts the editor in a read mode on touch devices by default, so
taps don't summon the OS virtual keyboard. A double-tap inside the
editor enters typing mode; tapping outside, or the browser reporting
the keyboard dismissed, exits back to read mode.

Fixes coder#8166
@elias-coderunner
elias-coderunner requested a review from a team as a code owner December 1, 2026 10:15

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant