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