KeyEvent.java revision 9812aed2765c671e6c3f5255ac1b8a2fe0e72ef6
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.text.method.MetaKeyKeyListener;
22import android.util.Log;
23import android.util.Slog;
24import android.util.SparseArray;
25import android.util.SparseIntArray;
26import android.view.KeyCharacterMap;
27import android.view.KeyCharacterMap.KeyData;
28
29/**
30 * Object used to report key and button events.
31 * <p>
32 * Each key press is described by a sequence of key events.  A key press
33 * starts with a key event with {@link #ACTION_DOWN}.  If the key is held
34 * sufficiently long that it repeats, then the initial down is followed
35 * additional key events with {@link #ACTION_DOWN} and a non-zero value for
36 * {@link #getRepeatCount()}.  The last key event is a {@link #ACTION_UP}
37 * for the key up.  If the key press is canceled, the key up event will have the
38 * {@link #FLAG_CANCELED} flag set.
39 * </p><p>
40 * Key events are generally accompanied by a key code ({@link #getKeyCode()}),
41 * scan code ({@link #getScanCode()}) and meta state ({@link #getMetaState()}).
42 * Key code constants are defined in this class.  Scan code constants are raw
43 * device-specific codes obtained from the OS and so are not generally meaningful
44 * to applications unless interpreted using the {@link KeyCharacterMap}.
45 * Meta states describe the pressed state of key modifiers
46 * such as {@link #META_SHIFT_ON} or {@link #META_ALT_ON}.
47 * </p><p>
48 * Key codes typically correspond one-to-one with individual keys on an input device.
49 * Many keys and key combinations serve quite different functions on different
50 * input devices so care must be taken when interpreting them.  Always use the
51 * {@link KeyCharacterMap} associated with the input device when mapping keys
52 * to characters.  Be aware that there may be multiple key input devices active
53 * at the same time and each will have its own key character map.
54 * </p><p>
55 * When interacting with an IME, the framework may deliver key events
56 * with the special action {@link #ACTION_MULTIPLE} that either specifies
57 * that single repeated key code or a sequence of characters to insert.
58 * </p><p>
59 * In general, the framework cannot guarantee that the key events it delivers
60 * to a view always constitute complete key sequences since some events may be dropped
61 * or modified by containing views before they are delivered.  The view implementation
62 * should be prepared to handle {@link #FLAG_CANCELED} and should tolerate anomalous
63 * situations such as receiving a new {@link #ACTION_DOWN} without first having
64 * received an {@link #ACTION_UP} for the prior key press.
65 * </p><p>
66 * Refer to {@link InputDevice} for more information about how different kinds of
67 * input devices and sources represent keys and buttons.
68 * </p>
69 */
70public class KeyEvent extends InputEvent implements Parcelable {
71    /** Key code constant: Unknown key code. */
72    public static final int KEYCODE_UNKNOWN         = 0;
73    /** Key code constant: Soft Left key.
74     * Usually situated below the display on phones and used as a multi-function
75     * feature key for selecting a software defined function shown on the bottom left
76     * of the display. */
77    public static final int KEYCODE_SOFT_LEFT       = 1;
78    /** Key code constant: Soft Right key.
79     * Usually situated below the display on phones and used as a multi-function
80     * feature key for selecting a software defined function shown on the bottom right
81     * of the display. */
82    public static final int KEYCODE_SOFT_RIGHT      = 2;
83    /** Key code constant: Home key.
84     * This key is handled by the framework and is never delivered to applications. */
85    public static final int KEYCODE_HOME            = 3;
86    /** Key code constant: Back key. */
87    public static final int KEYCODE_BACK            = 4;
88    /** Key code constant: Call key. */
89    public static final int KEYCODE_CALL            = 5;
90    /** Key code constant: End Call key. */
91    public static final int KEYCODE_ENDCALL         = 6;
92    /** Key code constant: '0' key. */
93    public static final int KEYCODE_0               = 7;
94    /** Key code constant: '1' key. */
95    public static final int KEYCODE_1               = 8;
96    /** Key code constant: '2' key. */
97    public static final int KEYCODE_2               = 9;
98    /** Key code constant: '3' key. */
99    public static final int KEYCODE_3               = 10;
100    /** Key code constant: '4' key. */
101    public static final int KEYCODE_4               = 11;
102    /** Key code constant: '5' key. */
103    public static final int KEYCODE_5               = 12;
104    /** Key code constant: '6' key. */
105    public static final int KEYCODE_6               = 13;
106    /** Key code constant: '7' key. */
107    public static final int KEYCODE_7               = 14;
108    /** Key code constant: '8' key. */
109    public static final int KEYCODE_8               = 15;
110    /** Key code constant: '9' key. */
111    public static final int KEYCODE_9               = 16;
112    /** Key code constant: '*' key. */
113    public static final int KEYCODE_STAR            = 17;
114    /** Key code constant: '#' key. */
115    public static final int KEYCODE_POUND           = 18;
116    /** Key code constant: Directional Pad Up key.
117     * May also be synthesized from trackball motions. */
118    public static final int KEYCODE_DPAD_UP         = 19;
119    /** Key code constant: Directional Pad Down key.
120     * May also be synthesized from trackball motions. */
121    public static final int KEYCODE_DPAD_DOWN       = 20;
122    /** Key code constant: Directional Pad Left key.
123     * May also be synthesized from trackball motions. */
124    public static final int KEYCODE_DPAD_LEFT       = 21;
125    /** Key code constant: Directional Pad Right key.
126     * May also be synthesized from trackball motions. */
127    public static final int KEYCODE_DPAD_RIGHT      = 22;
128    /** Key code constant: Directional Pad Center key.
129     * May also be synthesized from trackball motions. */
130    public static final int KEYCODE_DPAD_CENTER     = 23;
131    /** Key code constant: Volume Up key.
132     * Adjusts the speaker volume up. */
133    public static final int KEYCODE_VOLUME_UP       = 24;
134    /** Key code constant: Volume Down key.
135     * Adjusts the speaker volume down. */
136    public static final int KEYCODE_VOLUME_DOWN     = 25;
137    /** Key code constant: Power key. */
138    public static final int KEYCODE_POWER           = 26;
139    /** Key code constant: Camera key.
140     * Used to launch a camera application or take pictures. */
141    public static final int KEYCODE_CAMERA          = 27;
142    /** Key code constant: Clear key. */
143    public static final int KEYCODE_CLEAR           = 28;
144    /** Key code constant: 'A' key. */
145    public static final int KEYCODE_A               = 29;
146    /** Key code constant: 'B' key. */
147    public static final int KEYCODE_B               = 30;
148    /** Key code constant: 'C' key. */
149    public static final int KEYCODE_C               = 31;
150    /** Key code constant: 'D' key. */
151    public static final int KEYCODE_D               = 32;
152    /** Key code constant: 'E' key. */
153    public static final int KEYCODE_E               = 33;
154    /** Key code constant: 'F' key. */
155    public static final int KEYCODE_F               = 34;
156    /** Key code constant: 'G' key. */
157    public static final int KEYCODE_G               = 35;
158    /** Key code constant: 'H' key. */
159    public static final int KEYCODE_H               = 36;
160    /** Key code constant: 'I' key. */
161    public static final int KEYCODE_I               = 37;
162    /** Key code constant: 'J' key. */
163    public static final int KEYCODE_J               = 38;
164    /** Key code constant: 'K' key. */
165    public static final int KEYCODE_K               = 39;
166    /** Key code constant: 'L' key. */
167    public static final int KEYCODE_L               = 40;
168    /** Key code constant: 'M' key. */
169    public static final int KEYCODE_M               = 41;
170    /** Key code constant: 'N' key. */
171    public static final int KEYCODE_N               = 42;
172    /** Key code constant: 'O' key. */
173    public static final int KEYCODE_O               = 43;
174    /** Key code constant: 'P' key. */
175    public static final int KEYCODE_P               = 44;
176    /** Key code constant: 'Q' key. */
177    public static final int KEYCODE_Q               = 45;
178    /** Key code constant: 'R' key. */
179    public static final int KEYCODE_R               = 46;
180    /** Key code constant: 'S' key. */
181    public static final int KEYCODE_S               = 47;
182    /** Key code constant: 'T' key. */
183    public static final int KEYCODE_T               = 48;
184    /** Key code constant: 'U' key. */
185    public static final int KEYCODE_U               = 49;
186    /** Key code constant: 'V' key. */
187    public static final int KEYCODE_V               = 50;
188    /** Key code constant: 'W' key. */
189    public static final int KEYCODE_W               = 51;
190    /** Key code constant: 'X' key. */
191    public static final int KEYCODE_X               = 52;
192    /** Key code constant: 'Y' key. */
193    public static final int KEYCODE_Y               = 53;
194    /** Key code constant: 'Z' key. */
195    public static final int KEYCODE_Z               = 54;
196    /** Key code constant: ',' key. */
197    public static final int KEYCODE_COMMA           = 55;
198    /** Key code constant: '.' key. */
199    public static final int KEYCODE_PERIOD          = 56;
200    /** Key code constant: Left Alt modifier key. */
201    public static final int KEYCODE_ALT_LEFT        = 57;
202    /** Key code constant: Right Alt modifier key. */
203    public static final int KEYCODE_ALT_RIGHT       = 58;
204    /** Key code constant: Left Shift modifier key. */
205    public static final int KEYCODE_SHIFT_LEFT      = 59;
206    /** Key code constant: Right Shift modifier key. */
207    public static final int KEYCODE_SHIFT_RIGHT     = 60;
208    /** Key code constant: Tab key. */
209    public static final int KEYCODE_TAB             = 61;
210    /** Key code constant: Space key. */
211    public static final int KEYCODE_SPACE           = 62;
212    /** Key code constant: Symbol modifier key.
213     * Used to enter alternate symbols. */
214    public static final int KEYCODE_SYM             = 63;
215    /** Key code constant: Explorer special function key.
216     * Used to launch a browser application. */
217    public static final int KEYCODE_EXPLORER        = 64;
218    /** Key code constant: Envelope special function key.
219     * Used to launch a mail application. */
220    public static final int KEYCODE_ENVELOPE        = 65;
221    /** Key code constant: Enter key. */
222    public static final int KEYCODE_ENTER           = 66;
223    /** Key code constant: Backspace key.
224     * Deletes characters before the insertion point, unlike {@link #KEYCODE_FORWARD_DEL}. */
225    public static final int KEYCODE_DEL             = 67;
226    /** Key code constant: '`' (backtick) key. */
227    public static final int KEYCODE_GRAVE           = 68;
228    /** Key code constant: '-'. */
229    public static final int KEYCODE_MINUS           = 69;
230    /** Key code constant: '=' key. */
231    public static final int KEYCODE_EQUALS          = 70;
232    /** Key code constant: '[' key. */
233    public static final int KEYCODE_LEFT_BRACKET    = 71;
234    /** Key code constant: ']' key. */
235    public static final int KEYCODE_RIGHT_BRACKET   = 72;
236    /** Key code constant: '\' key. */
237    public static final int KEYCODE_BACKSLASH       = 73;
238    /** Key code constant: ';' key. */
239    public static final int KEYCODE_SEMICOLON       = 74;
240    /** Key code constant: ''' (apostrophe) key. */
241    public static final int KEYCODE_APOSTROPHE      = 75;
242    /** Key code constant: '/' key. */
243    public static final int KEYCODE_SLASH           = 76;
244    /** Key code constant: '@' key. */
245    public static final int KEYCODE_AT              = 77;
246    /** Key code constant: Number modifier key.
247     * Used to enter numeric symbols.
248     * This key is not Num Lock; it is more like {@link #KEYCODE_ALT_LEFT} and is
249     * interpreted as an ALT key by {@link android.text.method.MetaKeyKeyListener}. */
250    public static final int KEYCODE_NUM             = 78;
251    /** Key code constant: Headset Hook key.
252     * Used to hang up calls and stop media. */
253    public static final int KEYCODE_HEADSETHOOK     = 79;
254    /** Key code constant: Camera Focus key.
255     * Used to focus the camera. */
256    public static final int KEYCODE_FOCUS           = 80;   // *Camera* focus
257    /** Key code constant: '+' key. */
258    public static final int KEYCODE_PLUS            = 81;
259    /** Key code constant: Menu key. */
260    public static final int KEYCODE_MENU            = 82;
261    /** Key code constant: Notification key. */
262    public static final int KEYCODE_NOTIFICATION    = 83;
263    /** Key code constant: Search key. */
264    public static final int KEYCODE_SEARCH          = 84;
265    /** Key code constant: Play/Pause media key. */
266    public static final int KEYCODE_MEDIA_PLAY_PAUSE= 85;
267    /** Key code constant: Stop media key. */
268    public static final int KEYCODE_MEDIA_STOP      = 86;
269    /** Key code constant: Play Next media key. */
270    public static final int KEYCODE_MEDIA_NEXT      = 87;
271    /** Key code constant: Play Previous media key. */
272    public static final int KEYCODE_MEDIA_PREVIOUS  = 88;
273    /** Key code constant: Rewind media key. */
274    public static final int KEYCODE_MEDIA_REWIND    = 89;
275    /** Key code constant: Fast Forward media key. */
276    public static final int KEYCODE_MEDIA_FAST_FORWARD = 90;
277    /** Key code constant: Mute key.
278     * Mutes the microphone, unlike {@link #KEYCODE_VOLUME_MUTE}. */
279    public static final int KEYCODE_MUTE            = 91;
280    /** Key code constant: Page Up key. */
281    public static final int KEYCODE_PAGE_UP         = 92;
282    /** Key code constant: Page Down key. */
283    public static final int KEYCODE_PAGE_DOWN       = 93;
284    /** Key code constant: Picture Symbols modifier key.
285     * Used to switch symbol sets (Emoji, Kao-moji). */
286    public static final int KEYCODE_PICTSYMBOLS     = 94;   // switch symbol-sets (Emoji,Kao-moji)
287    /** Key code constant: Switch Charset modifier key.
288     * Used to switch character sets (Kanji, Katakana). */
289    public static final int KEYCODE_SWITCH_CHARSET  = 95;   // switch char-sets (Kanji,Katakana)
290    /** Key code constant: A Button key.
291     * On a game controller, the A button should be either the button labeled A
292     * or the first button on the upper row of controller buttons. */
293    public static final int KEYCODE_BUTTON_A        = 96;
294    /** Key code constant: B Button key.
295     * On a game controller, the B button should be either the button labeled B
296     * or the second button on the upper row of controller buttons. */
297    public static final int KEYCODE_BUTTON_B        = 97;
298    /** Key code constant: C Button key.
299     * On a game controller, the C button should be either the button labeled C
300     * or the third button on the upper row of controller buttons. */
301    public static final int KEYCODE_BUTTON_C        = 98;
302    /** Key code constant: X Button key.
303     * On a game controller, the X button should be either the button labeled X
304     * or the first button on the lower row of controller buttons. */
305    public static final int KEYCODE_BUTTON_X        = 99;
306    /** Key code constant: Y Button key.
307     * On a game controller, the Y button should be either the button labeled Y
308     * or the second button on the lower row of controller buttons. */
309    public static final int KEYCODE_BUTTON_Y        = 100;
310    /** Key code constant: Z Button key.
311     * On a game controller, the Z button should be either the button labeled Z
312     * or the third button on the lower row of controller buttons. */
313    public static final int KEYCODE_BUTTON_Z        = 101;
314    /** Key code constant: L1 Button key.
315     * On a game controller, the L1 button should be either the button labeled L1 (or L)
316     * or the top left trigger button. */
317    public static final int KEYCODE_BUTTON_L1       = 102;
318    /** Key code constant: R1 Button key.
319     * On a game controller, the R1 button should be either the button labeled R1 (or R)
320     * or the top right trigger button. */
321    public static final int KEYCODE_BUTTON_R1       = 103;
322    /** Key code constant: L2 Button key.
323     * On a game controller, the L2 button should be either the button labeled L2
324     * or the bottom left trigger button. */
325    public static final int KEYCODE_BUTTON_L2       = 104;
326    /** Key code constant: R2 Button key.
327     * On a game controller, the R2 button should be either the button labeled R2
328     * or the bottom right trigger button. */
329    public static final int KEYCODE_BUTTON_R2       = 105;
330    /** Key code constant: Left Thumb Button key.
331     * On a game controller, the left thumb button indicates that the left (or only)
332     * joystick is pressed. */
333    public static final int KEYCODE_BUTTON_THUMBL   = 106;
334    /** Key code constant: Right Thumb Button key.
335     * On a game controller, the right thumb button indicates that the right
336     * joystick is pressed. */
337    public static final int KEYCODE_BUTTON_THUMBR   = 107;
338    /** Key code constant: Start Button key.
339     * On a game controller, the button labeled Start. */
340    public static final int KEYCODE_BUTTON_START    = 108;
341    /** Key code constant: Select Button key.
342     * On a game controller, the button labeled Select. */
343    public static final int KEYCODE_BUTTON_SELECT   = 109;
344    /** Key code constant: Mode Button key.
345     * On a game controller, the button labeled Mode. */
346    public static final int KEYCODE_BUTTON_MODE     = 110;
347    /** Key code constant: Escape key. */
348    public static final int KEYCODE_ESCAPE          = 111;
349    /** Key code constant: Forward Delete key.
350     * Deletes characters ahead of the insertion point, unlike {@link #KEYCODE_DEL}. */
351    public static final int KEYCODE_FORWARD_DEL     = 112;
352    /** Key code constant: Left Control modifier key. */
353    public static final int KEYCODE_CTRL_LEFT       = 113;
354    /** Key code constant: Right Control modifier key. */
355    public static final int KEYCODE_CTRL_RIGHT      = 114;
356    /** Key code constant: Caps Lock key. */
357    public static final int KEYCODE_CAPS_LOCK       = 115;
358    /** Key code constant: Scroll Lock key. */
359    public static final int KEYCODE_SCROLL_LOCK     = 116;
360    /** Key code constant: Left Meta modifier key. */
361    public static final int KEYCODE_META_LEFT       = 117;
362    /** Key code constant: Right Meta modifier key. */
363    public static final int KEYCODE_META_RIGHT      = 118;
364    /** Key code constant: Function modifier key. */
365    public static final int KEYCODE_FUNCTION        = 119;
366    /** Key code constant: System Request / Print Screen key. */
367    public static final int KEYCODE_SYSRQ           = 120;
368    /** Key code constant: Break / Pause key. */
369    public static final int KEYCODE_BREAK           = 121;
370    /** Key code constant: Home Movement key.
371     * Used for scrolling or moving the cursor around to the start of a line
372     * or to the top of a list. */
373    public static final int KEYCODE_MOVE_HOME       = 122;
374    /** Key code constant: End Movement key.
375     * Used for scrolling or moving the cursor around to the end of a line
376     * or to the bottom of a list. */
377    public static final int KEYCODE_MOVE_END        = 123;
378    /** Key code constant: Insert key.
379     * Toggles insert / overwrite edit mode. */
380    public static final int KEYCODE_INSERT          = 124;
381    /** Key code constant: Forward key.
382     * Navigates forward in the history stack.  Complement of {@link #KEYCODE_BACK}. */
383    public static final int KEYCODE_FORWARD         = 125;
384    /** Key code constant: Play media key. */
385    public static final int KEYCODE_MEDIA_PLAY      = 126;
386    /** Key code constant: Pause media key. */
387    public static final int KEYCODE_MEDIA_PAUSE     = 127;
388    /** Key code constant: Close media key.
389     * May be used to close a CD tray, for example. */
390    public static final int KEYCODE_MEDIA_CLOSE     = 128;
391    /** Key code constant: Eject media key.
392     * May be used to eject a CD tray, for example. */
393    public static final int KEYCODE_MEDIA_EJECT     = 129;
394    /** Key code constant: Record media key. */
395    public static final int KEYCODE_MEDIA_RECORD    = 130;
396    /** Key code constant: F1 key. */
397    public static final int KEYCODE_F1              = 131;
398    /** Key code constant: F2 key. */
399    public static final int KEYCODE_F2              = 132;
400    /** Key code constant: F3 key. */
401    public static final int KEYCODE_F3              = 133;
402    /** Key code constant: F4 key. */
403    public static final int KEYCODE_F4              = 134;
404    /** Key code constant: F5 key. */
405    public static final int KEYCODE_F5              = 135;
406    /** Key code constant: F6 key. */
407    public static final int KEYCODE_F6              = 136;
408    /** Key code constant: F7 key. */
409    public static final int KEYCODE_F7              = 137;
410    /** Key code constant: F8 key. */
411    public static final int KEYCODE_F8              = 138;
412    /** Key code constant: F9 key. */
413    public static final int KEYCODE_F9              = 139;
414    /** Key code constant: F10 key. */
415    public static final int KEYCODE_F10             = 140;
416    /** Key code constant: F11 key. */
417    public static final int KEYCODE_F11             = 141;
418    /** Key code constant: F12 key. */
419    public static final int KEYCODE_F12             = 142;
420    /** Key code constant: Num Lock key.
421     * This is the Num Lock key; it is different from {@link #KEYCODE_NUM}.
422     * This key alters the behavior of other keys on the numeric keypad. */
423    public static final int KEYCODE_NUM_LOCK        = 143;
424    /** Key code constant: Numeric keypad '0' key. */
425    public static final int KEYCODE_NUMPAD_0        = 144;
426    /** Key code constant: Numeric keypad '1' key. */
427    public static final int KEYCODE_NUMPAD_1        = 145;
428    /** Key code constant: Numeric keypad '2' key. */
429    public static final int KEYCODE_NUMPAD_2        = 146;
430    /** Key code constant: Numeric keypad '3' key. */
431    public static final int KEYCODE_NUMPAD_3        = 147;
432    /** Key code constant: Numeric keypad '4' key. */
433    public static final int KEYCODE_NUMPAD_4        = 148;
434    /** Key code constant: Numeric keypad '5' key. */
435    public static final int KEYCODE_NUMPAD_5        = 149;
436    /** Key code constant: Numeric keypad '6' key. */
437    public static final int KEYCODE_NUMPAD_6        = 150;
438    /** Key code constant: Numeric keypad '7' key. */
439    public static final int KEYCODE_NUMPAD_7        = 151;
440    /** Key code constant: Numeric keypad '8' key. */
441    public static final int KEYCODE_NUMPAD_8        = 152;
442    /** Key code constant: Numeric keypad '9' key. */
443    public static final int KEYCODE_NUMPAD_9        = 153;
444    /** Key code constant: Numeric keypad '/' key (for division). */
445    public static final int KEYCODE_NUMPAD_DIVIDE   = 154;
446    /** Key code constant: Numeric keypad '*' key (for multiplication). */
447    public static final int KEYCODE_NUMPAD_MULTIPLY = 155;
448    /** Key code constant: Numeric keypad '-' key (for subtraction). */
449    public static final int KEYCODE_NUMPAD_SUBTRACT = 156;
450    /** Key code constant: Numeric keypad '+' key (for addition). */
451    public static final int KEYCODE_NUMPAD_ADD      = 157;
452    /** Key code constant: Numeric keypad '.' key (for decimals or digit grouping). */
453    public static final int KEYCODE_NUMPAD_DOT      = 158;
454    /** Key code constant: Numeric keypad ',' key (for decimals or digit grouping). */
455    public static final int KEYCODE_NUMPAD_COMMA    = 159;
456    /** Key code constant: Numeric keypad Enter key. */
457    public static final int KEYCODE_NUMPAD_ENTER    = 160;
458    /** Key code constant: Numeric keypad '=' key. */
459    public static final int KEYCODE_NUMPAD_EQUALS   = 161;
460    /** Key code constant: Numeric keypad '(' key. */
461    public static final int KEYCODE_NUMPAD_LEFT_PAREN = 162;
462    /** Key code constant: Numeric keypad ')' key. */
463    public static final int KEYCODE_NUMPAD_RIGHT_PAREN = 163;
464    /** Key code constant: Volume Mute key.
465     * Mutes the speaker, unlike {@link #KEYCODE_MUTE}.
466     * This key should normally be implemented as a toggle such that the first press
467     * mutes the speaker and the second press restores the original volume. */
468    public static final int KEYCODE_VOLUME_MUTE     = 164;
469    /** Key code constant: Info key.
470     * Common on TV remotes to show additional information related to what is
471     * currently being viewed. */
472    public static final int KEYCODE_INFO            = 165;
473    /** Key code constant: Channel up key.
474     * On TV remotes, increments the television channel. */
475    public static final int KEYCODE_CHANNEL_UP      = 166;
476    /** Key code constant: Channel down key.
477     * On TV remotes, decrements the television channel. */
478    public static final int KEYCODE_CHANNEL_DOWN    = 167;
479    /** Key code constant: Zoom in key. */
480    public static final int KEYCODE_ZOOM_IN         = 168;
481    /** Key code constant: Zoom out key. */
482    public static final int KEYCODE_ZOOM_OUT        = 169;
483    /** Key code constant: TV key.
484     * On TV remotes, switches to viewing live TV. */
485    public static final int KEYCODE_TV              = 170;
486    /** Key code constant: Window key.
487     * On TV remotes, toggles picture-in-picture mode or other windowing functions. */
488    public static final int KEYCODE_WINDOW          = 171;
489    /** Key code constant: Guide key.
490     * On TV remotes, shows a programming guide. */
491    public static final int KEYCODE_GUIDE           = 172;
492    /** Key code constant: DVR key.
493     * On some TV remotes, switches to a DVR mode for recorded shows. */
494    public static final int KEYCODE_DVR             = 173;
495    /** Key code constant: Bookmark key.
496     * On some TV remotes, bookmarks content or web pages. */
497    public static final int KEYCODE_BOOKMARK        = 174;
498    /** Key code constant: Toggle captions key.
499     * Switches the mode for closed-captioning text, for example during television shows. */
500    public static final int KEYCODE_CAPTIONS        = 175;
501    /** Key code constant: Settings key.
502     * Starts the system settings activity. */
503    public static final int KEYCODE_SETTINGS        = 176;
504    /** Key code constant: TV power key.
505     * On TV remotes, toggles the power on a television screen. */
506    public static final int KEYCODE_TV_POWER        = 177;
507    /** Key code constant: TV input key.
508     * On TV remotes, switches the input on a television screen. */
509    public static final int KEYCODE_TV_INPUT        = 178;
510    /** Key code constant: Set-top-box power key.
511     * On TV remotes, toggles the power on an external Set-top-box. */
512    public static final int KEYCODE_STB_POWER       = 179;
513    /** Key code constant: Set-top-box input key.
514     * On TV remotes, switches the input mode on an external Set-top-box. */
515    public static final int KEYCODE_STB_INPUT       = 180;
516    /** Key code constant: A/V Receiver power key.
517     * On TV remotes, toggles the power on an external A/V Receiver. */
518    public static final int KEYCODE_AVR_POWER       = 181;
519    /** Key code constant: A/V Receiver input key.
520     * On TV remotes, switches the input mode on an external A/V Receiver. */
521    public static final int KEYCODE_AVR_INPUT       = 182;
522    /** Key code constant: Red "programmable" key.
523     * On TV remotes, acts as a contextual/programmable key. */
524    public static final int KEYCODE_PROG_RED        = 183;
525    /** Key code constant: Green "programmable" key.
526     * On TV remotes, actsas a contextual/programmable key. */
527    public static final int KEYCODE_PROG_GREEN      = 184;
528    /** Key code constant: Yellow "programmable" key.
529     * On TV remotes, acts as a contextual/programmable key. */
530    public static final int KEYCODE_PROG_YELLOW     = 185;
531    /** Key code constant: Blue "programmable" key.
532     * On TV remotes, acts as a contextual/programmable key. */
533    public static final int KEYCODE_PROG_BLUE       = 186;
534    /** Key code constant: App switch key.
535     * Should bring up the application switcher dialog. */
536    public static final int KEYCODE_APP_SWITCH      = 187;
537    /** Key code constant: Generic Game Pad Button #1.*/
538    public static final int KEYCODE_BUTTON_1        = 188;
539    /** Key code constant: Generic Game Pad Button #2.*/
540    public static final int KEYCODE_BUTTON_2        = 189;
541    /** Key code constant: Generic Game Pad Button #3.*/
542    public static final int KEYCODE_BUTTON_3        = 190;
543    /** Key code constant: Generic Game Pad Button #4.*/
544    public static final int KEYCODE_BUTTON_4        = 191;
545    /** Key code constant: Generic Game Pad Button #5.*/
546    public static final int KEYCODE_BUTTON_5        = 192;
547    /** Key code constant: Generic Game Pad Button #6.*/
548    public static final int KEYCODE_BUTTON_6        = 193;
549    /** Key code constant: Generic Game Pad Button #7.*/
550    public static final int KEYCODE_BUTTON_7        = 194;
551    /** Key code constant: Generic Game Pad Button #8.*/
552    public static final int KEYCODE_BUTTON_8        = 195;
553    /** Key code constant: Generic Game Pad Button #9.*/
554    public static final int KEYCODE_BUTTON_9        = 196;
555    /** Key code constant: Generic Game Pad Button #10.*/
556    public static final int KEYCODE_BUTTON_10       = 197;
557    /** Key code constant: Generic Game Pad Button #11.*/
558    public static final int KEYCODE_BUTTON_11       = 198;
559    /** Key code constant: Generic Game Pad Button #12.*/
560    public static final int KEYCODE_BUTTON_12       = 199;
561    /** Key code constant: Generic Game Pad Button #13.*/
562    public static final int KEYCODE_BUTTON_13       = 200;
563    /** Key code constant: Generic Game Pad Button #14.*/
564    public static final int KEYCODE_BUTTON_14       = 201;
565    /** Key code constant: Generic Game Pad Button #15.*/
566    public static final int KEYCODE_BUTTON_15       = 202;
567    /** Key code constant: Generic Game Pad Button #16.*/
568    public static final int KEYCODE_BUTTON_16       = 203;
569    /** Key code constant: Language Switch key.
570     * Toggles the current input language such as switching between English and Japanese on
571     * a QWERTY keyboard.  On some devices, the same function may be performed by
572     * pressing Shift+Spacebar. */
573    public static final int KEYCODE_LANGUAGE_SWITCH = 204;
574    /** Key code constant: Manner Mode key.
575     * Toggles silent or vibrate mode on and off to make the device behave more politely
576     * in certain settings such as on a crowded train.  On some devices, the key may only
577     * operate when long-pressed. */
578    public static final int KEYCODE_MANNER_MODE     = 205;
579    /** Key code constant: 3D Mode key.
580     * Toggles the display between 2D and 3D mode. */
581    public static final int KEYCODE_3D_MODE         = 206;
582
583    private static final int LAST_KEYCODE           = KEYCODE_BUTTON_16;
584
585    // NOTE: If you add a new keycode here you must also add it to:
586    //  isSystem()
587    //  native/include/android/keycodes.h
588    //  frameworks/base/include/ui/KeycodeLabels.h
589    //  external/webkit/WebKit/android/plugins/ANPKeyCodes.h
590    //  frameworks/base/core/res/res/values/attrs.xml
591    //  emulator?
592    //
593    //  Also Android currently does not reserve code ranges for vendor-
594    //  specific key codes.  If you have new key codes to have, you
595    //  MUST contribute a patch to the open source project to define
596    //  those new codes.  This is intended to maintain a consistent
597    //  set of key code definitions across all Android devices.
598
599    // Symbolic names of all key codes.
600    private static final SparseArray<String> KEYCODE_SYMBOLIC_NAMES = new SparseArray<String>();
601    private static void populateKeycodeSymbolicNames() {
602        SparseArray<String> names = KEYCODE_SYMBOLIC_NAMES;
603        names.append(KEYCODE_UNKNOWN, "KEYCODE_UNKNOWN");
604        names.append(KEYCODE_SOFT_LEFT, "KEYCODE_SOFT_LEFT");
605        names.append(KEYCODE_SOFT_RIGHT, "KEYCODE_SOFT_RIGHT");
606        names.append(KEYCODE_HOME, "KEYCODE_HOME");
607        names.append(KEYCODE_BACK, "KEYCODE_BACK");
608        names.append(KEYCODE_CALL, "KEYCODE_CALL");
609        names.append(KEYCODE_ENDCALL, "KEYCODE_ENDCALL");
610        names.append(KEYCODE_0, "KEYCODE_0");
611        names.append(KEYCODE_1, "KEYCODE_1");
612        names.append(KEYCODE_2, "KEYCODE_2");
613        names.append(KEYCODE_3, "KEYCODE_3");
614        names.append(KEYCODE_4, "KEYCODE_4");
615        names.append(KEYCODE_5, "KEYCODE_5");
616        names.append(KEYCODE_6, "KEYCODE_6");
617        names.append(KEYCODE_7, "KEYCODE_7");
618        names.append(KEYCODE_8, "KEYCODE_8");
619        names.append(KEYCODE_9, "KEYCODE_9");
620        names.append(KEYCODE_STAR, "KEYCODE_STAR");
621        names.append(KEYCODE_POUND, "KEYCODE_POUND");
622        names.append(KEYCODE_DPAD_UP, "KEYCODE_DPAD_UP");
623        names.append(KEYCODE_DPAD_DOWN, "KEYCODE_DPAD_DOWN");
624        names.append(KEYCODE_DPAD_LEFT, "KEYCODE_DPAD_LEFT");
625        names.append(KEYCODE_DPAD_RIGHT, "KEYCODE_DPAD_RIGHT");
626        names.append(KEYCODE_DPAD_CENTER, "KEYCODE_DPAD_CENTER");
627        names.append(KEYCODE_VOLUME_UP, "KEYCODE_VOLUME_UP");
628        names.append(KEYCODE_VOLUME_DOWN, "KEYCODE_VOLUME_DOWN");
629        names.append(KEYCODE_POWER, "KEYCODE_POWER");
630        names.append(KEYCODE_CAMERA, "KEYCODE_CAMERA");
631        names.append(KEYCODE_CLEAR, "KEYCODE_CLEAR");
632        names.append(KEYCODE_A, "KEYCODE_A");
633        names.append(KEYCODE_B, "KEYCODE_B");
634        names.append(KEYCODE_C, "KEYCODE_C");
635        names.append(KEYCODE_D, "KEYCODE_D");
636        names.append(KEYCODE_E, "KEYCODE_E");
637        names.append(KEYCODE_F, "KEYCODE_F");
638        names.append(KEYCODE_G, "KEYCODE_G");
639        names.append(KEYCODE_H, "KEYCODE_H");
640        names.append(KEYCODE_I, "KEYCODE_I");
641        names.append(KEYCODE_J, "KEYCODE_J");
642        names.append(KEYCODE_K, "KEYCODE_K");
643        names.append(KEYCODE_L, "KEYCODE_L");
644        names.append(KEYCODE_M, "KEYCODE_M");
645        names.append(KEYCODE_N, "KEYCODE_N");
646        names.append(KEYCODE_O, "KEYCODE_O");
647        names.append(KEYCODE_P, "KEYCODE_P");
648        names.append(KEYCODE_Q, "KEYCODE_Q");
649        names.append(KEYCODE_R, "KEYCODE_R");
650        names.append(KEYCODE_S, "KEYCODE_S");
651        names.append(KEYCODE_T, "KEYCODE_T");
652        names.append(KEYCODE_U, "KEYCODE_U");
653        names.append(KEYCODE_V, "KEYCODE_V");
654        names.append(KEYCODE_W, "KEYCODE_W");
655        names.append(KEYCODE_X, "KEYCODE_X");
656        names.append(KEYCODE_Y, "KEYCODE_Y");
657        names.append(KEYCODE_Z, "KEYCODE_Z");
658        names.append(KEYCODE_COMMA, "KEYCODE_COMMA");
659        names.append(KEYCODE_PERIOD, "KEYCODE_PERIOD");
660        names.append(KEYCODE_ALT_LEFT, "KEYCODE_ALT_LEFT");
661        names.append(KEYCODE_ALT_RIGHT, "KEYCODE_ALT_RIGHT");
662        names.append(KEYCODE_SHIFT_LEFT, "KEYCODE_SHIFT_LEFT");
663        names.append(KEYCODE_SHIFT_RIGHT, "KEYCODE_SHIFT_RIGHT");
664        names.append(KEYCODE_TAB, "KEYCODE_TAB");
665        names.append(KEYCODE_SPACE, "KEYCODE_SPACE");
666        names.append(KEYCODE_SYM, "KEYCODE_SYM");
667        names.append(KEYCODE_EXPLORER, "KEYCODE_EXPLORER");
668        names.append(KEYCODE_ENVELOPE, "KEYCODE_ENVELOPE");
669        names.append(KEYCODE_ENTER, "KEYCODE_ENTER");
670        names.append(KEYCODE_DEL, "KEYCODE_DEL");
671        names.append(KEYCODE_GRAVE, "KEYCODE_GRAVE");
672        names.append(KEYCODE_MINUS, "KEYCODE_MINUS");
673        names.append(KEYCODE_EQUALS, "KEYCODE_EQUALS");
674        names.append(KEYCODE_LEFT_BRACKET, "KEYCODE_LEFT_BRACKET");
675        names.append(KEYCODE_RIGHT_BRACKET, "KEYCODE_RIGHT_BRACKET");
676        names.append(KEYCODE_BACKSLASH, "KEYCODE_BACKSLASH");
677        names.append(KEYCODE_SEMICOLON, "KEYCODE_SEMICOLON");
678        names.append(KEYCODE_APOSTROPHE, "KEYCODE_APOSTROPHE");
679        names.append(KEYCODE_SLASH, "KEYCODE_SLASH");
680        names.append(KEYCODE_AT, "KEYCODE_AT");
681        names.append(KEYCODE_NUM, "KEYCODE_NUM");
682        names.append(KEYCODE_HEADSETHOOK, "KEYCODE_HEADSETHOOK");
683        names.append(KEYCODE_FOCUS, "KEYCODE_FOCUS");
684        names.append(KEYCODE_PLUS, "KEYCODE_PLUS");
685        names.append(KEYCODE_MENU, "KEYCODE_MENU");
686        names.append(KEYCODE_NOTIFICATION, "KEYCODE_NOTIFICATION");
687        names.append(KEYCODE_SEARCH, "KEYCODE_SEARCH");
688        names.append(KEYCODE_MEDIA_PLAY_PAUSE, "KEYCODE_MEDIA_PLAY_PAUSE");
689        names.append(KEYCODE_MEDIA_STOP, "KEYCODE_MEDIA_STOP");
690        names.append(KEYCODE_MEDIA_NEXT, "KEYCODE_MEDIA_NEXT");
691        names.append(KEYCODE_MEDIA_PREVIOUS, "KEYCODE_MEDIA_PREVIOUS");
692        names.append(KEYCODE_MEDIA_REWIND, "KEYCODE_MEDIA_REWIND");
693        names.append(KEYCODE_MEDIA_FAST_FORWARD, "KEYCODE_MEDIA_FAST_FORWARD");
694        names.append(KEYCODE_MUTE, "KEYCODE_MUTE");
695        names.append(KEYCODE_PAGE_UP, "KEYCODE_PAGE_UP");
696        names.append(KEYCODE_PAGE_DOWN, "KEYCODE_PAGE_DOWN");
697        names.append(KEYCODE_PICTSYMBOLS, "KEYCODE_PICTSYMBOLS");
698        names.append(KEYCODE_SWITCH_CHARSET, "KEYCODE_SWITCH_CHARSET");
699        names.append(KEYCODE_BUTTON_A, "KEYCODE_BUTTON_A");
700        names.append(KEYCODE_BUTTON_B, "KEYCODE_BUTTON_B");
701        names.append(KEYCODE_BUTTON_C, "KEYCODE_BUTTON_C");
702        names.append(KEYCODE_BUTTON_X, "KEYCODE_BUTTON_X");
703        names.append(KEYCODE_BUTTON_Y, "KEYCODE_BUTTON_Y");
704        names.append(KEYCODE_BUTTON_Z, "KEYCODE_BUTTON_Z");
705        names.append(KEYCODE_BUTTON_L1, "KEYCODE_BUTTON_L1");
706        names.append(KEYCODE_BUTTON_R1, "KEYCODE_BUTTON_R1");
707        names.append(KEYCODE_BUTTON_L2, "KEYCODE_BUTTON_L2");
708        names.append(KEYCODE_BUTTON_R2, "KEYCODE_BUTTON_R2");
709        names.append(KEYCODE_BUTTON_THUMBL, "KEYCODE_BUTTON_THUMBL");
710        names.append(KEYCODE_BUTTON_THUMBR, "KEYCODE_BUTTON_THUMBR");
711        names.append(KEYCODE_BUTTON_START, "KEYCODE_BUTTON_START");
712        names.append(KEYCODE_BUTTON_SELECT, "KEYCODE_BUTTON_SELECT");
713        names.append(KEYCODE_BUTTON_MODE, "KEYCODE_BUTTON_MODE");
714        names.append(KEYCODE_ESCAPE, "KEYCODE_ESCAPE");
715        names.append(KEYCODE_FORWARD_DEL, "KEYCODE_FORWARD_DEL");
716        names.append(KEYCODE_CTRL_LEFT, "KEYCODE_CTRL_LEFT");
717        names.append(KEYCODE_CTRL_RIGHT, "KEYCODE_CTRL_RIGHT");
718        names.append(KEYCODE_CAPS_LOCK, "KEYCODE_CAPS_LOCK");
719        names.append(KEYCODE_SCROLL_LOCK, "KEYCODE_SCROLL_LOCK");
720        names.append(KEYCODE_META_LEFT, "KEYCODE_META_LEFT");
721        names.append(KEYCODE_META_RIGHT, "KEYCODE_META_RIGHT");
722        names.append(KEYCODE_FUNCTION, "KEYCODE_FUNCTION");
723        names.append(KEYCODE_SYSRQ, "KEYCODE_SYSRQ");
724        names.append(KEYCODE_BREAK, "KEYCODE_BREAK");
725        names.append(KEYCODE_MOVE_HOME, "KEYCODE_MOVE_HOME");
726        names.append(KEYCODE_MOVE_END, "KEYCODE_MOVE_END");
727        names.append(KEYCODE_INSERT, "KEYCODE_INSERT");
728        names.append(KEYCODE_FORWARD, "KEYCODE_FORWARD");
729        names.append(KEYCODE_MEDIA_PLAY, "KEYCODE_MEDIA_PLAY");
730        names.append(KEYCODE_MEDIA_PAUSE, "KEYCODE_MEDIA_PAUSE");
731        names.append(KEYCODE_MEDIA_CLOSE, "KEYCODE_MEDIA_CLOSE");
732        names.append(KEYCODE_MEDIA_EJECT, "KEYCODE_MEDIA_EJECT");
733        names.append(KEYCODE_MEDIA_RECORD, "KEYCODE_MEDIA_RECORD");
734        names.append(KEYCODE_F1, "KEYCODE_F1");
735        names.append(KEYCODE_F2, "KEYCODE_F2");
736        names.append(KEYCODE_F3, "KEYCODE_F3");
737        names.append(KEYCODE_F4, "KEYCODE_F4");
738        names.append(KEYCODE_F5, "KEYCODE_F5");
739        names.append(KEYCODE_F6, "KEYCODE_F6");
740        names.append(KEYCODE_F7, "KEYCODE_F7");
741        names.append(KEYCODE_F8, "KEYCODE_F8");
742        names.append(KEYCODE_F9, "KEYCODE_F9");
743        names.append(KEYCODE_F10, "KEYCODE_F10");
744        names.append(KEYCODE_F11, "KEYCODE_F11");
745        names.append(KEYCODE_F12, "KEYCODE_F12");
746        names.append(KEYCODE_NUM_LOCK, "KEYCODE_NUM_LOCK");
747        names.append(KEYCODE_NUMPAD_0, "KEYCODE_NUMPAD_0");
748        names.append(KEYCODE_NUMPAD_1, "KEYCODE_NUMPAD_1");
749        names.append(KEYCODE_NUMPAD_2, "KEYCODE_NUMPAD_2");
750        names.append(KEYCODE_NUMPAD_3, "KEYCODE_NUMPAD_3");
751        names.append(KEYCODE_NUMPAD_4, "KEYCODE_NUMPAD_4");
752        names.append(KEYCODE_NUMPAD_5, "KEYCODE_NUMPAD_5");
753        names.append(KEYCODE_NUMPAD_6, "KEYCODE_NUMPAD_6");
754        names.append(KEYCODE_NUMPAD_7, "KEYCODE_NUMPAD_7");
755        names.append(KEYCODE_NUMPAD_8, "KEYCODE_NUMPAD_8");
756        names.append(KEYCODE_NUMPAD_9, "KEYCODE_NUMPAD_9");
757        names.append(KEYCODE_NUMPAD_DIVIDE, "KEYCODE_NUMPAD_DIVIDE");
758        names.append(KEYCODE_NUMPAD_MULTIPLY, "KEYCODE_NUMPAD_MULTIPLY");
759        names.append(KEYCODE_NUMPAD_SUBTRACT, "KEYCODE_NUMPAD_SUBTRACT");
760        names.append(KEYCODE_NUMPAD_ADD, "KEYCODE_NUMPAD_ADD");
761        names.append(KEYCODE_NUMPAD_DOT, "KEYCODE_NUMPAD_DOT");
762        names.append(KEYCODE_NUMPAD_COMMA, "KEYCODE_NUMPAD_COMMA");
763        names.append(KEYCODE_NUMPAD_ENTER, "KEYCODE_NUMPAD_ENTER");
764        names.append(KEYCODE_NUMPAD_EQUALS, "KEYCODE_NUMPAD_EQUALS");
765        names.append(KEYCODE_NUMPAD_LEFT_PAREN, "KEYCODE_NUMPAD_LEFT_PAREN");
766        names.append(KEYCODE_NUMPAD_RIGHT_PAREN, "KEYCODE_NUMPAD_RIGHT_PAREN");
767        names.append(KEYCODE_VOLUME_MUTE, "KEYCODE_VOLUME_MUTE");
768        names.append(KEYCODE_INFO, "KEYCODE_INFO");
769        names.append(KEYCODE_CHANNEL_UP, "KEYCODE_CHANNEL_UP");
770        names.append(KEYCODE_CHANNEL_DOWN, "KEYCODE_CHANNEL_DOWN");
771        names.append(KEYCODE_ZOOM_IN, "KEYCODE_ZOOM_IN");
772        names.append(KEYCODE_ZOOM_OUT, "KEYCODE_ZOOM_OUT");
773        names.append(KEYCODE_TV, "KEYCODE_TV");
774        names.append(KEYCODE_WINDOW, "KEYCODE_WINDOW");
775        names.append(KEYCODE_GUIDE, "KEYCODE_GUIDE");
776        names.append(KEYCODE_DVR, "KEYCODE_DVR");
777        names.append(KEYCODE_BOOKMARK, "KEYCODE_BOOKMARK");
778        names.append(KEYCODE_CAPTIONS, "KEYCODE_CAPTIONS");
779        names.append(KEYCODE_SETTINGS, "KEYCODE_SETTINGS");
780        names.append(KEYCODE_TV_POWER, "KEYCODE_TV_POWER");
781        names.append(KEYCODE_TV_INPUT, "KEYCODE_TV_INPUT");
782        names.append(KEYCODE_STB_INPUT, "KEYCODE_STB_INPUT");
783        names.append(KEYCODE_STB_POWER, "KEYCODE_STB_POWER");
784        names.append(KEYCODE_AVR_POWER, "KEYCODE_AVR_POWER");
785        names.append(KEYCODE_AVR_INPUT, "KEYCODE_AVR_INPUT");
786        names.append(KEYCODE_PROG_RED, "KEYCODE_PROG_RED");
787        names.append(KEYCODE_PROG_GREEN, "KEYCODE_PROG_GREEN");
788        names.append(KEYCODE_PROG_YELLOW, "KEYCODE_PROG_YELLOW");
789        names.append(KEYCODE_PROG_BLUE, "KEYCODE_PROG_BLUE");
790        names.append(KEYCODE_APP_SWITCH, "KEYCODE_APP_SWITCH");
791        names.append(KEYCODE_BUTTON_1, "KEYCODE_BUTTON_1");
792        names.append(KEYCODE_BUTTON_2, "KEYCODE_BUTTON_2");
793        names.append(KEYCODE_BUTTON_3, "KEYCODE_BUTTON_3");
794        names.append(KEYCODE_BUTTON_4, "KEYCODE_BUTTON_4");
795        names.append(KEYCODE_BUTTON_5, "KEYCODE_BUTTON_5");
796        names.append(KEYCODE_BUTTON_6, "KEYCODE_BUTTON_6");
797        names.append(KEYCODE_BUTTON_7, "KEYCODE_BUTTON_7");
798        names.append(KEYCODE_BUTTON_8, "KEYCODE_BUTTON_8");
799        names.append(KEYCODE_BUTTON_9, "KEYCODE_BUTTON_9");
800        names.append(KEYCODE_BUTTON_10, "KEYCODE_BUTTON_10");
801        names.append(KEYCODE_BUTTON_11, "KEYCODE_BUTTON_11");
802        names.append(KEYCODE_BUTTON_12, "KEYCODE_BUTTON_12");
803        names.append(KEYCODE_BUTTON_13, "KEYCODE_BUTTON_13");
804        names.append(KEYCODE_BUTTON_14, "KEYCODE_BUTTON_14");
805        names.append(KEYCODE_BUTTON_15, "KEYCODE_BUTTON_15");
806        names.append(KEYCODE_BUTTON_16, "KEYCODE_BUTTON_16");
807        names.append(KEYCODE_LANGUAGE_SWITCH, "KEYCODE_LANGUAGE_SWITCH");
808        names.append(KEYCODE_MANNER_MODE, "KEYCODE_MANNER_MODE");
809        names.append(KEYCODE_3D_MODE, "KEYCODE_3D_MODE");
810    };
811
812    // Symbolic names of all metakeys in bit order from least significant to most significant.
813    // Accordingly there are exactly 32 values in this table.
814    private static final String[] META_SYMBOLIC_NAMES = new String[] {
815        "META_SHIFT_ON",
816        "META_ALT_ON",
817        "META_SYM_ON",
818        "META_FUNCTION_ON",
819        "META_ALT_LEFT_ON",
820        "META_ALT_RIGHT_ON",
821        "META_SHIFT_LEFT_ON",
822        "META_SHIFT_RIGHT_ON",
823        "META_CAP_LOCKED",
824        "META_ALT_LOCKED",
825        "META_SYM_LOCKED",
826        "0x00000800",
827        "META_CTRL_ON",
828        "META_CTRL_LEFT_ON",
829        "META_CTRL_RIGHT_ON",
830        "0x00008000",
831        "META_META_ON",
832        "META_META_LEFT_ON",
833        "META_META_RIGHT_ON",
834        "0x00080000",
835        "META_CAPS_LOCK_ON",
836        "META_NUM_LOCK_ON",
837        "META_SCROLL_LOCK_ON",
838        "0x00800000",
839        "0x01000000",
840        "0x02000000",
841        "0x04000000",
842        "0x08000000",
843        "0x10000000",
844        "0x20000000",
845        "0x40000000",
846        "0x80000000",
847    };
848
849    /**
850     * @deprecated There are now more than MAX_KEYCODE keycodes.
851     * Use {@link #getMaxKeyCode()} instead.
852     */
853    @Deprecated
854    public static final int MAX_KEYCODE             = 84;
855
856    /**
857     * {@link #getAction} value: the key has been pressed down.
858     */
859    public static final int ACTION_DOWN             = 0;
860    /**
861     * {@link #getAction} value: the key has been released.
862     */
863    public static final int ACTION_UP               = 1;
864    /**
865     * {@link #getAction} value: multiple duplicate key events have
866     * occurred in a row, or a complex string is being delivered.  If the
867     * key code is not {#link {@link #KEYCODE_UNKNOWN} then the
868     * {#link {@link #getRepeatCount()} method returns the number of times
869     * the given key code should be executed.
870     * Otherwise, if the key code is {@link #KEYCODE_UNKNOWN}, then
871     * this is a sequence of characters as returned by {@link #getCharacters}.
872     */
873    public static final int ACTION_MULTIPLE         = 2;
874
875    /**
876     * SHIFT key locked in CAPS mode.
877     * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
878     * @hide
879     */
880    public static final int META_CAP_LOCKED = 0x100;
881
882    /**
883     * ALT key locked.
884     * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
885     * @hide
886     */
887    public static final int META_ALT_LOCKED = 0x200;
888
889    /**
890     * SYM key locked.
891     * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
892     * @hide
893     */
894    public static final int META_SYM_LOCKED = 0x400;
895
896    /**
897     * Text is in selection mode.
898     * Reserved for use by {@link MetaKeyKeyListener} for a private unpublished constant
899     * in its API that is currently being retained for legacy reasons.
900     * @hide
901     */
902    public static final int META_SELECTING = 0x800;
903
904    /**
905     * <p>This mask is used to check whether one of the ALT meta keys is pressed.</p>
906     *
907     * @see #isAltPressed()
908     * @see #getMetaState()
909     * @see #KEYCODE_ALT_LEFT
910     * @see #KEYCODE_ALT_RIGHT
911     */
912    public static final int META_ALT_ON = 0x02;
913
914    /**
915     * <p>This mask is used to check whether the left ALT meta key is pressed.</p>
916     *
917     * @see #isAltPressed()
918     * @see #getMetaState()
919     * @see #KEYCODE_ALT_LEFT
920     */
921    public static final int META_ALT_LEFT_ON = 0x10;
922
923    /**
924     * <p>This mask is used to check whether the right the ALT meta key is pressed.</p>
925     *
926     * @see #isAltPressed()
927     * @see #getMetaState()
928     * @see #KEYCODE_ALT_RIGHT
929     */
930    public static final int META_ALT_RIGHT_ON = 0x20;
931
932    /**
933     * <p>This mask is used to check whether one of the SHIFT meta keys is pressed.</p>
934     *
935     * @see #isShiftPressed()
936     * @see #getMetaState()
937     * @see #KEYCODE_SHIFT_LEFT
938     * @see #KEYCODE_SHIFT_RIGHT
939     */
940    public static final int META_SHIFT_ON = 0x1;
941
942    /**
943     * <p>This mask is used to check whether the left SHIFT meta key is pressed.</p>
944     *
945     * @see #isShiftPressed()
946     * @see #getMetaState()
947     * @see #KEYCODE_SHIFT_LEFT
948     */
949    public static final int META_SHIFT_LEFT_ON = 0x40;
950
951    /**
952     * <p>This mask is used to check whether the right SHIFT meta key is pressed.</p>
953     *
954     * @see #isShiftPressed()
955     * @see #getMetaState()
956     * @see #KEYCODE_SHIFT_RIGHT
957     */
958    public static final int META_SHIFT_RIGHT_ON = 0x80;
959
960    /**
961     * <p>This mask is used to check whether the SYM meta key is pressed.</p>
962     *
963     * @see #isSymPressed()
964     * @see #getMetaState()
965     */
966    public static final int META_SYM_ON = 0x4;
967
968    /**
969     * <p>This mask is used to check whether the FUNCTION meta key is pressed.</p>
970     *
971     * @see #isFunctionPressed()
972     * @see #getMetaState()
973     */
974    public static final int META_FUNCTION_ON = 0x8;
975
976    /**
977     * <p>This mask is used to check whether one of the CTRL meta keys is pressed.</p>
978     *
979     * @see #isCtrlPressed()
980     * @see #getMetaState()
981     * @see #KEYCODE_CTRL_LEFT
982     * @see #KEYCODE_CTRL_RIGHT
983     */
984    public static final int META_CTRL_ON = 0x1000;
985
986    /**
987     * <p>This mask is used to check whether the left CTRL meta key is pressed.</p>
988     *
989     * @see #isCtrlPressed()
990     * @see #getMetaState()
991     * @see #KEYCODE_CTRL_LEFT
992     */
993    public static final int META_CTRL_LEFT_ON = 0x2000;
994
995    /**
996     * <p>This mask is used to check whether the right CTRL meta key is pressed.</p>
997     *
998     * @see #isCtrlPressed()
999     * @see #getMetaState()
1000     * @see #KEYCODE_CTRL_RIGHT
1001     */
1002    public static final int META_CTRL_RIGHT_ON = 0x4000;
1003
1004    /**
1005     * <p>This mask is used to check whether one of the META meta keys is pressed.</p>
1006     *
1007     * @see #isMetaPressed()
1008     * @see #getMetaState()
1009     * @see #KEYCODE_META_LEFT
1010     * @see #KEYCODE_META_RIGHT
1011     */
1012    public static final int META_META_ON = 0x10000;
1013
1014    /**
1015     * <p>This mask is used to check whether the left META meta key is pressed.</p>
1016     *
1017     * @see #isMetaPressed()
1018     * @see #getMetaState()
1019     * @see #KEYCODE_META_LEFT
1020     */
1021    public static final int META_META_LEFT_ON = 0x20000;
1022
1023    /**
1024     * <p>This mask is used to check whether the right META meta key is pressed.</p>
1025     *
1026     * @see #isMetaPressed()
1027     * @see #getMetaState()
1028     * @see #KEYCODE_META_RIGHT
1029     */
1030    public static final int META_META_RIGHT_ON = 0x40000;
1031
1032    /**
1033     * <p>This mask is used to check whether the CAPS LOCK meta key is on.</p>
1034     *
1035     * @see #isCapsLockOn()
1036     * @see #getMetaState()
1037     * @see #KEYCODE_CAPS_LOCK
1038     */
1039    public static final int META_CAPS_LOCK_ON = 0x100000;
1040
1041    /**
1042     * <p>This mask is used to check whether the NUM LOCK meta key is on.</p>
1043     *
1044     * @see #isNumLockOn()
1045     * @see #getMetaState()
1046     * @see #KEYCODE_NUM_LOCK
1047     */
1048    public static final int META_NUM_LOCK_ON = 0x200000;
1049
1050    /**
1051     * <p>This mask is used to check whether the SCROLL LOCK meta key is on.</p>
1052     *
1053     * @see #isScrollLockOn()
1054     * @see #getMetaState()
1055     * @see #KEYCODE_SCROLL_LOCK
1056     */
1057    public static final int META_SCROLL_LOCK_ON = 0x400000;
1058
1059    /**
1060     * This mask is a combination of {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON}
1061     * and {@link #META_SHIFT_RIGHT_ON}.
1062     */
1063    public static final int META_SHIFT_MASK = META_SHIFT_ON
1064            | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON;
1065
1066    /**
1067     * This mask is a combination of {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON}
1068     * and {@link #META_ALT_RIGHT_ON}.
1069     */
1070    public static final int META_ALT_MASK = META_ALT_ON
1071            | META_ALT_LEFT_ON | META_ALT_RIGHT_ON;
1072
1073    /**
1074     * This mask is a combination of {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON}
1075     * and {@link #META_CTRL_RIGHT_ON}.
1076     */
1077    public static final int META_CTRL_MASK = META_CTRL_ON
1078            | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON;
1079
1080    /**
1081     * This mask is a combination of {@link #META_META_ON}, {@link #META_META_LEFT_ON}
1082     * and {@link #META_META_RIGHT_ON}.
1083     */
1084    public static final int META_META_MASK = META_META_ON
1085            | META_META_LEFT_ON | META_META_RIGHT_ON;
1086
1087    /**
1088     * This mask is set if the device woke because of this key event.
1089     */
1090    public static final int FLAG_WOKE_HERE = 0x1;
1091
1092    /**
1093     * This mask is set if the key event was generated by a software keyboard.
1094     */
1095    public static final int FLAG_SOFT_KEYBOARD = 0x2;
1096
1097    /**
1098     * This mask is set if we don't want the key event to cause us to leave
1099     * touch mode.
1100     */
1101    public static final int FLAG_KEEP_TOUCH_MODE = 0x4;
1102
1103    /**
1104     * This mask is set if an event was known to come from a trusted part
1105     * of the system.  That is, the event is known to come from the user,
1106     * and could not have been spoofed by a third party component.
1107     */
1108    public static final int FLAG_FROM_SYSTEM = 0x8;
1109
1110    /**
1111     * This mask is used for compatibility, to identify enter keys that are
1112     * coming from an IME whose enter key has been auto-labelled "next" or
1113     * "done".  This allows TextView to dispatch these as normal enter keys
1114     * for old applications, but still do the appropriate action when
1115     * receiving them.
1116     */
1117    public static final int FLAG_EDITOR_ACTION = 0x10;
1118
1119    /**
1120     * When associated with up key events, this indicates that the key press
1121     * has been canceled.  Typically this is used with virtual touch screen
1122     * keys, where the user can slide from the virtual key area on to the
1123     * display: in that case, the application will receive a canceled up
1124     * event and should not perform the action normally associated with the
1125     * key.  Note that for this to work, the application can not perform an
1126     * action for a key until it receives an up or the long press timeout has
1127     * expired.
1128     */
1129    public static final int FLAG_CANCELED = 0x20;
1130
1131    /**
1132     * This key event was generated by a virtual (on-screen) hard key area.
1133     * Typically this is an area of the touchscreen, outside of the regular
1134     * display, dedicated to "hardware" buttons.
1135     */
1136    public static final int FLAG_VIRTUAL_HARD_KEY = 0x40;
1137
1138    /**
1139     * This flag is set for the first key repeat that occurs after the
1140     * long press timeout.
1141     */
1142    public static final int FLAG_LONG_PRESS = 0x80;
1143
1144    /**
1145     * Set when a key event has {@link #FLAG_CANCELED} set because a long
1146     * press action was executed while it was down.
1147     */
1148    public static final int FLAG_CANCELED_LONG_PRESS = 0x100;
1149
1150    /**
1151     * Set for {@link #ACTION_UP} when this event's key code is still being
1152     * tracked from its initial down.  That is, somebody requested that tracking
1153     * started on the key down and a long press has not caused
1154     * the tracking to be canceled.
1155     */
1156    public static final int FLAG_TRACKING = 0x200;
1157
1158    /**
1159     * Set when a key event has been synthesized to implement default behavior
1160     * for an event that the application did not handle.
1161     * Fallback key events are generated by unhandled trackball motions
1162     * (to emulate a directional keypad) and by certain unhandled key presses
1163     * that are declared in the key map (such as special function numeric keypad
1164     * keys when numlock is off).
1165     */
1166    public static final int FLAG_FALLBACK = 0x400;
1167
1168    /**
1169     * Private control to determine when an app is tracking a key sequence.
1170     * @hide
1171     */
1172    public static final int FLAG_START_TRACKING = 0x40000000;
1173
1174    /**
1175     * Returns the maximum keycode.
1176     */
1177    public static int getMaxKeyCode() {
1178        return LAST_KEYCODE;
1179    }
1180
1181    /**
1182     * Get the character that is produced by putting accent on the character
1183     * c.
1184     * For example, getDeadChar('`', 'e') returns &egrave;.
1185     */
1186    public static int getDeadChar(int accent, int c) {
1187        return KeyCharacterMap.getDeadChar(accent, c);
1188    }
1189
1190    static final boolean DEBUG = false;
1191    static final String TAG = "KeyEvent";
1192
1193    private static final int MAX_RECYCLED = 10;
1194    private static final Object gRecyclerLock = new Object();
1195    private static int gRecyclerUsed;
1196    private static KeyEvent gRecyclerTop;
1197
1198    private KeyEvent mNext;
1199    private boolean mRecycled;
1200
1201    private int mDeviceId;
1202    private int mSource;
1203    private int mMetaState;
1204    private int mAction;
1205    private int mKeyCode;
1206    private int mScanCode;
1207    private int mRepeatCount;
1208    private int mFlags;
1209    private long mDownTime;
1210    private long mEventTime;
1211    private String mCharacters;
1212
1213    public interface Callback {
1214        /**
1215         * Called when a key down event has occurred.  If you return true,
1216         * you can first call {@link KeyEvent#startTracking()
1217         * KeyEvent.startTracking()} to have the framework track the event
1218         * through its {@link #onKeyUp(int, KeyEvent)} and also call your
1219         * {@link #onKeyLongPress(int, KeyEvent)} if it occurs.
1220         *
1221         * @param keyCode The value in event.getKeyCode().
1222         * @param event Description of the key event.
1223         *
1224         * @return If you handled the event, return true.  If you want to allow
1225         *         the event to be handled by the next receiver, return false.
1226         */
1227        boolean onKeyDown(int keyCode, KeyEvent event);
1228
1229        /**
1230         * Called when a long press has occurred.  If you return true,
1231         * the final key up will have {@link KeyEvent#FLAG_CANCELED} and
1232         * {@link KeyEvent#FLAG_CANCELED_LONG_PRESS} set.  Note that in
1233         * order to receive this callback, someone in the event change
1234         * <em>must</em> return true from {@link #onKeyDown} <em>and</em>
1235         * call {@link KeyEvent#startTracking()} on the event.
1236         *
1237         * @param keyCode The value in event.getKeyCode().
1238         * @param event Description of the key event.
1239         *
1240         * @return If you handled the event, return true.  If you want to allow
1241         *         the event to be handled by the next receiver, return false.
1242         */
1243        boolean onKeyLongPress(int keyCode, KeyEvent event);
1244
1245        /**
1246         * Called when a key up event has occurred.
1247         *
1248         * @param keyCode The value in event.getKeyCode().
1249         * @param event Description of the key event.
1250         *
1251         * @return If you handled the event, return true.  If you want to allow
1252         *         the event to be handled by the next receiver, return false.
1253         */
1254        boolean onKeyUp(int keyCode, KeyEvent event);
1255
1256        /**
1257         * Called when multiple down/up pairs of the same key have occurred
1258         * in a row.
1259         *
1260         * @param keyCode The value in event.getKeyCode().
1261         * @param count Number of pairs as returned by event.getRepeatCount().
1262         * @param event Description of the key event.
1263         *
1264         * @return If you handled the event, return true.  If you want to allow
1265         *         the event to be handled by the next receiver, return false.
1266         */
1267        boolean onKeyMultiple(int keyCode, int count, KeyEvent event);
1268    }
1269
1270    static {
1271        populateKeycodeSymbolicNames();
1272    }
1273
1274    private KeyEvent() {
1275    }
1276
1277    /**
1278     * Create a new key event.
1279     *
1280     * @param action Action code: either {@link #ACTION_DOWN},
1281     * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1282     * @param code The key code.
1283     */
1284    public KeyEvent(int action, int code) {
1285        mAction = action;
1286        mKeyCode = code;
1287        mRepeatCount = 0;
1288        mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
1289    }
1290
1291    /**
1292     * Create a new key event.
1293     *
1294     * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1295     * at which this key code originally went down.
1296     * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1297     * at which this event happened.
1298     * @param action Action code: either {@link #ACTION_DOWN},
1299     * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1300     * @param code The key code.
1301     * @param repeat A repeat count for down events (> 0 if this is after the
1302     * initial down) or event count for multiple events.
1303     */
1304    public KeyEvent(long downTime, long eventTime, int action,
1305                    int code, int repeat) {
1306        mDownTime = downTime;
1307        mEventTime = eventTime;
1308        mAction = action;
1309        mKeyCode = code;
1310        mRepeatCount = repeat;
1311        mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
1312    }
1313
1314    /**
1315     * Create a new key event.
1316     *
1317     * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1318     * at which this key code originally went down.
1319     * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1320     * at which this event happened.
1321     * @param action Action code: either {@link #ACTION_DOWN},
1322     * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1323     * @param code The key code.
1324     * @param repeat A repeat count for down events (> 0 if this is after the
1325     * initial down) or event count for multiple events.
1326     * @param metaState Flags indicating which meta keys are currently pressed.
1327     */
1328    public KeyEvent(long downTime, long eventTime, int action,
1329                    int code, int repeat, int metaState) {
1330        mDownTime = downTime;
1331        mEventTime = eventTime;
1332        mAction = action;
1333        mKeyCode = code;
1334        mRepeatCount = repeat;
1335        mMetaState = metaState;
1336        mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
1337    }
1338
1339    /**
1340     * Create a new key event.
1341     *
1342     * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1343     * at which this key code originally went down.
1344     * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1345     * at which this event happened.
1346     * @param action Action code: either {@link #ACTION_DOWN},
1347     * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1348     * @param code The key code.
1349     * @param repeat A repeat count for down events (> 0 if this is after the
1350     * initial down) or event count for multiple events.
1351     * @param metaState Flags indicating which meta keys are currently pressed.
1352     * @param deviceId The device ID that generated the key event.
1353     * @param scancode Raw device scan code of the event.
1354     */
1355    public KeyEvent(long downTime, long eventTime, int action,
1356                    int code, int repeat, int metaState,
1357                    int deviceId, int scancode) {
1358        mDownTime = downTime;
1359        mEventTime = eventTime;
1360        mAction = action;
1361        mKeyCode = code;
1362        mRepeatCount = repeat;
1363        mMetaState = metaState;
1364        mDeviceId = deviceId;
1365        mScanCode = scancode;
1366    }
1367
1368    /**
1369     * Create a new key event.
1370     *
1371     * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1372     * at which this key code originally went down.
1373     * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1374     * at which this event happened.
1375     * @param action Action code: either {@link #ACTION_DOWN},
1376     * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1377     * @param code The key code.
1378     * @param repeat A repeat count for down events (> 0 if this is after the
1379     * initial down) or event count for multiple events.
1380     * @param metaState Flags indicating which meta keys are currently pressed.
1381     * @param deviceId The device ID that generated the key event.
1382     * @param scancode Raw device scan code of the event.
1383     * @param flags The flags for this key event
1384     */
1385    public KeyEvent(long downTime, long eventTime, int action,
1386                    int code, int repeat, int metaState,
1387                    int deviceId, int scancode, int flags) {
1388        mDownTime = downTime;
1389        mEventTime = eventTime;
1390        mAction = action;
1391        mKeyCode = code;
1392        mRepeatCount = repeat;
1393        mMetaState = metaState;
1394        mDeviceId = deviceId;
1395        mScanCode = scancode;
1396        mFlags = flags;
1397    }
1398
1399    /**
1400     * Create a new key event.
1401     *
1402     * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1403     * at which this key code originally went down.
1404     * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1405     * at which this event happened.
1406     * @param action Action code: either {@link #ACTION_DOWN},
1407     * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1408     * @param code The key code.
1409     * @param repeat A repeat count for down events (> 0 if this is after the
1410     * initial down) or event count for multiple events.
1411     * @param metaState Flags indicating which meta keys are currently pressed.
1412     * @param deviceId The device ID that generated the key event.
1413     * @param scancode Raw device scan code of the event.
1414     * @param flags The flags for this key event
1415     * @param source The input source such as {@link InputDevice#SOURCE_KEYBOARD}.
1416     */
1417    public KeyEvent(long downTime, long eventTime, int action,
1418                    int code, int repeat, int metaState,
1419                    int deviceId, int scancode, int flags, int source) {
1420        mDownTime = downTime;
1421        mEventTime = eventTime;
1422        mAction = action;
1423        mKeyCode = code;
1424        mRepeatCount = repeat;
1425        mMetaState = metaState;
1426        mDeviceId = deviceId;
1427        mScanCode = scancode;
1428        mFlags = flags;
1429        mSource = source;
1430    }
1431
1432    /**
1433     * Create a new key event for a string of characters.  The key code,
1434     * action, repeat count and source will automatically be set to
1435     * {@link #KEYCODE_UNKNOWN}, {@link #ACTION_MULTIPLE}, 0, and
1436     * {@link InputDevice#SOURCE_KEYBOARD} for you.
1437     *
1438     * @param time The time (in {@link android.os.SystemClock#uptimeMillis})
1439     * at which this event occured.
1440     * @param characters The string of characters.
1441     * @param deviceId The device ID that generated the key event.
1442     * @param flags The flags for this key event
1443     */
1444    public KeyEvent(long time, String characters, int deviceId, int flags) {
1445        mDownTime = time;
1446        mEventTime = time;
1447        mCharacters = characters;
1448        mAction = ACTION_MULTIPLE;
1449        mKeyCode = KEYCODE_UNKNOWN;
1450        mRepeatCount = 0;
1451        mDeviceId = deviceId;
1452        mFlags = flags;
1453        mSource = InputDevice.SOURCE_KEYBOARD;
1454    }
1455
1456    /**
1457     * Make an exact copy of an existing key event.
1458     */
1459    public KeyEvent(KeyEvent origEvent) {
1460        mDownTime = origEvent.mDownTime;
1461        mEventTime = origEvent.mEventTime;
1462        mAction = origEvent.mAction;
1463        mKeyCode = origEvent.mKeyCode;
1464        mRepeatCount = origEvent.mRepeatCount;
1465        mMetaState = origEvent.mMetaState;
1466        mDeviceId = origEvent.mDeviceId;
1467        mSource = origEvent.mSource;
1468        mScanCode = origEvent.mScanCode;
1469        mFlags = origEvent.mFlags;
1470        mCharacters = origEvent.mCharacters;
1471    }
1472
1473    /**
1474     * Copy an existing key event, modifying its time and repeat count.
1475     *
1476     * @deprecated Use {@link #changeTimeRepeat(KeyEvent, long, int)}
1477     * instead.
1478     *
1479     * @param origEvent The existing event to be copied.
1480     * @param eventTime The new event time
1481     * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1482     * @param newRepeat The new repeat count of the event.
1483     */
1484    @Deprecated
1485    public KeyEvent(KeyEvent origEvent, long eventTime, int newRepeat) {
1486        mDownTime = origEvent.mDownTime;
1487        mEventTime = eventTime;
1488        mAction = origEvent.mAction;
1489        mKeyCode = origEvent.mKeyCode;
1490        mRepeatCount = newRepeat;
1491        mMetaState = origEvent.mMetaState;
1492        mDeviceId = origEvent.mDeviceId;
1493        mSource = origEvent.mSource;
1494        mScanCode = origEvent.mScanCode;
1495        mFlags = origEvent.mFlags;
1496        mCharacters = origEvent.mCharacters;
1497    }
1498
1499    private static KeyEvent obtain() {
1500        final KeyEvent ev;
1501        synchronized (gRecyclerLock) {
1502            ev = gRecyclerTop;
1503            if (ev == null) {
1504                return new KeyEvent();
1505            }
1506            gRecyclerTop = ev.mNext;
1507            gRecyclerUsed -= 1;
1508        }
1509        ev.mRecycled = false;
1510        ev.mNext = null;
1511        return ev;
1512    }
1513
1514    /**
1515     * Obtains a (potentially recycled) key event.
1516     *
1517     * @hide
1518     */
1519    public static KeyEvent obtain(long downTime, long eventTime, int action,
1520                    int code, int repeat, int metaState,
1521                    int deviceId, int scancode, int flags, int source, String characters) {
1522        KeyEvent ev = obtain();
1523        ev.mDownTime = downTime;
1524        ev.mEventTime = eventTime;
1525        ev.mAction = action;
1526        ev.mKeyCode = code;
1527        ev.mRepeatCount = repeat;
1528        ev.mMetaState = metaState;
1529        ev.mDeviceId = deviceId;
1530        ev.mScanCode = scancode;
1531        ev.mFlags = flags;
1532        ev.mSource = source;
1533        ev.mCharacters = characters;
1534        return ev;
1535    }
1536
1537    /**
1538     * Recycles a key event.
1539     * Key events should only be recycled if they are owned by the system since user
1540     * code expects them to be essentially immutable, "tracking" notwithstanding.
1541     *
1542     * @hide
1543     */
1544    public final void recycle() {
1545        if (mRecycled) {
1546            throw new RuntimeException(toString() + " recycled twice!");
1547        }
1548        mRecycled = true;
1549        mCharacters = null;
1550
1551        synchronized (gRecyclerLock) {
1552            if (gRecyclerUsed < MAX_RECYCLED) {
1553                gRecyclerUsed++;
1554                mNext = gRecyclerTop;
1555                gRecyclerTop = this;
1556            }
1557        }
1558    }
1559
1560    /**
1561     * Create a new key event that is the same as the given one, but whose
1562     * event time and repeat count are replaced with the given value.
1563     *
1564     * @param event The existing event to be copied.  This is not modified.
1565     * @param eventTime The new event time
1566     * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1567     * @param newRepeat The new repeat count of the event.
1568     */
1569    public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime,
1570            int newRepeat) {
1571        return new KeyEvent(event, eventTime, newRepeat);
1572    }
1573
1574    /**
1575     * Create a new key event that is the same as the given one, but whose
1576     * event time and repeat count are replaced with the given value.
1577     *
1578     * @param event The existing event to be copied.  This is not modified.
1579     * @param eventTime The new event time
1580     * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1581     * @param newRepeat The new repeat count of the event.
1582     * @param newFlags New flags for the event, replacing the entire value
1583     * in the original event.
1584     */
1585    public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime,
1586            int newRepeat, int newFlags) {
1587        KeyEvent ret = new KeyEvent(event);
1588        ret.mEventTime = eventTime;
1589        ret.mRepeatCount = newRepeat;
1590        ret.mFlags = newFlags;
1591        return ret;
1592    }
1593
1594    /**
1595     * Copy an existing key event, modifying its action.
1596     *
1597     * @param origEvent The existing event to be copied.
1598     * @param action The new action code of the event.
1599     */
1600    private KeyEvent(KeyEvent origEvent, int action) {
1601        mDownTime = origEvent.mDownTime;
1602        mEventTime = origEvent.mEventTime;
1603        mAction = action;
1604        mKeyCode = origEvent.mKeyCode;
1605        mRepeatCount = origEvent.mRepeatCount;
1606        mMetaState = origEvent.mMetaState;
1607        mDeviceId = origEvent.mDeviceId;
1608        mSource = origEvent.mSource;
1609        mScanCode = origEvent.mScanCode;
1610        mFlags = origEvent.mFlags;
1611        // Don't copy mCharacters, since one way or the other we'll lose it
1612        // when changing the action.
1613    }
1614
1615    /**
1616     * Create a new key event that is the same as the given one, but whose
1617     * action is replaced with the given value.
1618     *
1619     * @param event The existing event to be copied.  This is not modified.
1620     * @param action The new action code of the event.
1621     */
1622    public static KeyEvent changeAction(KeyEvent event, int action) {
1623        return new KeyEvent(event, action);
1624    }
1625
1626    /**
1627     * Create a new key event that is the same as the given one, but whose
1628     * flags are replaced with the given value.
1629     *
1630     * @param event The existing event to be copied.  This is not modified.
1631     * @param flags The new flags constant.
1632     */
1633    public static KeyEvent changeFlags(KeyEvent event, int flags) {
1634        event = new KeyEvent(event);
1635        event.mFlags = flags;
1636        return event;
1637    }
1638
1639    /**
1640     * Don't use in new code, instead explicitly check
1641     * {@link #getAction()}.
1642     *
1643     * @return If the action is ACTION_DOWN, returns true; else false.
1644     *
1645     * @deprecated
1646     * @hide
1647     */
1648    @Deprecated public final boolean isDown() {
1649        return mAction == ACTION_DOWN;
1650    }
1651
1652    /**
1653     * Is this a system key?  System keys can not be used for menu shortcuts.
1654     *
1655     * TODO: this information should come from a table somewhere.
1656     * TODO: should the dpad keys be here?  arguably, because they also shouldn't be menu shortcuts
1657     */
1658    public final boolean isSystem() {
1659        return native_isSystemKey(mKeyCode);
1660    }
1661
1662    /** @hide */
1663    public final boolean hasDefaultAction() {
1664        return native_hasDefaultAction(mKeyCode);
1665    }
1666
1667    /**
1668     * Returns true if the specified keycode is a gamepad button.
1669     * @return True if the keycode is a gamepad button, such as {@link #KEYCODE_BUTTON_A}.
1670     */
1671    public static final boolean isGamepadButton(int keyCode) {
1672        switch (keyCode) {
1673            case KeyEvent.KEYCODE_BUTTON_A:
1674            case KeyEvent.KEYCODE_BUTTON_B:
1675            case KeyEvent.KEYCODE_BUTTON_C:
1676            case KeyEvent.KEYCODE_BUTTON_X:
1677            case KeyEvent.KEYCODE_BUTTON_Y:
1678            case KeyEvent.KEYCODE_BUTTON_Z:
1679            case KeyEvent.KEYCODE_BUTTON_L1:
1680            case KeyEvent.KEYCODE_BUTTON_R1:
1681            case KeyEvent.KEYCODE_BUTTON_L2:
1682            case KeyEvent.KEYCODE_BUTTON_R2:
1683            case KeyEvent.KEYCODE_BUTTON_THUMBL:
1684            case KeyEvent.KEYCODE_BUTTON_THUMBR:
1685            case KeyEvent.KEYCODE_BUTTON_START:
1686            case KeyEvent.KEYCODE_BUTTON_SELECT:
1687            case KeyEvent.KEYCODE_BUTTON_MODE:
1688            case KeyEvent.KEYCODE_BUTTON_1:
1689            case KeyEvent.KEYCODE_BUTTON_2:
1690            case KeyEvent.KEYCODE_BUTTON_3:
1691            case KeyEvent.KEYCODE_BUTTON_4:
1692            case KeyEvent.KEYCODE_BUTTON_5:
1693            case KeyEvent.KEYCODE_BUTTON_6:
1694            case KeyEvent.KEYCODE_BUTTON_7:
1695            case KeyEvent.KEYCODE_BUTTON_8:
1696            case KeyEvent.KEYCODE_BUTTON_9:
1697            case KeyEvent.KEYCODE_BUTTON_10:
1698            case KeyEvent.KEYCODE_BUTTON_11:
1699            case KeyEvent.KEYCODE_BUTTON_12:
1700            case KeyEvent.KEYCODE_BUTTON_13:
1701            case KeyEvent.KEYCODE_BUTTON_14:
1702            case KeyEvent.KEYCODE_BUTTON_15:
1703            case KeyEvent.KEYCODE_BUTTON_16:
1704                return true;
1705            default:
1706                return false;
1707        }
1708    }
1709
1710    /** {@inheritDoc} */
1711    @Override
1712    public final int getDeviceId() {
1713        return mDeviceId;
1714    }
1715
1716    /** {@inheritDoc} */
1717    @Override
1718    public final int getSource() {
1719        return mSource;
1720    }
1721
1722    /** {@inheritDoc} */
1723    @Override
1724    public final void setSource(int source) {
1725        mSource = source;
1726    }
1727
1728    /**
1729     * <p>Returns the state of the meta keys.</p>
1730     *
1731     * @return an integer in which each bit set to 1 represents a pressed
1732     *         meta key
1733     *
1734     * @see #isAltPressed()
1735     * @see #isShiftPressed()
1736     * @see #isSymPressed()
1737     * @see #isCtrlPressed()
1738     * @see #isMetaPressed()
1739     * @see #isFunctionPressed()
1740     * @see #isCapsLockOn()
1741     * @see #isNumLockOn()
1742     * @see #isScrollLockOn()
1743     * @see #META_ALT_ON
1744     * @see #META_ALT_LEFT_ON
1745     * @see #META_ALT_RIGHT_ON
1746     * @see #META_SHIFT_ON
1747     * @see #META_SHIFT_LEFT_ON
1748     * @see #META_SHIFT_RIGHT_ON
1749     * @see #META_SYM_ON
1750     * @see #META_FUNCTION_ON
1751     * @see #META_CTRL_ON
1752     * @see #META_CTRL_LEFT_ON
1753     * @see #META_CTRL_RIGHT_ON
1754     * @see #META_META_ON
1755     * @see #META_META_LEFT_ON
1756     * @see #META_META_RIGHT_ON
1757     * @see #META_CAPS_LOCK_ON
1758     * @see #META_NUM_LOCK_ON
1759     * @see #META_SCROLL_LOCK_ON
1760     */
1761    public final int getMetaState() {
1762        return mMetaState;
1763    }
1764
1765    /**
1766     * Returns the flags for this key event.
1767     *
1768     * @see #FLAG_WOKE_HERE
1769     */
1770    public final int getFlags() {
1771        return mFlags;
1772    }
1773
1774    // Mask of all modifier key meta states.  Specifically excludes locked keys like caps lock.
1775    private static final int META_MODIFIER_MASK =
1776            META_SHIFT_ON | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON
1777            | META_ALT_ON | META_ALT_LEFT_ON | META_ALT_RIGHT_ON
1778            | META_CTRL_ON | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON
1779            | META_META_ON | META_META_LEFT_ON | META_META_RIGHT_ON
1780            | META_SYM_ON | META_FUNCTION_ON;
1781
1782    // Mask of all lock key meta states.
1783    private static final int META_LOCK_MASK =
1784            META_CAPS_LOCK_ON | META_NUM_LOCK_ON | META_SCROLL_LOCK_ON;
1785
1786    // Mask of all valid meta states.
1787    private static final int META_ALL_MASK = META_MODIFIER_MASK | META_LOCK_MASK;
1788
1789    // Mask of all synthetic meta states that are reserved for API compatibility with
1790    // historical uses in MetaKeyKeyListener.
1791    private static final int META_SYNTHETIC_MASK =
1792            META_CAP_LOCKED | META_ALT_LOCKED | META_SYM_LOCKED | META_SELECTING;
1793
1794    // Mask of all meta states that are not valid use in specifying a modifier key.
1795    // These bits are known to be used for purposes other than specifying modifiers.
1796    private static final int META_INVALID_MODIFIER_MASK =
1797            META_LOCK_MASK | META_SYNTHETIC_MASK;
1798
1799    /**
1800     * Gets a mask that includes all valid modifier key meta state bits.
1801     * <p>
1802     * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1803     * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1804     * not considered modifier keys.  Consequently, the mask specifically excludes
1805     * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1806     * </p>
1807     *
1808     * @return The modifier meta state mask which is a combination of
1809     * {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON}, {@link #META_SHIFT_RIGHT_ON},
1810     * {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON}, {@link #META_ALT_RIGHT_ON},
1811     * {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON}, {@link #META_CTRL_RIGHT_ON},
1812     * {@link #META_META_ON}, {@link #META_META_LEFT_ON}, {@link #META_META_RIGHT_ON},
1813     * {@link #META_SYM_ON}, {@link #META_FUNCTION_ON}.
1814     */
1815    public static int getModifierMetaStateMask() {
1816        return META_MODIFIER_MASK;
1817    }
1818
1819    /**
1820     * Returns true if this key code is a modifier key.
1821     * <p>
1822     * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1823     * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1824     * not considered modifier keys.  Consequently, this function return false
1825     * for those keys.
1826     * </p>
1827     *
1828     * @return True if the key code is one of
1829     * {@link #KEYCODE_SHIFT_LEFT} {@link #KEYCODE_SHIFT_RIGHT},
1830     * {@link #KEYCODE_ALT_LEFT}, {@link #KEYCODE_ALT_RIGHT},
1831     * {@link #KEYCODE_CTRL_LEFT}, {@link #KEYCODE_CTRL_RIGHT},
1832     * {@link #KEYCODE_META_LEFT}, or {@link #KEYCODE_META_RIGHT},
1833     * {@link #KEYCODE_SYM}, {@link #KEYCODE_NUM}, {@link #KEYCODE_FUNCTION}.
1834     */
1835    public static boolean isModifierKey(int keyCode) {
1836        switch (keyCode) {
1837            case KEYCODE_SHIFT_LEFT:
1838            case KEYCODE_SHIFT_RIGHT:
1839            case KEYCODE_ALT_LEFT:
1840            case KEYCODE_ALT_RIGHT:
1841            case KEYCODE_CTRL_LEFT:
1842            case KEYCODE_CTRL_RIGHT:
1843            case KEYCODE_META_LEFT:
1844            case KEYCODE_META_RIGHT:
1845            case KEYCODE_SYM:
1846            case KEYCODE_NUM:
1847            case KEYCODE_FUNCTION:
1848                return true;
1849            default:
1850                return false;
1851        }
1852    }
1853
1854    /**
1855     * Normalizes the specified meta state.
1856     * <p>
1857     * The meta state is normalized such that if either the left or right modifier meta state
1858     * bits are set then the result will also include the universal bit for that modifier.
1859     * </p><p>
1860     * If the specified meta state contains {@link #META_ALT_LEFT_ON} then
1861     * the result will also contain {@link #META_ALT_ON} in addition to {@link #META_ALT_LEFT_ON}
1862     * and the other bits that were specified in the input.  The same is process is
1863     * performed for shift, control and meta.
1864     * </p><p>
1865     * If the specified meta state contains synthetic meta states defined by
1866     * {@link MetaKeyKeyListener}, then those states are translated here and the original
1867     * synthetic meta states are removed from the result.
1868     * {@link MetaKeyKeyListener#META_CAP_LOCKED} is translated to {@link #META_CAPS_LOCK_ON}.
1869     * {@link MetaKeyKeyListener#META_ALT_LOCKED} is translated to {@link #META_ALT_ON}.
1870     * {@link MetaKeyKeyListener#META_SYM_LOCKED} is translated to {@link #META_SYM_ON}.
1871     * </p><p>
1872     * Undefined meta state bits are removed.
1873     * </p>
1874     *
1875     * @param metaState The meta state.
1876     * @return The normalized meta state.
1877     */
1878    public static int normalizeMetaState(int metaState) {
1879        if ((metaState & (META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON)) != 0) {
1880            metaState |= META_SHIFT_ON;
1881        }
1882        if ((metaState & (META_ALT_LEFT_ON | META_ALT_RIGHT_ON)) != 0) {
1883            metaState |= META_ALT_ON;
1884        }
1885        if ((metaState & (META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON)) != 0) {
1886            metaState |= META_CTRL_ON;
1887        }
1888        if ((metaState & (META_META_LEFT_ON | META_META_RIGHT_ON)) != 0) {
1889            metaState |= META_META_ON;
1890        }
1891        if ((metaState & MetaKeyKeyListener.META_CAP_LOCKED) != 0) {
1892            metaState |= META_CAPS_LOCK_ON;
1893        }
1894        if ((metaState & MetaKeyKeyListener.META_ALT_LOCKED) != 0) {
1895            metaState |= META_ALT_ON;
1896        }
1897        if ((metaState & MetaKeyKeyListener.META_SYM_LOCKED) != 0) {
1898            metaState |= META_SYM_ON;
1899        }
1900        return metaState & META_ALL_MASK;
1901    }
1902
1903    /**
1904     * Returns true if no modifiers keys are pressed according to the specified meta state.
1905     * <p>
1906     * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1907     * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1908     * not considered modifier keys.  Consequently, this function ignores
1909     * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1910     * </p><p>
1911     * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}.
1912     * </p>
1913     *
1914     * @param metaState The meta state to consider.
1915     * @return True if no modifier keys are pressed.
1916     * @see #hasNoModifiers()
1917     */
1918    public static boolean metaStateHasNoModifiers(int metaState) {
1919        return (normalizeMetaState(metaState) & META_MODIFIER_MASK) == 0;
1920    }
1921
1922    /**
1923     * Returns true if only the specified modifier keys are pressed according to
1924     * the specified meta state.  Returns false if a different combination of modifier
1925     * keys are pressed.
1926     * <p>
1927     * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1928     * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1929     * not considered modifier keys.  Consequently, this function ignores
1930     * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1931     * </p><p>
1932     * If the specified modifier mask includes directional modifiers, such as
1933     * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the
1934     * modifier is pressed on that side.
1935     * If the specified modifier mask includes non-directional modifiers, such as
1936     * {@link #META_SHIFT_ON}, then this method ensures that the modifier
1937     * is pressed on either side.
1938     * If the specified modifier mask includes both directional and non-directional modifiers
1939     * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON},
1940     * then this method throws an illegal argument exception.
1941     * </p>
1942     *
1943     * @param metaState The meta state to consider.
1944     * @param modifiers The meta state of the modifier keys to check.  May be a combination
1945     * of modifier meta states as defined by {@link #getModifierMetaStateMask()}.  May be 0 to
1946     * ensure that no modifier keys are pressed.
1947     * @return True if only the specified modifier keys are pressed.
1948     * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers
1949     * @see #hasModifiers
1950     */
1951    public static boolean metaStateHasModifiers(int metaState, int modifiers) {
1952        // Note: For forward compatibility, we allow the parameter to contain meta states
1953        //       that we do not recognize but we explicitly disallow meta states that
1954        //       are not valid modifiers.
1955        if ((modifiers & META_INVALID_MODIFIER_MASK) != 0) {
1956            throw new IllegalArgumentException("modifiers must not contain "
1957                    + "META_CAPS_LOCK_ON, META_NUM_LOCK_ON, META_SCROLL_LOCK_ON, "
1958                    + "META_CAP_LOCKED, META_ALT_LOCKED, META_SYM_LOCKED, "
1959                    + "or META_SELECTING");
1960        }
1961
1962        metaState = normalizeMetaState(metaState) & META_MODIFIER_MASK;
1963        metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
1964                META_SHIFT_ON, META_SHIFT_LEFT_ON, META_SHIFT_RIGHT_ON);
1965        metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
1966                META_ALT_ON, META_ALT_LEFT_ON, META_ALT_RIGHT_ON);
1967        metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
1968                META_CTRL_ON, META_CTRL_LEFT_ON, META_CTRL_RIGHT_ON);
1969        metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
1970                META_META_ON, META_META_LEFT_ON, META_META_RIGHT_ON);
1971        return metaState == modifiers;
1972    }
1973
1974    private static int metaStateFilterDirectionalModifiers(int metaState,
1975            int modifiers, int basic, int left, int right) {
1976        final boolean wantBasic = (modifiers & basic) != 0;
1977        final int directional = left | right;
1978        final boolean wantLeftOrRight = (modifiers & directional) != 0;
1979
1980        if (wantBasic) {
1981            if (wantLeftOrRight) {
1982                throw new IllegalArgumentException("modifiers must not contain "
1983                        + metaStateToString(basic) + " combined with "
1984                        + metaStateToString(left) + " or " + metaStateToString(right));
1985            }
1986            return metaState & ~directional;
1987        } else if (wantLeftOrRight) {
1988            return metaState & ~basic;
1989        } else {
1990            return metaState;
1991        }
1992    }
1993
1994    /**
1995     * Returns true if no modifier keys are pressed.
1996     * <p>
1997     * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1998     * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1999     * not considered modifier keys.  Consequently, this function ignores
2000     * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2001     * </p><p>
2002     * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}.
2003     * </p>
2004     *
2005     * @return True if no modifier keys are pressed.
2006     * @see #metaStateHasNoModifiers
2007     */
2008    public final boolean hasNoModifiers() {
2009        return metaStateHasNoModifiers(mMetaState);
2010    }
2011
2012    /**
2013     * Returns true if only the specified modifiers keys are pressed.
2014     * Returns false if a different combination of modifier keys are pressed.
2015     * <p>
2016     * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2017     * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2018     * not considered modifier keys.  Consequently, this function ignores
2019     * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2020     * </p><p>
2021     * If the specified modifier mask includes directional modifiers, such as
2022     * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the
2023     * modifier is pressed on that side.
2024     * If the specified modifier mask includes non-directional modifiers, such as
2025     * {@link #META_SHIFT_ON}, then this method ensures that the modifier
2026     * is pressed on either side.
2027     * If the specified modifier mask includes both directional and non-directional modifiers
2028     * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON},
2029     * then this method throws an illegal argument exception.
2030     * </p>
2031     *
2032     * @param modifiers The meta state of the modifier keys to check.  May be a combination
2033     * of modifier meta states as defined by {@link #getModifierMetaStateMask()}.  May be 0 to
2034     * ensure that no modifier keys are pressed.
2035     * @return True if only the specified modifier keys are pressed.
2036     * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers
2037     * @see #metaStateHasModifiers
2038     */
2039    public final boolean hasModifiers(int modifiers) {
2040        return metaStateHasModifiers(mMetaState, modifiers);
2041    }
2042
2043    /**
2044     * <p>Returns the pressed state of the ALT meta key.</p>
2045     *
2046     * @return true if the ALT key is pressed, false otherwise
2047     *
2048     * @see #KEYCODE_ALT_LEFT
2049     * @see #KEYCODE_ALT_RIGHT
2050     * @see #META_ALT_ON
2051     */
2052    public final boolean isAltPressed() {
2053        return (mMetaState & META_ALT_ON) != 0;
2054    }
2055
2056    /**
2057     * <p>Returns the pressed state of the SHIFT meta key.</p>
2058     *
2059     * @return true if the SHIFT key is pressed, false otherwise
2060     *
2061     * @see #KEYCODE_SHIFT_LEFT
2062     * @see #KEYCODE_SHIFT_RIGHT
2063     * @see #META_SHIFT_ON
2064     */
2065    public final boolean isShiftPressed() {
2066        return (mMetaState & META_SHIFT_ON) != 0;
2067    }
2068
2069    /**
2070     * <p>Returns the pressed state of the SYM meta key.</p>
2071     *
2072     * @return true if the SYM key is pressed, false otherwise
2073     *
2074     * @see #KEYCODE_SYM
2075     * @see #META_SYM_ON
2076     */
2077    public final boolean isSymPressed() {
2078        return (mMetaState & META_SYM_ON) != 0;
2079    }
2080
2081    /**
2082     * <p>Returns the pressed state of the CTRL meta key.</p>
2083     *
2084     * @return true if the CTRL key is pressed, false otherwise
2085     *
2086     * @see #KEYCODE_CTRL_LEFT
2087     * @see #KEYCODE_CTRL_RIGHT
2088     * @see #META_CTRL_ON
2089     */
2090    public final boolean isCtrlPressed() {
2091        return (mMetaState & META_CTRL_ON) != 0;
2092    }
2093
2094    /**
2095     * <p>Returns the pressed state of the META meta key.</p>
2096     *
2097     * @return true if the META key is pressed, false otherwise
2098     *
2099     * @see #KEYCODE_META_LEFT
2100     * @see #KEYCODE_META_RIGHT
2101     * @see #META_META_ON
2102     */
2103    public final boolean isMetaPressed() {
2104        return (mMetaState & META_META_ON) != 0;
2105    }
2106
2107    /**
2108     * <p>Returns the pressed state of the FUNCTION meta key.</p>
2109     *
2110     * @return true if the FUNCTION key is pressed, false otherwise
2111     *
2112     * @see #KEYCODE_FUNCTION
2113     * @see #META_FUNCTION_ON
2114     */
2115    public final boolean isFunctionPressed() {
2116        return (mMetaState & META_FUNCTION_ON) != 0;
2117    }
2118
2119    /**
2120     * <p>Returns the locked state of the CAPS LOCK meta key.</p>
2121     *
2122     * @return true if the CAPS LOCK key is on, false otherwise
2123     *
2124     * @see #KEYCODE_CAPS_LOCK
2125     * @see #META_CAPS_LOCK_ON
2126     */
2127    public final boolean isCapsLockOn() {
2128        return (mMetaState & META_CAPS_LOCK_ON) != 0;
2129    }
2130
2131    /**
2132     * <p>Returns the locked state of the NUM LOCK meta key.</p>
2133     *
2134     * @return true if the NUM LOCK key is on, false otherwise
2135     *
2136     * @see #KEYCODE_NUM_LOCK
2137     * @see #META_NUM_LOCK_ON
2138     */
2139    public final boolean isNumLockOn() {
2140        return (mMetaState & META_NUM_LOCK_ON) != 0;
2141    }
2142
2143    /**
2144     * <p>Returns the locked state of the SCROLL LOCK meta key.</p>
2145     *
2146     * @return true if the SCROLL LOCK key is on, false otherwise
2147     *
2148     * @see #KEYCODE_SCROLL_LOCK
2149     * @see #META_SCROLL_LOCK_ON
2150     */
2151    public final boolean isScrollLockOn() {
2152        return (mMetaState & META_SCROLL_LOCK_ON) != 0;
2153    }
2154
2155    /**
2156     * Retrieve the action of this key event.  May be either
2157     * {@link #ACTION_DOWN}, {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
2158     *
2159     * @return The event action: ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.
2160     */
2161    public final int getAction() {
2162        return mAction;
2163    }
2164
2165    /**
2166     * For {@link #ACTION_UP} events, indicates that the event has been
2167     * canceled as per {@link #FLAG_CANCELED}.
2168     */
2169    public final boolean isCanceled() {
2170        return (mFlags&FLAG_CANCELED) != 0;
2171    }
2172
2173    /**
2174     * Call this during {@link Callback#onKeyDown} to have the system track
2175     * the key through its final up (possibly including a long press).  Note
2176     * that only one key can be tracked at a time -- if another key down
2177     * event is received while a previous one is being tracked, tracking is
2178     * stopped on the previous event.
2179     */
2180    public final void startTracking() {
2181        mFlags |= FLAG_START_TRACKING;
2182    }
2183
2184    /**
2185     * For {@link #ACTION_UP} events, indicates that the event is still being
2186     * tracked from its initial down event as per
2187     * {@link #FLAG_TRACKING}.
2188     */
2189    public final boolean isTracking() {
2190        return (mFlags&FLAG_TRACKING) != 0;
2191    }
2192
2193    /**
2194     * For {@link #ACTION_DOWN} events, indicates that the event has been
2195     * canceled as per {@link #FLAG_LONG_PRESS}.
2196     */
2197    public final boolean isLongPress() {
2198        return (mFlags&FLAG_LONG_PRESS) != 0;
2199    }
2200
2201    /**
2202     * Retrieve the key code of the key event.  This is the physical key that
2203     * was pressed, <em>not</em> the Unicode character.
2204     *
2205     * @return The key code of the event.
2206     */
2207    public final int getKeyCode() {
2208        return mKeyCode;
2209    }
2210
2211    /**
2212     * For the special case of a {@link #ACTION_MULTIPLE} event with key
2213     * code of {@link #KEYCODE_UNKNOWN}, this is a raw string of characters
2214     * associated with the event.  In all other cases it is null.
2215     *
2216     * @return Returns a String of 1 or more characters associated with
2217     * the event.
2218     */
2219    public final String getCharacters() {
2220        return mCharacters;
2221    }
2222
2223    /**
2224     * Retrieve the hardware key id of this key event.  These values are not
2225     * reliable and vary from device to device.
2226     *
2227     * {@more}
2228     * Mostly this is here for debugging purposes.
2229     */
2230    public final int getScanCode() {
2231        return mScanCode;
2232    }
2233
2234    /**
2235     * Retrieve the repeat count of the event.  For both key up and key down
2236     * events, this is the number of times the key has repeated with the first
2237     * down starting at 0 and counting up from there.  For multiple key
2238     * events, this is the number of down/up pairs that have occurred.
2239     *
2240     * @return The number of times the key has repeated.
2241     */
2242    public final int getRepeatCount() {
2243        return mRepeatCount;
2244    }
2245
2246    /**
2247     * Retrieve the time of the most recent key down event,
2248     * in the {@link android.os.SystemClock#uptimeMillis} time base.  If this
2249     * is a down event, this will be the same as {@link #getEventTime()}.
2250     * Note that when chording keys, this value is the down time of the
2251     * most recently pressed key, which may <em>not</em> be the same physical
2252     * key of this event.
2253     *
2254     * @return Returns the most recent key down time, in the
2255     * {@link android.os.SystemClock#uptimeMillis} time base
2256     */
2257    public final long getDownTime() {
2258        return mDownTime;
2259    }
2260
2261    /**
2262     * Retrieve the time this event occurred,
2263     * in the {@link android.os.SystemClock#uptimeMillis} time base.
2264     *
2265     * @return Returns the time this event occurred,
2266     * in the {@link android.os.SystemClock#uptimeMillis} time base.
2267     */
2268    public final long getEventTime() {
2269        return mEventTime;
2270    }
2271
2272    /**
2273     * Renamed to {@link #getDeviceId}.
2274     *
2275     * @hide
2276     * @deprecated use {@link #getDeviceId()} instead.
2277     */
2278    @Deprecated
2279    public final int getKeyboardDevice() {
2280        return mDeviceId;
2281    }
2282
2283    /**
2284     * Gets the {@link KeyCharacterMap} associated with the keyboard device.
2285     *
2286     * @return The associated key character map.
2287     * @throws {@link UnavailableException} if the key character map
2288     * could not be loaded because it was malformed or the default key character map
2289     * is missing from the system.
2290     *
2291     * @see {@link KeyCharacterMap#load}
2292     */
2293    public final KeyCharacterMap getKeyCharacterMap() {
2294        return KeyCharacterMap.load(mDeviceId);
2295    }
2296
2297    /**
2298     * Gets the primary character for this key.
2299     * In other words, the label that is physically printed on it.
2300     *
2301     * @return The display label character, or 0 if none (eg. for non-printing keys).
2302     */
2303    public char getDisplayLabel() {
2304        return getKeyCharacterMap().getDisplayLabel(mKeyCode);
2305    }
2306
2307    /**
2308     * Gets the Unicode character generated by the specified key and meta
2309     * key state combination.
2310     * <p>
2311     * Returns the Unicode character that the specified key would produce
2312     * when the specified meta bits (see {@link MetaKeyKeyListener})
2313     * were active.
2314     * </p><p>
2315     * Returns 0 if the key is not one that is used to type Unicode
2316     * characters.
2317     * </p><p>
2318     * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the
2319     * key is a "dead key" that should be combined with another to
2320     * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} --
2321     * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}.
2322     * </p>
2323     *
2324     * @return The associated character or combining accent, or 0 if none.
2325     */
2326    public int getUnicodeChar() {
2327        return getUnicodeChar(mMetaState);
2328    }
2329
2330    /**
2331     * Gets the Unicode character generated by the specified key and meta
2332     * key state combination.
2333     * <p>
2334     * Returns the Unicode character that the specified key would produce
2335     * when the specified meta bits (see {@link MetaKeyKeyListener})
2336     * were active.
2337     * </p><p>
2338     * Returns 0 if the key is not one that is used to type Unicode
2339     * characters.
2340     * </p><p>
2341     * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the
2342     * key is a "dead key" that should be combined with another to
2343     * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} --
2344     * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}.
2345     * </p>
2346     *
2347     * @param metaState The meta key modifier state.
2348     * @return The associated character or combining accent, or 0 if none.
2349     */
2350    public int getUnicodeChar(int metaState) {
2351        return getKeyCharacterMap().get(mKeyCode, metaState);
2352    }
2353
2354    /**
2355     * Get the character conversion data for a given key code.
2356     *
2357     * @param results A {@link KeyCharacterMap.KeyData} instance that will be
2358     * filled with the results.
2359     * @return True if the key was mapped.  If the key was not mapped, results is not modified.
2360     *
2361     * @deprecated instead use {@link #getDisplayLabel()},
2362     * {@link #getNumber()} or {@link #getUnicodeChar(int)}.
2363     */
2364    @Deprecated
2365    public boolean getKeyData(KeyData results) {
2366        return getKeyCharacterMap().getKeyData(mKeyCode, results);
2367    }
2368
2369    /**
2370     * Gets the first character in the character array that can be generated
2371     * by the specified key code.
2372     * <p>
2373     * This is a convenience function that returns the same value as
2374     * {@link #getMatch(char[],int) getMatch(chars, 0)}.
2375     * </p>
2376     *
2377     * @param chars The array of matching characters to consider.
2378     * @return The matching associated character, or 0 if none.
2379     */
2380    public char getMatch(char[] chars) {
2381        return getMatch(chars, 0);
2382    }
2383
2384    /**
2385     * Gets the first character in the character array that can be generated
2386     * by the specified key code.  If there are multiple choices, prefers
2387     * the one that would be generated with the specified meta key modifier state.
2388     *
2389     * @param chars The array of matching characters to consider.
2390     * @param metaState The preferred meta key modifier state.
2391     * @return The matching associated character, or 0 if none.
2392     */
2393    public char getMatch(char[] chars, int metaState) {
2394        return getKeyCharacterMap().getMatch(mKeyCode, chars, metaState);
2395    }
2396
2397    /**
2398     * Gets the number or symbol associated with the key.
2399     * <p>
2400     * The character value is returned, not the numeric value.
2401     * If the key is not a number, but is a symbol, the symbol is retuned.
2402     * </p><p>
2403     * This method is intended to to support dial pads and other numeric or
2404     * symbolic entry on keyboards where certain keys serve dual function
2405     * as alphabetic and symbolic keys.  This method returns the number
2406     * or symbol associated with the key independent of whether the user
2407     * has pressed the required modifier.
2408     * </p><p>
2409     * For example, on one particular keyboard the keys on the top QWERTY row generate
2410     * numbers when ALT is pressed such that ALT-Q maps to '1'.  So for that keyboard
2411     * when {@link #getNumber} is called with {@link KeyEvent#KEYCODE_Q} it returns '1'
2412     * so that the user can type numbers without pressing ALT when it makes sense.
2413     * </p>
2414     *
2415     * @return The associated numeric or symbolic character, or 0 if none.
2416     */
2417    public char getNumber() {
2418        return getKeyCharacterMap().getNumber(mKeyCode);
2419    }
2420
2421    /**
2422     * Returns true if this key produces a glyph.
2423     *
2424     * @return True if the key is a printing key.
2425     */
2426    public boolean isPrintingKey() {
2427        return getKeyCharacterMap().isPrintingKey(mKeyCode);
2428    }
2429
2430    /**
2431     * @deprecated Use {@link #dispatch(Callback, DispatcherState, Object)} instead.
2432     */
2433    @Deprecated
2434    public final boolean dispatch(Callback receiver) {
2435        return dispatch(receiver, null, null);
2436    }
2437
2438    /**
2439     * Deliver this key event to a {@link Callback} interface.  If this is
2440     * an ACTION_MULTIPLE event and it is not handled, then an attempt will
2441     * be made to deliver a single normal event.
2442     *
2443     * @param receiver The Callback that will be given the event.
2444     * @param state State information retained across events.
2445     * @param target The target of the dispatch, for use in tracking.
2446     *
2447     * @return The return value from the Callback method that was called.
2448     */
2449    public final boolean dispatch(Callback receiver, DispatcherState state,
2450            Object target) {
2451        switch (mAction) {
2452            case ACTION_DOWN: {
2453                mFlags &= ~FLAG_START_TRACKING;
2454                if (DEBUG) Log.v(TAG, "Key down to " + target + " in " + state
2455                        + ": " + this);
2456                boolean res = receiver.onKeyDown(mKeyCode, this);
2457                if (state != null) {
2458                    if (res && mRepeatCount == 0 && (mFlags&FLAG_START_TRACKING) != 0) {
2459                        if (DEBUG) Log.v(TAG, "  Start tracking!");
2460                        state.startTracking(this, target);
2461                    } else if (isLongPress() && state.isTracking(this)) {
2462                        try {
2463                            if (receiver.onKeyLongPress(mKeyCode, this)) {
2464                                if (DEBUG) Log.v(TAG, "  Clear from long press!");
2465                                state.performedLongPress(this);
2466                                res = true;
2467                            }
2468                        } catch (AbstractMethodError e) {
2469                        }
2470                    }
2471                }
2472                return res;
2473            }
2474            case ACTION_UP:
2475                if (DEBUG) Log.v(TAG, "Key up to " + target + " in " + state
2476                        + ": " + this);
2477                if (state != null) {
2478                    state.handleUpEvent(this);
2479                }
2480                return receiver.onKeyUp(mKeyCode, this);
2481            case ACTION_MULTIPLE:
2482                final int count = mRepeatCount;
2483                final int code = mKeyCode;
2484                if (receiver.onKeyMultiple(code, count, this)) {
2485                    return true;
2486                }
2487                if (code != KeyEvent.KEYCODE_UNKNOWN) {
2488                    mAction = ACTION_DOWN;
2489                    mRepeatCount = 0;
2490                    boolean handled = receiver.onKeyDown(code, this);
2491                    if (handled) {
2492                        mAction = ACTION_UP;
2493                        receiver.onKeyUp(code, this);
2494                    }
2495                    mAction = ACTION_MULTIPLE;
2496                    mRepeatCount = count;
2497                    return handled;
2498                }
2499                return false;
2500        }
2501        return false;
2502    }
2503
2504    /**
2505     * Use with {@link KeyEvent#dispatch(Callback, DispatcherState, Object)}
2506     * for more advanced key dispatching, such as long presses.
2507     */
2508    public static class DispatcherState {
2509        int mDownKeyCode;
2510        Object mDownTarget;
2511        SparseIntArray mActiveLongPresses = new SparseIntArray();
2512
2513        /**
2514         * Reset back to initial state.
2515         */
2516        public void reset() {
2517            if (DEBUG) Log.v(TAG, "Reset: " + this);
2518            mDownKeyCode = 0;
2519            mDownTarget = null;
2520            mActiveLongPresses.clear();
2521        }
2522
2523        /**
2524         * Stop any tracking associated with this target.
2525         */
2526        public void reset(Object target) {
2527            if (mDownTarget == target) {
2528                if (DEBUG) Log.v(TAG, "Reset in " + target + ": " + this);
2529                mDownKeyCode = 0;
2530                mDownTarget = null;
2531            }
2532        }
2533
2534        /**
2535         * Start tracking the key code associated with the given event.  This
2536         * can only be called on a key down.  It will allow you to see any
2537         * long press associated with the key, and will result in
2538         * {@link KeyEvent#isTracking} return true on the long press and up
2539         * events.
2540         *
2541         * <p>This is only needed if you are directly dispatching events, rather
2542         * than handling them in {@link Callback#onKeyDown}.
2543         */
2544        public void startTracking(KeyEvent event, Object target) {
2545            if (event.getAction() != ACTION_DOWN) {
2546                throw new IllegalArgumentException(
2547                        "Can only start tracking on a down event");
2548            }
2549            if (DEBUG) Log.v(TAG, "Start trackingt in " + target + ": " + this);
2550            mDownKeyCode = event.getKeyCode();
2551            mDownTarget = target;
2552        }
2553
2554        /**
2555         * Return true if the key event is for a key code that is currently
2556         * being tracked by the dispatcher.
2557         */
2558        public boolean isTracking(KeyEvent event) {
2559            return mDownKeyCode == event.getKeyCode();
2560        }
2561
2562        /**
2563         * Keep track of the given event's key code as having performed an
2564         * action with a long press, so no action should occur on the up.
2565         * <p>This is only needed if you are directly dispatching events, rather
2566         * than handling them in {@link Callback#onKeyLongPress}.
2567         */
2568        public void performedLongPress(KeyEvent event) {
2569            mActiveLongPresses.put(event.getKeyCode(), 1);
2570        }
2571
2572        /**
2573         * Handle key up event to stop tracking.  This resets the dispatcher state,
2574         * and updates the key event state based on it.
2575         * <p>This is only needed if you are directly dispatching events, rather
2576         * than handling them in {@link Callback#onKeyUp}.
2577         */
2578        public void handleUpEvent(KeyEvent event) {
2579            final int keyCode = event.getKeyCode();
2580            if (DEBUG) Log.v(TAG, "Handle key up " + event + ": " + this);
2581            int index = mActiveLongPresses.indexOfKey(keyCode);
2582            if (index >= 0) {
2583                if (DEBUG) Log.v(TAG, "  Index: " + index);
2584                event.mFlags |= FLAG_CANCELED | FLAG_CANCELED_LONG_PRESS;
2585                mActiveLongPresses.removeAt(index);
2586            }
2587            if (mDownKeyCode == keyCode) {
2588                if (DEBUG) Log.v(TAG, "  Tracking!");
2589                event.mFlags |= FLAG_TRACKING;
2590                mDownKeyCode = 0;
2591                mDownTarget = null;
2592            }
2593        }
2594    }
2595
2596    @Override
2597    public String toString() {
2598        return "KeyEvent{action=" + actionToString(mAction)
2599                + " keycode=" + keyCodeToString(mKeyCode)
2600                + " scancode=" + mScanCode
2601                + " metaState=" + metaStateToString(mMetaState)
2602                + " flags=0x" + Integer.toHexString(mFlags)
2603                + " repeat=" + mRepeatCount
2604                + " device=" + mDeviceId
2605                + " source=0x" + Integer.toHexString(mSource)
2606                + "}";
2607    }
2608
2609    /**
2610     * Returns a string that represents the symbolic name of the specified action
2611     * such as "ACTION_DOWN", or an equivalent numeric constant such as "35" if unknown.
2612     *
2613     * @param action The action.
2614     * @return The symbolic name of the specified action.
2615     * @hide
2616     */
2617    public static String actionToString(int action) {
2618        switch (action) {
2619            case ACTION_DOWN:
2620                return "ACTION_DOWN";
2621            case ACTION_UP:
2622                return "ACTION_UP";
2623            case ACTION_MULTIPLE:
2624                return "ACTION_MULTIPLE";
2625            default:
2626                return Integer.toString(action);
2627        }
2628    }
2629
2630    /**
2631     * Returns a string that represents the symbolic name of the specified keycode
2632     * such as "KEYCODE_A", "KEYCODE_DPAD_UP", or an equivalent numeric constant
2633     * such as "1001" if unknown.
2634     *
2635     * @param keyCode The key code.
2636     * @return The symbolic name of the specified keycode.
2637     *
2638     * @see KeyCharacterMap#getDisplayLabel
2639     */
2640    public static String keyCodeToString(int keyCode) {
2641        String symbolicName = KEYCODE_SYMBOLIC_NAMES.get(keyCode);
2642        return symbolicName != null ? symbolicName : Integer.toString(keyCode);
2643    }
2644
2645    /**
2646     * Gets a keycode by its symbolic name such as "KEYCODE_A" or an equivalent
2647     * numeric constant such as "1001".
2648     *
2649     * @param symbolicName The symbolic name of the keycode.
2650     * @return The keycode or {@link #KEYCODE_UNKNOWN} if not found.
2651     * @see #keycodeToString
2652     */
2653    public static int keyCodeFromString(String symbolicName) {
2654        if (symbolicName == null) {
2655            throw new IllegalArgumentException("symbolicName must not be null");
2656        }
2657
2658        final int count = KEYCODE_SYMBOLIC_NAMES.size();
2659        for (int i = 0; i < count; i++) {
2660            if (symbolicName.equals(KEYCODE_SYMBOLIC_NAMES.valueAt(i))) {
2661                return i;
2662            }
2663        }
2664
2665        try {
2666            return Integer.parseInt(symbolicName, 10);
2667        } catch (NumberFormatException ex) {
2668            return KEYCODE_UNKNOWN;
2669        }
2670    }
2671
2672    /**
2673     * Returns a string that represents the symbolic name of the specified combined meta
2674     * key modifier state flags such as "0", "META_SHIFT_ON",
2675     * "META_ALT_ON|META_SHIFT_ON" or an equivalent numeric constant such as "0x10000000"
2676     * if unknown.
2677     *
2678     * @param metaState The meta state.
2679     * @return The symbolic name of the specified combined meta state flags.
2680     * @hide
2681     */
2682    public static String metaStateToString(int metaState) {
2683        if (metaState == 0) {
2684            return "0";
2685        }
2686        StringBuilder result = null;
2687        int i = 0;
2688        while (metaState != 0) {
2689            final boolean isSet = (metaState & 1) != 0;
2690            metaState >>>= 1; // unsigned shift!
2691            if (isSet) {
2692                final String name = META_SYMBOLIC_NAMES[i];
2693                if (result == null) {
2694                    if (metaState == 0) {
2695                        return name;
2696                    }
2697                    result = new StringBuilder(name);
2698                } else {
2699                    result.append('|');
2700                    result.append(name);
2701                }
2702            }
2703            i += 1;
2704        }
2705        return result.toString();
2706    }
2707
2708    public static final Parcelable.Creator<KeyEvent> CREATOR
2709            = new Parcelable.Creator<KeyEvent>() {
2710        public KeyEvent createFromParcel(Parcel in) {
2711            in.readInt(); // skip token, we already know this is a KeyEvent
2712            return KeyEvent.createFromParcelBody(in);
2713        }
2714
2715        public KeyEvent[] newArray(int size) {
2716            return new KeyEvent[size];
2717        }
2718    };
2719
2720    /** @hide */
2721    public static KeyEvent createFromParcelBody(Parcel in) {
2722        return new KeyEvent(in);
2723    }
2724
2725    private KeyEvent(Parcel in) {
2726        mDeviceId = in.readInt();
2727        mSource = in.readInt();
2728        mAction = in.readInt();
2729        mKeyCode = in.readInt();
2730        mRepeatCount = in.readInt();
2731        mMetaState = in.readInt();
2732        mScanCode = in.readInt();
2733        mFlags = in.readInt();
2734        mDownTime = in.readLong();
2735        mEventTime = in.readLong();
2736    }
2737
2738    public void writeToParcel(Parcel out, int flags) {
2739        out.writeInt(PARCEL_TOKEN_KEY_EVENT);
2740
2741        out.writeInt(mDeviceId);
2742        out.writeInt(mSource);
2743        out.writeInt(mAction);
2744        out.writeInt(mKeyCode);
2745        out.writeInt(mRepeatCount);
2746        out.writeInt(mMetaState);
2747        out.writeInt(mScanCode);
2748        out.writeInt(mFlags);
2749        out.writeLong(mDownTime);
2750        out.writeLong(mEventTime);
2751    }
2752
2753    private native boolean native_isSystemKey(int keyCode);
2754    private native boolean native_hasDefaultAction(int keyCode);
2755}
2756