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