navigator.virtualKeyboard is the experimental entry point to the VirtualKeyboard API. Learn overlaysContent, boundingRect, geometrychange, show() / hide(), secure-context rules, five examples, and try-it labs.
01
Kind
Read-only property
02
Returns
VirtualKeyboard
03
Status
Experimental
04
Context
Secure (HTTPS)
05
Opt out
overlaysContent
06
Geometry
boundingRect
Fundamentals
Introduction
On phones and tablets, focusing an input often opens an on-screen virtual keyboard. By default, many browsers shrink the visual viewport so the page does not sit under the keyboard.
The VirtualKeyboard API lets you take more control: opt out of that automatic resize, read the keyboard’s position and size, listen when it appears or disappears, and (when allowed) show or hide it from script.
💡
Who needs this?
Chat apps, editors, and full-screen forms that want a custom layout when the keyboard opens — instead of relying only on the browser’s automatic viewport shrink.
Concept
Understanding the virtualKeyboard Property
navigator.virtualKeyboard is a read-only entry point. The useful work happens on the returned VirtualKeyboard object.
overlaysContent — set true to stop the browser from auto-resizing the viewport for the keyboard.
boundingRect — a DOMRect with x, y, width, height.
geometrychange — event when the keyboard geometry changes (appear / disappear / resize).
show() / hide() — programmatically show or hide when supported.
Secure context — HTTPS / localhost required where supported.
Limited support — feature-detect; desktop browsers often lack it.
Foundation
📝 Syntax
General form of the property:
JavaScript
navigator.virtualKeyboard
Value
A VirtualKeyboard object (when supported).
Common patterns
JavaScript
// MDN-style feature detect + opt out + geometry
if ("virtualKeyboard" in navigator) {
navigator.virtualKeyboard.overlaysContent = true;
navigator.virtualKeyboard.addEventListener("geometrychange", (event) => {
const { x, y, width, height } = event.target.boundingRect;
console.log({ x, y, width, height });
});
} else {
console.log("VirtualKeyboard API not available — keep a CSS / viewport fallback.");
}
Cheat Sheet
⚡ Quick Reference
Goal
Code
Get VirtualKeyboard
navigator.virtualKeyboard
Feature detect
"virtualKeyboard" in navigator
Opt out of auto resize
navigator.virtualKeyboard.overlaysContent = true
Read geometry
navigator.virtualKeyboard.boundingRect
Listen for changes
addEventListener("geometrychange", …)
Show / hide
show() / hide()
Secure context?
window.isSecureContext
Snapshot
🔍 At a Glance
Four facts to remember about navigator.virtualKeyboard.
Returns
VirtualKeyboard
On-screen KB API
Status
Experimental
Not Baseline
Context
HTTPS
Secure required
Key flag
overlaysContent
Opt out auto
Compare
📋 Automatic Viewport Shrink vs VirtualKeyboard Control
Default browser behavior
With VirtualKeyboard API
When keyboard opens
Viewport often shrinks automatically
You can opt out with overlaysContent = true
Layout control
Browser decides
You adapt using boundingRect
Events
Indirect (resize / visualViewport)
geometrychange on the keyboard
Availability
Common on mobile browsers
Experimental / limited
Hands-On
Examples Gallery
Examples follow MDN navigator.virtualKeyboard patterns. Prefer Try It Yourself on a Chromium mobile / tablet device over HTTPS — many desktops report the API missing.
📚 Getting Started
Detect the API and opt out of automatic keyboard viewport handling.
Example 1 — Feature Detection
Check support and secure context before using the API.
x=0 y=0 w=0 h=0
(non-zero when the on-screen keyboard is visible on a supporting device)
How It Works
Use these numbers to pad the bottom of a chat composer or scroll an input into view manually.
Example 4 — geometrychange Listener
MDN example idea: update layout when the keyboard opens or closes.
JavaScript
if (!("virtualKeyboard" in navigator)) {
console.log("virtualKeyboard not supported");
} else {
navigator.virtualKeyboard.overlaysContent = true;
navigator.virtualKeyboard.addEventListener("geometrychange", (event) => {
const { x, y, width, height } = event.target.boundingRect;
console.log("geometrychange:", { x, y, width, height });
});
console.log("Listening for geometrychange — focus an input on a device with an OSK.");
}
Listening for geometrychange — focus an input on a device with an OSK.
(then geometrychange logs when the keyboard moves)
How It Works
The event fires when keyboard geometry changes — appear, disappear, or resize.
Example 5 — show() / hide() Safely
Call show/hide only when methods exist; focus an input first on real devices.
JavaScript
function tryShowHide() {
if (!("virtualKeyboard" in navigator)) {
return "virtualKeyboard not supported";
}
const vk = navigator.virtualKeyboard;
if (typeof vk.show !== "function" || typeof vk.hide !== "function") {
return "show/hide not available on this build";
}
// On a real device, focus an <input> first, then:
// vk.show();
// vk.hide();
return "show/hide methods exist — test with a focused input on mobile";
}
console.log(tryShowHide());
navigator.virtualKeyboard belongs to the experimental VirtualKeyboard API and is not Baseline. Support is limited (often Chromium on mobile / tablet). Always feature-detect, use HTTPS, and keep a layout fallback.
✓ Experimental · Not Baseline
Navigator.virtualKeyboard
VirtualKeyboard entry point — opt out of auto viewport shrink, read boundingRect, listen for geometrychange.
LimitedExperimental
Google ChromeVirtualKeyboard / Chromium (often mobile path)
Limited
Microsoft EdgeFollow Chromium VirtualKeyboard support
Limited
OperaFollow Chromium VirtualKeyboard support
Limited
Mozilla FirefoxTypically unavailable — feature-detect
Unavailable
Apple SafariTypically unavailable — feature-detect
Unavailable
Internet ExplorerNo virtualKeyboard support
Unavailable
virtualKeyboardLimited
Bottom line: Detect navigator.virtualKeyboard, set overlaysContent when you need custom layout, listen for geometrychange, and never require the API for core form UX.
Wrap Up
Conclusion
navigator.virtualKeyboard is the experimental entry point for controlling on-screen keyboard layout behavior. Use overlaysContent, boundingRect, and geometrychange when available — and keep a solid fallback for browsers without the API.
Experimental OSK control — overlaysContent, boundingRect, geometrychange, HTTPS.
5
Core concepts
📄01
Returns
VirtualKeyboard
Value
⚡02
Status
Experimental
Limited
🔒03
Context
secure HTTPS
Required
🖥04
Opt out
overlaysContent
Layout
🎯05
Measure
boundingRect
Geometry
❓ Frequently Asked Questions
A VirtualKeyboard object. You can opt out of automatic viewport resizing, read the keyboard’s boundingRect, listen for geometrychange, and call show() / hide() when supported.
MDN marks it Experimental and Limited availability (not Baseline). It is not Deprecated or Non-standard. Always feature-detect and keep a fallback.
Yes. MDN lists it as a secure-context feature (HTTPS or localhost) in supporting browsers.
A Boolean on VirtualKeyboard. When true, you opt out of the browser’s automatic behavior that shrinks the viewport to make room for the on-screen keyboard — so you can adapt the layout yourself.
A DOMRect describing the current position and size of the virtual keyboard. Use it after geometrychange or when the keyboard is visible.
The API targets devices with on-screen keyboards (phones, tablets). On many desktops the API is missing, or show/hide may have little effect. Always feature-detect.
Did you know?
Even without VirtualKeyboard, many sites already use window.visualViewport to react when mobile browsers resize around the on-screen keyboard. The VirtualKeyboard API is a more direct, keyboard-specific option when the browser supports it.