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