public class

KeyCharacterMap

extends Object
java.lang.Object
   ↳ android.view.KeyCharacterMap

Class Overview

Describes the keys provided by a keyboard device and their associated labels.

Summary

Nested Classes
class KeyCharacterMap.KeyData This class is deprecated. instead use getDisplayLabel(int), getNumber(int) and get(int, int).  
class KeyCharacterMap.UnavailableException Thrown by load(int) when a key character map could not be loaded. 
Constants
int ALPHA A keyboard with all the letters, and maybe some numbers.
int BUILT_IN_KEYBOARD This constant is deprecated. This constant should no longer be used because there is no guarantee that a device has a built-in keyboard that can be used for typing text. There might not be a built-in keyboard, the built-in keyboard might be a NUMERIC or SPECIAL_FUNCTION keyboard, or there might be multiple keyboards installed including external keyboards. When interpreting key presses received from the framework, applications should use the device id specified in the KeyEvent received. When synthesizing key presses for delivery elsewhere or when translating key presses from unknown keyboards, applications should use the special VIRTUAL_KEYBOARD device id.
int COMBINING_ACCENT
int COMBINING_ACCENT_MASK Mask the return value from get(int, int) with this value to get a printable representation of the accent character of a "dead key."
int FULL A full PC-style keyboard.
char HEX_INPUT This private-use character is used to trigger Unicode character input by hex digits.
int MODIFIER_BEHAVIOR_CHORDED Modifier keys may be chorded with character keys.
int MODIFIER_BEHAVIOR_CHORDED_OR_TOGGLED Modifier keys may be chorded with character keys or they may toggle into latched or locked states when pressed independently.
int NUMERIC A numeric (12-key) keyboard.
char PICKER_DIALOG_INPUT This private-use character is used to bring up a character picker for miscellaneous symbols.
int PREDICTIVE A keyboard with all the letters, but with more than one letter per key.
int SPECIAL_FUNCTION A keyboard that is only used to control special functions rather than for typing.
int VIRTUAL_KEYBOARD The id of a generic virtual keyboard with a full layout that can be used to synthesize key events.
Public Methods
static boolean deviceHasKey(int keyCode)
Queries the framework about whether any physical keys exist on the any keyboard attached to the device that are capable of producing the given key code.
static boolean[] deviceHasKeys(int[] keyCodes)
Queries the framework about whether any physical keys exist on the any keyboard attached to the device that are capable of producing the given array of key codes.
int get(int keyCode, int metaState)
Gets the Unicode character generated by the specified key and meta key state combination.
static int getDeadChar(int accent, int c)
Get the character that is produced by putting accent on the character c.
char getDisplayLabel(int keyCode)
Gets the primary character for this key.
KeyEvent[] getEvents(char[] chars)
Get an array of KeyEvent objects that if put into the input stream could plausibly generate the provided sequence of characters.
boolean getKeyData(int keyCode, KeyCharacterMap.KeyData results)
This method is deprecated. instead use getDisplayLabel(int), getNumber(int) or get(int, int).
int getKeyboardType()
Gets the keyboard type.
char getMatch(int keyCode, char[] chars)
Gets the first character in the character array that can be generated by the specified key code.
char getMatch(int keyCode, char[] chars, int metaState)
Gets the first character in the character array that can be generated by the specified key code.
int getModifierBehavior()
Gets a constant that describes the behavior of this keyboard's modifier keys such as KEYCODE_SHIFT_LEFT.
char getNumber(int keyCode)
Gets the number or symbol associated with the key.
boolean isPrintingKey(int keyCode)
Returns true if the specified key produces a glyph.
static KeyCharacterMap load(int deviceId)
Loads the key character maps for the keyboard with the specified device id.
Protected Methods
void finalize()
Called before the object's memory is reclaimed by the VM.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final int ALPHA

Since: API Level 1

A keyboard with all the letters, and maybe some numbers.

An alphabetic keyboard supports text entry directly but may have a condensed layout with a small form factor. In contrast to a full keyboard, some symbols may only be accessible using special on-screen character pickers. In addition, to improve typing speed and accuracy, the framework provides special affordances for alphabetic keyboards such as auto-capitalization and toggled / locked shift and alt keys.

This type of keyboard is generally designed for thumb typing.

Constant Value: 3 (0x00000003)

public static final int BUILT_IN_KEYBOARD

Since: API Level 1

This constant is deprecated.
This constant should no longer be used because there is no guarantee that a device has a built-in keyboard that can be used for typing text. There might not be a built-in keyboard, the built-in keyboard might be a NUMERIC or SPECIAL_FUNCTION keyboard, or there might be multiple keyboards installed including external keyboards. When interpreting key presses received from the framework, applications should use the device id specified in the KeyEvent received. When synthesizing key presses for delivery elsewhere or when translating key presses from unknown keyboards, applications should use the special VIRTUAL_KEYBOARD device id.

The id of the device's primary built in keyboard is always 0.

Constant Value: 0 (0x00000000)

public static final int COMBINING_ACCENT

Since: API Level 1

Constant Value: -2147483648 (0x80000000)

public static final int COMBINING_ACCENT_MASK

Since: API Level 1

Mask the return value from get(int, int) with this value to get a printable representation of the accent character of a "dead key."

Constant Value: 2147483647 (0x7fffffff)

public static final int FULL

Since: API Level 11

A full PC-style keyboard.

A full keyboard behaves like a PC keyboard. All symbols are accessed directly by pressing keys on the keyboard without on-screen support or affordances such as auto-capitalization.

This type of keyboard is generally designed for full two hand typing.

Constant Value: 4 (0x00000004)

public static final char HEX_INPUT

Since: API Level 1

This private-use character is used to trigger Unicode character input by hex digits.

Constant Value: 61184 (0x0000ef00)

public static final int MODIFIER_BEHAVIOR_CHORDED

Since: API Level 11

Modifier keys may be chorded with character keys.

Constant Value: 0 (0x00000000)

public static final int MODIFIER_BEHAVIOR_CHORDED_OR_TOGGLED

Since: API Level 11

Modifier keys may be chorded with character keys or they may toggle into latched or locked states when pressed independently.

Constant Value: 1 (0x00000001)

public static final int NUMERIC

Since: API Level 1

A numeric (12-key) keyboard.

A numeric keyboard supports text entry using a multi-tap approach. It may be necessary to tap a key multiple times to generate the desired letter or symbol.

This type of keyboard is generally designed for thumb typing.

Constant Value: 1 (0x00000001)

public static final char PICKER_DIALOG_INPUT

Since: API Level 1

This private-use character is used to bring up a character picker for miscellaneous symbols.

Constant Value: 61185 (0x0000ef01)

public static final int PREDICTIVE

Since: API Level 1

A keyboard with all the letters, but with more than one letter per key.

This type of keyboard is generally designed for thumb typing.

Constant Value: 2 (0x00000002)

public static final int SPECIAL_FUNCTION

Since: API Level 11

A keyboard that is only used to control special functions rather than for typing.

A special function keyboard consists only of non-printing keys such as HOME and POWER that are not actually used for typing.

Constant Value: 5 (0x00000005)

public static final int VIRTUAL_KEYBOARD

Since: API Level 11

The id of a generic virtual keyboard with a full layout that can be used to synthesize key events. Typically used with getEvents(char[]).

Constant Value: -1 (0xffffffff)

Public Methods

public static boolean deviceHasKey (int keyCode)

Since: API Level 3

Queries the framework about whether any physical keys exist on the any keyboard attached to the device that are capable of producing the given key code.

Parameters
keyCode The key code to query.
Returns
  • True if at least one attached keyboard supports the specified key code.

public static boolean[] deviceHasKeys (int[] keyCodes)

Since: API Level 3

Queries the framework about whether any physical keys exist on the any keyboard attached to the device that are capable of producing the given array of key codes.

Parameters
keyCodes The array of key codes to query.
Returns
  • A new array of the same size as the key codes array whose elements are set to true if at least one attached keyboard supports the corresponding key code at the same index in the key codes array.

public int get (int keyCode, int metaState)

Since: API Level 1

Gets the Unicode character generated by the specified key and meta key state combination.

Returns the Unicode character that the specified key would produce when the specified meta bits (see MetaKeyKeyListener) were active.

Returns 0 if the key is not one that is used to type Unicode characters.

If the return value has bit COMBINING_ACCENT set, the key is a "dead key" that should be combined with another to actually produce a character -- see getDeadChar(int, int) -- after masking with COMBINING_ACCENT_MASK.

Parameters
keyCode The key code.
metaState The meta key modifier state.
Returns
  • The associated character or combining accent, or 0 if none.

public static int getDeadChar (int accent, int c)

Since: API Level 1

Get the character that is produced by putting accent on the character c. For example, getDeadChar('`', 'e') returns è.

Parameters
accent The accent character. eg. '`'
c The basic character.
Returns
  • The combined character, or 0 if the characters cannot be combined.

public char getDisplayLabel (int keyCode)

Since: API Level 1

Gets the primary character for this key. In other words, the label that is physically printed on it.

Parameters
keyCode The key code.
Returns
  • The display label character, or 0 if none (eg. for non-printing keys).

public KeyEvent[] getEvents (char[] chars)

Since: API Level 1

Get an array of KeyEvent objects that if put into the input stream could plausibly generate the provided sequence of characters. It is not guaranteed that the sequence is the only way to generate these events or that it is optimal.

This function is primarily offered for instrumentation and testing purposes. It may fail to map characters to key codes. In particular, the key character map for the built-in keyboard device id may be empty. Consider using the key character map associated with the virtual keyboard device id instead.

For robust text entry, do not use this function. Instead construct a KeyEvent with action code ACTION_MULTIPLE that contains the desired string using KeyEvent(long, String, int, int).

