CARVIEW |
VirtualKeyboard API
More details about this document
- This version:
- https://www.w3.org/TR/2022/WD-virtual-keyboard-20220505/
- Latest published version:
- https://www.w3.org/TR/virtual-keyboard/
- Latest editor's draft:
- https://w3c.github.io/virtual-keyboard/
- History:
- https://www.w3.org/standards/history/virtual-keyboard
- Commit history
- Editor:
- Anupam Snigdha (Microsoft Corporation)
- Former editor:
- Grisha Lyukshin (Microsoft Corporation)
- Feedback:
- GitHub w3c/virtual-keyboard (pull requests, new issue, open issues)
- public-editing-tf@w3.org with subject line [virtual-keyboard] … message topic … (archives)
Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang). W3C liability, trademark and permissive document license rules apply.
Abstract
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.
Status of This Document
This section describes the status of this document at the time of its publication. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at https://www.w3.org/TR/.
This document was published by the Web Editing Working Group as a Working Draft using the Recommendation track.
Publication as a Working Draft does not imply endorsement by W3C and its Members.
This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.
This document was produced by a group operating under the W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.
This document is governed by the 2 November 2021 W3C Process Document.
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
As well as sections marked as non-normative, all authoring guidelines, diagrams, examples, and notes in this specification are non-normative. Everything else in this specification is normative.
The key words MUST and MUST NOT in this document are to be interpreted as described in BCP 14 [RFC2119] [RFC8174] when, and only when, they appear in all capitals, as shown here.
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.)
WebIDLpartial 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:
boundingRect
A DOMRect
, initially has zero values.
overlaysContent
A boolean, initially false
. When this attribute is set to true
, a user agent MUST NOT resize its document's viewport or visual viewport.
-
show()
method -
The method must follow these steps:
-
Let window be this's relevant global object. Assert that window is a
Window
object. - If window does not have sticky activation, abort these steps.
- If the focused element is not a form control (such as the value of the textarea element), or an editing host (e.g., using contenteditable) then abort these steps.
-
If the
virtualKeyboardPolicy
is notmanual
or inputMode's attribute value is none then abort these steps. - Call the system API to show the VK.
-
In parallel, follow these steps:
- Wait for the virtual keyboard to be shown by the system.
- Call
set the virtual keyboard bounding rect
with the keyboard's OS reported bounding rectangle and the document's viewport rectangle. - Fire an event named "geometrychange" at this.
-
Let window be this's relevant global object. Assert that window is a
-
hide()
method -
The method must follow these steps:
-
Let window be this's relevant global object. Assert that window is a
Window
object. - If window does not have sticky activation, abort these steps.
-
If the focused element's
virtualKeyboardPolicy
is notmanual
or inputMode's attribute value is none then abort these steps. - Call the system API to hide the VK.
-
In parallel, follow these steps:
- Wait for the virtual keyboard to be hidden by the system.
- Call
set the virtual keyboard bounding rect
with the keyboard's OS reported bounding rectangle (which has all 0 values) and the document's viewport rectangle. - Fire an event named "geometrychange" at this.
NotePlatform heuristics may impose additional restrictions on VK
show
()
andhide
()
. e.g., on Windows the pointer type MUST be touch or pen.NoteA few people have expressed concerns around
show
()
andhide
()
methods not being promise-based, but we believe it is a better design for web developers to useongeometrychange
event since it is fired when VK visibility changes, making the return values unnecessary. -
Let window be this's relevant global object. Assert that window is a
-
The
overlaysContent
getter steps are to return this'soverlaysContent
.The
overlaysContent
setter steps are to set this'soverlaysContent
to the given value. -
The attribute reports the intersection of the VK with the document's viewport in client coordinates. Call
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:-
Let osk be the on-screen keyboard rectangle that is the result of running the algorithm in
show
()
orhide
()
on this. -
Let lv be the document's viewport that is the result of running the algorithm in
show
()
orhide
()
on this. - Map osk to the coordinate space of lv.
-
Let bounds be a
DOMRect
object. - Update bounds by intersecting lv with the osk.
- Map bounds to the coordinate space of lv.
- Return bounds.
The
boundingRect
getter steps are to return this'sboundingRect
. -
Let osk be the on-screen keyboard rectangle that is the result of running the algorithm in
-
ongeometrychange
-
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.
WebIDL partial interface mixin ElementContentEditable
{
[CEReactions] attribute DOMString virtualKeyboardPolicy
;
};
Add handling of input and textarea.
The virtualKeyboardPolicy
is an enumerated attribute whose keywords are the empty 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 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.
- When auto enumerated attribute is set, the VK is handled by the browser.
- When manual enumerated attribute is set, it prevents the default handling of the VK in a browser and needs to be handled by the script.
- When empty string enumerated attribute is set, the VK is handled by the browser.
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 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.

-
User Agent MUST only allow
hide
()
andshow
()
methods to be called in a Secure Context. -
User Agent MUST only allow the
show
()
method to be called when sticky activation is present.
7.2 boundingRect
, overlaysContent
and ongeometrychange
attributes
overlaysContent
to be set in a secure, top-level browsing context.
WebIDLpartial 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
;
};
partial interface mixin ElementContentEditable
{
[CEReactions] attribute DOMString virtualKeyboardPolicy
;
};
Add contributors
- [dom]
- DOM Standard. Anne van Kesteren. WHATWG. Living Standard. URL: https://dom.spec.whatwg.org/
- [geometry-1]
- Geometry Interfaces Module Level 1. Simon Pieters; Chris Harrelson. W3C. 4 December 2018. W3C Candidate Recommendation. URL: https://www.w3.org/TR/geometry-1/
- [html]
- HTML Standard. Anne van Kesteren; Domenic Denicola; Ian Hickson; Philip Jägenstedt; Simon Pieters. WHATWG. Living Standard. URL: https://html.spec.whatwg.org/multipage/
- [RFC2119]
- Key words for use in RFCs to Indicate Requirement Levels. S. Bradner. IETF. March 1997. Best Current Practice. URL: https://www.rfc-editor.org/rfc/rfc2119
- [RFC8174]
- Ambiguity of Uppercase vs Lowercase in RFC 2119 Key Words. B. Leiba. IETF. May 2017. Best Current Practice. URL: https://www.rfc-editor.org/rfc/rfc8174
- [webidl]
- Web IDL Standard. Edgar Chen; Timothy Gu. WHATWG. Living Standard. URL: https://webidl.spec.whatwg.org/
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in: