1page.title=Key Character Map Files
2@jd:body
3
4<!--
5    Copyright 2013 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 character map files (<code>.kcm</code> files) are responsible for mapping combinations
20of Android key codes with modifiers to Unicode characters.</p>
21<p>Device-specific key layout files are <em>required</em> for all internal (built-in)
22input devices that have keys, if only to tell the system that the device
23is special purpose only (not a full keyboard).</p>
24<p>Device-specific key layout files are <em>optional</em> for external keyboards, and
25often aren't needed at all.  The system provides a generic key character map
26that is suitable for many external keyboards.</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 character map 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/keychars/Vendor_XXXX_Product_XXXX_Version_XXXX.kcm</code></li>
35<li><code>/system/usr/keychars/Vendor_XXXX_Product_XXXX.kcm</code></li>
36<li><code>/system/usr/keychars/DEVICE_NAME.kcm</code></li>
37<li><code>/data/system/devices/keychars/Vendor_XXXX_Product_XXXX_Version_XXXX.kcm</code></li>
38<li><code>/data/system/devices/keychars/Vendor_XXXX_Product_XXXX.kcm</code></li>
39<li><code>/data/system/devices/keychars/DEVICE_NAME.kcm</code></li>
40<li><code>/system/usr/keychars/Generic.kcm</code></li>
41<li><code>/data/system/devices/keychars/Generic.kcm</code></li>
42<li><code>/system/usr/keychars/Virtual.kcm</code></li>
43<li><code>/data/system/devices/keychars/Virtual.kcm</code></li>
44</ul>
45<p>When constructing a file path that contains the device name, all characters
46in the device name other than '0'-'9', 'a'-'z', 'A'-'Z', '-' or '<em>' are replaced by '</em>'.</p>
47<h2 id="generic-key-character-map-file">Generic Key Character Map File</h2>
48<p>The system provides a special built-in key character map file called <code>Generic.kcm</code>.
49This key character map is intended to support a variety of standard external
50keyboards.</p>
51<p><em>Do not modify the generic key character map!</em></p>
52<h2 id="virtual-key-character-map-file">Virtual Key Character Map File</h2>
53<p>The system provides a special built-in key character map file called <code>Virtual.kcm</code>
54that is used by the virtual keyboard devices.</p>
55<p>The virtual keyboard device is a synthetic input device whose id is -1
56(see <code>KeyCharacterMap.VIRTUAL_KEYBOARD</code>).  It is present on all Android devices
57beginning with Android Honeycomb 3.0.  The purpose of the virtual keyboard device
58is to provide a known built-in input device that can be used for injecting
59keystokes into applications by the IME or by test instrumentation, even
60for devices that do not have built-in keyboards.</p>
61<p>The virtual keyboard is assumed to have a full QWERTY layout that is the
62same on all devices.  This makes it possible for applications to inject
63keystrokes using the virtual keyboard device and always get the same results.</p>
64<p><em>Do not modify the virtual key character map!</em></p>
65<h2 id="syntax">Syntax</h2>
66<p>A key character map file is a plain text file consisting of a keyboard type
67declaration and a set of key declarations.</p>
68<h3 id="keyboard-type-declaration">Keyboard Type Declaration</h3>
69<p>A keyboard type declaration describes the overall behavior of the keyboard.
70A character map file must contain a keyboard type declaration.  For clarity,
71it is often placed at the top of the file.</p>
72<pre><code>type FULL
73</code></pre>
74<p>The following keyboard types are recognized:</p>
75<ul>
76<li>
77<p><code>NUMERIC</code>: A numeric (12-key) keyboard.</p>
78<p>A numeric keyboard supports text entry using a multi-tap approach.
79It may be necessary to tap a key multiple times to generate the desired letter or symbol.</p>
80<p>This type of keyboard is generally designed for thumb typing.</p>
81<p>Corresponds to <code>KeyCharacterMap.NUMERIC</code>.</p>
82</li>
83<li>
84<p><code>PREDICTIVE</code>: A keyboard with all the letters, but with more than one letter per key.</p>
85<p>This type of keyboard is generally designed for thumb typing.</p>
86<p>Corresponds to <code>KeyCharacterMap.PREDICTIVE</code>.</p>
87</li>
88<li>
89<p><code>ALPHA</code>: A keyboard with all the letters, and maybe some numbers.</p>
90<p>An alphabetic keyboard supports text entry directly but may have a condensed
91layout with a small form factor.  In contrast to a <code>FULL</code> keyboard, some
92symbols may only be accessible using special on-screen character pickers.
93In addition, to improve typing speed and accuracy, the framework provides
94special affordances for alphabetic keyboards such as auto-capitalization
95and toggled / locked SHIFT and ALT keys.</p>
96<p>This type of keyboard is generally designed for thumb typing.</p>
97</li>
98<li>
99<p><code>FULL</code>: A full PC-style keyboard.</p>
100<p>A full keyboard behaves like a PC keyboard.  All symbols are accessed directly
101by pressing keys on the keyboard without on-screen support or affordances such
102as auto-capitalization.</p>
103<p>This type of keyboard is generally designed for full two hand typing.</p>
104</li>
105<li>
106<p><code>SPECIAL_FUNCTION</code>: A keyboard that is only used to perform system control functions
107    rather than for typing.</p>
108<p>A special function keyboard consists only of non-printing keys such as
109HOME and POWER that are not actually used for typing.</p>
110</li>
111</ul>
112<p>The <code>Generic.kcm</code> and <code>Virtual.kcm</code> key character maps are both <code>FULL</code> keyboards.</p>
113<h3 id="key-declarations">Key Declarations</h3>
114<p>Key declarations each consist of the keyword <code>key</code> followed by an Android key code
115name, an open curly brace, a set of properties and behaviors and a close curly brace.</p>
116<pre><code>key A {
117    label:                              'A'
118    base:                               'a'
119    shift, capslock:                    'A'
120    ctrl, alt, meta:                    none
121}
122</code></pre>
123<h4 id="properties">Properties</h4>
124<p>Each key property establishes a mapping from a key to a behavior.  To make the
125key character map files more compact, several properties can be mapped to the
126same behavior by separating them with a comma.</p>
127<p>In the above example, the <code>label</code> property is assigned the <code>'A'</code> behavior.
128Likewise, the <code>ctrl</code>, <code>alt</code> and <code>meta</code> properties are all simultaneously assigned
129the <code>none</code> behavior.</p>
130<p>The following properties are recognized:</p>
131<ul>
132<li>
133<p><code>label</code>: Specifies the label that is physically printed on the key, when it
134    consists of a single character.  This is the value that is returned by
135    the <code>KeyCharacterMap.getDisplayLabel</code> method.</p>
136</li>
137<li>
138<p><code>number</code>: Specifies the behavior (character that should be typed) when a numeric
139    text view has focus, such as when the user is typing a phone number.</p>
140<p>Compact keyboards often combine multiple symbols into a single key, such that
141the same key might be used to type <code>'1'</code> and <code>'a'</code> or <code>'#'</code> and <code>'q'</code>, perhaps.
142For these keys, the <code>number</code> property should be set to indicate which symbol
143should be typed in a numeric context, if any.</p>
144<p>Some typical "numeric" symbols are digits <code>'0'</code> through <code>'9'</code>, <code>'#'</code>, <code>'+'</code>,
145<code>'('</code>, <code>')'</code>, <code>','</code>, and <code>'.'</code>.</p>
146</li>
147<li>
148<p><code>base</code>: Specifies the behavior (character that should be typed) when no modifiers
149    are pressed.</p>
150</li>
151<li>
152<p>&lt;modifier&gt; or &lt;modifier1&gt;<code>+</code>&lt;modifier2&gt;<code>+</code>...: Specifies the
153    behavior (character that should be typed) when the key is pressed and all of the
154    specified modifiers are active.</p>
155<p>For example, the modifier property <code>shift</code> specifies a behavior that applies when
156the either the LEFT SHIFT or RIGHT SHIFT modifier is pressed.</p>
157<p>Similarly, the modifier property <code>rshift+ralt</code> specifies a behavior that applies
158when the both RIGHT SHIFT and RIGHT ALT modifiers are pressed together.</p>
159</li>
160</ul>
161<p>The following modifiers are recognized in modifier properties:</p>
162<ul>
163<li><code>shift</code>: Applies when either the LEFT SHIFT or RIGHT SHIFT modifier is pressed.</li>
164<li><code>lshift</code>: Applies when the LEFT SHIFT modifier is pressed.</li>
165<li><code>rshift</code>: Applies when the RIGHT SHIFT modifier is pressed.</li>
166<li><code>alt</code>: Applies when either the LEFT ALT or RIGHT ALT modifier is pressed.</li>
167<li><code>lalt</code>: Applies when the LEFT ALT modifier is pressed.</li>
168<li><code>ralt</code>: Applies when the RIGHT ALT modifier is pressed.</li>
169<li><code>ctrl</code>: Applies when either the LEFT CONTROL or RIGHT CONTROL modifier is pressed.</li>
170<li><code>lctrl</code>: Applies when the LEFT CONTROL modifier is pressed.</li>
171<li><code>rctrl</code>: Applies when the RIGHT CONTROL modifier is pressed.</li>
172<li><code>meta</code>: Applies when either the LEFT META or RIGHT META modifier is pressed.</li>
173<li><code>lmeta</code>: Applies when the LEFT META modifier is pressed.</li>
174<li><code>rmeta</code>: Applies when the RIGHT META modifier is pressed.</li>
175<li><code>sym</code>: Applies when the SYMBOL modifier is pressed.</li>
176<li><code>fn</code>: Applies when the FUNCTION modifier is pressed.</li>
177<li><code>capslock</code>: Applies when the CAPS LOCK modifier is locked.</li>
178<li><code>numlock</code>: Applies when the NUM LOCK modifier is locked.</li>
179<li><code>scrolllock</code>: Applies when the SCROLL LOCK modifier is locked.</li>
180</ul>
181<p>The order in which the properties are listed is significant.  When mapping a key to
182a behavior, the system scans all relevant properties in order and returns the last
183applicable behavior that it found.</p>
184<p>Consequently, properties that are specified later override properties that are
185specified earlier for a given key.</p>
186<h4 id="behaviors">Behaviors</h4>
187<p>Each property maps to a behavior.  The most common behavior is typing a character
188but there are others.</p>
189<p>The following behaviors are recognized:</p>
190<ul>
191<li>
192<p><code>none</code>: Don't type a character.</p>
193<p>This behavior is the default when no character is specified.  Specifying <code>none</code>
194is optional but it improves clarity.</p>
195</li>
196<li>
197<p><code>'X'</code>: Type the specified character literal.</p>
198<p>This behavior causes the specified character to be entered into the focused
199text view.  The character literal may be any ASCII character, or one of the
200following escape sequences:</p>
201<ul>
202<li><code>'\\'</code>: Type a backslash character.</li>
203<li><code>'\n'</code>: Type a new line character (use this for ENTER / RETURN).</li>
204<li><code>'\t'</code>: Type a TAB character.</li>
205<li><code>'\''</code>: Type an apostrophe character.</li>
206<li><code>'\"'</code>: Type a quote character.</li>
207<li><code>'\uXXXX'</code>: Type the Unicode character whose code point is given in hex by XXXX.</li>
208</ul>
209</li>
210<li>
211<p><code>fallback</code> &lt;Android key code name&gt;: Perform a default action if the key is not
212    handled by the application.</p>
213<p>This behavior causes the system to simulate a different key press when an application
214does not handle the specified key natively.  It is used to support default behavior
215for new keys that not all applications know how to handle, such as ESCAPE or
216numeric keypad keys (when numlock is not pressed).</p>
217<p>When a fallback behavior is performed, the application will receive two key presses:
218one for the original key and another for the fallback key that was selected.
219If the application handles the original key during key up, then the fallback key
220event will be canceled (<code>KeyEvent.isCanceled</code> will return <code>true</code>).</p>
221</li>
222</ul>
223<p>The system reserves two Unicode characters to perform special functions:</p>
224<ul>
225<li>
226<p><code>'\uef00'</code>: When this behavior is performed, the text view consumes and removes the
227    four characters preceding the cursor, interprets them as hex digits, and inserts the
228    corresponding Unicode code point.</p>
229</li>
230<li>
231<p><code>'\uef01'</code>: When this behavior is performed, the text view displays a
232    character picker dialog that contains miscellaneous symbols.</p>
233</li>
234</ul>
235<p>The system recognizes the following Unicode characters as combining diacritical dead
236key characters:</p>
237<ul>
238<li><code>'\u0300'</code>: Grave accent.</li>
239<li><code>'\u0301'</code>: Acute accent.</li>
240<li><code>'\u0302'</code>: Circumflex accent.</li>
241<li><code>'\u0303'</code>: Tilde accent.</li>
242<li><code>'\u0308'</code>: Umlaut accent.</li>
243</ul>
244<p>When a dead key is typed followed by another character, the dead key and the following
245characters are composed.  For example, when the user types a grave accent dead
246key followed by the letter 'a', the result is '&agrave;'.</p>
247<p>Refer to <code>KeyCharacterMap.getDeadChar</code> for more information about dead key handling.</p>
248<h3 id="comments">Comments</h3>
249<p>Comment lines begin with '#' and continue to the end of the line.  Like this:</p>
250<pre><code># A comment!
251</code></pre>
252<p>Blank lines are ignored.</p>
253<h3 id="how-key-combinations-are-mapped-to-behaviors">How Key Combinations are Mapped to Behaviors</h3>
254<p>When the user presses a key, the system looks up the behavior associated with
255the combination of that key press and the currently pressed modifiers.</p>
256<h4 id="shift-a">SHIFT + A</h4>
257<p>Suppose the user pressed A and SHIFT together.  The system first locates
258the set of properties and behaviors associated with <code>KEYCODE_A</code>.</p>
259<pre><code>key A {
260    label:                              'A'
261    base:                               'a'
262    shift, capslock:                    'A'
263    ctrl, alt, meta:                    none
264}
265</code></pre>
266<p>The system scans the properties from first to last and left to right, ignoring
267the <code>label</code> and <code>number</code> properties, which are special.</p>
268<p>The first property encountered is <code>base</code>.  The <code>base</code> property always applies to
269a key, no matter what modifiers are pressed.  It essentially specifies the default
270behavior for the key unless it is overridden by following properties.
271Since the <code>base</code> property applies to this key press, the system makes note
272of the fact that its behavior is <code>'a'</code> (type the character <code>a</code>).</p>
273<p>The system then continues to scan subsequent properties in case any of them
274are more specific than <code>base</code> and override it.  It encounters <code>shift</code> which
275also applies to the key press SHIFT + A.  So the system decides to ignore
276the <code>base</code> property's behavior and chooses the behavior associated with
277the <code>shift</code> property, which is <code>'A'</code> (type the character <code>A</code>).</p>
278<p>It then continues to scan the table, however no other properties apply to this
279key press (CAPS LOCK is not locked, neither CONTROL key is pressed, neither
280ALT key is pressed and neither META key is pressed).</p>
281<p>So the resulting behavior for the key combination SHIFT + A is <code>'A'</code>.</p>
282<h4 id="control-a">CONTROL + A</h4>
283<p>Now consider what would happen if the user pressed A and CONTROL together.</p>
284<p>As before, the system would scan the table of properties.  It would notice
285that the <code>base</code> property applied but would also continue scanning until
286it eventually reached the <code>control</code> property.  As it happens, the <code>control</code>
287property appears after <code>base</code> so its behavior overrides the <code>base</code> behavior.</p>
288<p>So the resulting behavior for the key combination CONTROL + A is <code>none</code>.</p>
289<h4 id="escape">ESCAPE</h4>
290<p>Now suppose the user pressed ESCAPE.</p>
291<pre><code>key ESCAPE {
292    base:                               fallback BACK
293    alt, meta:                          fallback HOME
294    ctrl:                               fallback MENU
295}
296</code></pre>
297<p>This time the system obtains the behavior <code>fallback BACK</code>, a fallback behavior.
298Because no character literal appears, no character will be typed.</p>
299<p>When processing the key, the system will first deliver <code>KEYCODE_ESCAPE</code> to the
300application.  If the application does not handle it, then the system will try
301again but this time it will deliver <code>KEYCODE_BACK</code> to the application as
302requested by the fallback behavior.</p>
303<p>So applications that recognize and support <code>KEYCODE_ESCAPE</code> have the
304opportunity to handle it as is, but other applications that do not can instead
305perform the fallback action of treating the key as if it were <code>KEYCODE_BACK</code>.</p>
306<h4 id="numpad_0-with-or-without-num-lock">NUMPAD_0 with or without NUM LOCK</h4>
307<p>The numeric keypad keys have very different interpretations depending on whether
308the NUM LOCK key is locked.</p>
309<p>The following key declaration ensures that <code>KEYCODE_NUMPAD_0</code> types <code>0</code>
310when NUM LOCK is pressed.  When NUM LOCK is not pressed, the key is delivered
311to the application as usual, and if it is not handled, then the fallback
312key <code>KEYCODE_INSERT</code> is delivered instead.</p>
313<pre><code>key NUMPAD_0 {
314    label, number:                      '0'
315    base:                               fallback INSERT
316    numlock:                            '0'
317    ctrl, alt, meta:                    none
318}
319</code></pre>
320<p>As we can see, fallback key declarations greatly improve compatibility
321with older applications that do not recognize or directly support all of the keys
322that are present on a full PC style keyboard.</p>
323<h3 id="examples">Examples</h3>
324<h4 id="full-keyboard">Full Keyboard</h4>
325<pre><code># This is an example of part of a key character map file for a full keyboard
326# include a few fallback behaviors for special keys that few applications
327# handle themselves.
328
329type FULL
330
331key C {
332    label:                              'C'
333    base:                               'c'
334    shift, capslock:                    'C'
335    alt:                                '\u00e7'
336    shift+alt:                          '\u00c7'
337    ctrl, meta:                         none
338}
339
340key SPACE {
341    label:                              ' '
342    base:                               ' '
343    ctrl:                               none
344    alt, meta:                          fallback SEARCH
345}
346
347key NUMPAD_9 {
348    label, number:                      '9'
349    base:                               fallback PAGE_UP
350    numlock:                            '9'
351    ctrl, alt, meta:                    none
352}
353</code></pre>
354<h4 id="alphanumeric-keyboard">Alphanumeric Keyboard</h4>
355<pre><code># This is an example of part of a key character map file for an alphanumeric
356# thumb keyboard.  Some keys are combined, such as `A` and `2`.  Here we
357# specify `number` labels to tell the system what to do when the user is
358# typing a number into a dial pad.
359#
360# Also note the special character '\uef01' mapped to ALT+SPACE.
361# Pressing this combination of keys invokes an on-screen character picker.
362
363type ALPHA
364
365key A {
366    label:                              'A'
367    number:                             '2'
368    base:                               'a'
369    shift, capslock:                    'A'
370    alt:                                '#'
371    shift+alt, capslock+alt:            none
372}
373
374key SPACE {
375    label:                              ' '
376    number:                             ' '
377    base:                               ' '
378    shift:                              ' '
379    alt:                                '\uef01'
380    shift+alt:                          '\uef01'
381}
382</code></pre>
383<h4 id="game-pad">Game Pad</h4>
384<pre><code># This is an example of part of a key character map file for a game pad.
385# It defines fallback actions that enable the user to navigate the user interface
386# by pressing buttons.
387
388type SPECIAL_FUNCTION
389
390key BUTTON_A {
391    base:                               fallback BACK
392}
393
394key BUTTON_X {
395    base:                               fallback DPAD_CENTER
396}
397
398key BUTTON_START {
399    base:                               fallback HOME
400}
401
402key BUTTON_SELECT {
403    base:                               fallback MENU
404}
405</code></pre>
406<h2 id="compatibility-note">Compatibility Note</h2>
407<p>Prior to Android Honeycomb 3.0, the Android key character map was specified
408using a very different syntax and was compiled into a binary file format
409(<code>.kcm.bin</code>) at build time.</p>
410<p>Although the new format uses the same extension <code>.kcm</code>, the syntax is quite
411different (and much more powerful).</p>
412<p>As of Android Honeycomb 3.0, all Android key character map files must use
413the new syntax and plain text file format that is described in this document.
414The old syntax is not supported and the old <code>.kcm.bin</code> files are not recognized
415by the system.</p>
416<h2 id="language-note">Language Note</h2>
417<p>Android does not currently support multilingual keyboards.  Moreover, the
418built-in generic key character map assumes a US English keyboard layout.</p>
419<p>OEMs are encouraged to provide custom key character maps for their keyboards
420if they are designed for other languages.</p>
421<p>Future versions of Android may provide better support for multilingual keyboards
422or user-selectable keyboard layouts.</p>
423<h2 id="validation">Validation</h2>
424<p>Make sure to validate your key character map files using the
425<a href="validate-keymaps.html">Validate Keymaps</a> tool.</p>
426