1 page.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 28 to Android key codes and axis codes and specify associated policy flags. 29 Device-specific key layout files are:</p> 30 <ul> 31 <li><em>Required</em> for internal (built-in) input devices with keys, including 32 special keys such as volume, power, and headset media keys.</li> 33 <li><em>Optional</em> for other input devices but <em>recommended</em> for 34 special-purpose keyboards and joysticks.</li> 35 </ul> 36 <p>If no device-specific key layout file is available, the system chooses a 37 default instead.</p> 38 39 <h2 id="location">Location</h2> 40 <p>Key layout files are located by USB vendor, product (and optionally version) 41 id 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 53 in 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 60 standard external keyboards and joysticks. <strong>Do not modify the generic key 61 layout!</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 65 and 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 69 key code number and Android key code name, or the keyword usage followed by a 70 HID usage and Android key code name. The HID usage is represented as a 32-bit 71 integer, where the high 16-bits represent the HID usage page and the low 16-bits 72 represent the HID usage ID. Either declaration can be followed by an optional 73 set of whitespace-delimited policy flags.</p> 74 <pre><code>key 1 ESCAPE 75 key 114 VOLUME_DOWN 76 key 16 Q VIRTUAL 77 key 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 82 were also pressed.</li> 83 <li><code>GESTURE</code>: The key generated by a user gesture, such as palming 84 the touchscreen.</li> 85 <li><code>VIRTUAL</code>: The key is a virtual soft key (capacitive button) 86 adjacent to the main touch screen. This causes special debouncing logic to be 87 enabled (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 92 Linux axis code number and qualifiers that control the behavior of the axis 93 including 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 97 following declaration maps <code>ABS_X</code> (indicated by <code>0x00</code>) 98 to <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> 101 then <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 105 values less than or greater than a threshold are split across two different axes 106 when mapped. This mapping is useful when a single physical axis reported by the 107 device 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> 114 then <code>AXIS_GAS</code> is set to <code>2</code> (<code>0x7f - 0x7d</code>) 115 and <code>AXIS_BRAKE</code> is set to <code>0</code>. Conversely, if the value 116 of <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 119 the 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 124 declaration maps <code>ABS_RZ</code> (indicated by <code>0x05</code>) to 125 <code>AXIS_BRAKE</code> (indicated by <code>BRAKE</code>), and inverts the 126 output 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> 129 then <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 133 a directional pad is in the very middle of its range and the user is not 134 touching it.</p> 135 <p>The Linux input protocol provides a way for input device drivers to specify 136 the center flat position of joystick axes but not all of them do and some of 137 them provide incorrect values. To resolve this issue, an axis declaration may be 138 followed by a <code>flat</code> option that specifies the value of the center 139 flat 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 154 key 1 ESCAPE 155 key 2 1 156 key 3 2 157 key 4 3 158 key 5 4 159 key 6 5 160 key 7 6 161 key 8 7 162 key 9 8 163 key 10 9 164 key 11 0 165 key 12 MINUS 166 key 13 EQUALS 167 key 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 177 key 114 VOLUME_DOWN 178 key 115 VOLUME_UP 179 key 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 185 key 139 MENU VIRTUAL 186 key 102 HOME VIRTUAL 187 key 158 BACK VIRTUAL 188 key 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 198 key 163 MEDIA_NEXT 199 key 165 MEDIA_PREVIOUS 200 key 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. 207 key 304 BUTTON_A 208 key 305 BUTTON_B 209 key 307 BUTTON_X 210 key 308 BUTTON_Y 211 key 310 BUTTON_L1 212 key 311 BUTTON_R1 213 key 314 BUTTON_SELECT 214 key 315 BUTTON_START 215 key 316 BUTTON_MODE 216 key 317 BUTTON_THUMBL 217 key 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. 223 axis 0x00 X flat 4096 224 axis 0x01 Y flat 4096 225 axis 0x03 Z flat 4096 226 axis 0x04 RZ flat 4096 227 228 # Triggers. 229 axis 0x02 LTRIGGER 230 axis 0x05 RTRIGGER 231 232 # Hat. 233 axis 0x10 HAT_X 234 axis 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 239 in the following use cases:</p> 240 <ol> 241 <li>If the virtual soft keys are displayed graphically on the screen (such as on 242 the Galaxy Nexus), they are implemented by the Navigation Bar component in the 243 System UI package. Because graphical virtual soft keys are implemented at a high 244 layer in the system, key layout files are not involved and the following 245 information does not apply.</li> 246 <li>If the virtual soft keys are implemented as an extended touchable region 247 that is part of the main touch screen (such as on the Nexus One), the input 248 system uses a virtual key map file to translate X/Y touch coordinates into 249 Linux key codes, then uses the key layout file to translate Linux key codes into 250 Android 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 252 touch screen input device must specify the appropriate key mapping and include 253 the <code>VIRTUAL</code> flag for each key.</li> 254 <li>If the virtual soft keys are implemented as capacitive buttons separate from 255 the main touch screen (such as on the Nexus S), the kernel device driver or 256 firmware is responsible for translating touches into Linux key codes which the 257 input system then translates into Android key codes using the key layout file. 258 The key layout file for the capacitive button input device must specify the 259 appropriate 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 262 the touch screen, it is easy for users to accidentally press a button when 263 touching near the bottom of the screen or when sliding a finger top-to-bottom or 264 bottom-to-top on the screen. To prevent this, the input system applies a little 265 debouncing such that virtual soft key presses are ignored for a brief period of 266 time 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 271 input device with the <code>VIRTUAL</code> flag set for each key. 272 <pre><code>key 139 MENU VIRTUAL 273 key 102 HOME VIRTUAL 274 key 158 BACK VIRTUAL 275 key 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 279 framework <code>config.xml</code> resource. 280 <pre><code><!-- Specifies the amount of time to disable virtual keys after the screen 281 is touched to filter out accidental virtual key presses due to swiping gestures 282 or taps near the edge of the display. May be 0 to disable the feature. 283 It is recommended that this value be no more than 250 ms. 284 This 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>