Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. VirtualKeyboard API

VirtualKeyboard API

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Experimental:This is anexperimental technology
Check theBrowser compatibility table carefully before using this in production.

Secure context: This feature is available only insecure contexts (HTTPS), in some or allsupporting browsers.

The VirtualKeyboard API provides developers control over the layout of their applications when the on-screen virtual keyboard appears and disappears on devices such as tablets, mobile phones, or other devices where a hardware keyboard may not be available.

Web browsers usually deal with virtual keyboards on their own, by adjusting the viewport height and scrolling to input fields when focused.

The figure below illustrates the difference in viewport height and scroll position on a web page when the device has its on-screen virtual keyboard hidden, and when it is shown.

Two devices, one without a virtual keyboard, showing that the webpage can use most of the device's vertical space, and one with a virtual keyboard, showing that the webpage can only be displayed in the remaining space

More complex applications or specific devices such as multi-screen mobile phones may require more control of the layout when the virtual keyboard appears.

The figure below shows what happens on a dual-screen device when the virtual keyboard appears on just one of the two screens. The viewport becomes smaller on both screens to accommodate for the virtual keyboard, leaving wasted space on the screen where the virtual keyboard is not displayed.

A dual-screen device, with its virtual keyboard displayed on one screen, showing that the webpage can only use the vertical space that remains after the keyboard was displayed, even if that leaves empty space on the other screen

The VirtualKeyboard API can be used to opt out of the way the browser automatically handles the virtual keyboard, and take full control of it instead. With the VirtualKeyboard API, the keyboard still appears and disappears as necessary when form controls are focused, but the viewport does not change, and you can use JavaScript and CSS to adapt your layout.

Concept

The VirtualKeyboard API consists of three parts:

  • TheVirtualKeyboard interface, accessed throughnavigator.virtualKeyboard, is used to opt out of the automatic virtual keyboard behavior, show or hide the virtual keyboard programmatically, as well as get the current position and size of the virtual keyboard.
  • Thekeyboard-inset-* CSS environment variables provide information about the virtual keyboard's position and size.
  • Thevirtualkeyboardpolicy attribute specifies whether the virtual keyboard should appear oncontenteditable elements.

Opt out of the automatic virtual keyboard behavior

To opt out of the automatic virtual keyboard behavior of the browser, usenavigator.virtualKeyboard.overlaysContent = true. The browser will no longer automatically resize the viewport to make space for the virtual keyboard, which will overlay your content instead.

Detect the virtual keyboard geometry using JavaScript

Once you've opted out of the default browser behavior, you can get the current geometry of the virtual keyboard usingnavigator.virtualKeyboard.boundingRect, and adapt the layout as appropriate using CSS and JavaScript. In addition, you can listen to geometry changes, such as when the keyboard is shown or hidden, by using thegeometrychange event.

This is useful for positioning important UI elements in the area that's not covered by the virtual keyboard.

The code snippet below uses thegeometrychange event to detect when the virtual keyboard geometry changes; it then accesses theboundingRect property to query the size and position of the virtual keyboard:

js
if ("virtualKeyboard" in navigator) {  navigator.virtualKeyboard.overlaysContent = true;  navigator.virtualKeyboard.addEventListener("geometrychange", (event) => {    const { x, y, width, height } = event.target.boundingRect;  });}

Detect the virtual keyboard geometry using CSS environment variables

The VirtualKeyboard API also exposes the followingCSS environment variables:keyboard-inset-top,keyboard-inset-right,keyboard-inset-bottom,keyboard-inset-left,keyboard-inset-width, andkeyboard-inset-height.

Thekeyboard-inset-* CSS environment variables are useful to adapt your layout to the virtual keyboard appearance using CSS. They define a rectangle by its top, right, bottom, and left insets from the edge of the viewport. Thewidth andheight variables are also available if needed.

The code snippet below uses thekeyboard-inset-height CSS variable to reserve space for the virtual keyboard to appear below the list of messages and input field in a chat-like application. When the virtual keyboard is hidden, theenv() function returns0px and thekeyboard grid area is hidden. The messages and input elements can occupy the full height of the viewport. When the virtual keyboard appears, thekeyboard grid area gets the height of the virtual keyboard.

html
<ul></ul><input type="text" />
css
body {  display: grid;  margin: 0;  height: 100vh;  grid-template:    "messages" 1fr    "input" auto    "keyboard" env(keyboard-inset-height, 0px);}
js
if ("virtualKeyboard" in navigator) {  navigator.virtualKeyboard.overlaysContent = true;}

Control the virtual keyboard oncontenteditable elements

By default, elements using thecontenteditable attribute also trigger the virtual keyboard when tapped or clicked. In certain situations, it may be desirable to prevent this behavior and instead show the virtual keyboard after a different event.

Set thevirtualkeyboardpolicy attribute tomanual to prevent the default handling of the virtual keyboard in the browser, and instead handle it yourself by using theVirtualKeyboard interface'sshow() andhide() methods.

The code snippet below shows how to use thevirtualkeyboardpolicy attribute and thenavigator.virtualKeyboard.show() method to show the virtual keyboard on double-click instead:

html
<div contenteditable virtualkeyboardpolicy="manual"></div>
js
if ("virtualKeyboard" in navigator) {  navigator.virtualKeyboard.overlaysContent = true;  const editor = document.getElementById("editor");  editor.addEventListener("dblclick", () => {    navigator.virtualKeyboard.show();  });}

Interfaces

VirtualKeyboardExperimental

Provides functions that retrieve keyboard layout maps and toggle capturing of key presses from the physical keyboard.

Extensions to other interfaces

Navigator.virtualKeyboardRead onlyExperimental

Returns a reference to theVirtualKeyboard API, to take control of the on-screen virtual keyboard.

HTMLElement.virtualkeyboardpolicyExperimental

A string indicating whether to use the browser's default policy for showing the virtual keyboard when the element is focused, or to handle showing the virtual keyboard manually.

Specifications

Specification
VirtualKeyboard API
# the-virtualkeyboard-interface

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp