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