KeyEvent.java revision 51e7fe7545e3509ebb5c31c10440acd31cec89a2
1/*
2 * Copyright (C) 2006 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
17package android.view;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21import android.util.Log;
22import android.util.SparseIntArray;
23import android.view.KeyCharacterMap;
24import android.view.KeyCharacterMap.KeyData;
25
26/**
27 * Object used to report key and button events.
28 * <p>
29 * Each key press is described by a sequence of key events.  A key press
30 * starts with a key event with {@link #ACTION_DOWN}.  If the key is held
31 * sufficiently long that it repeats, then the initial down is followed
32 * additional key events with {@link #ACTION_DOWN} and a non-zero value for
33 * {@link #getRepeatCount()}.  The last key event is a {@link #ACTION_UP}
34 * for the key up.  If the key press is canceled, the key up event will have the
35 * {@link #FLAG_CANCELED} flag set.
36 * </p><p>
37 * Key events are generally accompanied by a key code ({@link #getKeyCode()}),
38 * scan code ({@link #getScanCode()}) and meta state ({@link #getMetaState()}).
39 * Key code constants are defined in this class.  Scan code constants are raw
40 * device-specific codes obtained from the OS and so are not generally meaningful
41 * to applications unless interpreted using the {@link KeyCharacterMap}.
42 * Meta states describe the pressed state of key modifiers
43 * such as {@link #META_SHIFT_ON} or {@link #META_ALT_ON}.
44 * </p><p>
45 * Key codes typically correspond one-to-one with individual keys on an input device.
46 * Many keys and key combinations serve quite different functions on different
47 * input devices so care must be taken when interpreting them.  Always use the
48 * {@link KeyCharacterMap} associated with the input device when mapping keys
49 * to characters.  Be aware that there may be multiple key input devices active
50 * at the same time and each will have its own key character map.
51 * </p><p>
52 * When interacting with an IME, the framework may deliver key events
53 * with the special action {@link #ACTION_MULTIPLE} that either specifies
54 * that single repeated key code or a sequence of characters to insert.
55 * </p><p>
56 * In general, the framework cannot guarantee that the key events it delivers
57 * to a view always constitute complete key sequences since some events may be dropped
58 * or modified by containing views before they are delivered.  The view implementation
59 * should be prepared to handle {@link #FLAG_CANCELED} and should tolerate anomalous
60 * situations such as receiving a new {@link #ACTION_DOWN} without first having
61 * received an {@link #ACTION_UP} for the prior key press.
62 * </p><p>
63 * Refer to {@link InputDevice} for more information about how different kinds of
64 * input devices and sources represent keys and buttons.
65 * </p>
66 */
67public class KeyEvent extends InputEvent implements Parcelable {
68    /** Key code constant: Unknown key code. */
69    public static final int KEYCODE_UNKNOWN         = 0;
70    /** Key code constant: Soft Left key.
71     * Usually situated below the display on phones and used as a multi-function
72     * feature key for selecting a software defined function shown on the bottom left
73     * of the display. */
74    public static final int KEYCODE_SOFT_LEFT       = 1;
75    /** Key code constant: Soft Right key.
76     * Usually situated below the display on phones and used as a multi-function
77     * feature key for selecting a software defined function shown on the bottom right
78     * of the display. */
79    public static final int KEYCODE_SOFT_RIGHT      = 2;
80    /** Key code constant: Home key.
81     * This key is handled by the framework and is never delivered to applications. */
82    public static final int KEYCODE_HOME            = 3;
83    /** Key code constant: Back key. */
84    public static final int KEYCODE_BACK            = 4;
85    /** Key code constant: Call key. */
86    public static final int KEYCODE_CALL            = 5;
87    /** Key code constant: End Call key. */
88    public static final int KEYCODE_ENDCALL         = 6;
89    /** Key code constant: '0' key. */
90    public static final int KEYCODE_0               = 7;
91    /** Key code constant: '1' key. */
92    public static final int KEYCODE_1               = 8;
93    /** Key code constant: '2' key. */
94    public static final int KEYCODE_2               = 9;
95    /** Key code constant: '3' key. */
96    public static final int KEYCODE_3               = 10;
97    /** Key code constant: '4' key. */
98    public static final int KEYCODE_4               = 11;
99    /** Key code constant: '5' key. */
100    public static final int KEYCODE_5               = 12;
101    /** Key code constant: '6' key. */
102    public static final int KEYCODE_6               = 13;
103    /** Key code constant: '7' key. */
104    public static final int KEYCODE_7               = 14;
105    /** Key code constant: '8' key. */
106    public static final int KEYCODE_8               = 15;
107    /** Key code constant: '9' key. */
108    public static final int KEYCODE_9               = 16;
109    /** Key code constant: '*' key. */
110    public static final int KEYCODE_STAR            = 17;
111    /** Key code constant: '#' key. */
112    public static final int KEYCODE_POUND           = 18;
113    /** Key code constant: Directional Pad Up key.
114     * May also be synthesized from trackball motions. */
115    public static final int KEYCODE_DPAD_UP         = 19;
116    /** Key code constant: Directional Pad Down key.
117     * May also be synthesized from trackball motions. */
118    public static final int KEYCODE_DPAD_DOWN       = 20;
119    /** Key code constant: Directional Pad Left key.
120     * May also be synthesized from trackball motions. */
121    public static final int KEYCODE_DPAD_LEFT       = 21;
122    /** Key code constant: Directional Pad Right key.
123     * May also be synthesized from trackball motions. */
124    public static final int KEYCODE_DPAD_RIGHT      = 22;
125    /** Key code constant: Directional Pad Center key.
126     * May also be synthesized from trackball motions. */
127    public static final int KEYCODE_DPAD_CENTER     = 23;
128    /** Key code constant: Volume Up key.
129     * Adjusts the speaker volume up. */
130    public static final int KEYCODE_VOLUME_UP       = 24;
131    /** Key code constant: Volume Down key.
132     * Adjusts the speaker volume down. */
133    public static final int KEYCODE_VOLUME_DOWN     = 25;
134    /** Key code constant: Power key. */
135    public static final int KEYCODE_POWER           = 26;
136    /** Key code constant: Camera key.
137     * Used to launch a camera application or take pictures. */
138    public static final int KEYCODE_CAMERA          = 27;
139    /** Key code constant: Clear key. */
140    public static final int KEYCODE_CLEAR           = 28;
141    /** Key code constant: 'A' key. */
142    public static final int KEYCODE_A               = 29;
143    /** Key code constant: 'B' key. */
144    public static final int KEYCODE_B               = 30;
145    /** Key code constant: 'C' key. */
146    public static final int KEYCODE_C               = 31;
147    /** Key code constant: 'D' key. */
148    public static final int KEYCODE_D               = 32;
149    /** Key code constant: 'E' key. */
150    public static final int KEYCODE_E               = 33;
151    /** Key code constant: 'F' key. */
152    public static final int KEYCODE_F               = 34;
153    /** Key code constant: 'G' key. */
154    public static final int KEYCODE_G               = 35;
155    /** Key code constant: 'H' key. */
156    public static final int KEYCODE_H               = 36;
157    /** Key code constant: 'I' key. */
158    public static final int KEYCODE_I               = 37;
159    /** Key code constant: 'J' key. */
160    public static final int KEYCODE_J               = 38;
161    /** Key code constant: 'K' key. */
162    public static final int KEYCODE_K               = 39;
163    /** Key code constant: 'L' key. */
164    public static final int KEYCODE_L               = 40;
165    /** Key code constant: 'M' key. */
166    public static final int KEYCODE_M               = 41;
167    /** Key code constant: 'N' key. */
168    public static final int KEYCODE_N               = 42;
169    /** Key code constant: 'O' key. */
170    public static final int KEYCODE_O               = 43;
171    /** Key code constant: 'P' key. */
172    public static final int KEYCODE_P               = 44;
173    /** Key code constant: 'Q' key. */
174    public static final int KEYCODE_Q               = 45;
175    /** Key code constant: 'R' key. */
176    public static final int KEYCODE_R               = 46;
177    /** Key code constant: 'S' key. */
178    public static final int KEYCODE_S               = 47;
179    /** Key code constant: 'T' key. */
180    public static final int KEYCODE_T               = 48;
181    /** Key code constant: 'U' key. */
182    public static final int KEYCODE_U               = 49;
183    /** Key code constant: 'V' key. */
184    public static final int KEYCODE_V               = 50;
185    /** Key code constant: 'W' key. */
186    public static final int KEYCODE_W               = 51;
187    /** Key code constant: 'X' key. */
188    public static final int KEYCODE_X               = 52;
189    /** Key code constant: 'Y' key. */
190    public static final int KEYCODE_Y               = 53;
191    /** Key code constant: 'Z' key. */
192    public static final int KEYCODE_Z               = 54;
193    /** Key code constant: ',' key. */
194    public static final int KEYCODE_COMMA           = 55;
195    /** Key code constant: '.' key. */
196    public static final int KEYCODE_PERIOD          = 56;
197    /** Key code constant: Left Alt modifier key. */
198    public static final int KEYCODE_ALT_LEFT        = 57;
199    /** Key code constant: Right Alt modifier key. */
200    public static final int KEYCODE_ALT_RIGHT       = 58;
201    /** Key code constant: Left Shift modifier key. */
202    public static final int KEYCODE_SHIFT_LEFT      = 59;
203    /** Key code constant: Right Shift modifier key. */
204    public static final int KEYCODE_SHIFT_RIGHT     = 60;
205    /** Key code constant: Tab key. */
206    public static final int KEYCODE_TAB             = 61;
207    /** Key code constant: Space key. */
208    public static final int KEYCODE_SPACE           = 62;
209    /** Key code constant: Symbol modifier key.
210     * Used to enter alternate symbols. */
211    public static final int KEYCODE_SYM             = 63;
212    /** Key code constant: Explorer special function key.
213     * Used to launch a browser application. */
214    public static final int KEYCODE_EXPLORER        = 64;
215    /** Key code constant: Envelope special function key.
216     * Used to launch a mail application. */
217    public static final int KEYCODE_ENVELOPE        = 65;
218    /** Key code constant: Enter key. */
219    public static final int KEYCODE_ENTER           = 66;
220    /** Key code constant: Backspace key.
221     * Deletes characters before the insertion point, unlike {@link #KEYCODE_FORWARD_DEL}. */
222    public static final int KEYCODE_DEL             = 67;
223    /** Key code constant: '`' (backtick) key. */
224    public static final int KEYCODE_GRAVE           = 68;
225    /** Key code constant: '-'. */
226    public static final int KEYCODE_MINUS           = 69;
227    /** Key code constant: '=' key. */
228    public static final int KEYCODE_EQUALS          = 70;
229    /** Key code constant: '[' key. */
230    public static final int KEYCODE_LEFT_BRACKET    = 71;
231    /** Key code constant: ']' key. */
232    public static final int KEYCODE_RIGHT_BRACKET   = 72;
233    /** Key code constant: '\' key. */
234    public static final int KEYCODE_BACKSLASH       = 73;
235    /** Key code constant: ';' key. */
236    public static final int KEYCODE_SEMICOLON       = 74;
237    /** Key code constant: ''' (apostrophe) key. */
238    public static final int KEYCODE_APOSTROPHE      = 75;
239    /** Key code constant: '/' key. */
240    public static final int KEYCODE_SLASH           = 76;
241    /** Key code constant: '@' key. */
242    public static final int KEYCODE_AT              = 77;
243    /** Key code constant: Number modifier key.
244     * Used to enter numeric symbols.
245     * This key is not Num Lock; it is more like {@link #KEYCODE_ALT_LEFT} and is
246     * interpreted as an ALT key by {@link android.text.method.MetaKeyKeyListener}. */
247    public static final int KEYCODE_NUM             = 78;
248    /** Key code constant: Headset Hook key.
249     * Used to hang up calls and stop media. */
250    public static final int KEYCODE_HEADSETHOOK     = 79;
251    /** Key code constant: Camera Focus key.
252     * Used to focus the camera. */
253    public static final int KEYCODE_FOCUS           = 80;   // *Camera* focus
254    /** Key code constant: '+' key. */
255    public static final int KEYCODE_PLUS            = 81;
256    /** Key code constant: Menu key. */
257    public static final int KEYCODE_MENU            = 82;
258    /** Key code constant: Notification key. */
259    public static final int KEYCODE_NOTIFICATION    = 83;
260    /** Key code constant: Search key. */
261    public static final int KEYCODE_SEARCH          = 84;
262    /** Key code constant: Play/Pause media key. */
263    public static final int KEYCODE_MEDIA_PLAY_PAUSE= 85;
264    /** Key code constant: Stop media key. */
265    public static final int KEYCODE_MEDIA_STOP      = 86;
266    /** Key code constant: Play Next media key. */
267    public static final int KEYCODE_MEDIA_NEXT      = 87;
268    /** Key code constant: Play Previous media key. */
269    public static final int KEYCODE_MEDIA_PREVIOUS  = 88;
270    /** Key code constant: Rewind media key. */
271    public static final int KEYCODE_MEDIA_REWIND    = 89;
272    /** Key code constant: Fast Forward media key. */
273    public static final int KEYCODE_MEDIA_FAST_FORWARD = 90;
274    /** Key code constant: Mute key.
275     * Mutes the microphone, unlike {@link #KEYCODE_VOLUME_MUTE}. */
276    public static final int KEYCODE_MUTE            = 91;
277    /** Key code constant: Page Up key. */
278    public static final int KEYCODE_PAGE_UP         = 92;
279    /** Key code constant: Page Down key. */
280    public static final int KEYCODE_PAGE_DOWN       = 93;
281    /** Key code constant: Picture Symbols modifier key.
282     * Used to switch symbol sets (Emoji, Kao-moji). */
283    public static final int KEYCODE_PICTSYMBOLS     = 94;   // switch symbol-sets (Emoji,Kao-moji)
284    /** Key code constant: Switch Charset modifier key.
285     * Used to switch character sets (Kanji, Katakana). */
286    public static final int KEYCODE_SWITCH_CHARSET  = 95;   // switch char-sets (Kanji,Katakana)
287    /** Key code constant: A Button key.
288     * On a game controller, the A button should be either the button labeled A
289     * or the first button on the upper row of controller buttons. */
290    public static final int KEYCODE_BUTTON_A        = 96;
291    /** Key code constant: B Button key.
292     * On a game controller, the B button should be either the button labeled B
293     * or the second button on the upper row of controller buttons. */
294    public static final int KEYCODE_BUTTON_B        = 97;
295    /** Key code constant: C Button key.
296     * On a game controller, the C button should be either the button labeled C
297     * or the third button on the upper row of controller buttons. */
298    public static final int KEYCODE_BUTTON_C        = 98;
299    /** Key code constant: X Button key.
300     * On a game controller, the X button should be either the button labeled X
301     * or the first button on the lower row of controller buttons. */
302    public static final int KEYCODE_BUTTON_X        = 99;
303    /** Key code constant: Y Button key.
304     * On a game controller, the Y button should be either the button labeled Y
305     * or the second button on the lower row of controller buttons. */
306    public static final int KEYCODE_BUTTON_Y        = 100;
307    /** Key code constant: Z Button key.
308     * On a game controller, the Z button should be either the button labeled Z
309     * or the third button on the lower row of controller buttons. */
310    public static final int KEYCODE_BUTTON_Z        = 101;
311    /** Key code constant: L1 Button key.
312     * On a game controller, the L1 button should be either the button labeled L1 (or L)
313     * or the top left trigger button. */
314    public static final int KEYCODE_BUTTON_L1       = 102;
315    /** Key code constant: R1 Button key.
316     * On a game controller, the R1 button should be either the button labeled R1 (or R)
317     * or the top right trigger button. */
318    public static final int KEYCODE_BUTTON_R1       = 103;
319    /** Key code constant: L2 Button key.
320     * On a game controller, the L2 button should be either the button labeled L2
321     * or the bottom left trigger button. */
322    public static final int KEYCODE_BUTTON_L2       = 104;
323    /** Key code constant: R2 Button key.
324     * On a game controller, the R2 button should be either the button labeled R2
325     * or the bottom right trigger button. */
326    public static final int KEYCODE_BUTTON_R2       = 105;
327    /** Key code constant: Left Thumb Button key.
328     * On a game controller, the left thumb button indicates that the left (or only)
329     * joystick is pressed. */
330    public static final int KEYCODE_BUTTON_THUMBL   = 106;
331    /** Key code constant: Right Thumb Button key.
332     * On a game controller, the right thumb button indicates that the right
333     * joystick is pressed. */
334    public static final int KEYCODE_BUTTON_THUMBR   = 107;
335    /** Key code constant: Start Button key.
336     * On a game controller, the button labeled Start. */
337    public static final int KEYCODE_BUTTON_START    = 108;
338    /** Key code constant: Select Button key.
339     * On a game controller, the button labeled Select. */
340    public static final int KEYCODE_BUTTON_SELECT   = 109;
341    /** Key code constant: Mode Button key.
342     * On a game controller, the button labeled Mode. */
343    public static final int KEYCODE_BUTTON_MODE     = 110;
344    /** Key code constant: Escape key. */
345    public static final int KEYCODE_ESCAPE          = 111;
346    /** Key code constant: Forward Delete key.
347     * Deletes characters ahead of the insertion point, unlike {@link #KEYCODE_DEL}. */
348    public static final int KEYCODE_FORWARD_DEL     = 112;
349    /** Key code constant: Left Control modifier key. */
350    public static final int KEYCODE_CTRL_LEFT       = 113;
351    /** Key code constant: Right Control modifier key. */
352    public static final int KEYCODE_CTRL_RIGHT      = 114;
353    /** Key code constant: Caps Lock modifier key. */
354    public static final int KEYCODE_CAPS_LOCK       = 115;
355    /** Key code constant: Scroll Lock key. */
356    public static final int KEYCODE_SCROLL_LOCK     = 116;
357    /** Key code constant: Left Meta modifier key. */
358    public static final int KEYCODE_META_LEFT       = 117;
359    /** Key code constant: Right Meta modifier key. */
360    public static final int KEYCODE_META_RIGHT      = 118;
361    /** Key code constant: Function modifier key. */
362    public static final int KEYCODE_FUNCTION        = 119;
363    /** Key code constant: System Request / Print Screen key. */
364    public static final int KEYCODE_SYSRQ           = 120;
365    /** Key code constant: Break / Pause key. */
366    public static final int KEYCODE_BREAK           = 121;
367    /** Key code constant: Home Movement key.
368     * Used for scrolling or moving the cursor around to the start of a line
369     * or to the top of a list. */
370    public static final int KEYCODE_MOVE_HOME       = 122;
371    /** Key code constant: End Movement key.
372     * Used for scrolling or moving the cursor around to the end of a line
373     * or to the bottom of a list. */
374    public static final int KEYCODE_MOVE_END        = 123;
375    /** Key code constant: Insert key.
376     * Toggles insert / overwrite edit mode. */
377    public static final int KEYCODE_INSERT          = 124;
378    /** Key code constant: Forward key.
379     * Navigates forward in the history stack.  Complement of {@link #KEYCODE_BACK}. */
380    public static final int KEYCODE_FORWARD         = 125;
381    /** Key code constant: Play media key. */
382    public static final int KEYCODE_MEDIA_PLAY      = 126;
383    /** Key code constant: Pause media key. */
384    public static final int KEYCODE_MEDIA_PAUSE     = 127;
385    /** Key code constant: Close media key.
386     * May be used to close a CD tray, for example. */
387    public static final int KEYCODE_MEDIA_CLOSE     = 128;
388    /** Key code constant: Eject media key.
389     * May be used to eject a CD tray, for example. */
390    public static final int KEYCODE_MEDIA_EJECT     = 129;
391    /** Key code constant: Record media key. */
392    public static final int KEYCODE_MEDIA_RECORD    = 130;
393    /** Key code constant: F1 key. */
394    public static final int KEYCODE_F1              = 131;
395    /** Key code constant: F2 key. */
396    public static final int KEYCODE_F2              = 132;
397    /** Key code constant: F3 key. */
398    public static final int KEYCODE_F3              = 133;
399    /** Key code constant: F4 key. */
400    public static final int KEYCODE_F4              = 134;
401    /** Key code constant: F5 key. */
402    public static final int KEYCODE_F5              = 135;
403    /** Key code constant: F6 key. */
404    public static final int KEYCODE_F6              = 136;
405    /** Key code constant: F7 key. */
406    public static final int KEYCODE_F7              = 137;
407    /** Key code constant: F8 key. */
408    public static final int KEYCODE_F8              = 138;
409    /** Key code constant: F9 key. */
410    public static final int KEYCODE_F9              = 139;
411    /** Key code constant: F10 key. */
412    public static final int KEYCODE_F10             = 140;
413    /** Key code constant: F11 key. */
414    public static final int KEYCODE_F11             = 141;
415    /** Key code constant: F12 key. */
416    public static final int KEYCODE_F12             = 142;
417    /** Key code constant: Num Lock modifier key.
418     * This is the Num Lock key; it is different from {@link #KEYCODE_NUM}.
419     * This key generally modifies the behavior of other keys on the numeric keypad. */
420    public static final int KEYCODE_NUM_LOCK        = 143;
421    /** Key code constant: Numeric keypad '0' key. */
422    public static final int KEYCODE_NUMPAD_0        = 144;
423    /** Key code constant: Numeric keypad '1' key. */
424    public static final int KEYCODE_NUMPAD_1        = 145;
425    /** Key code constant: Numeric keypad '2' key. */
426    public static final int KEYCODE_NUMPAD_2        = 146;
427    /** Key code constant: Numeric keypad '3' key. */
428    public static final int KEYCODE_NUMPAD_3        = 147;
429    /** Key code constant: Numeric keypad '4' key. */
430    public static final int KEYCODE_NUMPAD_4        = 148;
431    /** Key code constant: Numeric keypad '5' key. */
432    public static final int KEYCODE_NUMPAD_5        = 149;
433    /** Key code constant: Numeric keypad '6' key. */
434    public static final int KEYCODE_NUMPAD_6        = 150;
435    /** Key code constant: Numeric keypad '7' key. */
436    public static final int KEYCODE_NUMPAD_7        = 151;
437    /** Key code constant: Numeric keypad '8' key. */
438    public static final int KEYCODE_NUMPAD_8        = 152;
439    /** Key code constant: Numeric keypad '9' key. */
440    public static final int KEYCODE_NUMPAD_9        = 153;
441    /** Key code constant: Numeric keypad '/' key (for division). */
442    public static final int KEYCODE_NUMPAD_DIVIDE   = 154;
443    /** Key code constant: Numeric keypad '*' key (for multiplication). */
444    public static final int KEYCODE_NUMPAD_MULTIPLY = 155;
445    /** Key code constant: Numeric keypad '-' key (for subtraction). */
446    public static final int KEYCODE_NUMPAD_SUBTRACT = 156;
447    /** Key code constant: Numeric keypad '+' key (for addition). */
448    public static final int KEYCODE_NUMPAD_ADD      = 157;
449    /** Key code constant: Numeric keypad '.' key (for decimals or digit grouping). */
450    public static final int KEYCODE_NUMPAD_DOT      = 158;
451    /** Key code constant: Numeric keypad ',' key (for decimals or digit grouping). */
452    public static final int KEYCODE_NUMPAD_COMMA    = 159;
453    /** Key code constant: Numeric keypad Enter key. */
454    public static final int KEYCODE_NUMPAD_ENTER    = 160;
455    /** Key code constant: Numeric keypad '=' key. */
456    public static final int KEYCODE_NUMPAD_EQUALS   = 161;
457    /** Key code constant: Numeric keypad '(' key. */
458    public static final int KEYCODE_NUMPAD_LEFT_PAREN = 162;
459    /** Key code constant: Numeric keypad ')' key. */
460    public static final int KEYCODE_NUMPAD_RIGHT_PAREN = 163;
461    /** Key code constant: Volume Mute key.
462     * Mutes the speaker, unlike {@link #KEYCODE_MUTE}.
463     * This key should normally be implemented as a toggle such that the first press
464     * mutes the speaker and the second press restores the original volume. */
465    public static final int KEYCODE_VOLUME_MUTE     = 164;
466
467    private static final int LAST_KEYCODE           = KEYCODE_VOLUME_MUTE;
468
469    // NOTE: If you add a new keycode here you must also add it to:
470    //  isSystem()
471    //  native/include/android/keycodes.h
472    //  frameworks/base/include/ui/KeycodeLabels.h
473    //  external/webkit/WebKit/android/plugins/ANPKeyCodes.h
474    //  tools/puppet_master/PuppetMaster/nav_keys.py
475    //  frameworks/base/core/res/res/values/attrs.xml
476    //  emulator?
477    //
478    //  Also Android currently does not reserve code ranges for vendor-
479    //  specific key codes.  If you have new key codes to have, you
480    //  MUST contribute a patch to the open source project to define
481    //  those new codes.  This is intended to maintain a consistent
482    //  set of key code definitions across all Android devices.
483
484    // Symbolic names of all keys indexed by keycode.
485    // There should be exactly LAST_KEYCODE + 1 entries in this table.
486    private static final String[] KEYCODE_SYMBOLIC_NAMES = new String[] {
487        "KEYCODE_UNKNOWN",
488        "KEYCODE_SOFT_LEFT",
489        "KEYCODE_SOFT_RIGHT",
490        "KEYCODE_HOME",
491        "KEYCODE_BACK",
492        "KEYCODE_CALL",
493        "KEYCODE_ENDCALL",
494        "KEYCODE_0",
495        "KEYCODE_1",
496        "KEYCODE_2",
497        "KEYCODE_3",
498        "KEYCODE_4",
499        "KEYCODE_5",
500        "KEYCODE_6",
501        "KEYCODE_7",
502        "KEYCODE_8",
503        "KEYCODE_9",
504        "KEYCODE_STAR",
505        "KEYCODE_POUND",
506        "KEYCODE_DPAD_UP",
507        "KEYCODE_DPAD_DOWN",
508        "KEYCODE_DPAD_LEFT",
509        "KEYCODE_DPAD_RIGHT",
510        "KEYCODE_DPAD_CENTER",
511        "KEYCODE_VOLUME_UP",
512        "KEYCODE_VOLUME_DOWN",
513        "KEYCODE_POWER",
514        "KEYCODE_CAMERA",
515        "KEYCODE_CLEAR",
516        "KEYCODE_A",
517        "KEYCODE_B",
518        "KEYCODE_C",
519        "KEYCODE_D",
520        "KEYCODE_E",
521        "KEYCODE_F",
522        "KEYCODE_G",
523        "KEYCODE_H",
524        "KEYCODE_I",
525        "KEYCODE_J",
526        "KEYCODE_K",
527        "KEYCODE_L",
528        "KEYCODE_M",
529        "KEYCODE_N",
530        "KEYCODE_O",
531        "KEYCODE_P",
532        "KEYCODE_Q",
533        "KEYCODE_R",
534        "KEYCODE_S",
535        "KEYCODE_T",
536        "KEYCODE_U",
537        "KEYCODE_V",
538        "KEYCODE_W",
539        "KEYCODE_X",
540        "KEYCODE_Y",
541        "KEYCODE_Z",
542        "KEYCODE_COMMA",
543        "KEYCODE_PERIOD",
544        "KEYCODE_ALT_LEFT",
545        "KEYCODE_ALT_RIGHT",
546        "KEYCODE_SHIFT_LEFT",
547        "KEYCODE_SHIFT_RIGHT",
548        "KEYCODE_TAB",
549        "KEYCODE_SPACE",
550        "KEYCODE_SYM",
551        "KEYCODE_EXPLORER",
552        "KEYCODE_ENVELOPE",
553        "KEYCODE_ENTER",
554        "KEYCODE_DEL",
555        "KEYCODE_GRAVE",
556        "KEYCODE_MINUS",
557        "KEYCODE_EQUALS",
558        "KEYCODE_LEFT_BRACKET",
559        "KEYCODE_RIGHT_BRACKET",
560        "KEYCODE_BACKSLASH",
561        "KEYCODE_SEMICOLON",
562        "KEYCODE_APOSTROPHE",
563        "KEYCODE_SLASH",
564        "KEYCODE_AT",
565        "KEYCODE_NUM",
566        "KEYCODE_HEADSETHOOK",
567        "KEYCODE_FOCUS",
568        "KEYCODE_PLUS",
569        "KEYCODE_MENU",
570        "KEYCODE_NOTIFICATION",
571        "KEYCODE_SEARCH",
572        "KEYCODE_MEDIA_PLAY_PAUSE",
573        "KEYCODE_MEDIA_STOP",
574        "KEYCODE_MEDIA_NEXT",
575        "KEYCODE_MEDIA_PREVIOUS",
576        "KEYCODE_MEDIA_REWIND",
577        "KEYCODE_MEDIA_FAST_FORWARD",
578        "KEYCODE_MUTE",
579        "KEYCODE_PAGE_UP",
580        "KEYCODE_PAGE_DOWN",
581        "KEYCODE_PICTSYMBOLS",
582        "KEYCODE_SWITCH_CHARSET",
583        "KEYCODE_BUTTON_A",
584        "KEYCODE_BUTTON_B",
585        "KEYCODE_BUTTON_C",
586        "KEYCODE_BUTTON_X",
587        "KEYCODE_BUTTON_Y",
588        "KEYCODE_BUTTON_Z",
589        "KEYCODE_BUTTON_L1",
590        "KEYCODE_BUTTON_R1",
591        "KEYCODE_BUTTON_L2",
592        "KEYCODE_BUTTON_R2",
593        "KEYCODE_BUTTON_THUMBL",
594        "KEYCODE_BUTTON_THUMBR",
595        "KEYCODE_BUTTON_START",
596        "KEYCODE_BUTTON_SELECT",
597        "KEYCODE_BUTTON_MODE",
598        "KEYCODE_ESCAPE",
599        "KEYCODE_FORWARD_DEL",
600        "KEYCODE_CTRL_LEFT",
601        "KEYCODE_CTRL_RIGHT",
602        "KEYCODE_CAPS_LOCK",
603        "KEYCODE_SCROLL_LOCK",
604        "KEYCODE_META_LEFT",
605        "KEYCODE_META_RIGHT",
606        "KEYCODE_FUNCTION",
607        "KEYCODE_SYSRQ",
608        "KEYCODE_BREAK",
609        "KEYCODE_MOVE_HOME",
610        "KEYCODE_MOVE_END",
611        "KEYCODE_INSERT",
612        "KEYCODE_FORWARD",
613        "KEYCODE_MEDIA_PLAY",
614        "KEYCODE_MEDIA_PAUSE",
615        "KEYCODE_MEDIA_CLOSE",
616        "KEYCODE_MEDIA_EJECT",
617        "KEYCODE_MEDIA_RECORD",
618        "KEYCODE_F1",
619        "KEYCODE_F2",
620        "KEYCODE_F3",
621        "KEYCODE_F4",
622        "KEYCODE_F5",
623        "KEYCODE_F6",
624        "KEYCODE_F7",
625        "KEYCODE_F8",
626        "KEYCODE_F9",
627        "KEYCODE_F10",
628        "KEYCODE_F11",
629        "KEYCODE_F12",
630        "KEYCODE_NUM_LOCK",
631        "KEYCODE_NUMPAD_0",
632        "KEYCODE_NUMPAD_1",
633        "KEYCODE_NUMPAD_2",
634        "KEYCODE_NUMPAD_3",
635        "KEYCODE_NUMPAD_4",
636        "KEYCODE_NUMPAD_5",
637        "KEYCODE_NUMPAD_6",
638        "KEYCODE_NUMPAD_7",
639        "KEYCODE_NUMPAD_8",
640        "KEYCODE_NUMPAD_9",
641        "KEYCODE_NUMPAD_DIVIDE",
642        "KEYCODE_NUMPAD_MULTIPLY",
643        "KEYCODE_MUMPAD_SUBTRACT",
644        "KEYCODE_NUMPAD_ADD",
645        "KEYCODE_NUMPAD_DOT",
646        "KEYCODE_NUMPAD_COMMA",
647        "KEYCODE_NUMPAD_ENTER",
648        "KEYCODE_NUMPAD_EQUALS",
649        "KEYCODE_NUMPAD_LEFT_PAREN",
650        "KEYCODE_NUMPAD_RIGHT_PAREN",
651        "KEYCODE_VOLUME_MUTE",
652    };
653
654    // Symbolic names of all metakeys in bit order from least significant to most significant.
655    // Accordingly there are exactly 32 values in this table.
656    private static final String[] META_SYMBOLIC_NAMES = new String[] {
657        "META_SHIFT_ON",
658        "META_ALT_ON",
659        "META_SYM_ON",
660        "META_FUNCTION_ON",
661        "META_ALT_LEFT_ON",
662        "META_ALT_RIGHT_ON",
663        "META_SHIFT_LEFT_ON",
664        "META_SHIFT_RIGHT_ON",
665        "META_CAP_LOCKED",
666        "META_ALT_LOCKED",
667        "META_SYM_LOCKED",
668        "0x00000800",
669        "META_CTRL_ON",
670        "META_CTRL_LEFT_ON",
671        "META_CTRL_RIGHT_ON",
672        "0x00008000",
673        "META_META_ON",
674        "META_META_LEFT_ON",
675        "META_META_RIGHT_ON",
676        "0x00080000",
677        "META_CAPS_LOCK_ON",
678        "META_NUM_LOCK_ON",
679        "META_SCROLL_LOCK_ON",
680        "0x00800000",
681        "0x01000000",
682        "0x02000000",
683        "0x04000000",
684        "0x08000000",
685        "0x10000000",
686        "0x20000000",
687        "0x40000000",
688        "0x80000000",
689    };
690
691    /**
692     * @deprecated There are now more than MAX_KEYCODE keycodes.
693     * Use {@link #getMaxKeyCode()} instead.
694     */
695    @Deprecated
696    public static final int MAX_KEYCODE             = 84;
697
698    /**
699     * {@link #getAction} value: the key has been pressed down.
700     */
701    public static final int ACTION_DOWN             = 0;
702    /**
703     * {@link #getAction} value: the key has been released.
704     */
705    public static final int ACTION_UP               = 1;
706    /**
707     * {@link #getAction} value: multiple duplicate key events have
708     * occurred in a row, or a complex string is being delivered.  If the
709     * key code is not {#link {@link #KEYCODE_UNKNOWN} then the
710     * {#link {@link #getRepeatCount()} method returns the number of times
711     * the given key code should be executed.
712     * Otherwise, if the key code is {@link #KEYCODE_UNKNOWN}, then
713     * this is a sequence of characters as returned by {@link #getCharacters}.
714     */
715    public static final int ACTION_MULTIPLE         = 2;
716
717    /**
718     * SHIFT key locked in CAPS mode.
719     * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
720     * @hide
721     */
722    public static final int META_CAP_LOCKED = 0x100;
723
724    /**
725     * ALT key locked.
726     * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
727     * @hide
728     */
729    public static final int META_ALT_LOCKED = 0x200;
730
731    /**
732     * SYM key locked.
733     * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
734     * @hide
735     */
736    public static final int META_SYM_LOCKED = 0x400;
737
738    /**
739     * Text is in selection mode.
740     * Reserved for use by {@link MetaKeyKeyListener} for a private unpublished constant
741     * in its API that is currently being retained for legacy reasons.
742     * @hide
743     */
744    public static final int META_SELECTING = 0x800;
745
746    /**
747     * <p>This mask is used to check whether one of the ALT meta keys is pressed.</p>
748     *
749     * @see #isAltPressed()
750     * @see #getMetaState()
751     * @see #KEYCODE_ALT_LEFT
752     * @see #KEYCODE_ALT_RIGHT
753     */
754    public static final int META_ALT_ON = 0x02;
755
756    /**
757     * <p>This mask is used to check whether the left ALT meta key is pressed.</p>
758     *
759     * @see #isAltPressed()
760     * @see #getMetaState()
761     * @see #KEYCODE_ALT_LEFT
762     */
763    public static final int META_ALT_LEFT_ON = 0x10;
764
765    /**
766     * <p>This mask is used to check whether the right the ALT meta key is pressed.</p>
767     *
768     * @see #isAltPressed()
769     * @see #getMetaState()
770     * @see #KEYCODE_ALT_RIGHT
771     */
772    public static final int META_ALT_RIGHT_ON = 0x20;
773
774    /**
775     * <p>This mask is used to check whether one of the SHIFT meta keys is pressed.</p>
776     *
777     * @see #isShiftPressed()
778     * @see #getMetaState()
779     * @see #KEYCODE_SHIFT_LEFT
780     * @see #KEYCODE_SHIFT_RIGHT
781     */
782    public static final int META_SHIFT_ON = 0x1;
783
784    /**
785     * <p>This mask is used to check whether the left SHIFT meta key is pressed.</p>
786     *
787     * @see #isShiftPressed()
788     * @see #getMetaState()
789     * @see #KEYCODE_SHIFT_LEFT
790     */
791    public static final int META_SHIFT_LEFT_ON = 0x40;
792
793    /**
794     * <p>This mask is used to check whether the right SHIFT meta key is pressed.</p>
795     *
796     * @see #isShiftPressed()
797     * @see #getMetaState()
798     * @see #KEYCODE_SHIFT_RIGHT
799     */
800    public static final int META_SHIFT_RIGHT_ON = 0x80;
801
802    /**
803     * <p>This mask is used to check whether the SYM meta key is pressed.</p>
804     *
805     * @see #isSymPressed()
806     * @see #getMetaState()
807     */
808    public static final int META_SYM_ON = 0x4;
809
810    /**
811     * <p>This mask is used to check whether the FUNCTION meta key is pressed.</p>
812     *
813     * @see #isFunctionPressed()
814     * @see #getMetaState()
815     */
816    public static final int META_FUNCTION_ON = 0x8;
817
818    /**
819     * <p>This mask is used to check whether one of the CTRL meta keys is pressed.</p>
820     *
821     * @see #isCtrlPressed()
822     * @see #getMetaState()
823     * @see #KEYCODE_CTRL_LEFT
824     * @see #KEYCODE_CTRL_RIGHT
825     */
826    public static final int META_CTRL_ON = 0x1000;
827
828    /**
829     * <p>This mask is used to check whether the left CTRL meta key is pressed.</p>
830     *
831     * @see #isCtrlPressed()
832     * @see #getMetaState()
833     * @see #KEYCODE_CTRL_LEFT
834     */
835    public static final int META_CTRL_LEFT_ON = 0x2000;
836
837    /**
838     * <p>This mask is used to check whether the right CTRL meta key is pressed.</p>
839     *
840     * @see #isCtrlPressed()
841     * @see #getMetaState()
842     * @see #KEYCODE_CTRL_RIGHT
843     */
844    public static final int META_CTRL_RIGHT_ON = 0x4000;
845
846    /**
847     * <p>This mask is used to check whether one of the META meta keys is pressed.</p>
848     *
849     * @see #isMetaPressed()
850     * @see #getMetaState()
851     * @see #KEYCODE_META_LEFT
852     * @see #KEYCODE_META_RIGHT
853     */
854    public static final int META_META_ON = 0x10000;
855
856    /**
857     * <p>This mask is used to check whether the left META meta key is pressed.</p>
858     *
859     * @see #isMetaPressed()
860     * @see #getMetaState()
861     * @see #KEYCODE_META_LEFT
862     */
863    public static final int META_META_LEFT_ON = 0x20000;
864
865    /**
866     * <p>This mask is used to check whether the right META meta key is pressed.</p>
867     *
868     * @see #isMetaPressed()
869     * @see #getMetaState()
870     * @see #KEYCODE_META_RIGHT
871     */
872    public static final int META_META_RIGHT_ON = 0x40000;
873
874    /**
875     * <p>This mask is used to check whether the CAPS LOCK meta key is on.</p>
876     *
877     * @see #isCapsLockOn()
878     * @see #getMetaState()
879     * @see #KEYCODE_CAPS_LOCK
880     */
881    public static final int META_CAPS_LOCK_ON = 0x100000;
882
883    /**
884     * <p>This mask is used to check whether the NUM LOCK meta key is on.</p>
885     *
886     * @see #isNumLockOn()
887     * @see #getMetaState()
888     * @see #KEYCODE_NUM_LOCK
889     */
890    public static final int META_NUM_LOCK_ON = 0x200000;
891
892    /**
893     * <p>This mask is used to check whether the SCROLL LOCK meta key is on.</p>
894     *
895     * @see #isScrollLockOn()
896     * @see #getMetaState()
897     * @see #KEYCODE_SCROLL_LOCK
898     */
899    public static final int META_SCROLL_LOCK_ON = 0x400000;
900
901    /**
902     * This mask is set if the device woke because of this key event.
903     */
904    public static final int FLAG_WOKE_HERE = 0x1;
905
906    /**
907     * This mask is set if the key event was generated by a software keyboard.
908     */
909    public static final int FLAG_SOFT_KEYBOARD = 0x2;
910
911    /**
912     * This mask is set if we don't want the key event to cause us to leave
913     * touch mode.
914     */
915    public static final int FLAG_KEEP_TOUCH_MODE = 0x4;
916
917    /**
918     * This mask is set if an event was known to come from a trusted part
919     * of the system.  That is, the event is known to come from the user,
920     * and could not have been spoofed by a third party component.
921     */
922    public static final int FLAG_FROM_SYSTEM = 0x8;
923
924    /**
925     * This mask is used for compatibility, to identify enter keys that are
926     * coming from an IME whose enter key has been auto-labelled "next" or
927     * "done".  This allows TextView to dispatch these as normal enter keys
928     * for old applications, but still do the appropriate action when
929     * receiving them.
930     */
931    public static final int FLAG_EDITOR_ACTION = 0x10;
932
933    /**
934     * When associated with up key events, this indicates that the key press
935     * has been canceled.  Typically this is used with virtual touch screen
936     * keys, where the user can slide from the virtual key area on to the
937     * display: in that case, the application will receive a canceled up
938     * event and should not perform the action normally associated with the
939     * key.  Note that for this to work, the application can not perform an
940     * action for a key until it receives an up or the long press timeout has
941     * expired.
942     */
943    public static final int FLAG_CANCELED = 0x20;
944
945    /**
946     * This key event was generated by a virtual (on-screen) hard key area.
947     * Typically this is an area of the touchscreen, outside of the regular
948     * display, dedicated to "hardware" buttons.
949     */
950    public static final int FLAG_VIRTUAL_HARD_KEY = 0x40;
951
952    /**
953     * This flag is set for the first key repeat that occurs after the
954     * long press timeout.
955     */
956    public static final int FLAG_LONG_PRESS = 0x80;
957
958    /**
959     * Set when a key event has {@link #FLAG_CANCELED} set because a long
960     * press action was executed while it was down.
961     */
962    public static final int FLAG_CANCELED_LONG_PRESS = 0x100;
963
964    /**
965     * Set for {@link #ACTION_UP} when this event's key code is still being
966     * tracked from its initial down.  That is, somebody requested that tracking
967     * started on the key down and a long press has not caused
968     * the tracking to be canceled.
969     */
970    public static final int FLAG_TRACKING = 0x200;
971
972    /**
973     * Private control to determine when an app is tracking a key sequence.
974     * @hide
975     */
976    public static final int FLAG_START_TRACKING = 0x40000000;
977
978    /**
979     * Returns the maximum keycode.
980     */
981    public static int getMaxKeyCode() {
982        return LAST_KEYCODE;
983    }
984
985    /**
986     * Get the character that is produced by putting accent on the character
987     * c.
988     * For example, getDeadChar('`', 'e') returns &egrave;.
989     */
990    public static int getDeadChar(int accent, int c) {
991        return KeyCharacterMap.getDeadChar(accent, c);
992    }
993
994    static final boolean DEBUG = false;
995    static final String TAG = "KeyEvent";
996
997    private int mMetaState;
998    private int mAction;
999    private int mKeyCode;
1000    private int mScanCode;
1001    private int mRepeatCount;
1002    private int mFlags;
1003    private long mDownTime;
1004    private long mEventTime;
1005    private String mCharacters;
1006
1007    public interface Callback {
1008        /**
1009         * Called when a key down event has occurred.  If you return true,
1010         * you can first call {@link KeyEvent#startTracking()
1011         * KeyEvent.startTracking()} to have the framework track the event
1012         * through its {@link #onKeyUp(int, KeyEvent)} and also call your
1013         * {@link #onKeyLongPress(int, KeyEvent)} if it occurs.
1014         *
1015         * @param keyCode The value in event.getKeyCode().
1016         * @param event Description of the key event.
1017         *
1018         * @return If you handled the event, return true.  If you want to allow
1019         *         the event to be handled by the next receiver, return false.
1020         */
1021        boolean onKeyDown(int keyCode, KeyEvent event);
1022
1023        /**
1024         * Called when a long press has occurred.  If you return true,
1025         * the final key up will have {@link KeyEvent#FLAG_CANCELED} and
1026         * {@link KeyEvent#FLAG_CANCELED_LONG_PRESS} set.  Note that in
1027         * order to receive this callback, someone in the event change
1028         * <em>must</em> return true from {@link #onKeyDown} <em>and</em>
1029         * call {@link KeyEvent#startTracking()} on the event.
1030         *
1031         * @param keyCode The value in event.getKeyCode().
1032         * @param event Description of the key event.
1033         *
1034         * @return If you handled the event, return true.  If you want to allow
1035         *         the event to be handled by the next receiver, return false.
1036         */
1037        boolean onKeyLongPress(int keyCode, KeyEvent event);
1038
1039        /**
1040         * Called when a key up event has occurred.
1041         *
1042         * @param keyCode The value in event.getKeyCode().
1043         * @param event Description of the key event.
1044         *
1045         * @return If you handled the event, return true.  If you want to allow
1046         *         the event to be handled by the next receiver, return false.
1047         */
1048        boolean onKeyUp(int keyCode, KeyEvent event);
1049
1050        /**
1051         * Called when multiple down/up pairs of the same key have occurred
1052         * in a row.
1053         *
1054         * @param keyCode The value in event.getKeyCode().
1055         * @param count Number of pairs as returned by event.getRepeatCount().
1056         * @param event Description of the key event.
1057         *
1058         * @return If you handled the event, return true.  If you want to allow
1059         *         the event to be handled by the next receiver, return false.
1060         */
1061        boolean onKeyMultiple(int keyCode, int count, KeyEvent event);
1062    }
1063
1064    static {
1065        if (META_SYMBOLIC_NAMES.length != 32) {
1066            throw new IllegalStateException(
1067                    "META_SYMBOLIC_NAMES array should contain exactly 32 entries.");
1068        }
1069        if (KEYCODE_SYMBOLIC_NAMES.length != LAST_KEYCODE + 1) {
1070            throw new IllegalStateException(
1071                    "KEYCODE_SYMBOLIC_NAMES array is out of sync with the keycode constants.");
1072        }
1073    }
1074
1075    /**
1076     * Create a new key event.
1077     *
1078     * @param action Action code: either {@link #ACTION_DOWN},
1079     * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1080     * @param code The key code.
1081     */
1082    public KeyEvent(int action, int code) {
1083        mAction = action;
1084        mKeyCode = code;
1085        mRepeatCount = 0;
1086    }
1087
1088    /**
1089     * Create a new key event.
1090     *
1091     * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1092     * at which this key code originally went down.
1093     * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1094     * at which this event happened.
1095     * @param action Action code: either {@link #ACTION_DOWN},
1096     * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1097     * @param code The key code.
1098     * @param repeat A repeat count for down events (> 0 if this is after the
1099     * initial down) or event count for multiple events.
1100     */
1101    public KeyEvent(long downTime, long eventTime, int action,
1102                    int code, int repeat) {
1103        mDownTime = downTime;
1104        mEventTime = eventTime;
1105        mAction = action;
1106        mKeyCode = code;
1107        mRepeatCount = repeat;
1108    }
1109
1110    /**
1111     * Create a new key event.
1112     *
1113     * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1114     * at which this key code originally went down.
1115     * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1116     * at which this event happened.
1117     * @param action Action code: either {@link #ACTION_DOWN},
1118     * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1119     * @param code The key code.
1120     * @param repeat A repeat count for down events (> 0 if this is after the
1121     * initial down) or event count for multiple events.
1122     * @param metaState Flags indicating which meta keys are currently pressed.
1123     */
1124    public KeyEvent(long downTime, long eventTime, int action,
1125                    int code, int repeat, int metaState) {
1126        mDownTime = downTime;
1127        mEventTime = eventTime;
1128        mAction = action;
1129        mKeyCode = code;
1130        mRepeatCount = repeat;
1131        mMetaState = metaState;
1132    }
1133
1134    /**
1135     * Create a new key event.
1136     *
1137     * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1138     * at which this key code originally went down.
1139     * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1140     * at which this event happened.
1141     * @param action Action code: either {@link #ACTION_DOWN},
1142     * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1143     * @param code The key code.
1144     * @param repeat A repeat count for down events (> 0 if this is after the
1145     * initial down) or event count for multiple events.
1146     * @param metaState Flags indicating which meta keys are currently pressed.
1147     * @param deviceId The device ID that generated the key event.
1148     * @param scancode Raw device scan code of the event.
1149     */
1150    public KeyEvent(long downTime, long eventTime, int action,
1151                    int code, int repeat, int metaState,
1152                    int deviceId, int scancode) {
1153        mDownTime = downTime;
1154        mEventTime = eventTime;
1155        mAction = action;
1156        mKeyCode = code;
1157        mRepeatCount = repeat;
1158        mMetaState = metaState;
1159        mDeviceId = deviceId;
1160        mScanCode = scancode;
1161    }
1162
1163    /**
1164     * Create a new key event.
1165     *
1166     * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1167     * at which this key code originally went down.
1168     * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1169     * at which this event happened.
1170     * @param action Action code: either {@link #ACTION_DOWN},
1171     * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1172     * @param code The key code.
1173     * @param repeat A repeat count for down events (> 0 if this is after the
1174     * initial down) or event count for multiple events.
1175     * @param metaState Flags indicating which meta keys are currently pressed.
1176     * @param deviceId The device ID that generated the key event.
1177     * @param scancode Raw device scan code of the event.
1178     * @param flags The flags for this key event
1179     */
1180    public KeyEvent(long downTime, long eventTime, int action,
1181                    int code, int repeat, int metaState,
1182                    int deviceId, int scancode, int flags) {
1183        mDownTime = downTime;
1184        mEventTime = eventTime;
1185        mAction = action;
1186        mKeyCode = code;
1187        mRepeatCount = repeat;
1188        mMetaState = metaState;
1189        mDeviceId = deviceId;
1190        mScanCode = scancode;
1191        mFlags = flags;
1192    }
1193
1194    /**
1195     * Create a new key event.
1196     *
1197     * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1198     * at which this key code originally went down.
1199     * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1200     * at which this event happened.
1201     * @param action Action code: either {@link #ACTION_DOWN},
1202     * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1203     * @param code The key code.
1204     * @param repeat A repeat count for down events (> 0 if this is after the
1205     * initial down) or event count for multiple events.
1206     * @param metaState Flags indicating which meta keys are currently pressed.
1207     * @param deviceId The device ID that generated the key event.
1208     * @param scancode Raw device scan code of the event.
1209     * @param flags The flags for this key event
1210     * @param source The input source such as {@link InputDevice#SOURCE_KEYBOARD}.
1211     */
1212    public KeyEvent(long downTime, long eventTime, int action,
1213                    int code, int repeat, int metaState,
1214                    int deviceId, int scancode, int flags, int source) {
1215        mDownTime = downTime;
1216        mEventTime = eventTime;
1217        mAction = action;
1218        mKeyCode = code;
1219        mRepeatCount = repeat;
1220        mMetaState = metaState;
1221        mDeviceId = deviceId;
1222        mScanCode = scancode;
1223        mFlags = flags;
1224        mSource = source;
1225    }
1226
1227    /**
1228     * Create a new key event for a string of characters.  The key code,
1229     * action, repeat count and source will automatically be set to
1230     * {@link #KEYCODE_UNKNOWN}, {@link #ACTION_MULTIPLE}, 0, and
1231     * {@link InputDevice#SOURCE_KEYBOARD} for you.
1232     *
1233     * @param time The time (in {@link android.os.SystemClock#uptimeMillis})
1234     * at which this event occured.
1235     * @param characters The string of characters.
1236     * @param deviceId The device ID that generated the key event.
1237     * @param flags The flags for this key event
1238     */
1239    public KeyEvent(long time, String characters, int deviceId, int flags) {
1240        mDownTime = time;
1241        mEventTime = time;
1242        mCharacters = characters;
1243        mAction = ACTION_MULTIPLE;
1244        mKeyCode = KEYCODE_UNKNOWN;
1245        mRepeatCount = 0;
1246        mDeviceId = deviceId;
1247        mFlags = flags;
1248        mSource = InputDevice.SOURCE_KEYBOARD;
1249    }
1250
1251    /**
1252     * Make an exact copy of an existing key event.
1253     */
1254    public KeyEvent(KeyEvent origEvent) {
1255        mDownTime = origEvent.mDownTime;
1256        mEventTime = origEvent.mEventTime;
1257        mAction = origEvent.mAction;
1258        mKeyCode = origEvent.mKeyCode;
1259        mRepeatCount = origEvent.mRepeatCount;
1260        mMetaState = origEvent.mMetaState;
1261        mDeviceId = origEvent.mDeviceId;
1262        mSource = origEvent.mSource;
1263        mScanCode = origEvent.mScanCode;
1264        mFlags = origEvent.mFlags;
1265        mCharacters = origEvent.mCharacters;
1266    }
1267
1268    /**
1269     * Copy an existing key event, modifying its time and repeat count.
1270     *
1271     * @deprecated Use {@link #changeTimeRepeat(KeyEvent, long, int)}
1272     * instead.
1273     *
1274     * @param origEvent The existing event to be copied.
1275     * @param eventTime The new event time
1276     * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1277     * @param newRepeat The new repeat count of the event.
1278     */
1279    @Deprecated
1280    public KeyEvent(KeyEvent origEvent, long eventTime, int newRepeat) {
1281        mDownTime = origEvent.mDownTime;
1282        mEventTime = eventTime;
1283        mAction = origEvent.mAction;
1284        mKeyCode = origEvent.mKeyCode;
1285        mRepeatCount = newRepeat;
1286        mMetaState = origEvent.mMetaState;
1287        mDeviceId = origEvent.mDeviceId;
1288        mSource = origEvent.mSource;
1289        mScanCode = origEvent.mScanCode;
1290        mFlags = origEvent.mFlags;
1291        mCharacters = origEvent.mCharacters;
1292    }
1293
1294    /**
1295     * Create a new key event that is the same as the given one, but whose
1296     * event time and repeat count are replaced with the given value.
1297     *
1298     * @param event The existing event to be copied.  This is not modified.
1299     * @param eventTime The new event time
1300     * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1301     * @param newRepeat The new repeat count of the event.
1302     */
1303    public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime,
1304            int newRepeat) {
1305        return new KeyEvent(event, eventTime, newRepeat);
1306    }
1307
1308    /**
1309     * Create a new key event that is the same as the given one, but whose
1310     * event time and repeat count are replaced with the given value.
1311     *
1312     * @param event The existing event to be copied.  This is not modified.
1313     * @param eventTime The new event time
1314     * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1315     * @param newRepeat The new repeat count of the event.
1316     * @param newFlags New flags for the event, replacing the entire value
1317     * in the original event.
1318     */
1319    public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime,
1320            int newRepeat, int newFlags) {
1321        KeyEvent ret = new KeyEvent(event);
1322        ret.mEventTime = eventTime;
1323        ret.mRepeatCount = newRepeat;
1324        ret.mFlags = newFlags;
1325        return ret;
1326    }
1327
1328    /**
1329     * Copy an existing key event, modifying its action.
1330     *
1331     * @param origEvent The existing event to be copied.
1332     * @param action The new action code of the event.
1333     */
1334    private KeyEvent(KeyEvent origEvent, int action) {
1335        mDownTime = origEvent.mDownTime;
1336        mEventTime = origEvent.mEventTime;
1337        mAction = action;
1338        mKeyCode = origEvent.mKeyCode;
1339        mRepeatCount = origEvent.mRepeatCount;
1340        mMetaState = origEvent.mMetaState;
1341        mDeviceId = origEvent.mDeviceId;
1342        mSource = origEvent.mSource;
1343        mScanCode = origEvent.mScanCode;
1344        mFlags = origEvent.mFlags;
1345        // Don't copy mCharacters, since one way or the other we'll lose it
1346        // when changing the action.
1347    }
1348
1349    /**
1350     * Create a new key event that is the same as the given one, but whose
1351     * action is replaced with the given value.
1352     *
1353     * @param event The existing event to be copied.  This is not modified.
1354     * @param action The new action code of the event.
1355     */
1356    public static KeyEvent changeAction(KeyEvent event, int action) {
1357        return new KeyEvent(event, action);
1358    }
1359
1360    /**
1361     * Create a new key event that is the same as the given one, but whose
1362     * flags are replaced with the given value.
1363     *
1364     * @param event The existing event to be copied.  This is not modified.
1365     * @param flags The new flags constant.
1366     */
1367    public static KeyEvent changeFlags(KeyEvent event, int flags) {
1368        event = new KeyEvent(event);
1369        event.mFlags = flags;
1370        return event;
1371    }
1372
1373    /**
1374     * Don't use in new code, instead explicitly check
1375     * {@link #getAction()}.
1376     *
1377     * @return If the action is ACTION_DOWN, returns true; else false.
1378     *
1379     * @deprecated
1380     * @hide
1381     */
1382    @Deprecated public final boolean isDown() {
1383        return mAction == ACTION_DOWN;
1384    }
1385
1386    /**
1387     * Is this a system key?  System keys can not be used for menu shortcuts.
1388     *
1389     * TODO: this information should come from a table somewhere.
1390     * TODO: should the dpad keys be here?  arguably, because they also shouldn't be menu shortcuts
1391     */
1392    public final boolean isSystem() {
1393        return native_isSystemKey(mKeyCode);
1394    }
1395
1396    /** @hide */
1397    public final boolean hasDefaultAction() {
1398        return native_hasDefaultAction(mKeyCode);
1399    }
1400
1401
1402    /**
1403     * <p>Returns the state of the meta keys.</p>
1404     *
1405     * @return an integer in which each bit set to 1 represents a pressed
1406     *         meta key
1407     *
1408     * @see #isAltPressed()
1409     * @see #isShiftPressed()
1410     * @see #isSymPressed()
1411     * @see #isCtrlPressed()
1412     * @see #isMetaPressed()
1413     * @see #isFunctionPressed()
1414     * @see #isCapsLockOn()
1415     * @see #isNumLockOn()
1416     * @see #isScrollLockOn()
1417     * @see #META_ALT_ON
1418     * @see #META_ALT_LEFT_ON
1419     * @see #META_ALT_RIGHT_ON
1420     * @see #META_SHIFT_ON
1421     * @see #META_SHIFT_LEFT_ON
1422     * @see #META_SHIFT_RIGHT_ON
1423     * @see #META_SYM_ON
1424     * @see #META_FUNCTION_ON
1425     * @see #META_CTRL_ON
1426     * @see #META_CTRL_LEFT_ON
1427     * @see #META_CTRL_RIGHT_ON
1428     * @see #META_META_ON
1429     * @see #META_META_LEFT_ON
1430     * @see #META_META_RIGHT_ON
1431     * @see #META_CAPS_LOCK_ON
1432     * @see #META_NUM_LOCK_ON
1433     * @see #META_SCROLL_LOCK_ON
1434     */
1435    public final int getMetaState() {
1436        return mMetaState;
1437    }
1438
1439    /**
1440     * Returns the flags for this key event.
1441     *
1442     * @see #FLAG_WOKE_HERE
1443     */
1444    public final int getFlags() {
1445        return mFlags;
1446    }
1447
1448    /**
1449     * Returns true if this key code is a modifier key.
1450     *
1451     * @return whether the provided keyCode is one of
1452     * {@link #KEYCODE_SHIFT_LEFT} {@link #KEYCODE_SHIFT_RIGHT},
1453     * {@link #KEYCODE_ALT_LEFT}, {@link #KEYCODE_ALT_RIGHT},
1454     * {@link #KEYCODE_SYM}, {@link #KEYCODE_NUM}, {@link #KEYCODE_FUNCTION},
1455     * {@link #KEYCODE_CTRL_LEFT}, {@link #KEYCODE_CTRL_RIGHT},
1456     * {@link #KEYCODE_META_LEFT}, or {@link #KEYCODE_META_RIGHT}.
1457     */
1458    public static boolean isModifierKey(int keyCode) {
1459        switch (keyCode) {
1460            case KEYCODE_SHIFT_LEFT:
1461            case KEYCODE_SHIFT_RIGHT:
1462            case KEYCODE_ALT_LEFT:
1463            case KEYCODE_ALT_RIGHT:
1464            case KEYCODE_SYM:
1465            case KEYCODE_NUM:
1466            case KEYCODE_FUNCTION:
1467            case KEYCODE_CTRL_LEFT:
1468            case KEYCODE_CTRL_RIGHT:
1469            case KEYCODE_META_LEFT:
1470            case KEYCODE_META_RIGHT:
1471                return true;
1472            default:
1473                return false;
1474        }
1475    }
1476
1477    /**
1478     * <p>Returns the pressed state of the ALT meta key.</p>
1479     *
1480     * @return true if the ALT key is pressed, false otherwise
1481     *
1482     * @see #KEYCODE_ALT_LEFT
1483     * @see #KEYCODE_ALT_RIGHT
1484     * @see #META_ALT_ON
1485     */
1486    public final boolean isAltPressed() {
1487        return (mMetaState & META_ALT_ON) != 0;
1488    }
1489
1490    /**
1491     * <p>Returns the pressed state of the SHIFT meta key.</p>
1492     *
1493     * @return true if the SHIFT key is pressed, false otherwise
1494     *
1495     * @see #KEYCODE_SHIFT_LEFT
1496     * @see #KEYCODE_SHIFT_RIGHT
1497     * @see #META_SHIFT_ON
1498     */
1499    public final boolean isShiftPressed() {
1500        return (mMetaState & META_SHIFT_ON) != 0;
1501    }
1502
1503    /**
1504     * <p>Returns the pressed state of the SYM meta key.</p>
1505     *
1506     * @return true if the SYM key is pressed, false otherwise
1507     *
1508     * @see #KEYCODE_SYM
1509     * @see #META_SYM_ON
1510     */
1511    public final boolean isSymPressed() {
1512        return (mMetaState & META_SYM_ON) != 0;
1513    }
1514
1515    /**
1516     * <p>Returns the pressed state of the CTRL meta key.</p>
1517     *
1518     * @return true if the CTRL key is pressed, false otherwise
1519     *
1520     * @see #KEYCODE_CTRL_LEFT
1521     * @see #KEYCODE_CTRL_RIGHT
1522     * @see #META_CTRL_ON
1523     */
1524    public final boolean isCtrlPressed() {
1525        return (mMetaState & META_CTRL_ON) != 0;
1526    }
1527
1528    /**
1529     * <p>Returns the pressed state of the META meta key.</p>
1530     *
1531     * @return true if the META key is pressed, false otherwise
1532     *
1533     * @see #KEYCODE_META_LEFT
1534     * @see #KEYCODE_META_RIGHT
1535     * @see #META_META_ON
1536     */
1537    public final boolean isMetaPressed() {
1538        return (mMetaState & META_META_ON) != 0;
1539    }
1540
1541    /**
1542     * <p>Returns the pressed state of the FUNCTION meta key.</p>
1543     *
1544     * @return true if the FUNCTION key is pressed, false otherwise
1545     *
1546     * @see #KEYCODE_FUNCTION
1547     * @see #META_FUNCTION_ON
1548     */
1549    public final boolean isFunctionPressed() {
1550        return (mMetaState & META_FUNCTION_ON) != 0;
1551    }
1552
1553    /**
1554     * <p>Returns the locked state of the CAPS LOCK meta key.</p>
1555     *
1556     * @return true if the CAPS LOCK key is on, false otherwise
1557     *
1558     * @see #KEYCODE_CAPS_LOCK
1559     * @see #META_CAPS_LOCK_ON
1560     */
1561    public final boolean isCapsLockOn() {
1562        return (mMetaState & META_CAPS_LOCK_ON) != 0;
1563    }
1564
1565    /**
1566     * <p>Returns the locked state of the NUM LOCK meta key.</p>
1567     *
1568     * @return true if the NUM LOCK key is on, false otherwise
1569     *
1570     * @see #KEYCODE_NUM_LOCK
1571     * @see #META_NUM_LOCK_ON
1572     */
1573    public final boolean isNumLockOn() {
1574        return (mMetaState & META_NUM_LOCK_ON) != 0;
1575    }
1576
1577    /**
1578     * <p>Returns the locked state of the SCROLL LOCK meta key.</p>
1579     *
1580     * @return true if the SCROLL LOCK key is on, false otherwise
1581     *
1582     * @see #KEYCODE_SCROLL_LOCK
1583     * @see #META_SCROLL_LOCK_ON
1584     */
1585    public final boolean isScrollLockOn() {
1586        return (mMetaState & META_SCROLL_LOCK_ON) != 0;
1587    }
1588
1589    /**
1590     * Retrieve the action of this key event.  May be either
1591     * {@link #ACTION_DOWN}, {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1592     *
1593     * @return The event action: ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.
1594     */
1595    public final int getAction() {
1596        return mAction;
1597    }
1598
1599    /**
1600     * For {@link #ACTION_UP} events, indicates that the event has been
1601     * canceled as per {@link #FLAG_CANCELED}.
1602     */
1603    public final boolean isCanceled() {
1604        return (mFlags&FLAG_CANCELED) != 0;
1605    }
1606
1607    /**
1608     * Call this during {@link Callback#onKeyDown} to have the system track
1609     * the key through its final up (possibly including a long press).  Note
1610     * that only one key can be tracked at a time -- if another key down
1611     * event is received while a previous one is being tracked, tracking is
1612     * stopped on the previous event.
1613     */
1614    public final void startTracking() {
1615        mFlags |= FLAG_START_TRACKING;
1616    }
1617
1618    /**
1619     * For {@link #ACTION_UP} events, indicates that the event is still being
1620     * tracked from its initial down event as per
1621     * {@link #FLAG_TRACKING}.
1622     */
1623    public final boolean isTracking() {
1624        return (mFlags&FLAG_TRACKING) != 0;
1625    }
1626
1627    /**
1628     * For {@link #ACTION_DOWN} events, indicates that the event has been
1629     * canceled as per {@link #FLAG_LONG_PRESS}.
1630     */
1631    public final boolean isLongPress() {
1632        return (mFlags&FLAG_LONG_PRESS) != 0;
1633    }
1634
1635    /**
1636     * Retrieve the key code of the key event.  This is the physical key that
1637     * was pressed, <em>not</em> the Unicode character.
1638     *
1639     * @return The key code of the event.
1640     */
1641    public final int getKeyCode() {
1642        return mKeyCode;
1643    }
1644
1645    /**
1646     * For the special case of a {@link #ACTION_MULTIPLE} event with key
1647     * code of {@link #KEYCODE_UNKNOWN}, this is a raw string of characters
1648     * associated with the event.  In all other cases it is null.
1649     *
1650     * @return Returns a String of 1 or more characters associated with
1651     * the event.
1652     */
1653    public final String getCharacters() {
1654        return mCharacters;
1655    }
1656
1657    /**
1658     * Retrieve the hardware key id of this key event.  These values are not
1659     * reliable and vary from device to device.
1660     *
1661     * {@more}
1662     * Mostly this is here for debugging purposes.
1663     */
1664    public final int getScanCode() {
1665        return mScanCode;
1666    }
1667
1668    /**
1669     * Retrieve the repeat count of the event.  For both key up and key down
1670     * events, this is the number of times the key has repeated with the first
1671     * down starting at 0 and counting up from there.  For multiple key
1672     * events, this is the number of down/up pairs that have occurred.
1673     *
1674     * @return The number of times the key has repeated.
1675     */
1676    public final int getRepeatCount() {
1677        return mRepeatCount;
1678    }
1679
1680    /**
1681     * Retrieve the time of the most recent key down event,
1682     * in the {@link android.os.SystemClock#uptimeMillis} time base.  If this
1683     * is a down event, this will be the same as {@link #getEventTime()}.
1684     * Note that when chording keys, this value is the down time of the
1685     * most recently pressed key, which may <em>not</em> be the same physical
1686     * key of this event.
1687     *
1688     * @return Returns the most recent key down time, in the
1689     * {@link android.os.SystemClock#uptimeMillis} time base
1690     */
1691    public final long getDownTime() {
1692        return mDownTime;
1693    }
1694
1695    /**
1696     * Retrieve the time this event occurred,
1697     * in the {@link android.os.SystemClock#uptimeMillis} time base.
1698     *
1699     * @return Returns the time this event occurred,
1700     * in the {@link android.os.SystemClock#uptimeMillis} time base.
1701     */
1702    public final long getEventTime() {
1703        return mEventTime;
1704    }
1705
1706    /**
1707     * Renamed to {@link #getDeviceId}.
1708     *
1709     * @hide
1710     * @deprecated
1711     */
1712    public final int getKeyboardDevice() {
1713        return mDeviceId;
1714    }
1715
1716    /**
1717     * Get the primary character for this key.  In other words, the label
1718     * that is physically printed on it.
1719     */
1720    public char getDisplayLabel() {
1721        return KeyCharacterMap.load(mDeviceId).getDisplayLabel(mKeyCode);
1722    }
1723
1724    /**
1725     * <p>
1726     * Returns the Unicode character that the key would produce.
1727     * </p><p>
1728     * Returns 0 if the key is not one that is used to type Unicode
1729     * characters.
1730     * </p><p>
1731     * If the return value has bit
1732     * {@link KeyCharacterMap#COMBINING_ACCENT}
1733     * set, the key is a "dead key" that should be combined with another to
1734     * actually produce a character -- see {@link #getDeadChar} --
1735     * after masking with
1736     * {@link KeyCharacterMap#COMBINING_ACCENT_MASK}.
1737     * </p>
1738     */
1739    public int getUnicodeChar() {
1740        return getUnicodeChar(mMetaState);
1741    }
1742
1743    /**
1744     * <p>
1745     * Returns the Unicode character that the key would produce.
1746     * </p><p>
1747     * Returns 0 if the key is not one that is used to type Unicode
1748     * characters.
1749     * </p><p>
1750     * If the return value has bit
1751     * {@link KeyCharacterMap#COMBINING_ACCENT}
1752     * set, the key is a "dead key" that should be combined with another to
1753     * actually produce a character -- see {@link #getDeadChar} -- after masking
1754     * with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}.
1755     * </p>
1756     */
1757    public int getUnicodeChar(int meta) {
1758        return KeyCharacterMap.load(mDeviceId).get(mKeyCode, meta);
1759    }
1760
1761    /**
1762     * Get the characters conversion data for the key event..
1763     *
1764     * @param results a {@link KeyData} that will be filled with the results.
1765     *
1766     * @return whether the key was mapped or not.  If the key was not mapped,
1767     *         results is not modified.
1768     */
1769    public boolean getKeyData(KeyData results) {
1770        return KeyCharacterMap.load(mDeviceId).getKeyData(mKeyCode, results);
1771    }
1772
1773    /**
1774     * The same as {@link #getMatch(char[],int) getMatch(chars, 0)}.
1775     */
1776    public char getMatch(char[] chars) {
1777        return getMatch(chars, 0);
1778    }
1779
1780    /**
1781     * If one of the chars in the array can be generated by the keyCode of this
1782     * key event, return the char; otherwise return '\0'.
1783     * @param chars the characters to try to find
1784     * @param modifiers the modifier bits to prefer.  If any of these bits
1785     *                  are set, if there are multiple choices, that could
1786     *                  work, the one for this modifier will be set.
1787     */
1788    public char getMatch(char[] chars, int modifiers) {
1789        return KeyCharacterMap.load(mDeviceId).getMatch(mKeyCode, chars, modifiers);
1790    }
1791
1792    /**
1793     * Gets the number or symbol associated with the key.  The character value
1794     * is returned, not the numeric value.  If the key is not a number, but is
1795     * a symbol, the symbol is retuned.
1796     */
1797    public char getNumber() {
1798        return KeyCharacterMap.load(mDeviceId).getNumber(mKeyCode);
1799    }
1800
1801    /**
1802     * Does the key code of this key produce a glyph?
1803     */
1804    public boolean isPrintingKey() {
1805        return KeyCharacterMap.load(mDeviceId).isPrintingKey(mKeyCode);
1806    }
1807
1808    /**
1809     * @deprecated Use {@link #dispatch(Callback, DispatcherState, Object)} instead.
1810     */
1811    @Deprecated
1812    public final boolean dispatch(Callback receiver) {
1813        return dispatch(receiver, null, null);
1814    }
1815
1816    /**
1817     * Deliver this key event to a {@link Callback} interface.  If this is
1818     * an ACTION_MULTIPLE event and it is not handled, then an attempt will
1819     * be made to deliver a single normal event.
1820     *
1821     * @param receiver The Callback that will be given the event.
1822     * @param state State information retained across events.
1823     * @param target The target of the dispatch, for use in tracking.
1824     *
1825     * @return The return value from the Callback method that was called.
1826     */
1827    public final boolean dispatch(Callback receiver, DispatcherState state,
1828            Object target) {
1829        switch (mAction) {
1830            case ACTION_DOWN: {
1831                mFlags &= ~FLAG_START_TRACKING;
1832                if (DEBUG) Log.v(TAG, "Key down to " + target + " in " + state
1833                        + ": " + this);
1834                boolean res = receiver.onKeyDown(mKeyCode, this);
1835                if (state != null) {
1836                    if (res && mRepeatCount == 0 && (mFlags&FLAG_START_TRACKING) != 0) {
1837                        if (DEBUG) Log.v(TAG, "  Start tracking!");
1838                        state.startTracking(this, target);
1839                    } else if (isLongPress() && state.isTracking(this)) {
1840                        try {
1841                            if (receiver.onKeyLongPress(mKeyCode, this)) {
1842                                if (DEBUG) Log.v(TAG, "  Clear from long press!");
1843                                state.performedLongPress(this);
1844                                res = true;
1845                            }
1846                        } catch (AbstractMethodError e) {
1847                        }
1848                    }
1849                }
1850                return res;
1851            }
1852            case ACTION_UP:
1853                if (DEBUG) Log.v(TAG, "Key up to " + target + " in " + state
1854                        + ": " + this);
1855                if (state != null) {
1856                    state.handleUpEvent(this);
1857                }
1858                return receiver.onKeyUp(mKeyCode, this);
1859            case ACTION_MULTIPLE:
1860                final int count = mRepeatCount;
1861                final int code = mKeyCode;
1862                if (receiver.onKeyMultiple(code, count, this)) {
1863                    return true;
1864                }
1865                if (code != KeyEvent.KEYCODE_UNKNOWN) {
1866                    mAction = ACTION_DOWN;
1867                    mRepeatCount = 0;
1868                    boolean handled = receiver.onKeyDown(code, this);
1869                    if (handled) {
1870                        mAction = ACTION_UP;
1871                        receiver.onKeyUp(code, this);
1872                    }
1873                    mAction = ACTION_MULTIPLE;
1874                    mRepeatCount = count;
1875                    return handled;
1876                }
1877                return false;
1878        }
1879        return false;
1880    }
1881
1882    /**
1883     * Use with {@link KeyEvent#dispatch(Callback, DispatcherState, Object)}
1884     * for more advanced key dispatching, such as long presses.
1885     */
1886    public static class DispatcherState {
1887        int mDownKeyCode;
1888        Object mDownTarget;
1889        SparseIntArray mActiveLongPresses = new SparseIntArray();
1890
1891        /**
1892         * Reset back to initial state.
1893         */
1894        public void reset() {
1895            if (DEBUG) Log.v(TAG, "Reset: " + this);
1896            mDownKeyCode = 0;
1897            mDownTarget = null;
1898            mActiveLongPresses.clear();
1899        }
1900
1901        /**
1902         * Stop any tracking associated with this target.
1903         */
1904        public void reset(Object target) {
1905            if (mDownTarget == target) {
1906                if (DEBUG) Log.v(TAG, "Reset in " + target + ": " + this);
1907                mDownKeyCode = 0;
1908                mDownTarget = null;
1909            }
1910        }
1911
1912        /**
1913         * Start tracking the key code associated with the given event.  This
1914         * can only be called on a key down.  It will allow you to see any
1915         * long press associated with the key, and will result in
1916         * {@link KeyEvent#isTracking} return true on the long press and up
1917         * events.
1918         *
1919         * <p>This is only needed if you are directly dispatching events, rather
1920         * than handling them in {@link Callback#onKeyDown}.
1921         */
1922        public void startTracking(KeyEvent event, Object target) {
1923            if (event.getAction() != ACTION_DOWN) {
1924                throw new IllegalArgumentException(
1925                        "Can only start tracking on a down event");
1926            }
1927            if (DEBUG) Log.v(TAG, "Start trackingt in " + target + ": " + this);
1928            mDownKeyCode = event.getKeyCode();
1929            mDownTarget = target;
1930        }
1931
1932        /**
1933         * Return true if the key event is for a key code that is currently
1934         * being tracked by the dispatcher.
1935         */
1936        public boolean isTracking(KeyEvent event) {
1937            return mDownKeyCode == event.getKeyCode();
1938        }
1939
1940        /**
1941         * Keep track of the given event's key code as having performed an
1942         * action with a long press, so no action should occur on the up.
1943         * <p>This is only needed if you are directly dispatching events, rather
1944         * than handling them in {@link Callback#onKeyLongPress}.
1945         */
1946        public void performedLongPress(KeyEvent event) {
1947            mActiveLongPresses.put(event.getKeyCode(), 1);
1948        }
1949
1950        /**
1951         * Handle key up event to stop tracking.  This resets the dispatcher state,
1952         * and updates the key event state based on it.
1953         * <p>This is only needed if you are directly dispatching events, rather
1954         * than handling them in {@link Callback#onKeyUp}.
1955         */
1956        public void handleUpEvent(KeyEvent event) {
1957            final int keyCode = event.getKeyCode();
1958            if (DEBUG) Log.v(TAG, "Handle key up " + event + ": " + this);
1959            int index = mActiveLongPresses.indexOfKey(keyCode);
1960            if (index >= 0) {
1961                if (DEBUG) Log.v(TAG, "  Index: " + index);
1962                event.mFlags |= FLAG_CANCELED | FLAG_CANCELED_LONG_PRESS;
1963                mActiveLongPresses.removeAt(index);
1964            }
1965            if (mDownKeyCode == keyCode) {
1966                if (DEBUG) Log.v(TAG, "  Tracking!");
1967                event.mFlags |= FLAG_TRACKING;
1968                mDownKeyCode = 0;
1969                mDownTarget = null;
1970            }
1971        }
1972    }
1973
1974    @Override
1975    public String toString() {
1976        return "KeyEvent{action=" + actionToString(mAction)
1977                + " keycode=" + keyCodeToString(mKeyCode)
1978                + " scancode=" + mScanCode
1979                + " meta=" + metaStateToString(mMetaState)
1980                + " flags=0x" + Integer.toHexString(mFlags)
1981                + " repeat=" + mRepeatCount
1982                + " device=" + mDeviceId
1983                + " source=0x" + Integer.toHexString(mSource)
1984                + "}";
1985    }
1986
1987    /**
1988     * Returns a string that represents the symbolic name of the specified action
1989     * such as "ACTION_DOWN", or "35" (if unknown).
1990     *
1991     * @param action The action.
1992     * @return The symbolic name of the specified action.
1993     * @hide
1994     */
1995    public static String actionToString(int action) {
1996        switch (action) {
1997            case ACTION_DOWN:
1998                return "ACTION_DOWN";
1999            case ACTION_UP:
2000                return "ACTION_UP";
2001            case ACTION_MULTIPLE:
2002                return "ACTION_MULTIPLE";
2003            default:
2004                return Integer.toString(action);
2005        }
2006    }
2007
2008    /**
2009     * Returns a string that represents the symbolic name of the specified keycode
2010     * such as "KEYCODE_A", "KEYCODE_DPAD_UP", or "1001" (if unknown).
2011     *
2012     * @param keyCode The key code.
2013     * @return The symbolic name of the specified keycode.
2014     *
2015     * @see KeyCharacterMap#getDisplayLabel
2016     * @hide
2017     */
2018    public static String keyCodeToString(int keyCode) {
2019        if (keyCode >= 0 && keyCode < KEYCODE_SYMBOLIC_NAMES.length) {
2020            return KEYCODE_SYMBOLIC_NAMES[keyCode];
2021        }
2022        return Integer.toString(keyCode);
2023    }
2024
2025    /**
2026     * Gets a keycode by its symbolic name such as "KEYCODE_A" or "1001" (if unknown).
2027     *
2028     * @param symbolicName The symbolic name of the keycode.
2029     * @return The keycode or -1 if not found.
2030     * @see #keycodeToString
2031     * @hide
2032     */
2033    public static int keyCodeFromString(String symbolicName) {
2034        if (symbolicName == null) {
2035            throw new IllegalArgumentException("symbolicName must not be null");
2036        }
2037
2038        final int count = KEYCODE_SYMBOLIC_NAMES.length;
2039        for (int i = 0; i < count; i++) {
2040            if (symbolicName.equals(KEYCODE_SYMBOLIC_NAMES[i])) {
2041                return i;
2042            }
2043        }
2044
2045        try {
2046            return Integer.parseInt(symbolicName,10);
2047        } catch (NumberFormatException ex) {
2048            return -1;
2049        }
2050    }
2051
2052    /**
2053     * Returns a string that represents the symbolic name of the specified combined meta
2054     * key modifier state flags such as "0", "META_SHIFT_ON",
2055     * "META_ALT_ON|META_SHIFT_ON" or "0x10000000" (if unknown).
2056     *
2057     * @param metaState The meta state.
2058     * @return The symbolic name of the specified combined meta state flags.
2059     * @hide
2060     */
2061    public static String metaStateToString(int metaState) {
2062        if (metaState == 0) {
2063            return "0";
2064        }
2065        StringBuilder result = null;
2066        int i = 0;
2067        while (metaState != 0) {
2068            final boolean isSet = (metaState & 1) != 0;
2069            metaState >>>= 1; // unsigned shift!
2070            if (isSet) {
2071                final String name = META_SYMBOLIC_NAMES[i];
2072                if (result == null) {
2073                    if (metaState == 0) {
2074                        return name;
2075                    }
2076                    result = new StringBuilder(name);
2077                } else {
2078                    result.append('|');
2079                    result.append(name);
2080                }
2081            }
2082            i += 1;
2083        }
2084        return result.toString();
2085    }
2086
2087    public static final Parcelable.Creator<KeyEvent> CREATOR
2088            = new Parcelable.Creator<KeyEvent>() {
2089        public KeyEvent createFromParcel(Parcel in) {
2090            in.readInt(); // skip token, we already know this is a KeyEvent
2091            return KeyEvent.createFromParcelBody(in);
2092        }
2093
2094        public KeyEvent[] newArray(int size) {
2095            return new KeyEvent[size];
2096        }
2097    };
2098
2099    /** @hide */
2100    public static KeyEvent createFromParcelBody(Parcel in) {
2101        return new KeyEvent(in);
2102    }
2103
2104    private KeyEvent(Parcel in) {
2105        readBaseFromParcel(in);
2106
2107        mAction = in.readInt();
2108        mKeyCode = in.readInt();
2109        mRepeatCount = in.readInt();
2110        mMetaState = in.readInt();
2111        mScanCode = in.readInt();
2112        mFlags = in.readInt();
2113        mDownTime = in.readLong();
2114        mEventTime = in.readLong();
2115    }
2116
2117    public void writeToParcel(Parcel out, int flags) {
2118        out.writeInt(PARCEL_TOKEN_KEY_EVENT);
2119
2120        writeBaseToParcel(out);
2121
2122        out.writeInt(mAction);
2123        out.writeInt(mKeyCode);
2124        out.writeInt(mRepeatCount);
2125        out.writeInt(mMetaState);
2126        out.writeInt(mScanCode);
2127        out.writeInt(mFlags);
2128        out.writeLong(mDownTime);
2129        out.writeLong(mEventTime);
2130    }
2131
2132    private native boolean native_isSystemKey(int keyCode);
2133    private native boolean native_hasDefaultAction(int keyCode);
2134}
2135