Parameters
chars The sequence of characters to generate.
Returns
  • An array of KeyEvent objects, or null if the given char array can not be generated using the current key character map.

public boolean getKeyData (int keyCode, KeyCharacterMap.KeyData results)

Since: API Level 1

This method is deprecated.
instead use getDisplayLabel(int), getNumber(int) or get(int, int).

Get the character conversion data for a given key code.

Parameters
keyCode The keyCode to query.
results A KeyCharacterMap.KeyData instance that will be filled with the results.
Returns
  • True if the key was mapped. If the key was not mapped, results is not modified.

public int getKeyboardType ()

Since: API Level 1

Gets the keyboard type. Returns NUMERIC, PREDICTIVE, ALPHA or FULL.

Different keyboard types have different semantics. Refer to the documentation associated with the keyboard type constants for details.

Returns
  • The keyboard type.

public char getMatch (int keyCode, char[] chars)

Since: API Level 1

Gets the first character in the character array that can be generated by the specified key code.

This is a convenience function that returns the same value as getMatch(keyCode, chars, 0).

Parameters
keyCode The keycode.
chars The array of matching characters to consider.
Returns
  • The matching associated character, or 0 if none.

public char getMatch (int keyCode, char[] chars, int metaState)

Since: API Level 1

Gets the first character in the character array that can be generated by the specified key code. If there are multiple choices, prefers the one that would be generated with the specified meta key modifier state.

Parameters
keyCode The key code.
chars The array of matching characters to consider.
metaState The preferred meta key modifier state.
Returns
  • The matching associated character, or 0 if none.

public int getModifierBehavior ()

Since: API Level 11

Gets a constant that describes the behavior of this keyboard's modifier keys such as KEYCODE_SHIFT_LEFT.

Currently there are two behaviors that may be combined:

  • Chorded behavior: When the modifier key is pressed together with one or more character keys, the keyboard inserts the modified keys and then resets the modifier state when the modifier key is released.
  • Toggled behavior: When the modifier key is pressed and released on its own it first toggles into a latched state. When latched, the modifier will apply to next character key that is pressed and will then reset itself to the initial state. If the modifier is already latched and the modifier key is pressed and release on its own again, then it toggles into a locked state. When locked, the modifier will apply to all subsequent character keys that are pressed until unlocked by pressing the modifier key on its own one more time to reset it to the initial state. Toggled behavior is useful for small profile keyboards designed for thumb typing.

This function currently returns MODIFIER_BEHAVIOR_CHORDED when the keyboard type is FULL or SPECIAL_FUNCTION and MODIFIER_BEHAVIOR_CHORDED_OR_TOGGLED otherwise. In the future, the function may also take into account global keyboard accessibility settings, other user preferences, or new device capabilities.

Returns
  • The modifier behavior for this keyboard.

public char getNumber (int keyCode)

Since: API Level 1

Gets the number or symbol associated with the key.

The character value is returned, not the numeric value. If the key is not a number, but is a symbol, the symbol is retuned.

This method is intended to to support dial pads and other numeric or symbolic entry on keyboards where certain keys serve dual function as alphabetic and symbolic keys. This method returns the number or symbol associated with the key independent of whether the user has pressed the required modifier.

For example, on one particular keyboard the keys on the top QWERTY row generate numbers when ALT is pressed such that ALT-Q maps to '1'. So for that keyboard when getNumber(int) is called with KEYCODE_Q it returns '1' so that the user can type numbers without pressing ALT when it makes sense.

Parameters
keyCode The key code.
Returns
  • The associated numeric or symbolic character, or 0 if none.

public boolean isPrintingKey (int keyCode)

Since: API Level 1

Returns true if the specified key produces a glyph.

Parameters
keyCode The key code.
Returns
  • True if the key is a printing key.

public static KeyCharacterMap load (int deviceId)

Since: API Level 1

Loads the key character maps for the keyboard with the specified device id.

Parameters
deviceId The device id of the keyboard.
Returns
  • The associated key character map.
Throws
UnavailableException} if the key character map could not be loaded because it was malformed or the default key character map is missing from the system.

Protected Methods

protected void finalize ()

Since: API Level 1

Called before the object's memory is reclaimed by the VM. This can only happen once the garbage collector has detected that the object is no longer reachable by any thread of the running application.

The method can be used to free system resources or perform other cleanup before the object is garbage collected. The default implementation of the method is empty, which is also expected by the VM, but subclasses can override finalize() as required. Uncaught exceptions which are thrown during the execution of this method cause it to terminate immediately but are otherwise ignored.

Note that the VM does guarantee that finalize() is called at most once for any object, but it doesn't guarantee when (if at all) finalize() will be called. For example, object B's finalize() can delay the execution of object A's finalize() method and therefore it can delay the reclamation of A's memory. To be safe, use a ReferenceQueue, because it provides more control over the way the VM deals with references during garbage collection.

Throws
Throwable