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