Skip to content

API in Capabilities

VirtualKeyboard API

Published Updated

The navigator.virtualKeyboard property exposes the VirtualKeyboard interface, which lets a page opt out of automatic viewport resizing when the on-screen keyboard appears. The interface reports the keyboard’s intersection with the layout viewport, provides CSS insets for that rectangle, and accepts requests to show or hide the keyboard for an editable element using manual policy.

BCD api.VirtualKeyboard records support from Chrome 94 and resolves its mirrored desktop entry to Edge 94. Its Chrome Android, Oculus, Opera, Samsung Internet, and WebView entries also mirror another browser’s data; Firefox and Safari are recorded as unsupported. BCD api.VisualViewport records the fallback interface from Firefox 91 and Safari 13. Check the API compatibility data together with runtime detection before enabling overlay layout.

navigator.virtualKeyboard.overlaysContent = true
navigator.virtualKeyboard.boundingRect
navigator.virtualKeyboard.show()
navigator.virtualKeyboard.hide()
navigator.virtualKeyboard.addEventListener('geometrychange', handler)

The Web IDL exposes navigator.virtualKeyboard in secure contexts as a same-object, read-only property. VirtualKeyboard extends EventTarget and is exposed on Window. The specification initializes boundingRect with zero values and initializes overlaysContent to false.

The CSS environment variables derive from the keyboard rectangle. Their default value is 0px; the specification updates them when boundingRect changes.

.composer {
padding-bottom: env(keyboard-inset-height, 0px);
}

The keyboard-inset-* family contains keyboard-inset-top, -right, -bottom, -left, -width, and -height.

The interface declaration contains the members below. The ongeometrychange event-handler attribute is separate from the read-only rectangle and the overlay setting.

Member Type Defined behavior
boundingRect read-only DOMRect Reports the intersection of the on-screen keyboard and the document viewport in client coordinates.
overlaysContent boolean When true, the user agent must not resize the document viewport or visual viewport for the keyboard.
ongeometrychange EventHandler Receives the Event dispatched when the keyboard and viewport intersection changes; read the rectangle from event.target.boundingRect.
show() returns undefined Requests that the system show the keyboard after the algorithm’s checks pass.
hide() returns undefined Requests that the system hide the keyboard after the algorithm’s checks pass.

The show() algorithm first checks for sticky activation. It then checks that the focused element is a form control such as <input> or an editing host such as a contenteditable element, that its virtualKeyboardPolicy is manual, and that its inputMode is not none. The corresponding content attributes are virtualkeyboardpolicy and inputmode; inputmode="none" prevents the request. If a check fails, the algorithm aborts. After the system reports that the keyboard is shown, the algorithm updates the rectangle and fires geometrychange.

The hide() algorithm also checks sticky activation, manual policy, and an inputMode other than none. It does not contain the show() algorithm’s separate form-control-or-editing-host check. After the system reports that the keyboard is hidden, the algorithm stores an all-zero keyboard rectangle and fires geometrychange. The specification also allows platform heuristics to restrict both methods; its Windows example requires a touch or pen pointer.

The virtualkeyboardpolicy keywords are the empty string, auto, and manual. With the empty string or auto, the browser handles the keyboard; manual prevents that default handling so script can control it. The specification exposes the interface in secure contexts. Set navigator.virtualKeyboard.overlaysContent only when your page will reposition content from boundingRect or the CSS insets. Chromium additionally restricts the assignment to its outermost main frame, as detailed in the caution below. When it is true, the keyboard overlays the page because the user agent no longer resizes the document or visual viewport for it.

None.

The methods return undefined, so a fulfilled promise cannot confirm that the keyboard changed. The specification directs authors to observe geometrychange, which fires after the keyboard rectangle changes. A missing event can therefore follow an aborted precondition check or an additional platform restriction; inspect the focused element, policy, input mode, and user activation before changing layout state.

These examples feature-detect the navigator property. The supported path opts into overlay layout.

Follow the visual viewport when the API is absent

Section titled “Follow the visual viewport when the API is absent”

Use window.visualViewport when 'virtualKeyboard' in navigator is false. This preserves the old page’s fallback while keeping the VirtualKeyboard path tied to geometrychange.

const composer = document.querySelector('.composer');
if ('virtualKeyboard' in navigator) {
navigator.virtualKeyboard.overlaysContent = true;
navigator.virtualKeyboard.addEventListener('geometrychange', (event) => {
const { height } = event.target.boundingRect;
composer.style.setProperty('--keyboard-height', `${height}px`);
});
} else if (window.visualViewport) {
const vv = window.visualViewport;
vv.addEventListener('resize', () => {
const covered = window.innerHeight - vv.height - vv.offsetTop;
composer.style.setProperty('--keyboard-height', `${Math.max(0, covered)}px`);
});
}

