blob: eb6ab10aa27b34f986d9b5391378866e65235434 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
---
title: Keyboard
slug: Web/API/Keyboard
translation_of: Web/API/Keyboard
---
<div>{{SeeCompatTable}}{{APIRef("Keyboard API")}}</div>
<p><span class="seoSummary">The <strong><code>Keyboard</code></strong> interface of the the <a href="/docs/Web/API/Keyboard_API">Keyboard API</a> provides functions that retrieve keyboard layout maps and toggle capturing of key presses from the physical keyboard.</span></p>
<p>A list of valid code values is found in the <a href="https://www.w3.org/TR/uievents-code/#key-alphanumeric-writing-system">UI Events KeyboardEvent code Values</a> spec.</p>
<h2 id="Properties">Properties</h2>
<p>None.</p>
<h2 id="Methods">Methods</h2>
<dl>
<dt>{{domxref('Keyboard.getLayoutMap()')}} {{experimental_inline}}</dt>
<dd>Returns a {{jsxref('Promise')}} that resolves with an instance of {{domxref('KeyboardLayoutMap')}} which is a map-like object with functions for retrieving the strings associated with specific physical keys.</dd>
<dt>{{domxref('Keyboard.lock()')}} {{experimental_inline}}</dt>
<dd>Returns a {{jsxref('Promise')}} after enabling the capture of keypresses for any or all of the keys on the physical keyboard.</dd>
<dt>{{domxref('Keyboard.unlock()')}} {{experimental_inline}}</dt>
<dd>Unlocks all keys captured by the <code>lock()</code> method and returns synchronously.</dd>
</dl>
<h2 id="Example">Example</h2>
<p>The following example demonstrates how to get the location- or layout-specific string associated with the key that corresponds to the 'W' key on an English QWERTY keyboard.</p>
<pre class="brush: js">if (navigator.keyboard) {
var keyboard = navigator.keyboard;
keyboard.getLayoutMap()
.then(keyboardLayoutMap => {
var upKey = keyboardLayoutMap.get('KeyW');
window.alert('Press ' + upKey + ' to move up.');
});
} else {
// Do something else.
}</pre>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('Keyboard Map','#keyboard-interface','Keyboard')}}</td>
<td>{{Spec2('Keyboard Map')}}</td>
<td>Initial definition.</td>
</tr>
<tr>
<td>{{SpecName('Keyboard Lock','#keyboard-interface','Keyboard')}}</td>
<td>{{Spec2('Keyboard Lock')}}</td>
<td>Adds <code>lock()</code> and <code>unlock()</code>.</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{Compat("api.Keyboard")}}</p>
|