The {{VirtualKeyboard}} API provides authors with greater control over the visibility of the virtual keyboard (VK), and greater ability to adapt the layout of web pages when VK visibility changes.
This section is non-normative.
The Virtual Keyboard (VK) is the on-screen keyboard used for input in scenarios where a hardware keyboard may not be available. User agents respond to the presence of the VK, without any exposure of this information to web developers in the following way: 1. Repositioning the user agent above the VK 2. Reducing the size of the layout viewport so the VK doesn't occlude it 3. Reducing the size of the visual viewport and padding the layout viewport to ensure it can be shifted above the VK This API provides a fourth option that allows the user agent to leave its layout and visual viewports unchanged and instead provide information about the intersection of the VK and layout viewport so that the author can adapt the layout of their web pages using JavaScript or CSS environment variables.
Figure showing virtual keyboard on dual screen device
Figure showing virtual keyboard on single-touch screen device
This specification defines conformance criteria that apply to a single product: the user agent that implements the interfaces that it contains.
Conformance requirements phrased as algorithms or specific steps may be implemented in any manner, so long as the end result is equivalent. (In particular, the algorithms defined in this specification are intended to be easy to follow, and not intended to be performant.)
partial interface Navigator { [SecureContext, SameObject] readonly attribute VirtualKeyboard virtualKeyboard; }; [Exposed=Window, SecureContext] interface VirtualKeyboard : EventTarget { undefined show(); undefined hide(); readonly attribute DOMRect boundingRect; attribute boolean overlaysContent; attribute EventHandler ongeometrychange; };
The {{VirtualKeyboard}} object has an associated:
boundingRectA {{DOMRect}}, initially has zero values.
overlaysContentA boolean, initially `false`. When this attribute is set to `true`, a user agent MUST NOT resize its [=document=]'s viewport or visual viewport.
The method must follow these steps:
The method must follow these steps:
Platform heuristics may impose additional restrictions on VK {{VirtualKeyboard/show()}} and {{VirtualKeyboard/hide()}}. e.g., on Windows the pointer type MUST be touch or pen.
A few people have expressed concerns around {{VirtualKeyboard/show()}} and {{VirtualKeyboard/hide()}} methods not being promise-based, but we believe it is a better design for web developers to use {{VirtualKeyboard/ongeometrychange}} event since it is fired when VK visibility changes, making the return values unnecessary.
{{VirtualKeyboard/overlaysContent}}
The {{VirtualKeyboard/overlaysContent}} getter steps are to return [=this=]'s {{VirtualKeyboard/overlaysContent}}.
The {{VirtualKeyboard/overlaysContent}} setter steps are to set [=this=]'s {{VirtualKeyboard/overlaysContent}} to the given value.
{{VirtualKeyboard/boundingRect}}
The attribute reports the intersection of the VK with the [=document=]'s viewport in client coordinates. Call {{VirtualKeyboard/set the virtual keyboard bounding rect}}.
set the virtual keyboard bounding rect
To set the virtual keyboard bounding rect, with |osk| (a DOMRect representing the on-screen keyboard rectangle), and |lv| (a DOMRect representing the [=document=]'s viewport rectangle) as inputs, run the following steps:The {{VirtualKeyboard/boundingRect}} getter steps are to return [=this=]'s {{VirtualKeyboard/boundingRect}}.
The event is dispatched when the intersection of the VK and the [=document=]'s viewport changes, e.g., in response to the VK being shown or hidden or the browser window being repositioned.
partial interface ElementContentEditable { [CEReactions] attribute DOMString virtualKeyboardPolicy; };
Add handling of input and textarea.
The virtualKeyboardPolicy is an enumerated attribute whose keywords are the string
, auto
, and manual
. The IDL attribute must reflect the respective content attributes of the same name.
When specified on an element that is a contenteditable host, auto
causes the corresponding editable element to automatically show the VK when it is focused or tapped & manual
decouples focus and tap on the editable element from changes in the VK’s current state - the VK remains as it was.
The change in setting of any {{ElementContentEditable/virtualKeyboardPolicy}} attribute value, negates currently defined behavior, unless it is a change from auto
to empty string
or vice versa.
Specify timing relative to events like focus.
The {{VirtualKeyboard}} API proposes six new CSS environment variables that the web developers can utilize to calculate the virtual keyboard's size and adjust layout in a declarative way.
These CSS env should be added to the CSS env variable spec.
Name | Value |
---|---|
keyboard-inset-top | length |
keyboard-inset-right | length |
keyboard-inset-bottom | length |
keyboard-inset-left | length |
keyboard-inset-width | length |
keyboard-inset-height | length |
The keyboard insets are six environment variables that define a rectangle by its top, right, bottom, and left insets from the edge of the viewport. Default value of the keyboard insets are "0px" if a fallback value is not provided else it gets updated when {{VirtualKeyboard/boundingRect}} value changes. The width & height insets are calculated from the remaining insets for developer ergonomics.
The figure and markup below are a representation of a canvas-based spreadsheet that repositions the active cell when the VK is shown. The `geometrychange` event triggers a paint request for the canvas. The painting code can then use the `boundingRect` property to render the active cell in the proper location.
The figure below represents a map application that presents a map on one window segment and search results on another. Using the proposal for Window Segments and media queries, the search box shown will increase its bottom margin to remain visible whenever the virtual keyboard appears on the left side of the foldable device.
Add contributors