Both branches write the same custom property. The supported branch reads the keyboard rectangle, while the fallback derives the covered portion from the visual viewport.

.composer {
position: fixed;
bottom: 0;
padding-bottom: var(--keyboard-height, env(keyboard-inset-height, 0px));
}

The CSS keeps the composer at the bottom and uses 0px when neither source has supplied a height.

Place the form near the end of the document. The handler reads the updated rectangle from the event target, while the CSS environment variable covers layout before the first event supplies a nonzero height.

<form id="composer" class="composer">
<label for="message">Message</label>
<input id="message" name="message" autocomplete="off">
<button type="submit">Send</button>
<output id="keyboard-mode" aria-live="polite"></output>
</form>
<script type="module">
const composer = document.querySelector('#composer');
const mode = document.querySelector('#keyboard-mode');
if ('virtualKeyboard' in navigator) {
navigator.virtualKeyboard.overlaysContent = true;
document.documentElement.classList.add('virtual-keyboard-overlay');
navigator.virtualKeyboard.addEventListener('geometrychange', (event) => {
composer.style.setProperty('--keyboard-height', `${event.target.boundingRect.height}px`);
});
mode.value = 'Overlay layout enabled';
} else {
mode.value = 'Browser-managed viewport resizing enabled';
}
</script>

Apply the reported height to the fixed form. When feature detection takes the fallback branch, do not fix the form over the viewport; normal document flow gives the browser room to resize and reveal the focused input.

.composer {
background: Canvas;
padding: 0.75rem;
}
html.virtual-keyboard-overlay .composer {
position: fixed;
right: 0;
bottom: 0;
left: 0;
padding-bottom: var(--keyboard-height, env(keyboard-inset-height, 0px));
}

Keeping the class change beside overlaysContent = true prevents the fallback from adopting an overlay layout it cannot measure. The unsupported branch leaves the form in document flow.

Use a visible editing host with virtualkeyboardpolicy="manual". The button supplies the user interaction, focuses the host, checks the author-controlled preconditions, and requests the keyboard only after those checks pass.

<button id="edit" type="button">Edit value</button>
<div id="editor" contenteditable="true"
virtualkeyboardpolicy="manual" inputmode="text"></div>
<output id="status" aria-live="polite">Keyboard not requested</output>
<script type="module">
const editor = document.querySelector('#editor');
const status = document.querySelector('#status');
document.querySelector('#edit').addEventListener('click', () => {
editor.focus();
if (!('virtualKeyboard' in navigator)) {
status.value = 'Use the focused editor with browser-managed keyboard behavior';
return;
}
if (editor.virtualKeyboardPolicy !== 'manual' || editor.inputMode === 'none') {
status.value = 'Fix virtualkeyboardpolicy or inputmode before requesting the keyboard';
return;
}
status.value = 'Waiting for geometrychange';
navigator.virtualKeyboard.show();
});
navigator.virtualKeyboard?.addEventListener('geometrychange', (event) => {
status.value = `Keyboard intersection height: ${event.target.boundingRect.height}px`;
});
</script>

If the supported path stays at Waiting for geometrychange, confirm that the click handler still runs directly from the user action and inspect the Chromium warning described above when testing that implementation. Do not treat the return value from show() as success: the method returns undefined, and the specification places the geometry update after the system reports the keyboard.

A <canvas> cannot itself serve as the focused form control or editing host required by show(). Pair it with an editable element using manual policy.

<div id="cell-input" contenteditable="true" virtualkeyboardpolicy="manual" inputmode="text"></div>

Add the canvas and completion button that the script addresses.

<canvas width="320" height="180" aria-label="Spreadsheet"></canvas>
<button id="done" type="button">Done</button>

Request the keyboard from the pointer handler, and blur the editable fallback when the API is absent.

const input = document.querySelector('#cell-input');
const canvas = document.querySelector('canvas');
canvas.addEventListener('pointerup', () => {
input.focus();
if ('virtualKeyboard' in navigator) {
navigator.virtualKeyboard.show(); // the tap supplied the sticky activation
}
});
document.querySelector('#done').addEventListener('click', () => {
if ('virtualKeyboard' in navigator) {
navigator.virtualKeyboard.hide();
} else {
input.blur();
}
});

The editable element remains the focus target in both paths. The canvas supplies the application interaction, while the button supplies an explicit completion action.

Specifications

SpecificationStatus
VirtualKeyboard API: the VirtualKeyboard interfaceW3C draft
VirtualKeyboard API: overlaysContent attributeW3C draft