blob: 2b7cd53171d64ee5514ae25f2fa95d8f3c1a6a90 [file] [log] [blame]
Robert Ly35f2fda2013-01-29 16:27:05 -08001page.title=Key Layout Files
2@jd:body
3
4<!--
Clay Murphy1d5f8fe2015-05-18 16:44:07 -07005 Copyright 2015 The Android Open Source Project
Robert Ly35f2fda2013-01-29 16:27:05 -08006
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18-->
Clay Murphy1d5f8fe2015-05-18 16:44:07 -070019<div id="qv-wrapper">
20 <div id="qv">
21 <h2>In this document</h2>
22 <ol id="auto-toc">
23 </ol>
24 </div>
25</div>
26
Robert Ly35f2fda2013-01-29 16:27:05 -080027<p>Key layout files (<code>.kl</code> files) are responsible for mapping Linux key codes
28and axis codes to Android key codes and axis codes and specifying associated
29policy flags.</p>
30<p>Device-specific key layout files are <em>required</em> for all internal (built-in)
31input devices that have keys, including special keys such as volume, power
32and headset media keys.</p>
33<p>Device-specific key layout files are <em>optional</em> for other input devices but
34they are <em>recommended</em> for special-purpose keyboards and joysticks.</p>
35<p>If no device-specific key layout file is available, then the system will
36choose a default instead.</p>
37<h2 id="location">Location</h2>
38<p>Key layout files are located by USB vendor, product (and optionally version)
39id or by input device name.</p>
40<p>The following paths are consulted in order.</p>
41<ul>
42<li><code>/system/usr/keylayout/Vendor_XXXX_Product_XXXX_Version_XXXX.kl</code></li>
43<li><code>/system/usr/keylayout/Vendor_XXXX_Product_XXXX.kl</code></li>
44<li><code>/system/usr/keylayout/DEVICE_NAME.kl</code></li>
45<li><code>/data/system/devices/keylayout/Vendor_XXXX_Product_XXXX_Version_XXXX.kl</code></li>
46<li><code>/data/system/devices/keylayout/Vendor_XXXX_Product_XXXX.kl</code></li>
47<li><code>/data/system/devices/keylayout/DEVICE_NAME.kl</code></li>
48<li><code>/system/usr/keylayout/Generic.kl</code></li>
49<li><code>/data/system/devices/keylayout/Generic.kl</code></li>
50</ul>
51<p>When constructing a file path that contains the device name, all characters
52in the device name other than '0'-'9', 'a'-'z', 'A'-'Z', '-' or '<em>' are replaced by '</em>'.</p>
53<h2 id="generic-key-layout-file">Generic Key Layout File</h2>
54<p>The system provides a special built-in generic key layout file called <code>Generic.kl</code>.
55This key layout is intended to support a variety of standard external
56keyboards and joysticks.</p>
57<p><em>Do not modify the generic key layout!</em></p>
58<h2 id="syntax">Syntax</h2>
59<p>A key layout file is a plain text file consisting of key or axis declarations
60and flags.</p>
61<h3 id="key-declarations">Key Declarations</h3>
62<p>Key declarations each consist of the keyword <code>key</code> followed by a Linux key code
Robert Ly40e3b6d2013-04-17 18:12:10 -070063number and an Android key code name, or the keyword `usage` followed by a HID
64usage and an Android key code name. The HID usage is represented as a 32-bit
65integer, where the high 16-bits represent the HID usage page and the low
6616-bits represent the HID usage ID. Either of these declarations can then be
67followed by an optional set of whitespace delimited policy flags.</p>
68<pre><code>
69key 1 ESCAPE
Robert Ly35f2fda2013-01-29 16:27:05 -080070key 114 VOLUME_DOWN WAKE
71key 16 Q VIRTUAL WAKE
Robert Ly40e3b6d2013-04-17 18:12:10 -070072key usage 0x0c006F BRIGHTNESS_UP
Robert Ly35f2fda2013-01-29 16:27:05 -080073</code></pre>
74<p>The following policy flags are recognized:</p>
75<ul>
76<li><code>WAKE</code>: The key should wake the device when it is asleep. For historical reasons,
77 this flag behaves in the same manner as <code>WAKE_DROPPED</code> below.</li>
78<li><code>WAKE_DROPPED</code>: The key should wake the device when it is asleep but the key itself
79 should be dropped when the wake-up occurs. In a sense, the key's action was to
80 wake the device, but the key itself is not processed.</li>
81<li><code>SHIFT</code>: The key should be interpreted as if the SHIFT key were also pressed.</li>
82<li><code>CAPS_LOCK</code>: The key should be interpreted as if the CAPS LOCK key were also pressed.</li>
83<li><code>ALT</code>: The key should be interpreted as if the ALT key were also pressed.</li>
84<li><code>ALT_GR</code>: The key should be interpreted as if the RIGHT ALT key were also pressed.</li>
85<li><code>FUNCTION</code>: The key should be interpreted as if the FUNCTION key were also pressed.</li>
86<li><code>VIRTUAL</code>: The key is a virtual soft key (capacitive button) that is adjacent to
87 the main touch screen. This causes special debouncing logic to be enabled, see below.</li>
88<li><code>MENU</code>: Deprecated. Do not use.</li>
89<li><code>LAUNCHER</code>: Deprecated. Do not use.</li>
90</ul>
91<h3 id="axis-declarations">Axis Declarations</h3>
92<p>Axis declarations each consist of the keyword <code>axis</code> followed by a Linux axis code
93number, and qualifiers that control the behavior of the axis including at least
94one Android axis code name.</p>
95<h4 id="basic-axes">Basic Axes</h4>
96<p>A basic axis simply maps a Linux axis code to an Android axis code name.</p>
97<p>The following declaration maps <code>ABS_X</code> (indicated by <code>0x00</code>) to <code>AXIS_X</code> (indicated by <code>X</code>).</p>
98<pre><code>axis 0x00 X
99</code></pre>
100<p>In the above example, if the value of <code>ABS_X</code> is <code>5</code> then <code>AXIS_X</code> will be set to <code>5</code>.</p>
101<h4 id="split-axes">Split Axes</h4>
102<p>A split axis maps a Linux axis code to two Android axis code names, such that
103values less than or greater than a threshold are split across two different axes when
104mapped. This mapping is useful when a single physical axis reported by the device
105encodes two different mutually exclusive logical axes.</p>
106<p>The following declaration maps values of the <code>ABS_Y</code> axis (indicated by <code>0x01</code>) to
107<code>AXIS_GAS</code> when less than <code>0x7f</code> or to <code>AXIS_BRAKE</code> when greater than <code>0x7f</code>.</p>
108<pre><code>axis 0x01 split 0x7f GAS BRAKE
109</code></pre>
110<p>In the above example, if the value of <code>ABS_Y</code> is <code>0x7d</code> then <code>AXIS_GAS</code> is set
111to <code>2</code> (<code>0x7f - 0x7d</code>) and <code>AXIS_BRAKE</code> is set to <code>0</code>. Conversely, if the value of
112<code>ABS_Y</code> is <code>0x83</code> then <code>AXIS_GAS</code> is set to <code>0</code> and <code>AXIS_BRAKE</code> is set to <code>4</code>
113(<code>0x83 - 0x7f</code>). Finally, if the value of <code>ABS_Y</code> equals the split value of <code>0x7f</code>
114then both <code>AXIS_GAS</code> and <code>AXIS_BRAKE</code> are set to <code>0</code>.</p>
115<h4 id="inverted-axes">Inverted Axes</h4>
116<p>An inverted axis inverts the sign of the axis value.</p>
117<p>The following declaration maps <code>ABS_RZ</code> (indicated by <code>0x05</code>) to <code>AXIS_BRAKE</code>
118(indicated by <code>BRAKE</code>), and inverts the output by negating it.</p>
Michael Wrightc0f3f332013-04-09 17:58:21 -0700119<pre><code>axis 0x05 invert BRAKE
Robert Ly35f2fda2013-01-29 16:27:05 -0800120</code></pre>
Michael Wrightc0f3f332013-04-09 17:58:21 -0700121<p>In the above example, if the value of <code>ABS_RZ</code> is <code>2</code> then
122<code>AXIS_BRAKE</code> is set to <code>-2</code>.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800123<h4 id="center-flat-position-option">Center Flat Position Option</h4>
124<p>The Linux input protocol provides a way for input device drivers to specify the
125center flat position of joystick axes but not all of them do and some of them
126provide incorrect values.</p>
127<p>The center flat position is the neutral position of the axis, such as when
128a directional pad is in the very middle of its range and the user is not
129touching it.</p>
130<p>To resolve this issue, an axis declaration may be followed by a <code>flat</code>
131option that specifies the value of the center flat position for the axis.</p>
132<pre><code>axis 0x03 Z flat 4096
133</code></pre>
134<p>In the above example, the center flat position is set to <code>4096</code>.</p>
135<h3 id="comments">Comments</h3>
136<p>Comment lines begin with '#' and continue to the end of the line. Like this:</p>
137<pre><code># A comment!
138</code></pre>
139<p>Blank lines are ignored.</p>
140<h3 id="examples">Examples</h3>
141<h4 id="keyboard">Keyboard</h4>
142<pre><code># This is an example of a key layout file for a keyboard.
143
144key 1 ESCAPE
145key 2 1
146key 3 2
147key 4 3
148key 5 4
149key 6 5
150key 7 6
151key 8 7
152key 9 8
153key 10 9
154key 11 0
155key 12 MINUS
156key 13 EQUALS
157key 14 DEL
158
159# etc...
160</code></pre>
161<h4 id="system-controls">System Controls</h4>
162<pre><code># This is an example of a key layout file for basic system controls, such as
163# volume and power keys which are typically implemented as GPIO pins that
164# the device decodes into key presses.
165
166key 114 VOLUME_DOWN WAKE
167key 115 VOLUME_UP WAKE
168key 116 POWER WAKE
169</code></pre>
170<h4 id="capacitive-buttons">Capacitive Buttons</h4>
171<pre><code># This is an example of a key layout file for a touch device with capacitive buttons.
172
173key 139 MENU VIRTUAL
174key 102 HOME VIRTUAL
175key 158 BACK VIRTUAL
176key 217 SEARCH VIRTUAL
177</code></pre>
178<h4 id="headset-jack-media-controls">Headset Jack Media Controls</h4>
179<pre><code># This is an example of a key layout file for headset mounted media controls.
180# A typical headset jack interface might have special control wires or detect known
181# resistive loads as corresponding to media functions or volume controls.
182# This file assumes that the driver decodes these signals and reports media
183# controls as key presses.
184
185key 163 MEDIA_NEXT WAKE
186key 165 MEDIA_PREVIOUS WAKE
187key 226 HEADSETHOOK WAKE
188</code></pre>
189<h4 id="joystick">Joystick</h4>
190<pre><code># This is an example of a key layout file for a joystick.
191
192# These are the buttons that the joystick supports, represented as keys.
193key 304 BUTTON_A
194key 305 BUTTON_B
195key 307 BUTTON_X
196key 308 BUTTON_Y
197key 310 BUTTON_L1
198key 311 BUTTON_R1
199key 314 BUTTON_SELECT
200key 315 BUTTON_START
201key 316 BUTTON_MODE
202key 317 BUTTON_THUMBL
203key 318 BUTTON_THUMBR
204
205# Left and right stick.
206# The reported value for flat is 128 out of a range from -32767 to 32768, which is absurd.
207# This confuses applications that rely on the flat value because the joystick actually
208# settles in a flat range of +/- 4096 or so. We override it here.
209axis 0x00 X flat 4096
210axis 0x01 Y flat 4096
211axis 0x03 Z flat 4096
212axis 0x04 RZ flat 4096
213
214# Triggers.
215axis 0x02 LTRIGGER
216axis 0x05 RTRIGGER
217
218# Hat.
219axis 0x10 HAT_X
220axis 0x11 HAT_Y
221</code></pre>
222<h2 id="wake-keys">Wake Keys</h2>
223<p>Wake keys are special keys that wake the device from sleep, such as the power key.</p>
224<p>By default, for internal keyboard devices, no key is a wake key. For external
225keyboard device, all keys are wake keys.</p>
226<p>To make a key be a wake key, set the <code>WAKE_DROPPED</code> flag in the key layout file
227for the keyboard device.</p>
228<p>Note that the <code>WindowManagerPolicy</code> component is responsible for implementing wake
229key behavior. Moreover, the key guard may prevent certain keys from functioning
230as wake keys. A good place to start understanding wake key behavior is
231<code>PhoneWindowManager.interceptKeyBeforeQueueing</code>.</p>
232<h2 id="virtual-soft-keys">Virtual Soft Keys</h2>
233<p>The input system provides special features for implementing virtual soft keys.</p>
234<p>There are three cases:</p>
235<ol>
236<li>
237<p>If the virtual soft keys are displayed graphically on the screen, as on the
238 Galaxy Nexus, then they are implemented by the Navigation Bar component in
239 the System UI package.</p>
240<p>Because graphical virtual soft keys are implemented at a high layer in the
241system, key layout files are not involved and the following information does
242not apply.</p>
243</li>
244<li>
245<p>If the virtual soft keys are implemented as an extended touchable region
246 that is part of the main touch screen, as on the Nexus One, then the
247 input system uses a virtual key map file to translate X / Y touch coordinates
248 into Linux key codes, then uses the key layout file to translate
249 Linux key codes into Android key codes.</p>
Clay Murphy72bdea02013-06-18 16:44:01 -0700250<p>Refer to the section on <a href="touch-devices.html">Touch Devices</a>
Robert Ly35f2fda2013-01-29 16:27:05 -0800251for more details about virtual key map files.</p>
252<p>The key layout file for the touch screen input device must specify the
253appropriate key mapping and include the <code>VIRTUAL</code> flag for each key.</p>
254</li>
255<li>
256<p>If the virtual soft keys are implemented as capacitive buttons that are
257 separate from the main touch screen, as on the Nexus S, then the kernel
258 device driver or firmware is responsible for translating touches into
259 Linux key codes which the input system then translates into Android
260 key codes using the key layout file.</p>
261<p>The key layout file for the capacitive button input device must specify the
262appropriate key mapping and include the <code>VIRTUAL</code> flag for each key.</p>
263</li>
264</ol>
265<p>When virtual soft key are located within or in close physical proximity of the
266touch screen, it is easy for the user to accidentally press one of the buttons
267when touching near the bottom of the screen or when sliding a finger from top
268to bottom or from bottom to top on the screen.</p>
269<p>To prevent this from happening, the input system applies a little debouncing
270such that virtual soft key presses are ignored for a brief period of time
271after the most recent touch on the touch screen. The delay is called the
272virtual key quiet time.</p>
273<p>To enable virtual soft key debouncing, we must do two things.</p>
274<p>First, we provide a key layout file for the touch screen or capacitive button
275input device with the <code>VIRTUAL</code> flag set for each key.</p>
276<pre><code>key 139 MENU VIRTUAL
277key 102 HOME VIRTUAL
278key 158 BACK VIRTUAL
279key 217 SEARCH VIRTUAL
280</code></pre>
281<p>Then, we set the value of the virtual key quiet time in a resource overlay
282for the framework <code>config.xml</code> resource.</p>
283<pre><code>&lt;!-- Specifies the amount of time to disable virtual keys after the screen is touched
284 in order to filter out accidental virtual key presses due to swiping gestures
285 or taps near the edge of the display. May be 0 to disable the feature.
286 It is recommended that this value be no more than 250 ms.
287 This feature should be disabled for most devices. --&gt;
288&lt;integer name="config_virtualKeyQuietTimeMillis"&gt;250&lt;/integer&gt;
289</code></pre>
290<h2 id="validation">Validation</h2>
291<p>Make sure to validate your key layout files using the
Clay Murphy72bdea02013-06-18 16:44:01 -0700292<a href="validate-keymaps.html">Validate Keymaps</a> tool.</p>