Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 1 | page.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 |
| 20 | and axis codes to Android key codes and axis codes and specifying associated |
| 21 | policy flags.</p> |
| 22 | <p>Device-specific key layout files are <em>required</em> for all internal (built-in) |
| 23 | input devices that have keys, including special keys such as volume, power |
| 24 | and headset media keys.</p> |
| 25 | <p>Device-specific key layout files are <em>optional</em> for other input devices but |
| 26 | they 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 |
| 28 | choose a default instead.</p> |
| 29 | <h2 id="location">Location</h2> |
| 30 | <p>Key layout files are located by USB vendor, product (and optionally version) |
| 31 | id 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 |
| 44 | in 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>. |
| 47 | This key layout is intended to support a variety of standard external |
| 48 | keyboards 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 |
| 52 | and 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 |
| 55 | number, an Android key code name, and optional set of whitespace delimited policy flags.</p> |
| 56 | <pre><code>key 1 ESCAPE |
| 57 | key 114 VOLUME_DOWN WAKE |
| 58 | key 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 |
| 79 | number, and qualifiers that control the behavior of the axis including at least |
| 80 | one 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 |
| 89 | values less than or greater than a threshold are split across two different axes when |
| 90 | mapped. This mapping is useful when a single physical axis reported by the device |
| 91 | encodes 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 |
| 97 | to <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> |
| 100 | then 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> |
Michael Wright | c0f3f33 | 2013-04-09 17:58:21 -0700 | [diff] [blame^] | 105 | <pre><code>axis 0x05 invert BRAKE |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 106 | </code></pre> |
Michael Wright | c0f3f33 | 2013-04-09 17:58:21 -0700 | [diff] [blame^] | 107 | <p>In the above example, if the value of <code>ABS_RZ</code> is <code>2</code> then |
| 108 | <code>AXIS_BRAKE</code> is set to <code>-2</code>.</p> |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 109 | <h4 id="center-flat-position-option">Center Flat Position Option</h4> |
| 110 | <p>The Linux input protocol provides a way for input device drivers to specify the |
| 111 | center flat position of joystick axes but not all of them do and some of them |
| 112 | provide incorrect values.</p> |
| 113 | <p>The center flat position is the neutral position of the axis, such as when |
| 114 | a directional pad is in the very middle of its range and the user is not |
| 115 | touching it.</p> |
| 116 | <p>To resolve this issue, an axis declaration may be followed by a <code>flat</code> |
| 117 | option that specifies the value of the center flat position for the axis.</p> |
| 118 | <pre><code>axis 0x03 Z flat 4096 |
| 119 | </code></pre> |
| 120 | <p>In the above example, the center flat position is set to <code>4096</code>.</p> |
| 121 | <h3 id="comments">Comments</h3> |
| 122 | <p>Comment lines begin with '#' and continue to the end of the line. Like this:</p> |
| 123 | <pre><code># A comment! |
| 124 | </code></pre> |
| 125 | <p>Blank lines are ignored.</p> |
| 126 | <h3 id="examples">Examples</h3> |
| 127 | <h4 id="keyboard">Keyboard</h4> |
| 128 | <pre><code># This is an example of a key layout file for a keyboard. |
| 129 | |
| 130 | key 1 ESCAPE |
| 131 | key 2 1 |
| 132 | key 3 2 |
| 133 | key 4 3 |
| 134 | key 5 4 |
| 135 | key 6 5 |
| 136 | key 7 6 |
| 137 | key 8 7 |
| 138 | key 9 8 |
| 139 | key 10 9 |
| 140 | key 11 0 |
| 141 | key 12 MINUS |
| 142 | key 13 EQUALS |
| 143 | key 14 DEL |
| 144 | |
| 145 | # etc... |
| 146 | </code></pre> |
| 147 | <h4 id="system-controls">System Controls</h4> |
| 148 | <pre><code># This is an example of a key layout file for basic system controls, such as |
| 149 | # volume and power keys which are typically implemented as GPIO pins that |
| 150 | # the device decodes into key presses. |
| 151 | |
| 152 | key 114 VOLUME_DOWN WAKE |
| 153 | key 115 VOLUME_UP WAKE |
| 154 | key 116 POWER WAKE |
| 155 | </code></pre> |
| 156 | <h4 id="capacitive-buttons">Capacitive Buttons</h4> |
| 157 | <pre><code># This is an example of a key layout file for a touch device with capacitive buttons. |
| 158 | |
| 159 | key 139 MENU VIRTUAL |
| 160 | key 102 HOME VIRTUAL |
| 161 | key 158 BACK VIRTUAL |
| 162 | key 217 SEARCH VIRTUAL |
| 163 | </code></pre> |
| 164 | <h4 id="headset-jack-media-controls">Headset Jack Media Controls</h4> |
| 165 | <pre><code># This is an example of a key layout file for headset mounted media controls. |
| 166 | # A typical headset jack interface might have special control wires or detect known |
| 167 | # resistive loads as corresponding to media functions or volume controls. |
| 168 | # This file assumes that the driver decodes these signals and reports media |
| 169 | # controls as key presses. |
| 170 | |
| 171 | key 163 MEDIA_NEXT WAKE |
| 172 | key 165 MEDIA_PREVIOUS WAKE |
| 173 | key 226 HEADSETHOOK WAKE |
| 174 | </code></pre> |
| 175 | <h4 id="joystick">Joystick</h4> |
| 176 | <pre><code># This is an example of a key layout file for a joystick. |
| 177 | |
| 178 | # These are the buttons that the joystick supports, represented as keys. |
| 179 | key 304 BUTTON_A |
| 180 | key 305 BUTTON_B |
| 181 | key 307 BUTTON_X |
| 182 | key 308 BUTTON_Y |
| 183 | key 310 BUTTON_L1 |
| 184 | key 311 BUTTON_R1 |
| 185 | key 314 BUTTON_SELECT |
| 186 | key 315 BUTTON_START |
| 187 | key 316 BUTTON_MODE |
| 188 | key 317 BUTTON_THUMBL |
| 189 | key 318 BUTTON_THUMBR |
| 190 | |
| 191 | # Left and right stick. |
| 192 | # The reported value for flat is 128 out of a range from -32767 to 32768, which is absurd. |
| 193 | # This confuses applications that rely on the flat value because the joystick actually |
| 194 | # settles in a flat range of +/- 4096 or so. We override it here. |
| 195 | axis 0x00 X flat 4096 |
| 196 | axis 0x01 Y flat 4096 |
| 197 | axis 0x03 Z flat 4096 |
| 198 | axis 0x04 RZ flat 4096 |
| 199 | |
| 200 | # Triggers. |
| 201 | axis 0x02 LTRIGGER |
| 202 | axis 0x05 RTRIGGER |
| 203 | |
| 204 | # Hat. |
| 205 | axis 0x10 HAT_X |
| 206 | axis 0x11 HAT_Y |
| 207 | </code></pre> |
| 208 | <h2 id="wake-keys">Wake Keys</h2> |
| 209 | <p>Wake keys are special keys that wake the device from sleep, such as the power key.</p> |
| 210 | <p>By default, for internal keyboard devices, no key is a wake key. For external |
| 211 | keyboard device, all keys are wake keys.</p> |
| 212 | <p>To make a key be a wake key, set the <code>WAKE_DROPPED</code> flag in the key layout file |
| 213 | for the keyboard device.</p> |
| 214 | <p>Note that the <code>WindowManagerPolicy</code> component is responsible for implementing wake |
| 215 | key behavior. Moreover, the key guard may prevent certain keys from functioning |
| 216 | as wake keys. A good place to start understanding wake key behavior is |
| 217 | <code>PhoneWindowManager.interceptKeyBeforeQueueing</code>.</p> |
| 218 | <h2 id="virtual-soft-keys">Virtual Soft Keys</h2> |
| 219 | <p>The input system provides special features for implementing virtual soft keys.</p> |
| 220 | <p>There are three cases:</p> |
| 221 | <ol> |
| 222 | <li> |
| 223 | <p>If the virtual soft keys are displayed graphically on the screen, as on the |
| 224 | Galaxy Nexus, then they are implemented by the Navigation Bar component in |
| 225 | the System UI package.</p> |
| 226 | <p>Because graphical virtual soft keys are implemented at a high layer in the |
| 227 | system, key layout files are not involved and the following information does |
| 228 | not apply.</p> |
| 229 | </li> |
| 230 | <li> |
| 231 | <p>If the virtual soft keys are implemented as an extended touchable region |
| 232 | that is part of the main touch screen, as on the Nexus One, then the |
| 233 | input system uses a virtual key map file to translate X / Y touch coordinates |
| 234 | into Linux key codes, then uses the key layout file to translate |
| 235 | Linux key codes into Android key codes.</p> |
| 236 | <p>Refer to the section on <a href="/tech/input/touch-devices.html">Touch Devices</a> |
| 237 | for more details about virtual key map files.</p> |
| 238 | <p>The key layout file for the touch screen input device must specify the |
| 239 | appropriate key mapping and include the <code>VIRTUAL</code> flag for each key.</p> |
| 240 | </li> |
| 241 | <li> |
| 242 | <p>If the virtual soft keys are implemented as capacitive buttons that are |
| 243 | separate from the main touch screen, as on the Nexus S, then the kernel |
| 244 | device driver or firmware is responsible for translating touches into |
| 245 | Linux key codes which the input system then translates into Android |
| 246 | key codes using the key layout file.</p> |
| 247 | <p>The key layout file for the capacitive button input device must specify the |
| 248 | appropriate key mapping and include the <code>VIRTUAL</code> flag for each key.</p> |
| 249 | </li> |
| 250 | </ol> |
| 251 | <p>When virtual soft key are located within or in close physical proximity of the |
| 252 | touch screen, it is easy for the user to accidentally press one of the buttons |
| 253 | when touching near the bottom of the screen or when sliding a finger from top |
| 254 | to bottom or from bottom to top on the screen.</p> |
| 255 | <p>To prevent this from happening, the input system applies a little debouncing |
| 256 | such that virtual soft key presses are ignored for a brief period of time |
| 257 | after the most recent touch on the touch screen. The delay is called the |
| 258 | virtual key quiet time.</p> |
| 259 | <p>To enable virtual soft key debouncing, we must do two things.</p> |
| 260 | <p>First, we provide a key layout file for the touch screen or capacitive button |
| 261 | input device with the <code>VIRTUAL</code> flag set for each key.</p> |
| 262 | <pre><code>key 139 MENU VIRTUAL |
| 263 | key 102 HOME VIRTUAL |
| 264 | key 158 BACK VIRTUAL |
| 265 | key 217 SEARCH VIRTUAL |
| 266 | </code></pre> |
| 267 | <p>Then, we set the value of the virtual key quiet time in a resource overlay |
| 268 | for the framework <code>config.xml</code> resource.</p> |
| 269 | <pre><code><!-- Specifies the amount of time to disable virtual keys after the screen is touched |
| 270 | in order to filter out accidental virtual key presses due to swiping gestures |
| 271 | or taps near the edge of the display. May be 0 to disable the feature. |
| 272 | It is recommended that this value be no more than 250 ms. |
| 273 | This feature should be disabled for most devices. --> |
| 274 | <integer name="config_virtualKeyQuietTimeMillis">250</integer> |
| 275 | </code></pre> |
| 276 | <h2 id="validation">Validation</h2> |
| 277 | <p>Make sure to validate your key layout files using the |
| 278 | <a href="/tech/input/validate-keymaps.html">Validate Keymaps</a> tool.</p> |