JavaScript Navigator virtualKeyboard Property

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Experimental

What You’ll Learn

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

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.

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.

📝 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.");
}

⚡ Quick Reference

GoalCode
Get VirtualKeyboardnavigator.virtualKeyboard
Feature detect"virtualKeyboard" in navigator
Opt out of auto resizenavigator.virtualKeyboard.overlaysContent = true
Read geometrynavigator.virtualKeyboard.boundingRect
Listen for changesaddEventListener("geometrychange", …)
Show / hideshow() / hide()
Secure context?window.isSecureContext

🔍 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

📋 Automatic Viewport Shrink vs VirtualKeyboard Control

Default browser behaviorWith VirtualKeyboard API
When keyboard opensViewport often shrinks automaticallyYou can opt out with overlaysContent = true
Layout controlBrowser decidesYou adapt using boundingRect
EventsIndirect (resize / visualViewport)geometrychange on the keyboard
AvailabilityCommon on mobile browsersExperimental / limited

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.

JavaScript
const lines = [
  "virtualKeyboard: " + ("virtualKeyboard" in navigator ? "available" : "missing"),
  "isSecureContext: " + window.isSecureContext
];
console.log(lines.join("\n"));
Try It Yourself

How It Works

Missing is normal on desktop. Build a CSS / visualViewport fallback for those browsers.

Example 2 — Set overlaysContent

MDN pattern: opt out of automatic virtual keyboard viewport behavior.

JavaScript
if (!("virtualKeyboard" in navigator)) {
  console.log("virtualKeyboard not supported");
} else {
  navigator.virtualKeyboard.overlaysContent = true;
  console.log("overlaysContent: " + navigator.virtualKeyboard.overlaysContent);
}
Try It Yourself

How It Works

When overlaysContent is true, you take responsibility for adapting the layout using keyboard geometry.

📈 Practical Patterns

Read geometry, listen for changes, and try show / hide safely.

Example 3 — Read boundingRect

Log the current keyboard rectangle (often zeros when the keyboard is hidden).

JavaScript
if (!("virtualKeyboard" in navigator)) {
  console.log("virtualKeyboard not supported");
} else {
  const r = navigator.virtualKeyboard.boundingRect;
  console.log(
    "x=" + r.x + " y=" + r.y + " w=" + r.width + " h=" + r.height
  );
}
Try It Yourself

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.");
}
Try It Yourself

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());
Try It Yourself

How It Works

Programmatic show/hide is powerful but device- and policy-dependent — never assume it works everywhere.

🚀 Common Use Cases

  • Chat / messaging UIs — keep the composer above the keyboard using boundingRect.height.
  • Full-screen editors — opt out of viewport shrink and draw your own inset.
  • Forms in PWAs — listen for geometrychange to scroll focused fields into view.
  • Custom keyboards / toolbars — align sticky toolbars with the OSK top edge.
  • Progressive enhancement — fall back to visualViewport / CSS when the API is missing.

🧠 How navigator.virtualKeyboard Works

1

Feature-detect on HTTPS

Check "virtualKeyboard" in navigator in a secure context.

Detect
2

Opt out if needed

Set overlaysContent = true to manage layout yourself.

Overlay
3

Listen and measure

Use geometrychange + boundingRect when the OSK moves.

Geometry
4

Keep a fallback

Without the API, rely on normal focus behavior and visualViewport / CSS.

📝 Notes

  • Experimental & Limited availability — not Baseline; feature-detect always.
  • Not Deprecated or Non-standard — Experimental banner only.
  • Secure context required (HTTPS / localhost).
  • Returns a VirtualKeyboard object; main pieces are overlaysContent, boundingRect, geometrychange, show(), hide().
  • Related: wakeLock, userActivation, keyboard, Window.

Limited / Experimental Support

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.

Limited Experimental
Google Chrome VirtualKeyboard / Chromium (often mobile path)
Limited
Microsoft Edge Follow Chromium VirtualKeyboard support
Limited
Opera Follow Chromium VirtualKeyboard support
Limited
Mozilla Firefox Typically unavailable — feature-detect
Unavailable
Apple Safari Typically unavailable — feature-detect
Unavailable
Internet Explorer No virtualKeyboard support
Unavailable
virtualKeyboard Limited

Bottom line: Detect navigator.virtualKeyboard, set overlaysContent when you need custom layout, listen for geometrychange, and never require the API for core form UX.

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.

Continue with wakeLock, userActivation, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Feature-detect "virtualKeyboard" in navigator
  • Use HTTPS / localhost
  • Test on a real phone / tablet with an OSK
  • Listen for geometrychange after opting out
  • Keep a visualViewport / CSS fallback

❌ Don’t

  • Assume Baseline desktop support
  • Require the API for basic form usability
  • Ignore secure-context requirements
  • Forget that boundingRect may be zeros when hidden
  • Assign to navigator.virtualKeyboard

Key Takeaways

Knowledge Unlocked

Five things to remember about virtualKeyboard

Experimental OSK control — overlaysContent, boundingRect, geometrychange, HTTPS.

5
Core concepts
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.

Learn wakeLock Next

Baseline Screen Wake Lock — keep the display on for recipes, maps, and more.

wakeLock →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful