KeyEvent.java revision 6f2fba428ca5e77a26d991ad728e346cc47609ee
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
570    private static final int LAST_KEYCODE           = KEYCODE_BUTTON_16;
571
572    // NOTE: If you add a new keycode here you must also add it to:
573    //  isSystem()
574    //  native/include/android/keycodes.h
575    //  frameworks/base/include/ui/KeycodeLabels.h
576    //  external/webkit/WebKit/android/plugins/ANPKeyCodes.h
577    //  frameworks/base/core/res/res/values/attrs.xml
578    //  emulator?
579    //
580    //  Also Android currently does not reserve code ranges for vendor-
581    //  specific key codes.  If you have new key codes to have, you
582    //  MUST contribute a patch to the open source project to define
583    //  those new codes.  This is intended to maintain a consistent
584    //  set of key code definitions across all Android devices.
585
586    // Symbolic names of all key codes.
587    private static final SparseArray<String> KEYCODE_SYMBOLIC_NAMES = new SparseArray<String>();
588    private static void populateKeycodeSymbolicNames() {
589        SparseArray<String> names = KEYCODE_SYMBOLIC_NAMES;
590        names.append(KEYCODE_UNKNOWN, "KEYCODE_UNKNOWN");
591        names.append(KEYCODE_SOFT_LEFT, "KEYCODE_SOFT_LEFT");
592        names.append(KEYCODE_SOFT_RIGHT, "KEYCODE_SOFT_RIGHT");
593        names.append(KEYCODE_HOME, "KEYCODE_HOME");
594        names.append(KEYCODE_BACK, "KEYCODE_BACK");
595        names.append(KEYCODE_CALL, "KEYCODE_CALL");
596        names.append(KEYCODE_ENDCALL, "KEYCODE_ENDCALL");
597        names.append(KEYCODE_0, "KEYCODE_0");
598        names.append(KEYCODE_1, "KEYCODE_1");
599        names.append(KEYCODE_2, "KEYCODE_2");
600        names.append(KEYCODE_3, "KEYCODE_3");
601        names.append(KEYCODE_4, "KEYCODE_4");
602        names.append(KEYCODE_5, "KEYCODE_5");
603        names.append(KEYCODE_6, "KEYCODE_6");
604        names.append(KEYCODE_7, "KEYCODE_7");
605        names.append(KEYCODE_8, "KEYCODE_8");
606        names.append(KEYCODE_9, "KEYCODE_9");
607        names.append(KEYCODE_STAR, "KEYCODE_STAR");
608        names.append(KEYCODE_POUND, "KEYCODE_POUND");
609        names.append(KEYCODE_DPAD_UP, "KEYCODE_DPAD_UP");
610        names.append(KEYCODE_DPAD_DOWN, "KEYCODE_DPAD_DOWN");
611        names.append(KEYCODE_DPAD_LEFT, "KEYCODE_DPAD_LEFT");
612        names.append(KEYCODE_DPAD_RIGHT, "KEYCODE_DPAD_RIGHT");
613        names.append(KEYCODE_DPAD_CENTER, "KEYCODE_DPAD_CENTER");
614        names.append(KEYCODE_VOLUME_UP, "KEYCODE_VOLUME_UP");
615        names.append(KEYCODE_VOLUME_DOWN, "KEYCODE_VOLUME_DOWN");
616        names.append(KEYCODE_POWER, "KEYCODE_POWER");
617        names.append(KEYCODE_CAMERA, "KEYCODE_CAMERA");
618        names.append(KEYCODE_CLEAR, "KEYCODE_CLEAR");
619        names.append(KEYCODE_A, "KEYCODE_A");
620        names.append(KEYCODE_B, "KEYCODE_B");
621        names.append(KEYCODE_C, "KEYCODE_C");
622        names.append(KEYCODE_D, "KEYCODE_D");
623        names.append(KEYCODE_E, "KEYCODE_E");
624        names.append(KEYCODE_F, "KEYCODE_F");
625        names.append(KEYCODE_G, "KEYCODE_G");
626        names.append(KEYCODE_H, "KEYCODE_H");
627        names.append(KEYCODE_I, "KEYCODE_I");
628        names.append(KEYCODE_J, "KEYCODE_J");
629        names.append(KEYCODE_K, "KEYCODE_K");
630        names.append(KEYCODE_L, "KEYCODE_L");
631        names.append(KEYCODE_M, "KEYCODE_M");
632        names.append(KEYCODE_N, "KEYCODE_N");
633        names.append(KEYCODE_O, "KEYCODE_O");
634        names.append(KEYCODE_P, "KEYCODE_P");
635        names.append(KEYCODE_Q, "KEYCODE_Q");
636        names.append(KEYCODE_R, "KEYCODE_R");
637        names.append(KEYCODE_S, "KEYCODE_S");
638        names.append(KEYCODE_T, "KEYCODE_T");
639        names.append(KEYCODE_U, "KEYCODE_U");
640        names.append(KEYCODE_V, "KEYCODE_V");
641        names.append(KEYCODE_W, "KEYCODE_W");
642        names.append(KEYCODE_X, "KEYCODE_X");
643        names.append(KEYCODE_Y, "KEYCODE_Y");
644        names.append(KEYCODE_Z, "KEYCODE_Z");
645        names.append(KEYCODE_COMMA, "KEYCODE_COMMA");
646        names.append(KEYCODE_PERIOD, "KEYCODE_PERIOD");
647        names.append(KEYCODE_ALT_LEFT, "KEYCODE_ALT_LEFT");
648        names.append(KEYCODE_ALT_RIGHT, "KEYCODE_ALT_RIGHT");
649        names.append(KEYCODE_SHIFT_LEFT, "KEYCODE_SHIFT_LEFT");
650        names.append(KEYCODE_SHIFT_RIGHT, "KEYCODE_SHIFT_RIGHT");
651        names.append(KEYCODE_TAB, "KEYCODE_TAB");
652        names.append(KEYCODE_SPACE, "KEYCODE_SPACE");
653        names.append(KEYCODE_SYM, "KEYCODE_SYM");
654        names.append(KEYCODE_EXPLORER, "KEYCODE_EXPLORER");
655        names.append(KEYCODE_ENVELOPE, "KEYCODE_ENVELOPE");
656        names.append(KEYCODE_ENTER, "KEYCODE_ENTER");
657        names.append(KEYCODE_DEL, "KEYCODE_DEL");
658        names.append(KEYCODE_GRAVE, "KEYCODE_GRAVE");
659        names.append(KEYCODE_MINUS, "KEYCODE_MINUS");
660        names.append(KEYCODE_EQUALS, "KEYCODE_EQUALS");
661        names.append(KEYCODE_LEFT_BRACKET, "KEYCODE_LEFT_BRACKET");
662        names.append(KEYCODE_RIGHT_BRACKET, "KEYCODE_RIGHT_BRACKET");
663        names.append(KEYCODE_BACKSLASH, "KEYCODE_BACKSLASH");
664        names.append(KEYCODE_SEMICOLON, "KEYCODE_SEMICOLON");
665        names.append(KEYCODE_APOSTROPHE, "KEYCODE_APOSTROPHE");
666        names.append(KEYCODE_SLASH, "KEYCODE_SLASH");
667        names.append(KEYCODE_AT, "KEYCODE_AT");
668        names.append(KEYCODE_NUM, "KEYCODE_NUM");
669        names.append(KEYCODE_HEADSETHOOK, "KEYCODE_HEADSETHOOK");
670        names.append(KEYCODE_FOCUS, "KEYCODE_FOCUS");
671        names.append(KEYCODE_PLUS, "KEYCODE_PLUS");
672        names.append(KEYCODE_MENU, "KEYCODE_MENU");
673        names.append(KEYCODE_NOTIFICATION, "KEYCODE_NOTIFICATION");
674        names.append(KEYCODE_SEARCH, "KEYCODE_SEARCH");
675        names.append(KEYCODE_MEDIA_PLAY_PAUSE, "KEYCODE_MEDIA_PLAY_PAUSE");
676        names.append(KEYCODE_MEDIA_STOP, "KEYCODE_MEDIA_STOP");
677        names.append(KEYCODE_MEDIA_NEXT, "KEYCODE_MEDIA_NEXT");
678        names.append(KEYCODE_MEDIA_PREVIOUS, "KEYCODE_MEDIA_PREVIOUS");
679        names.append(KEYCODE_MEDIA_REWIND, "KEYCODE_MEDIA_REWIND");
680        names.append(KEYCODE_MEDIA_FAST_FORWARD, "KEYCODE_MEDIA_FAST_FORWARD");
681        names.append(KEYCODE_MUTE, "KEYCODE_MUTE");
682        names.append(KEYCODE_PAGE_UP, "KEYCODE_PAGE_UP");
683        names.append(KEYCODE_PAGE_DOWN, "KEYCODE_PAGE_DOWN");
684        names.append(KEYCODE_PICTSYMBOLS, "KEYCODE_PICTSYMBOLS");
685        names.append(KEYCODE_SWITCH_CHARSET, "KEYCODE_SWITCH_CHARSET");
686        names.append(KEYCODE_BUTTON_A, "KEYCODE_BUTTON_A");
687        names.append(KEYCODE_BUTTON_B, "KEYCODE_BUTTON_B");
688        names.append(KEYCODE_BUTTON_C, "KEYCODE_BUTTON_C");
689        names.append(KEYCODE_BUTTON_X, "KEYCODE_BUTTON_X");
690        names.append(KEYCODE_BUTTON_Y, "KEYCODE_BUTTON_Y");
691        names.append(KEYCODE_BUTTON_Z, "KEYCODE_BUTTON_Z");
692        names.append(KEYCODE_BUTTON_L1, "KEYCODE_BUTTON_L1");
693        names.append(KEYCODE_BUTTON_R1, "KEYCODE_BUTTON_R1");
694        names.append(KEYCODE_BUTTON_L2, "KEYCODE_BUTTON_L2");
695        names.append(KEYCODE_BUTTON_R2, "KEYCODE_BUTTON_R2");
696        names.append(KEYCODE_BUTTON_THUMBL, "KEYCODE_BUTTON_THUMBL");
697        names.append(KEYCODE_BUTTON_THUMBR, "KEYCODE_BUTTON_THUMBR");
698        names.append(KEYCODE_BUTTON_START, "KEYCODE_BUTTON_START");
699        names.append(KEYCODE_BUTTON_SELECT, "KEYCODE_BUTTON_SELECT");
700        names.append(KEYCODE_BUTTON_MODE, "KEYCODE_BUTTON_MODE");
701        names.append(KEYCODE_ESCAPE, "KEYCODE_ESCAPE");
702        names.append(KEYCODE_FORWARD_DEL, "KEYCODE_FORWARD_DEL");
703        names.append(KEYCODE_CTRL_LEFT, "KEYCODE_CTRL_LEFT");
704        names.append(KEYCODE_CTRL_RIGHT, "KEYCODE_CTRL_RIGHT");
705        names.append(KEYCODE_CAPS_LOCK, "KEYCODE_CAPS_LOCK");
706        names.append(KEYCODE_SCROLL_LOCK, "KEYCODE_SCROLL_LOCK");
707        names.append(KEYCODE_META_LEFT, "KEYCODE_META_LEFT");
708        names.append(KEYCODE_META_RIGHT, "KEYCODE_META_RIGHT");
709        names.append(KEYCODE_FUNCTION, "KEYCODE_FUNCTION");
710        names.append(KEYCODE_SYSRQ, "KEYCODE_SYSRQ");
711        names.append(KEYCODE_BREAK, "KEYCODE_BREAK");
712        names.append(KEYCODE_MOVE_HOME, "KEYCODE_MOVE_HOME");
713        names.append(KEYCODE_MOVE_END, "KEYCODE_MOVE_END");
714        names.append(KEYCODE_INSERT, "KEYCODE_INSERT");
715        names.append(KEYCODE_FORWARD, "KEYCODE_FORWARD");
716        names.append(KEYCODE_MEDIA_PLAY, "KEYCODE_MEDIA_PLAY");
717        names.append(KEYCODE_MEDIA_PAUSE, "KEYCODE_MEDIA_PAUSE");
718        names.append(KEYCODE_MEDIA_CLOSE, "KEYCODE_MEDIA_CLOSE");
719        names.append(KEYCODE_MEDIA_EJECT, "KEYCODE_MEDIA_EJECT");
720        names.append(KEYCODE_MEDIA_RECORD, "KEYCODE_MEDIA_RECORD");
721        names.append(KEYCODE_F1, "KEYCODE_F1");
722        names.append(KEYCODE_F2, "KEYCODE_F2");
723        names.append(KEYCODE_F3, "KEYCODE_F3");
724        names.append(KEYCODE_F4, "KEYCODE_F4");
725        names.append(KEYCODE_F5, "KEYCODE_F5");
726        names.append(KEYCODE_F6, "KEYCODE_F6");
727        names.append(KEYCODE_F7, "KEYCODE_F7");
728        names.append(KEYCODE_F8, "KEYCODE_F8");
729        names.append(KEYCODE_F9, "KEYCODE_F9");
730        names.append(KEYCODE_F10, "KEYCODE_F10");
731        names.append(KEYCODE_F11, "KEYCODE_F11");
732        names.append(KEYCODE_F12, "KEYCODE_F12");
733        names.append(KEYCODE_NUM_LOCK, "KEYCODE_NUM_LOCK");
734        names.append(KEYCODE_NUMPAD_0, "KEYCODE_NUMPAD_0");
735        names.append(KEYCODE_NUMPAD_1, "KEYCODE_NUMPAD_1");
736        names.append(KEYCODE_NUMPAD_2, "KEYCODE_NUMPAD_2");
737        names.append(KEYCODE_NUMPAD_3, "KEYCODE_NUMPAD_3");
738        names.append(KEYCODE_NUMPAD_4, "KEYCODE_NUMPAD_4");
739        names.append(KEYCODE_NUMPAD_5, "KEYCODE_NUMPAD_5");
740        names.append(KEYCODE_NUMPAD_6, "KEYCODE_NUMPAD_6");
741        names.append(KEYCODE_NUMPAD_7, "KEYCODE_NUMPAD_7");
742        names.append(KEYCODE_NUMPAD_8, "KEYCODE_NUMPAD_8");
743        names.append(KEYCODE_NUMPAD_9, "KEYCODE_NUMPAD_9");
744        names.append(KEYCODE_NUMPAD_DIVIDE, "KEYCODE_NUMPAD_DIVIDE");
745        names.append(KEYCODE_NUMPAD_MULTIPLY, "KEYCODE_NUMPAD_MULTIPLY");
746        names.append(KEYCODE_NUMPAD_SUBTRACT, "KEYCODE_NUMPAD_SUBTRACT");
747        names.append(KEYCODE_NUMPAD_ADD, "KEYCODE_NUMPAD_ADD");
748        names.append(KEYCODE_NUMPAD_DOT, "KEYCODE_NUMPAD_DOT");
749        names.append(KEYCODE_NUMPAD_COMMA, "KEYCODE_NUMPAD_COMMA");
750        names.append(KEYCODE_NUMPAD_ENTER, "KEYCODE_NUMPAD_ENTER");
751        names.append(KEYCODE_NUMPAD_EQUALS, "KEYCODE_NUMPAD_EQUALS");
752        names.append(KEYCODE_NUMPAD_LEFT_PAREN, "KEYCODE_NUMPAD_LEFT_PAREN");
753        names.append(KEYCODE_NUMPAD_RIGHT_PAREN, "KEYCODE_NUMPAD_RIGHT_PAREN");
754        names.append(KEYCODE_VOLUME_MUTE, "KEYCODE_VOLUME_MUTE");
755        names.append(KEYCODE_INFO, "KEYCODE_INFO");
756        names.append(KEYCODE_CHANNEL_UP, "KEYCODE_CHANNEL_UP");
757        names.append(KEYCODE_CHANNEL_DOWN, "KEYCODE_CHANNEL_DOWN");
758        names.append(KEYCODE_ZOOM_IN, "KEYCODE_ZOOM_IN");
759        names.append(KEYCODE_ZOOM_OUT, "KEYCODE_ZOOM_OUT");
760        names.append(KEYCODE_TV, "KEYCODE_TV");
761        names.append(KEYCODE_WINDOW, "KEYCODE_WINDOW");
762        names.append(KEYCODE_GUIDE, "KEYCODE_GUIDE");
763        names.append(KEYCODE_DVR, "KEYCODE_DVR");
764        names.append(KEYCODE_BOOKMARK, "KEYCODE_BOOKMARK");
765        names.append(KEYCODE_CAPTIONS, "KEYCODE_CAPTIONS");
766        names.append(KEYCODE_SETTINGS, "KEYCODE_SETTINGS");
767        names.append(KEYCODE_TV_POWER, "KEYCODE_TV_POWER");
768        names.append(KEYCODE_TV_INPUT, "KEYCODE_TV_INPUT");
769        names.append(KEYCODE_STB_INPUT, "KEYCODE_STB_INPUT");
770        names.append(KEYCODE_STB_POWER, "KEYCODE_STB_POWER");
771        names.append(KEYCODE_AVR_POWER, "KEYCODE_AVR_POWER");
772        names.append(KEYCODE_AVR_INPUT, "KEYCODE_AVR_INPUT");
773        names.append(KEYCODE_PROG_RED, "KEYCODE_PROG_RED");
774        names.append(KEYCODE_PROG_GREEN, "KEYCODE_PROG_GREEN");
775        names.append(KEYCODE_PROG_YELLOW, "KEYCODE_PROG_YELLOW");
776        names.append(KEYCODE_PROG_BLUE, "KEYCODE_PROG_BLUE");
777        names.append(KEYCODE_APP_SWITCH, "KEYCODE_APP_SWITCH");
778        names.append(KEYCODE_BUTTON_1, "KEYCODE_BUTTON_1");
779        names.append(KEYCODE_BUTTON_2, "KEYCODE_BUTTON_2");
780        names.append(KEYCODE_BUTTON_3, "KEYCODE_BUTTON_3");
781        names.append(KEYCODE_BUTTON_4, "KEYCODE_BUTTON_4");
782        names.append(KEYCODE_BUTTON_5, "KEYCODE_BUTTON_5");
783        names.append(KEYCODE_BUTTON_6, "KEYCODE_BUTTON_6");
784        names.append(KEYCODE_BUTTON_7, "KEYCODE_BUTTON_7");
785        names.append(KEYCODE_BUTTON_8, "KEYCODE_BUTTON_8");
786        names.append(KEYCODE_BUTTON_9, "KEYCODE_BUTTON_9");
787        names.append(KEYCODE_BUTTON_10, "KEYCODE_BUTTON_10");
788        names.append(KEYCODE_BUTTON_11, "KEYCODE_BUTTON_11");
789        names.append(KEYCODE_BUTTON_12, "KEYCODE_BUTTON_12");
790        names.append(KEYCODE_BUTTON_13, "KEYCODE_BUTTON_13");
791        names.append(KEYCODE_BUTTON_14, "KEYCODE_BUTTON_14");
792        names.append(KEYCODE_BUTTON_15, "KEYCODE_BUTTON_15");
793        names.append(KEYCODE_BUTTON_16, "KEYCODE_BUTTON_16");
794    };
795
796    // Symbolic names of all metakeys in bit order from least significant to most significant.
797    // Accordingly there are exactly 32 values in this table.
798    private static final String[] META_SYMBOLIC_NAMES = new String[] {
799        "META_SHIFT_ON",
800        "META_ALT_ON",
801        "META_SYM_ON",
802        "META_FUNCTION_ON",
803        "META_ALT_LEFT_ON",
804        "META_ALT_RIGHT_ON",
805        "META_SHIFT_LEFT_ON",
806        "META_SHIFT_RIGHT_ON",
807        "META_CAP_LOCKED",
808        "META_ALT_LOCKED",
809        "META_SYM_LOCKED",
810        "0x00000800",
811        "META_CTRL_ON",
812        "META_CTRL_LEFT_ON",
813        "META_CTRL_RIGHT_ON",
814        "0x00008000",
815        "META_META_ON",
816        "META_META_LEFT_ON",
817        "META_META_RIGHT_ON",
818        "0x00080000",
819        "META_CAPS_LOCK_ON",
820        "META_NUM_LOCK_ON",
821        "META_SCROLL_LOCK_ON",
822        "0x00800000",
823        "0x01000000",
824        "0x02000000",
825        "0x04000000",
826        "0x08000000",
827        "0x10000000",
828        "0x20000000",
829        "0x40000000",
830        "0x80000000",
831    };
832
833    /**
834     * @deprecated There are now more than MAX_KEYCODE keycodes.
835     * Use {@link #getMaxKeyCode()} instead.
836     */
837    @Deprecated
838    public static final int MAX_KEYCODE             = 84;
839
840    /**
841     * {@link #getAction} value: the key has been pressed down.
842     */
843    public static final int ACTION_DOWN             = 0;
844    /**
845     * {@link #getAction} value: the key has been released.
846     */
847    public static final int ACTION_UP               = 1;
848    /**
849     * {@link #getAction} value: multiple duplicate key events have
850     * occurred in a row, or a complex string is being delivered.  If the
851     * key code is not {#link {@link #KEYCODE_UNKNOWN} then the
852     * {#link {@link #getRepeatCount()} method returns the number of times
853     * the given key code should be executed.
854     * Otherwise, if the key code is {@link #KEYCODE_UNKNOWN}, then
855     * this is a sequence of characters as returned by {@link #getCharacters}.
856     */
857    public static final int ACTION_MULTIPLE         = 2;
858
859    /**
860     * SHIFT key locked in CAPS mode.
861     * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
862     * @hide
863     */
864    public static final int META_CAP_LOCKED = 0x100;
865
866    /**
867     * ALT key locked.
868     * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
869     * @hide
870     */
871    public static final int META_ALT_LOCKED = 0x200;
872
873    /**
874     * SYM key locked.
875     * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
876     * @hide
877     */
878    public static final int META_SYM_LOCKED = 0x400;
879
880    /**
881     * Text is in selection mode.
882     * Reserved for use by {@link MetaKeyKeyListener} for a private unpublished constant
883     * in its API that is currently being retained for legacy reasons.
884     * @hide
885     */
886    public static final int META_SELECTING = 0x800;
887
888    /**
889     * <p>This mask is used to check whether one of the ALT meta keys is pressed.</p>
890     *
891     * @see #isAltPressed()
892     * @see #getMetaState()
893     * @see #KEYCODE_ALT_LEFT
894     * @see #KEYCODE_ALT_RIGHT
895     */
896    public static final int META_ALT_ON = 0x02;
897
898    /**
899     * <p>This mask is used to check whether the left ALT meta key is pressed.</p>
900     *
901     * @see #isAltPressed()
902     * @see #getMetaState()
903     * @see #KEYCODE_ALT_LEFT
904     */
905    public static final int META_ALT_LEFT_ON = 0x10;
906
907    /**
908     * <p>This mask is used to check whether the right the ALT meta key is pressed.</p>
909     *
910     * @see #isAltPressed()
911     * @see #getMetaState()
912     * @see #KEYCODE_ALT_RIGHT
913     */
914    public static final int META_ALT_RIGHT_ON = 0x20;
915
916    /**
917     * <p>This mask is used to check whether one of the SHIFT meta keys is pressed.</p>
918     *
919     * @see #isShiftPressed()
920     * @see #getMetaState()
921     * @see #KEYCODE_SHIFT_LEFT
922     * @see #KEYCODE_SHIFT_RIGHT
923     */
924    public static final int META_SHIFT_ON = 0x1;
925
926    /**
927     * <p>This mask is used to check whether the left SHIFT meta key is pressed.</p>
928     *
929     * @see #isShiftPressed()
930     * @see #getMetaState()
931     * @see #KEYCODE_SHIFT_LEFT
932     */
933    public static final int META_SHIFT_LEFT_ON = 0x40;
934
935    /**
936     * <p>This mask is used to check whether the right SHIFT meta key is pressed.</p>
937     *
938     * @see #isShiftPressed()
939     * @see #getMetaState()
940     * @see #KEYCODE_SHIFT_RIGHT
941     */
942    public static final int META_SHIFT_RIGHT_ON = 0x80;
943
944    /**
945     * <p>This mask is used to check whether the SYM meta key is pressed.</p>
946     *
947     * @see #isSymPressed()
948     * @see #getMetaState()
949     */
950    public static final int META_SYM_ON = 0x4;
951
952    /**
953     * <p>This mask is used to check whether the FUNCTION meta key is pressed.</p>
954     *
955     * @see #isFunctionPressed()
956     * @see #getMetaState()
957     */
958    public static final int META_FUNCTION_ON = 0x8;
959
960    /**
961     * <p>This mask is used to check whether one of the CTRL meta keys is pressed.</p>
962     *
963     * @see #isCtrlPressed()
964     * @see #getMetaState()
965     * @see #KEYCODE_CTRL_LEFT
966     * @see #KEYCODE_CTRL_RIGHT
967     */
968    public static final int META_CTRL_ON = 0x1000;
969
970    /**
971     * <p>This mask is used to check whether the left CTRL meta key is pressed.</p>
972     *
973     * @see #isCtrlPressed()
974     * @see #getMetaState()
975     * @see #KEYCODE_CTRL_LEFT
976     */
977    public static final int META_CTRL_LEFT_ON = 0x2000;
978
979    /**
980     * <p>This mask is used to check whether the right CTRL meta key is pressed.</p>
981     *
982     * @see #isCtrlPressed()
983     * @see #getMetaState()
984     * @see #KEYCODE_CTRL_RIGHT
985     */
986    public static final int META_CTRL_RIGHT_ON = 0x4000;
987
988    /**
989     * <p>This mask is used to check whether one of the META meta keys is pressed.</p>
990     *
991     * @see #isMetaPressed()
992     * @see #getMetaState()
993     * @see #KEYCODE_META_LEFT
994     * @see #KEYCODE_META_RIGHT
995     */
996    public static final int META_META_ON = 0x10000;
997
998    /**
999     * <p>This mask is used to check whether the left META meta key is pressed.</p>
1000     *
1001     * @see #isMetaPressed()
1002     * @see #getMetaState()
1003     * @see #KEYCODE_META_LEFT
1004     */
1005    public static final int META_META_LEFT_ON = 0x20000;
1006
1007    /**
1008     * <p>This mask is used to check whether the right META meta key is pressed.</p>
1009     *
1010     * @see #isMetaPressed()
1011     * @see #getMetaState()
1012     * @see #KEYCODE_META_RIGHT
1013     */
1014    public static final int META_META_RIGHT_ON = 0x40000;
1015
1016    /**
1017     * <p>This mask is used to check whether the CAPS LOCK meta key is on.</p>
1018     *
1019     * @see #isCapsLockOn()
1020     * @see #getMetaState()
1021     * @see #KEYCODE_CAPS_LOCK
1022     */
1023    public static final int META_CAPS_LOCK_ON = 0x100000;
1024
1025    /**
1026     * <p>This mask is used to check whether the NUM LOCK meta key is on.</p>
1027     *
1028     * @see #isNumLockOn()
1029     * @see #getMetaState()
1030     * @see #KEYCODE_NUM_LOCK
1031     */
1032    public static final int META_NUM_LOCK_ON = 0x200000;
1033
1034    /**
1035     * <p>This mask is used to check whether the SCROLL LOCK meta key is on.</p>
1036     *
1037     * @see #isScrollLockOn()
1038     * @see #getMetaState()
1039     * @see #KEYCODE_SCROLL_LOCK
1040     */
1041    public static final int META_SCROLL_LOCK_ON = 0x400000;
1042
1043    /**
1044     * This mask is a combination of {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON}
1045     * and {@link #META_SHIFT_RIGHT_ON}.
1046     */
1047    public static final int META_SHIFT_MASK = META_SHIFT_ON
1048            | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON;
1049
1050    /**
1051     * This mask is a combination of {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON}
1052     * and {@link #META_ALT_RIGHT_ON}.
1053     */
1054    public static final int META_ALT_MASK = META_ALT_ON
1055            | META_ALT_LEFT_ON | META_ALT_RIGHT_ON;
1056
1057    /**
1058     * This mask is a combination of {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON}
1059     * and {@link #META_CTRL_RIGHT_ON}.
1060     */
1061    public static final int META_CTRL_MASK = META_CTRL_ON
1062            | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON;
1063
1064    /**
1065     * This mask is a combination of {@link #META_META_ON}, {@link #META_META_LEFT_ON}
1066     * and {@link #META_META_RIGHT_ON}.
1067     */
1068    public static final int META_META_MASK = META_META_ON
1069            | META_META_LEFT_ON | META_META_RIGHT_ON;
1070
1071    /**
1072     * This mask is set if the device woke because of this key event.
1073     */
1074    public static final int FLAG_WOKE_HERE = 0x1;
1075
1076    /**
1077     * This mask is set if the key event was generated by a software keyboard.
1078     */
1079    public static final int FLAG_SOFT_KEYBOARD = 0x2;
1080
1081    /**
1082     * This mask is set if we don't want the key event to cause us to leave
1083     * touch mode.
1084     */
1085    public static final int FLAG_KEEP_TOUCH_MODE = 0x4;
1086
1087    /**
1088     * This mask is set if an event was known to come from a trusted part
1089     * of the system.  That is, the event is known to come from the user,
1090     * and could not have been spoofed by a third party component.
1091     */
1092    public static final int FLAG_FROM_SYSTEM = 0x8;
1093
1094    /**
1095     * This mask is used for compatibility, to identify enter keys that are
1096     * coming from an IME whose enter key has been auto-labelled "next" or
1097     * "done".  This allows TextView to dispatch these as normal enter keys
1098     * for old applications, but still do the appropriate action when
1099     * receiving them.
1100     */
1101    public static final int FLAG_EDITOR_ACTION = 0x10;
1102
1103    /**
1104     * When associated with up key events, this indicates that the key press
1105     * has been canceled.  Typically this is used with virtual touch screen
1106     * keys, where the user can slide from the virtual key area on to the
1107     * display: in that case, the application will receive a canceled up
1108     * event and should not perform the action normally associated with the
1109     * key.  Note that for this to work, the application can not perform an
1110     * action for a key until it receives an up or the long press timeout has
1111     * expired.
1112     */
1113    public static final int FLAG_CANCELED = 0x20;
1114
1115    /**
1116     * This key event was generated by a virtual (on-screen) hard key area.
1117     * Typically this is an area of the touchscreen, outside of the regular
1118     * display, dedicated to "hardware" buttons.
1119     */
1120    public static final int FLAG_VIRTUAL_HARD_KEY = 0x40;
1121
1122    /**
1123     * This flag is set for the first key repeat that occurs after the
1124     * long press timeout.
1125     */
1126    public static final int FLAG_LONG_PRESS = 0x80;
1127
1128    /**
1129     * Set when a key event has {@link #FLAG_CANCELED} set because a long
1130     * press action was executed while it was down.
1131     */
1132    public static final int FLAG_CANCELED_LONG_PRESS = 0x100;
1133
1134    /**
1135     * Set for {@link #ACTION_UP} when this event's key code is still being
1136     * tracked from its initial down.  That is, somebody requested that tracking
1137     * started on the key down and a long press has not caused
1138     * the tracking to be canceled.
1139     */
1140    public static final int FLAG_TRACKING = 0x200;
1141
1142    /**
1143     * Set when a key event has been synthesized to implement default behavior
1144     * for an event that the application did not handle.
1145     * Fallback key events are generated by unhandled trackball motions
1146     * (to emulate a directional keypad) and by certain unhandled key presses
1147     * that are declared in the key map (such as special function numeric keypad
1148     * keys when numlock is off).
1149     */
1150    public static final int FLAG_FALLBACK = 0x400;
1151
1152    /**
1153     * Private control to determine when an app is tracking a key sequence.
1154     * @hide
1155     */
1156    public static final int FLAG_START_TRACKING = 0x40000000;
1157
1158    /**
1159     * Returns the maximum keycode.
1160     */
1161    public static int getMaxKeyCode() {
1162        return LAST_KEYCODE;
1163    }
1164
1165    /**
1166     * Get the character that is produced by putting accent on the character
1167     * c.
1168     * For example, getDeadChar('`', 'e') returns &egrave;.
1169     */
1170    public static int getDeadChar(int accent, int c) {
1171        return KeyCharacterMap.getDeadChar(accent, c);
1172    }
1173
1174    static final boolean DEBUG = false;
1175    static final String TAG = "KeyEvent";
1176
1177    private static final int MAX_RECYCLED = 10;
1178    private static final Object gRecyclerLock = new Object();
1179    private static int gRecyclerUsed;
1180    private static KeyEvent gRecyclerTop;
1181
1182    private KeyEvent mNext;
1183    private boolean mRecycled;
1184
1185    private int mDeviceId;
1186    private int mSource;
1187    private int mMetaState;
1188    private int mAction;
1189    private int mKeyCode;
1190    private int mScanCode;
1191    private int mRepeatCount;
1192    private int mFlags;
1193    private long mDownTime;
1194    private long mEventTime;
1195    private String mCharacters;
1196
1197    public interface Callback {
1198        /**
1199         * Called when a key down event has occurred.  If you return true,
1200         * you can first call {@link KeyEvent#startTracking()
1201         * KeyEvent.startTracking()} to have the framework track the event
1202         * through its {@link #onKeyUp(int, KeyEvent)} and also call your
1203         * {@link #onKeyLongPress(int, KeyEvent)} if it occurs.
1204         *
1205         * @param keyCode The value in event.getKeyCode().
1206         * @param event Description of the key event.
1207         *
1208         * @return If you handled the event, return true.  If you want to allow
1209         *         the event to be handled by the next receiver, return false.
1210         */
1211        boolean onKeyDown(int keyCode, KeyEvent event);
1212
1213        /**
1214         * Called when a long press has occurred.  If you return true,
1215         * the final key up will have {@link KeyEvent#FLAG_CANCELED} and
1216         * {@link KeyEvent#FLAG_CANCELED_LONG_PRESS} set.  Note that in
1217         * order to receive this callback, someone in the event change
1218         * <em>must</em> return true from {@link #onKeyDown} <em>and</em>
1219         * call {@link KeyEvent#startTracking()} on the event.
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 onKeyLongPress(int keyCode, KeyEvent event);
1228
1229        /**
1230         * Called when a key up event has occurred.
1231         *
1232         * @param keyCode The value in event.getKeyCode().
1233         * @param event Description of the key event.
1234         *
1235         * @return If you handled the event, return true.  If you want to allow
1236         *         the event to be handled by the next receiver, return false.
1237         */
1238        boolean onKeyUp(int keyCode, KeyEvent event);
1239
1240        /**
1241         * Called when multiple down/up pairs of the same key have occurred
1242         * in a row.
1243         *
1244         * @param keyCode The value in event.getKeyCode().
1245         * @param count Number of pairs as returned by event.getRepeatCount().
1246         * @param event Description of the key event.
1247         *
1248         * @return If you handled the event, return true.  If you want to allow
1249         *         the event to be handled by the next receiver, return false.
1250         */
1251        boolean onKeyMultiple(int keyCode, int count, KeyEvent event);
1252    }
1253
1254    static {
1255        populateKeycodeSymbolicNames();
1256    }
1257
1258    private KeyEvent() {
1259    }
1260
1261    /**
1262     * Create a new key event.
1263     *
1264     * @param action Action code: either {@link #ACTION_DOWN},
1265     * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1266     * @param code The key code.
1267     */
1268    public KeyEvent(int action, int code) {
1269        mAction = action;
1270        mKeyCode = code;
1271        mRepeatCount = 0;
1272        mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
1273    }
1274
1275    /**
1276     * Create a new key event.
1277     *
1278     * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1279     * at which this key code originally went down.
1280     * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1281     * at which this event happened.
1282     * @param action Action code: either {@link #ACTION_DOWN},
1283     * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1284     * @param code The key code.
1285     * @param repeat A repeat count for down events (> 0 if this is after the
1286     * initial down) or event count for multiple events.
1287     */
1288    public KeyEvent(long downTime, long eventTime, int action,
1289                    int code, int repeat) {
1290        mDownTime = downTime;
1291        mEventTime = eventTime;
1292        mAction = action;
1293        mKeyCode = code;
1294        mRepeatCount = repeat;
1295        mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
1296    }
1297
1298    /**
1299     * Create a new key event.
1300     *
1301     * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1302     * at which this key code originally went down.
1303     * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1304     * at which this event happened.
1305     * @param action Action code: either {@link #ACTION_DOWN},
1306     * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1307     * @param code The key code.
1308     * @param repeat A repeat count for down events (> 0 if this is after the
1309     * initial down) or event count for multiple events.
1310     * @param metaState Flags indicating which meta keys are currently pressed.
1311     */
1312    public KeyEvent(long downTime, long eventTime, int action,
1313                    int code, int repeat, int metaState) {
1314        mDownTime = downTime;
1315        mEventTime = eventTime;
1316        mAction = action;
1317        mKeyCode = code;
1318        mRepeatCount = repeat;
1319        mMetaState = metaState;
1320        mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
1321    }
1322
1323    /**
1324     * Create a new key event.
1325     *
1326     * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1327     * at which this key code originally went down.
1328     * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1329     * at which this event happened.
1330     * @param action Action code: either {@link #ACTION_DOWN},
1331     * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1332     * @param code The key code.
1333     * @param repeat A repeat count for down events (> 0 if this is after the
1334     * initial down) or event count for multiple events.
1335     * @param metaState Flags indicating which meta keys are currently pressed.
1336     * @param deviceId The device ID that generated the key event.
1337     * @param scancode Raw device scan code of the event.
1338     */
1339    public KeyEvent(long downTime, long eventTime, int action,
1340                    int code, int repeat, int metaState,
1341                    int deviceId, int scancode) {
1342        mDownTime = downTime;
1343        mEventTime = eventTime;
1344        mAction = action;
1345        mKeyCode = code;
1346        mRepeatCount = repeat;
1347        mMetaState = metaState;
1348        mDeviceId = deviceId;
1349        mScanCode = scancode;
1350    }
1351
1352    /**
1353     * Create a new key event.
1354     *
1355     * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1356     * at which this key code originally went down.
1357     * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1358     * at which this event happened.
1359     * @param action Action code: either {@link #ACTION_DOWN},
1360     * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1361     * @param code The key code.
1362     * @param repeat A repeat count for down events (> 0 if this is after the
1363     * initial down) or event count for multiple events.
1364     * @param metaState Flags indicating which meta keys are currently pressed.
1365     * @param deviceId The device ID that generated the key event.
1366     * @param scancode Raw device scan code of the event.
1367     * @param flags The flags for this key event
1368     */
1369    public KeyEvent(long downTime, long eventTime, int action,
1370                    int code, int repeat, int metaState,
1371                    int deviceId, int scancode, int flags) {
1372        mDownTime = downTime;
1373        mEventTime = eventTime;
1374        mAction = action;
1375        mKeyCode = code;
1376        mRepeatCount = repeat;
1377        mMetaState = metaState;
1378        mDeviceId = deviceId;
1379        mScanCode = scancode;
1380        mFlags = flags;
1381    }
1382
1383    /**
1384     * Create a new key event.
1385     *
1386     * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1387     * at which this key code originally went down.
1388     * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1389     * at which this event happened.
1390     * @param action Action code: either {@link #ACTION_DOWN},
1391     * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1392     * @param code The key code.
1393     * @param repeat A repeat count for down events (> 0 if this is after the
1394     * initial down) or event count for multiple events.
1395     * @param metaState Flags indicating which meta keys are currently pressed.
1396     * @param deviceId The device ID that generated the key event.
1397     * @param scancode Raw device scan code of the event.
1398     * @param flags The flags for this key event
1399     * @param source The input source such as {@link InputDevice#SOURCE_KEYBOARD}.
1400     */
1401    public KeyEvent(long downTime, long eventTime, int action,
1402                    int code, int repeat, int metaState,
1403                    int deviceId, int scancode, int flags, int source) {
1404        mDownTime = downTime;
1405        mEventTime = eventTime;
1406        mAction = action;
1407        mKeyCode = code;
1408        mRepeatCount = repeat;
1409        mMetaState = metaState;
1410        mDeviceId = deviceId;
1411        mScanCode = scancode;
1412        mFlags = flags;
1413        mSource = source;
1414    }
1415
1416    /**
1417     * Create a new key event for a string of characters.  The key code,
1418     * action, repeat count and source will automatically be set to
1419     * {@link #KEYCODE_UNKNOWN}, {@link #ACTION_MULTIPLE}, 0, and
1420     * {@link InputDevice#SOURCE_KEYBOARD} for you.
1421     *
1422     * @param time The time (in {@link android.os.SystemClock#uptimeMillis})
1423     * at which this event occured.
1424     * @param characters The string of characters.
1425     * @param deviceId The device ID that generated the key event.
1426     * @param flags The flags for this key event
1427     */
1428    public KeyEvent(long time, String characters, int deviceId, int flags) {
1429        mDownTime = time;
1430        mEventTime = time;
1431        mCharacters = characters;
1432        mAction = ACTION_MULTIPLE;
1433        mKeyCode = KEYCODE_UNKNOWN;
1434        mRepeatCount = 0;
1435        mDeviceId = deviceId;
1436        mFlags = flags;
1437        mSource = InputDevice.SOURCE_KEYBOARD;
1438    }
1439
1440    /**
1441     * Make an exact copy of an existing key event.
1442     */
1443    public KeyEvent(KeyEvent origEvent) {
1444        mDownTime = origEvent.mDownTime;
1445        mEventTime = origEvent.mEventTime;
1446        mAction = origEvent.mAction;
1447        mKeyCode = origEvent.mKeyCode;
1448        mRepeatCount = origEvent.mRepeatCount;
1449        mMetaState = origEvent.mMetaState;
1450        mDeviceId = origEvent.mDeviceId;
1451        mSource = origEvent.mSource;
1452        mScanCode = origEvent.mScanCode;
1453        mFlags = origEvent.mFlags;
1454        mCharacters = origEvent.mCharacters;
1455    }
1456
1457    /**
1458     * Copy an existing key event, modifying its time and repeat count.
1459     *
1460     * @deprecated Use {@link #changeTimeRepeat(KeyEvent, long, int)}
1461     * instead.
1462     *
1463     * @param origEvent The existing event to be copied.
1464     * @param eventTime The new event time
1465     * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1466     * @param newRepeat The new repeat count of the event.
1467     */
1468    @Deprecated
1469    public KeyEvent(KeyEvent origEvent, long eventTime, int newRepeat) {
1470        mDownTime = origEvent.mDownTime;
1471        mEventTime = eventTime;
1472        mAction = origEvent.mAction;
1473        mKeyCode = origEvent.mKeyCode;
1474        mRepeatCount = newRepeat;
1475        mMetaState = origEvent.mMetaState;
1476        mDeviceId = origEvent.mDeviceId;
1477        mSource = origEvent.mSource;
1478        mScanCode = origEvent.mScanCode;
1479        mFlags = origEvent.mFlags;
1480        mCharacters = origEvent.mCharacters;
1481    }
1482
1483    private static KeyEvent obtain() {
1484        final KeyEvent ev;
1485        synchronized (gRecyclerLock) {
1486            ev = gRecyclerTop;
1487            if (ev == null) {
1488                return new KeyEvent();
1489            }
1490            gRecyclerTop = ev.mNext;
1491            gRecyclerUsed -= 1;
1492        }
1493        ev.mRecycled = false;
1494        ev.mNext = null;
1495        return ev;
1496    }
1497
1498    /**
1499     * Obtains a (potentially recycled) key event.
1500     *
1501     * @hide
1502     */
1503    public static KeyEvent obtain(long downTime, long eventTime, int action,
1504                    int code, int repeat, int metaState,
1505                    int deviceId, int scancode, int flags, int source, String characters) {
1506        KeyEvent ev = obtain();
1507        ev.mDownTime = downTime;
1508        ev.mEventTime = eventTime;
1509        ev.mAction = action;
1510        ev.mKeyCode = code;
1511        ev.mRepeatCount = repeat;
1512        ev.mMetaState = metaState;
1513        ev.mDeviceId = deviceId;
1514        ev.mScanCode = scancode;
1515        ev.mFlags = flags;
1516        ev.mSource = source;
1517        ev.mCharacters = characters;
1518        return ev;
1519    }
1520
1521    /**
1522     * Recycles a key event.
1523     * Key events should only be recycled if they are owned by the system since user
1524     * code expects them to be essentially immutable, "tracking" notwithstanding.
1525     *
1526     * @hide
1527     */
1528    public final void recycle() {
1529        if (mRecycled) {
1530            throw new RuntimeException(toString() + " recycled twice!");
1531        }
1532        mRecycled = true;
1533        mCharacters = null;
1534
1535        synchronized (gRecyclerLock) {
1536            if (gRecyclerUsed < MAX_RECYCLED) {
1537                gRecyclerUsed++;
1538                mNext = gRecyclerTop;
1539                gRecyclerTop = this;
1540            }
1541        }
1542    }
1543
1544    /**
1545     * Create a new key event that is the same as the given one, but whose
1546     * event time and repeat count are replaced with the given value.
1547     *
1548     * @param event The existing event to be copied.  This is not modified.
1549     * @param eventTime The new event time
1550     * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1551     * @param newRepeat The new repeat count of the event.
1552     */
1553    public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime,
1554            int newRepeat) {
1555        return new KeyEvent(event, eventTime, newRepeat);
1556    }
1557
1558    /**
1559     * Create a new key event that is the same as the given one, but whose
1560     * event time and repeat count are replaced with the given value.
1561     *
1562     * @param event The existing event to be copied.  This is not modified.
1563     * @param eventTime The new event time
1564     * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1565     * @param newRepeat The new repeat count of the event.
1566     * @param newFlags New flags for the event, replacing the entire value
1567     * in the original event.
1568     */
1569    public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime,
1570            int newRepeat, int newFlags) {
1571        KeyEvent ret = new KeyEvent(event);
1572        ret.mEventTime = eventTime;
1573        ret.mRepeatCount = newRepeat;
1574        ret.mFlags = newFlags;
1575        return ret;
1576    }
1577
1578    /**
1579     * Copy an existing key event, modifying its action.
1580     *
1581     * @param origEvent The existing event to be copied.
1582     * @param action The new action code of the event.
1583     */
1584    private KeyEvent(KeyEvent origEvent, int action) {
1585        mDownTime = origEvent.mDownTime;
1586        mEventTime = origEvent.mEventTime;
1587        mAction = action;
1588        mKeyCode = origEvent.mKeyCode;
1589        mRepeatCount = origEvent.mRepeatCount;
1590        mMetaState = origEvent.mMetaState;
1591        mDeviceId = origEvent.mDeviceId;
1592        mSource = origEvent.mSource;
1593        mScanCode = origEvent.mScanCode;
1594        mFlags = origEvent.mFlags;
1595        // Don't copy mCharacters, since one way or the other we'll lose it
1596        // when changing the action.
1597    }
1598
1599    /**
1600     * Create a new key event that is the same as the given one, but whose
1601     * action is replaced with the given value.
1602     *
1603     * @param event The existing event to be copied.  This is not modified.
1604     * @param action The new action code of the event.
1605     */
1606    public static KeyEvent changeAction(KeyEvent event, int action) {
1607        return new KeyEvent(event, action);
1608    }
1609
1610    /**
1611     * Create a new key event that is the same as the given one, but whose
1612     * flags are replaced with the given value.
1613     *
1614     * @param event The existing event to be copied.  This is not modified.
1615     * @param flags The new flags constant.
1616     */
1617    public static KeyEvent changeFlags(KeyEvent event, int flags) {
1618        event = new KeyEvent(event);
1619        event.mFlags = flags;
1620        return event;
1621    }
1622
1623    /**
1624     * Don't use in new code, instead explicitly check
1625     * {@link #getAction()}.
1626     *
1627     * @return If the action is ACTION_DOWN, returns true; else false.
1628     *
1629     * @deprecated
1630     * @hide
1631     */
1632    @Deprecated public final boolean isDown() {
1633        return mAction == ACTION_DOWN;
1634    }
1635
1636    /**
1637     * Is this a system key?  System keys can not be used for menu shortcuts.
1638     *
1639     * TODO: this information should come from a table somewhere.
1640     * TODO: should the dpad keys be here?  arguably, because they also shouldn't be menu shortcuts
1641     */
1642    public final boolean isSystem() {
1643        return native_isSystemKey(mKeyCode);
1644    }
1645
1646    /** @hide */
1647    public final boolean hasDefaultAction() {
1648        return native_hasDefaultAction(mKeyCode);
1649    }
1650
1651    /**
1652     * Returns true if the specified keycode is a gamepad button.
1653     * @return True if the keycode is a gamepad button, such as {@link #KEYCODE_BUTTON_A}.
1654     */
1655    public static final boolean isGamepadButton(int keyCode) {
1656        switch (keyCode) {
1657            case KeyEvent.KEYCODE_BUTTON_A:
1658            case KeyEvent.KEYCODE_BUTTON_B:
1659            case KeyEvent.KEYCODE_BUTTON_C:
1660            case KeyEvent.KEYCODE_BUTTON_X:
1661            case KeyEvent.KEYCODE_BUTTON_Y:
1662            case KeyEvent.KEYCODE_BUTTON_Z:
1663            case KeyEvent.KEYCODE_BUTTON_L1:
1664            case KeyEvent.KEYCODE_BUTTON_R1:
1665            case KeyEvent.KEYCODE_BUTTON_L2:
1666            case KeyEvent.KEYCODE_BUTTON_R2:
1667            case KeyEvent.KEYCODE_BUTTON_THUMBL:
1668            case KeyEvent.KEYCODE_BUTTON_THUMBR:
1669            case KeyEvent.KEYCODE_BUTTON_START:
1670            case KeyEvent.KEYCODE_BUTTON_SELECT:
1671            case KeyEvent.KEYCODE_BUTTON_MODE:
1672            case KeyEvent.KEYCODE_BUTTON_1:
1673            case KeyEvent.KEYCODE_BUTTON_2:
1674            case KeyEvent.KEYCODE_BUTTON_3:
1675            case KeyEvent.KEYCODE_BUTTON_4:
1676            case KeyEvent.KEYCODE_BUTTON_5:
1677            case KeyEvent.KEYCODE_BUTTON_6:
1678            case KeyEvent.KEYCODE_BUTTON_7:
1679            case KeyEvent.KEYCODE_BUTTON_8:
1680            case KeyEvent.KEYCODE_BUTTON_9:
1681            case KeyEvent.KEYCODE_BUTTON_10:
1682            case KeyEvent.KEYCODE_BUTTON_11:
1683            case KeyEvent.KEYCODE_BUTTON_12:
1684            case KeyEvent.KEYCODE_BUTTON_13:
1685            case KeyEvent.KEYCODE_BUTTON_14:
1686            case KeyEvent.KEYCODE_BUTTON_15:
1687            case KeyEvent.KEYCODE_BUTTON_16:
1688                return true;
1689            default:
1690                return false;
1691        }
1692    }
1693
1694    /** {@inheritDoc} */
1695    @Override
1696    public final int getDeviceId() {
1697        return mDeviceId;
1698    }
1699
1700    /** {@inheritDoc} */
1701    @Override
1702    public final int getSource() {
1703        return mSource;
1704    }
1705
1706    /** {@inheritDoc} */
1707    @Override
1708    public final void setSource(int source) {
1709        mSource = source;
1710    }
1711
1712    /**
1713     * <p>Returns the state of the meta keys.</p>
1714     *
1715     * @return an integer in which each bit set to 1 represents a pressed
1716     *         meta key
1717     *
1718     * @see #isAltPressed()
1719     * @see #isShiftPressed()
1720     * @see #isSymPressed()
1721     * @see #isCtrlPressed()
1722     * @see #isMetaPressed()
1723     * @see #isFunctionPressed()
1724     * @see #isCapsLockOn()
1725     * @see #isNumLockOn()
1726     * @see #isScrollLockOn()
1727     * @see #META_ALT_ON
1728     * @see #META_ALT_LEFT_ON
1729     * @see #META_ALT_RIGHT_ON
1730     * @see #META_SHIFT_ON
1731     * @see #META_SHIFT_LEFT_ON
1732     * @see #META_SHIFT_RIGHT_ON
1733     * @see #META_SYM_ON
1734     * @see #META_FUNCTION_ON
1735     * @see #META_CTRL_ON
1736     * @see #META_CTRL_LEFT_ON
1737     * @see #META_CTRL_RIGHT_ON
1738     * @see #META_META_ON
1739     * @see #META_META_LEFT_ON
1740     * @see #META_META_RIGHT_ON
1741     * @see #META_CAPS_LOCK_ON
1742     * @see #META_NUM_LOCK_ON
1743     * @see #META_SCROLL_LOCK_ON
1744     */
1745    public final int getMetaState() {
1746        return mMetaState;
1747    }
1748
1749    /**
1750     * Returns the flags for this key event.
1751     *
1752     * @see #FLAG_WOKE_HERE
1753     */
1754    public final int getFlags() {
1755        return mFlags;
1756    }
1757
1758    // Mask of all modifier key meta states.  Specifically excludes locked keys like caps lock.
1759    private static final int META_MODIFIER_MASK =
1760            META_SHIFT_ON | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON
1761            | META_ALT_ON | META_ALT_LEFT_ON | META_ALT_RIGHT_ON
1762            | META_CTRL_ON | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON
1763            | META_META_ON | META_META_LEFT_ON | META_META_RIGHT_ON
1764            | META_SYM_ON | META_FUNCTION_ON;
1765
1766    // Mask of all lock key meta states.
1767    private static final int META_LOCK_MASK =
1768            META_CAPS_LOCK_ON | META_NUM_LOCK_ON | META_SCROLL_LOCK_ON;
1769
1770    // Mask of all valid meta states.
1771    private static final int META_ALL_MASK = META_MODIFIER_MASK | META_LOCK_MASK;
1772
1773    // Mask of all synthetic meta states that are reserved for API compatibility with
1774    // historical uses in MetaKeyKeyListener.
1775    private static final int META_SYNTHETIC_MASK =
1776            META_CAP_LOCKED | META_ALT_LOCKED | META_SYM_LOCKED | META_SELECTING;
1777
1778    // Mask of all meta states that are not valid use in specifying a modifier key.
1779    // These bits are known to be used for purposes other than specifying modifiers.
1780    private static final int META_INVALID_MODIFIER_MASK =
1781            META_LOCK_MASK | META_SYNTHETIC_MASK;
1782
1783    /**
1784     * Gets a mask that includes all valid modifier key meta state bits.
1785     * <p>
1786     * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1787     * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1788     * not considered modifier keys.  Consequently, the mask specifically excludes
1789     * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1790     * </p>
1791     *
1792     * @return The modifier meta state mask which is a combination of
1793     * {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON}, {@link #META_SHIFT_RIGHT_ON},
1794     * {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON}, {@link #META_ALT_RIGHT_ON},
1795     * {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON}, {@link #META_CTRL_RIGHT_ON},
1796     * {@link #META_META_ON}, {@link #META_META_LEFT_ON}, {@link #META_META_RIGHT_ON},
1797     * {@link #META_SYM_ON}, {@link #META_FUNCTION_ON}.
1798     */
1799    public static int getModifierMetaStateMask() {
1800        return META_MODIFIER_MASK;
1801    }
1802
1803    /**
1804     * Returns true if this key code is a modifier key.
1805     * <p>
1806     * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1807     * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1808     * not considered modifier keys.  Consequently, this function return false
1809     * for those keys.
1810     * </p>
1811     *
1812     * @return True if the key code is one of
1813     * {@link #KEYCODE_SHIFT_LEFT} {@link #KEYCODE_SHIFT_RIGHT},
1814     * {@link #KEYCODE_ALT_LEFT}, {@link #KEYCODE_ALT_RIGHT},
1815     * {@link #KEYCODE_CTRL_LEFT}, {@link #KEYCODE_CTRL_RIGHT},
1816     * {@link #KEYCODE_META_LEFT}, or {@link #KEYCODE_META_RIGHT},
1817     * {@link #KEYCODE_SYM}, {@link #KEYCODE_NUM}, {@link #KEYCODE_FUNCTION}.
1818     */
1819    public static boolean isModifierKey(int keyCode) {
1820        switch (keyCode) {
1821            case KEYCODE_SHIFT_LEFT:
1822            case KEYCODE_SHIFT_RIGHT:
1823            case KEYCODE_ALT_LEFT:
1824            case KEYCODE_ALT_RIGHT:
1825            case KEYCODE_CTRL_LEFT:
1826            case KEYCODE_CTRL_RIGHT:
1827            case KEYCODE_META_LEFT:
1828            case KEYCODE_META_RIGHT:
1829            case KEYCODE_SYM:
1830            case KEYCODE_NUM:
1831            case KEYCODE_FUNCTION:
1832                return true;
1833            default:
1834                return false;
1835        }
1836    }
1837
1838    /**
1839     * Normalizes the specified meta state.
1840     * <p>
1841     * The meta state is normalized such that if either the left or right modifier meta state
1842     * bits are set then the result will also include the universal bit for that modifier.
1843     * </p><p>
1844     * If the specified meta state contains {@link #META_ALT_LEFT_ON} then
1845     * the result will also contain {@link #META_ALT_ON} in addition to {@link #META_ALT_LEFT_ON}
1846     * and the other bits that were specified in the input.  The same is process is
1847     * performed for shift, control and meta.
1848     * </p><p>
1849     * If the specified meta state contains synthetic meta states defined by
1850     * {@link MetaKeyKeyListener}, then those states are translated here and the original
1851     * synthetic meta states are removed from the result.
1852     * {@link MetaKeyKeyListener#META_CAP_LOCKED} is translated to {@link #META_CAPS_LOCK_ON}.
1853     * {@link MetaKeyKeyListener#META_ALT_LOCKED} is translated to {@link #META_ALT_ON}.
1854     * {@link MetaKeyKeyListener#META_SYM_LOCKED} is translated to {@link #META_SYM_ON}.
1855     * </p><p>
1856     * Undefined meta state bits are removed.
1857     * </p>
1858     *
1859     * @param metaState The meta state.
1860     * @return The normalized meta state.
1861     */
1862    public static int normalizeMetaState(int metaState) {
1863        if ((metaState & (META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON)) != 0) {
1864            metaState |= META_SHIFT_ON;
1865        }
1866        if ((metaState & (META_ALT_LEFT_ON | META_ALT_RIGHT_ON)) != 0) {
1867            metaState |= META_ALT_ON;
1868        }
1869        if ((metaState & (META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON)) != 0) {
1870            metaState |= META_CTRL_ON;
1871        }
1872        if ((metaState & (META_META_LEFT_ON | META_META_RIGHT_ON)) != 0) {
1873            metaState |= META_META_ON;
1874        }
1875        if ((metaState & MetaKeyKeyListener.META_CAP_LOCKED) != 0) {
1876            metaState |= META_CAPS_LOCK_ON;
1877        }
1878        if ((metaState & MetaKeyKeyListener.META_ALT_LOCKED) != 0) {
1879            metaState |= META_ALT_ON;
1880        }
1881        if ((metaState & MetaKeyKeyListener.META_SYM_LOCKED) != 0) {
1882            metaState |= META_SYM_ON;
1883        }
1884        return metaState & META_ALL_MASK;
1885    }
1886
1887    /**
1888     * Returns true if no modifiers keys are pressed according to the specified meta state.
1889     * <p>
1890     * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1891     * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1892     * not considered modifier keys.  Consequently, this function ignores
1893     * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1894     * </p><p>
1895     * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}.
1896     * </p>
1897     *
1898     * @param metaState The meta state to consider.
1899     * @return True if no modifier keys are pressed.
1900     * @see #hasNoModifiers()
1901     */
1902    public static boolean metaStateHasNoModifiers(int metaState) {
1903        return (normalizeMetaState(metaState) & META_MODIFIER_MASK) == 0;
1904    }
1905
1906    /**
1907     * Returns true if only the specified modifier keys are pressed according to
1908     * the specified meta state.  Returns false if a different combination of modifier
1909     * keys are pressed.
1910     * <p>
1911     * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1912     * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1913     * not considered modifier keys.  Consequently, this function ignores
1914     * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1915     * </p><p>
1916     * If the specified modifier mask includes directional modifiers, such as
1917     * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the
1918     * modifier is pressed on that side.
1919     * If the specified modifier mask includes non-directional modifiers, such as
1920     * {@link #META_SHIFT_ON}, then this method ensures that the modifier
1921     * is pressed on either side.
1922     * If the specified modifier mask includes both directional and non-directional modifiers
1923     * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON},
1924     * then this method throws an illegal argument exception.
1925     * </p>
1926     *
1927     * @param metaState The meta state to consider.
1928     * @param modifiers The meta state of the modifier keys to check.  May be a combination
1929     * of modifier meta states as defined by {@link #getModifierMetaStateMask()}.  May be 0 to
1930     * ensure that no modifier keys are pressed.
1931     * @return True if only the specified modifier keys are pressed.
1932     * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers
1933     * @see #hasModifiers
1934     */
1935    public static boolean metaStateHasModifiers(int metaState, int modifiers) {
1936        // Note: For forward compatibility, we allow the parameter to contain meta states
1937        //       that we do not recognize but we explicitly disallow meta states that
1938        //       are not valid modifiers.
1939        if ((modifiers & META_INVALID_MODIFIER_MASK) != 0) {
1940            throw new IllegalArgumentException("modifiers must not contain "
1941                    + "META_CAPS_LOCK_ON, META_NUM_LOCK_ON, META_SCROLL_LOCK_ON, "
1942                    + "META_CAP_LOCKED, META_ALT_LOCKED, META_SYM_LOCKED, "
1943                    + "or META_SELECTING");
1944        }
1945
1946        metaState = normalizeMetaState(metaState) & META_MODIFIER_MASK;
1947        metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
1948                META_SHIFT_ON, META_SHIFT_LEFT_ON, META_SHIFT_RIGHT_ON);
1949        metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
1950                META_ALT_ON, META_ALT_LEFT_ON, META_ALT_RIGHT_ON);
1951        metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
1952                META_CTRL_ON, META_CTRL_LEFT_ON, META_CTRL_RIGHT_ON);
1953        metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
1954                META_META_ON, META_META_LEFT_ON, META_META_RIGHT_ON);
1955        return metaState == modifiers;
1956    }
1957
1958    private static int metaStateFilterDirectionalModifiers(int metaState,
1959            int modifiers, int basic, int left, int right) {
1960        final boolean wantBasic = (modifiers & basic) != 0;
1961        final int directional = left | right;
1962        final boolean wantLeftOrRight = (modifiers & directional) != 0;
1963
1964        if (wantBasic) {
1965            if (wantLeftOrRight) {
1966                throw new IllegalArgumentException("modifiers must not contain "
1967                        + metaStateToString(basic) + " combined with "
1968                        + metaStateToString(left) + " or " + metaStateToString(right));
1969            }
1970            return metaState & ~directional;
1971        } else if (wantLeftOrRight) {
1972            return metaState & ~basic;
1973        } else {
1974            return metaState;
1975        }
1976    }
1977
1978    /**
1979     * Returns true if no modifier keys are pressed.
1980     * <p>
1981     * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1982     * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1983     * not considered modifier keys.  Consequently, this function ignores
1984     * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1985     * </p><p>
1986     * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}.
1987     * </p>
1988     *
1989     * @return True if no modifier keys are pressed.
1990     * @see #metaStateHasNoModifiers
1991     */
1992    public final boolean hasNoModifiers() {
1993        return metaStateHasNoModifiers(mMetaState);
1994    }
1995
1996    /**
1997     * Returns true if only the specified modifiers keys are pressed.
1998     * Returns false if a different combination of modifier keys are pressed.
1999     * <p>
2000     * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2001     * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2002     * not considered modifier keys.  Consequently, this function ignores
2003     * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2004     * </p><p>
2005     * If the specified modifier mask includes directional modifiers, such as
2006     * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the
2007     * modifier is pressed on that side.
2008     * If the specified modifier mask includes non-directional modifiers, such as
2009     * {@link #META_SHIFT_ON}, then this method ensures that the modifier
2010     * is pressed on either side.
2011     * If the specified modifier mask includes both directional and non-directional modifiers
2012     * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON},
2013     * then this method throws an illegal argument exception.
2014     * </p>
2015     *
2016     * @param modifiers The meta state of the modifier keys to check.  May be a combination
2017     * of modifier meta states as defined by {@link #getModifierMetaStateMask()}.  May be 0 to
2018     * ensure that no modifier keys are pressed.
2019     * @return True if only the specified modifier keys are pressed.
2020     * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers
2021     * @see #metaStateHasModifiers
2022     */
2023    public final boolean hasModifiers(int modifiers) {
2024        return metaStateHasModifiers(mMetaState, modifiers);
2025    }
2026
2027    /**
2028     * <p>Returns the pressed state of the ALT meta key.</p>
2029     *
2030     * @return true if the ALT key is pressed, false otherwise
2031     *
2032     * @see #KEYCODE_ALT_LEFT
2033     * @see #KEYCODE_ALT_RIGHT
2034     * @see #META_ALT_ON
2035     */
2036    public final boolean isAltPressed() {
2037        return (mMetaState & META_ALT_ON) != 0;
2038    }
2039
2040    /**
2041     * <p>Returns the pressed state of the SHIFT meta key.</p>
2042     *
2043     * @return true if the SHIFT key is pressed, false otherwise
2044     *
2045     * @see #KEYCODE_SHIFT_LEFT
2046     * @see #KEYCODE_SHIFT_RIGHT
2047     * @see #META_SHIFT_ON
2048     */
2049    public final boolean isShiftPressed() {
2050        return (mMetaState & META_SHIFT_ON) != 0;
2051    }
2052
2053    /**
2054     * <p>Returns the pressed state of the SYM meta key.</p>
2055     *
2056     * @return true if the SYM key is pressed, false otherwise
2057     *
2058     * @see #KEYCODE_SYM
2059     * @see #META_SYM_ON
2060     */
2061    public final boolean isSymPressed() {
2062        return (mMetaState & META_SYM_ON) != 0;
2063    }
2064
2065    /**
2066     * <p>Returns the pressed state of the CTRL meta key.</p>
2067     *
2068     * @return true if the CTRL key is pressed, false otherwise
2069     *
2070     * @see #KEYCODE_CTRL_LEFT
2071     * @see #KEYCODE_CTRL_RIGHT
2072     * @see #META_CTRL_ON
2073     */
2074    public final boolean isCtrlPressed() {
2075        return (mMetaState & META_CTRL_ON) != 0;
2076    }
2077
2078    /**
2079     * <p>Returns the pressed state of the META meta key.</p>
2080     *
2081     * @return true if the META key is pressed, false otherwise
2082     *
2083     * @see #KEYCODE_META_LEFT
2084     * @see #KEYCODE_META_RIGHT
2085     * @see #META_META_ON
2086     */
2087    public final boolean isMetaPressed() {
2088        return (mMetaState & META_META_ON) != 0;
2089    }
2090
2091    /**
2092     * <p>Returns the pressed state of the FUNCTION meta key.</p>
2093     *
2094     * @return true if the FUNCTION key is pressed, false otherwise
2095     *
2096     * @see #KEYCODE_FUNCTION
2097     * @see #META_FUNCTION_ON
2098     */
2099    public final boolean isFunctionPressed() {
2100        return (mMetaState & META_FUNCTION_ON) != 0;
2101    }
2102
2103    /**
2104     * <p>Returns the locked state of the CAPS LOCK meta key.</p>
2105     *
2106     * @return true if the CAPS LOCK key is on, false otherwise
2107     *
2108     * @see #KEYCODE_CAPS_LOCK
2109     * @see #META_CAPS_LOCK_ON
2110     */
2111    public final boolean isCapsLockOn() {
2112        return (mMetaState & META_CAPS_LOCK_ON) != 0;
2113    }
2114
2115    /**
2116     * <p>Returns the locked state of the NUM LOCK meta key.</p>
2117     *
2118     * @return true if the NUM LOCK key is on, false otherwise
2119     *
2120     * @see #KEYCODE_NUM_LOCK
2121     * @see #META_NUM_LOCK_ON
2122     */
2123    public final boolean isNumLockOn() {
2124        return (mMetaState & META_NUM_LOCK_ON) != 0;
2125    }
2126
2127    /**
2128     * <p>Returns the locked state of the SCROLL LOCK meta key.</p>
2129     *
2130     * @return true if the SCROLL LOCK key is on, false otherwise
2131     *
2132     * @see #KEYCODE_SCROLL_LOCK
2133     * @see #META_SCROLL_LOCK_ON
2134     */
2135    public final boolean isScrollLockOn() {
2136        return (mMetaState & META_SCROLL_LOCK_ON) != 0;
2137    }
2138
2139    /**
2140     * Retrieve the action of this key event.  May be either
2141     * {@link #ACTION_DOWN}, {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
2142     *
2143     * @return The event action: ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.
2144     */
2145    public final int getAction() {
2146        return mAction;
2147    }
2148
2149    /**
2150     * For {@link #ACTION_UP} events, indicates that the event has been
2151     * canceled as per {@link #FLAG_CANCELED}.
2152     */
2153    public final boolean isCanceled() {
2154        return (mFlags&FLAG_CANCELED) != 0;
2155    }
2156
2157    /**
2158     * Call this during {@link Callback#onKeyDown} to have the system track
2159     * the key through its final up (possibly including a long press).  Note
2160     * that only one key can be tracked at a time -- if another key down
2161     * event is received while a previous one is being tracked, tracking is
2162     * stopped on the previous event.
2163     */
2164    public final void startTracking() {
2165        mFlags |= FLAG_START_TRACKING;
2166    }
2167
2168    /**
2169     * For {@link #ACTION_UP} events, indicates that the event is still being
2170     * tracked from its initial down event as per
2171     * {@link #FLAG_TRACKING}.
2172     */
2173    public final boolean isTracking() {
2174        return (mFlags&FLAG_TRACKING) != 0;
2175    }
2176
2177    /**
2178     * For {@link #ACTION_DOWN} events, indicates that the event has been
2179     * canceled as per {@link #FLAG_LONG_PRESS}.
2180     */
2181    public final boolean isLongPress() {
2182        return (mFlags&FLAG_LONG_PRESS) != 0;
2183    }
2184
2185    /**
2186     * Retrieve the key code of the key event.  This is the physical key that
2187     * was pressed, <em>not</em> the Unicode character.
2188     *
2189     * @return The key code of the event.
2190     */
2191    public final int getKeyCode() {
2192        return mKeyCode;
2193    }
2194
2195    /**
2196     * For the special case of a {@link #ACTION_MULTIPLE} event with key
2197     * code of {@link #KEYCODE_UNKNOWN}, this is a raw string of characters
2198     * associated with the event.  In all other cases it is null.
2199     *
2200     * @return Returns a String of 1 or more characters associated with
2201     * the event.
2202     */
2203    public final String getCharacters() {
2204        return mCharacters;
2205    }
2206
2207    /**
2208     * Retrieve the hardware key id of this key event.  These values are not
2209     * reliable and vary from device to device.
2210     *
2211     * {@more}
2212     * Mostly this is here for debugging purposes.
2213     */
2214    public final int getScanCode() {
2215        return mScanCode;
2216    }
2217
2218    /**
2219     * Retrieve the repeat count of the event.  For both key up and key down
2220     * events, this is the number of times the key has repeated with the first
2221     * down starting at 0 and counting up from there.  For multiple key
2222     * events, this is the number of down/up pairs that have occurred.
2223     *
2224     * @return The number of times the key has repeated.
2225     */
2226    public final int getRepeatCount() {
2227        return mRepeatCount;
2228    }
2229
2230    /**
2231     * Retrieve the time of the most recent key down event,
2232     * in the {@link android.os.SystemClock#uptimeMillis} time base.  If this
2233     * is a down event, this will be the same as {@link #getEventTime()}.
2234     * Note that when chording keys, this value is the down time of the
2235     * most recently pressed key, which may <em>not</em> be the same physical
2236     * key of this event.
2237     *
2238     * @return Returns the most recent key down time, in the
2239     * {@link android.os.SystemClock#uptimeMillis} time base
2240     */
2241    public final long getDownTime() {
2242        return mDownTime;
2243    }
2244
2245    /**
2246     * Retrieve the time this event occurred,
2247     * in the {@link android.os.SystemClock#uptimeMillis} time base.
2248     *
2249     * @return Returns the time this event occurred,
2250     * in the {@link android.os.SystemClock#uptimeMillis} time base.
2251     */
2252    public final long getEventTime() {
2253        return mEventTime;
2254    }
2255
2256    /**
2257     * Renamed to {@link #getDeviceId}.
2258     *
2259     * @hide
2260     * @deprecated use {@link #getDeviceId()} instead.
2261     */
2262    @Deprecated
2263    public final int getKeyboardDevice() {
2264        return mDeviceId;
2265    }
2266
2267    /**
2268     * Gets the {@link KeyCharacterMap} associated with the keyboard device.
2269     *
2270     * @return The associated key character map.
2271     * @throws {@link UnavailableException} if the key character map
2272     * could not be loaded because it was malformed or the default key character map
2273     * is missing from the system.
2274     *
2275     * @see {@link KeyCharacterMap#load}
2276     */
2277    public final KeyCharacterMap getKeyCharacterMap() {
2278        return KeyCharacterMap.load(mDeviceId);
2279    }
2280
2281    /**
2282     * Gets the primary character for this key.
2283     * In other words, the label that is physically printed on it.
2284     *
2285     * @return The display label character, or 0 if none (eg. for non-printing keys).
2286     */
2287    public char getDisplayLabel() {
2288        return getKeyCharacterMap().getDisplayLabel(mKeyCode);
2289    }
2290
2291    /**
2292     * Gets the Unicode character generated by the specified key and meta
2293     * key state combination.
2294     * <p>
2295     * Returns the Unicode character that the specified key would produce
2296     * when the specified meta bits (see {@link MetaKeyKeyListener})
2297     * were active.
2298     * </p><p>
2299     * Returns 0 if the key is not one that is used to type Unicode
2300     * characters.
2301     * </p><p>
2302     * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the
2303     * key is a "dead key" that should be combined with another to
2304     * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} --
2305     * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}.
2306     * </p>
2307     *
2308     * @return The associated character or combining accent, or 0 if none.
2309     */
2310    public int getUnicodeChar() {
2311        return getUnicodeChar(mMetaState);
2312    }
2313
2314    /**
2315     * Gets the Unicode character generated by the specified key and meta
2316     * key state combination.
2317     * <p>
2318     * Returns the Unicode character that the specified key would produce
2319     * when the specified meta bits (see {@link MetaKeyKeyListener})
2320     * were active.
2321     * </p><p>
2322     * Returns 0 if the key is not one that is used to type Unicode
2323     * characters.
2324     * </p><p>
2325     * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the
2326     * key is a "dead key" that should be combined with another to
2327     * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} --
2328     * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}.
2329     * </p>
2330     *
2331     * @param metaState The meta key modifier state.
2332     * @return The associated character or combining accent, or 0 if none.
2333     */
2334    public int getUnicodeChar(int metaState) {
2335        return getKeyCharacterMap().get(mKeyCode, metaState);
2336    }
2337
2338    /**
2339     * Get the character conversion data for a given key code.
2340     *
2341     * @param results A {@link KeyCharacterMap.KeyData} instance that will be
2342     * filled with the results.
2343     * @return True if the key was mapped.  If the key was not mapped, results is not modified.
2344     *
2345     * @deprecated instead use {@link #getDisplayLabel()},
2346     * {@link #getNumber()} or {@link #getUnicodeChar(int)}.
2347     */
2348    @Deprecated
2349    public boolean getKeyData(KeyData results) {
2350        return getKeyCharacterMap().getKeyData(mKeyCode, results);
2351    }
2352
2353    /**
2354     * Gets the first character in the character array that can be generated
2355     * by the specified key code.
2356     * <p>
2357     * This is a convenience function that returns the same value as
2358     * {@link #getMatch(char[],int) getMatch(chars, 0)}.
2359     * </p>
2360     *
2361     * @param chars The array of matching characters to consider.
2362     * @return The matching associated character, or 0 if none.
2363     */
2364    public char getMatch(char[] chars) {
2365        return getMatch(chars, 0);
2366    }
2367
2368    /**
2369     * Gets the first character in the character array that can be generated
2370     * by the specified key code.  If there are multiple choices, prefers
2371     * the one that would be generated with the specified meta key modifier state.
2372     *
2373     * @param chars The array of matching characters to consider.
2374     * @param metaState The preferred meta key modifier state.
2375     * @return The matching associated character, or 0 if none.
2376     */
2377    public char getMatch(char[] chars, int metaState) {
2378        return getKeyCharacterMap().getMatch(mKeyCode, chars, metaState);
2379    }
2380
2381    /**
2382     * Gets the number or symbol associated with the key.
2383     * <p>
2384     * The character value is returned, not the numeric value.
2385     * If the key is not a number, but is a symbol, the symbol is retuned.
2386     * </p><p>
2387     * This method is intended to to support dial pads and other numeric or
2388     * symbolic entry on keyboards where certain keys serve dual function
2389     * as alphabetic and symbolic keys.  This method returns the number
2390     * or symbol associated with the key independent of whether the user
2391     * has pressed the required modifier.
2392     * </p><p>
2393     * For example, on one particular keyboard the keys on the top QWERTY row generate
2394     * numbers when ALT is pressed such that ALT-Q maps to '1'.  So for that keyboard
2395     * when {@link #getNumber} is called with {@link KeyEvent#KEYCODE_Q} it returns '1'
2396     * so that the user can type numbers without pressing ALT when it makes sense.
2397     * </p>
2398     *
2399     * @return The associated numeric or symbolic character, or 0 if none.
2400     */
2401    public char getNumber() {
2402        return getKeyCharacterMap().getNumber(mKeyCode);
2403    }
2404
2405    /**
2406     * Returns true if this key produces a glyph.
2407     *
2408     * @return True if the key is a printing key.
2409     */
2410    public boolean isPrintingKey() {
2411        return getKeyCharacterMap().isPrintingKey(mKeyCode);
2412    }
2413
2414    /**
2415     * @deprecated Use {@link #dispatch(Callback, DispatcherState, Object)} instead.
2416     */
2417    @Deprecated
2418    public final boolean dispatch(Callback receiver) {
2419        return dispatch(receiver, null, null);
2420    }
2421
2422    /**
2423     * Deliver this key event to a {@link Callback} interface.  If this is
2424     * an ACTION_MULTIPLE event and it is not handled, then an attempt will
2425     * be made to deliver a single normal event.
2426     *
2427     * @param receiver The Callback that will be given the event.
2428     * @param state State information retained across events.
2429     * @param target The target of the dispatch, for use in tracking.
2430     *
2431     * @return The return value from the Callback method that was called.
2432     */
2433    public final boolean dispatch(Callback receiver, DispatcherState state,
2434            Object target) {
2435        switch (mAction) {
2436            case ACTION_DOWN: {
2437                mFlags &= ~FLAG_START_TRACKING;
2438                if (DEBUG) Log.v(TAG, "Key down to " + target + " in " + state
2439                        + ": " + this);
2440                boolean res = receiver.onKeyDown(mKeyCode, this);
2441                if (state != null) {
2442                    if (res && mRepeatCount == 0 && (mFlags&FLAG_START_TRACKING) != 0) {
2443                        if (DEBUG) Log.v(TAG, "  Start tracking!");
2444                        state.startTracking(this, target);
2445                    } else if (isLongPress() && state.isTracking(this)) {
2446                        try {
2447                            if (receiver.onKeyLongPress(mKeyCode, this)) {
2448                                if (DEBUG) Log.v(TAG, "  Clear from long press!");
2449                                state.performedLongPress(this);
2450                                res = true;
2451                            }
2452                        } catch (AbstractMethodError e) {
2453                        }
2454                    }
2455                }
2456                return res;
2457            }
2458            case ACTION_UP:
2459                if (DEBUG) Log.v(TAG, "Key up to " + target + " in " + state
2460                        + ": " + this);
2461                if (state != null) {
2462                    state.handleUpEvent(this);
2463                }
2464                return receiver.onKeyUp(mKeyCode, this);
2465            case ACTION_MULTIPLE:
2466                final int count = mRepeatCount;
2467                final int code = mKeyCode;
2468                if (receiver.onKeyMultiple(code, count, this)) {
2469                    return true;
2470                }
2471                if (code != KeyEvent.KEYCODE_UNKNOWN) {
2472                    mAction = ACTION_DOWN;
2473                    mRepeatCount = 0;
2474                    boolean handled = receiver.onKeyDown(code, this);
2475                    if (handled) {
2476                        mAction = ACTION_UP;
2477                        receiver.onKeyUp(code, this);
2478                    }
2479                    mAction = ACTION_MULTIPLE;
2480                    mRepeatCount = count;
2481                    return handled;
2482                }
2483                return false;
2484        }
2485        return false;
2486    }
2487
2488    /**
2489     * Use with {@link KeyEvent#dispatch(Callback, DispatcherState, Object)}
2490     * for more advanced key dispatching, such as long presses.
2491     */
2492    public static class DispatcherState {
2493        int mDownKeyCode;
2494        Object mDownTarget;
2495        SparseIntArray mActiveLongPresses = new SparseIntArray();
2496
2497        /**
2498         * Reset back to initial state.
2499         */
2500        public void reset() {
2501            if (DEBUG) Log.v(TAG, "Reset: " + this);
2502            mDownKeyCode = 0;
2503            mDownTarget = null;
2504            mActiveLongPresses.clear();
2505        }
2506
2507        /**
2508         * Stop any tracking associated with this target.
2509         */
2510        public void reset(Object target) {
2511            if (mDownTarget == target) {
2512                if (DEBUG) Log.v(TAG, "Reset in " + target + ": " + this);
2513                mDownKeyCode = 0;
2514                mDownTarget = null;
2515            }
2516        }
2517
2518        /**
2519         * Start tracking the key code associated with the given event.  This
2520         * can only be called on a key down.  It will allow you to see any
2521         * long press associated with the key, and will result in
2522         * {@link KeyEvent#isTracking} return true on the long press and up
2523         * events.
2524         *
2525         * <p>This is only needed if you are directly dispatching events, rather
2526         * than handling them in {@link Callback#onKeyDown}.
2527         */
2528        public void startTracking(KeyEvent event, Object target) {
2529            if (event.getAction() != ACTION_DOWN) {
2530                throw new IllegalArgumentException(
2531                        "Can only start tracking on a down event");
2532            }
2533            if (DEBUG) Log.v(TAG, "Start trackingt in " + target + ": " + this);
2534            mDownKeyCode = event.getKeyCode();
2535            mDownTarget = target;
2536        }
2537
2538        /**
2539         * Return true if the key event is for a key code that is currently
2540         * being tracked by the dispatcher.
2541         */
2542        public boolean isTracking(KeyEvent event) {
2543            return mDownKeyCode == event.getKeyCode();
2544        }
2545
2546        /**
2547         * Keep track of the given event's key code as having performed an
2548         * action with a long press, so no action should occur on the up.
2549         * <p>This is only needed if you are directly dispatching events, rather
2550         * than handling them in {@link Callback#onKeyLongPress}.
2551         */
2552        public void performedLongPress(KeyEvent event) {
2553            mActiveLongPresses.put(event.getKeyCode(), 1);
2554        }
2555
2556        /**
2557         * Handle key up event to stop tracking.  This resets the dispatcher state,
2558         * and updates the key event state based on it.
2559         * <p>This is only needed if you are directly dispatching events, rather
2560         * than handling them in {@link Callback#onKeyUp}.
2561         */
2562        public void handleUpEvent(KeyEvent event) {
2563            final int keyCode = event.getKeyCode();
2564            if (DEBUG) Log.v(TAG, "Handle key up " + event + ": " + this);
2565            int index = mActiveLongPresses.indexOfKey(keyCode);
2566            if (index >= 0) {
2567                if (DEBUG) Log.v(TAG, "  Index: " + index);
2568                event.mFlags |= FLAG_CANCELED | FLAG_CANCELED_LONG_PRESS;
2569                mActiveLongPresses.removeAt(index);
2570            }
2571            if (mDownKeyCode == keyCode) {
2572                if (DEBUG) Log.v(TAG, "  Tracking!");
2573                event.mFlags |= FLAG_TRACKING;
2574                mDownKeyCode = 0;
2575                mDownTarget = null;
2576            }
2577        }
2578    }
2579
2580    @Override
2581    public String toString() {
2582        return "KeyEvent{action=" + actionToString(mAction)
2583                + " keycode=" + keyCodeToString(mKeyCode)
2584                + " scancode=" + mScanCode
2585                + " metaState=" + metaStateToString(mMetaState)
2586                + " flags=0x" + Integer.toHexString(mFlags)
2587                + " repeat=" + mRepeatCount
2588                + " device=" + mDeviceId
2589                + " source=0x" + Integer.toHexString(mSource)
2590                + "}";
2591    }
2592
2593    /**
2594     * Returns a string that represents the symbolic name of the specified action
2595     * such as "ACTION_DOWN", or an equivalent numeric constant such as "35" if unknown.
2596     *
2597     * @param action The action.
2598     * @return The symbolic name of the specified action.
2599     * @hide
2600     */
2601    public static String actionToString(int action) {
2602        switch (action) {
2603            case ACTION_DOWN:
2604                return "ACTION_DOWN";
2605            case ACTION_UP:
2606                return "ACTION_UP";
2607            case ACTION_MULTIPLE:
2608                return "ACTION_MULTIPLE";
2609            default:
2610                return Integer.toString(action);
2611        }
2612    }
2613
2614    /**
2615     * Returns a string that represents the symbolic name of the specified keycode
2616     * such as "KEYCODE_A", "KEYCODE_DPAD_UP", or an equivalent numeric constant
2617     * such as "1001" if unknown.
2618     *
2619     * @param keyCode The key code.
2620     * @return The symbolic name of the specified keycode.
2621     *
2622     * @see KeyCharacterMap#getDisplayLabel
2623     */
2624    public static String keyCodeToString(int keyCode) {
2625        String symbolicName = KEYCODE_SYMBOLIC_NAMES.get(keyCode);
2626        return symbolicName != null ? symbolicName : Integer.toString(keyCode);
2627    }
2628
2629    /**
2630     * Gets a keycode by its symbolic name such as "KEYCODE_A" or an equivalent
2631     * numeric constant such as "1001".
2632     *
2633     * @param symbolicName The symbolic name of the keycode.
2634     * @return The keycode or {@link #KEYCODE_UNKNOWN} if not found.
2635     * @see #keycodeToString
2636     */
2637    public static int keyCodeFromString(String symbolicName) {
2638        if (symbolicName == null) {
2639            throw new IllegalArgumentException("symbolicName must not be null");
2640        }
2641
2642        final int count = KEYCODE_SYMBOLIC_NAMES.size();
2643        for (int i = 0; i < count; i++) {
2644            if (symbolicName.equals(KEYCODE_SYMBOLIC_NAMES.valueAt(i))) {
2645                return i;
2646            }
2647        }
2648
2649        try {
2650            return Integer.parseInt(symbolicName, 10);
2651        } catch (NumberFormatException ex) {
2652            return KEYCODE_UNKNOWN;
2653        }
2654    }
2655
2656    /**
2657     * Returns a string that represents the symbolic name of the specified combined meta
2658     * key modifier state flags such as "0", "META_SHIFT_ON",
2659     * "META_ALT_ON|META_SHIFT_ON" or an equivalent numeric constant such as "0x10000000"
2660     * if unknown.
2661     *
2662     * @param metaState The meta state.
2663     * @return The symbolic name of the specified combined meta state flags.
2664     * @hide
2665     */
2666    public static String metaStateToString(int metaState) {
2667        if (metaState == 0) {
2668            return "0";
2669        }
2670        StringBuilder result = null;
2671        int i = 0;
2672        while (metaState != 0) {
2673            final boolean isSet = (metaState & 1) != 0;
2674            metaState >>>= 1; // unsigned shift!
2675            if (isSet) {
2676                final String name = META_SYMBOLIC_NAMES[i];
2677                if (result == null) {
2678                    if (metaState == 0) {
2679                        return name;
2680                    }
2681                    result = new StringBuilder(name);
2682                } else {
2683                    result.append('|');
2684                    result.append(name);
2685                }
2686            }
2687            i += 1;
2688        }
2689        return result.toString();
2690    }
2691
2692    public static final Parcelable.Creator<KeyEvent> CREATOR
2693            = new Parcelable.Creator<KeyEvent>() {
2694        public KeyEvent createFromParcel(Parcel in) {
2695            in.readInt(); // skip token, we already know this is a KeyEvent
2696            return KeyEvent.createFromParcelBody(in);
2697        }
2698
2699        public KeyEvent[] newArray(int size) {
2700            return new KeyEvent[size];
2701        }
2702    };
2703
2704    /** @hide */
2705    public static KeyEvent createFromParcelBody(Parcel in) {
2706        return new KeyEvent(in);
2707    }
2708
2709    private KeyEvent(Parcel in) {
2710        mDeviceId = in.readInt();
2711        mSource = in.readInt();
2712        mAction = in.readInt();
2713        mKeyCode = in.readInt();
2714        mRepeatCount = in.readInt();
2715        mMetaState = in.readInt();
2716        mScanCode = in.readInt();
2717        mFlags = in.readInt();
2718        mDownTime = in.readLong();
2719        mEventTime = in.readLong();
2720    }
2721
2722    public void writeToParcel(Parcel out, int flags) {
2723        out.writeInt(PARCEL_TOKEN_KEY_EVENT);
2724
2725        out.writeInt(mDeviceId);
2726        out.writeInt(mSource);
2727        out.writeInt(mAction);
2728        out.writeInt(mKeyCode);
2729        out.writeInt(mRepeatCount);
2730        out.writeInt(mMetaState);
2731        out.writeInt(mScanCode);
2732        out.writeInt(mFlags);
2733        out.writeLong(mDownTime);
2734        out.writeLong(mEventTime);
2735    }
2736
2737    private native boolean native_isSystemKey(int keyCode);
2738    private native boolean native_hasDefaultAction(int keyCode);
2739}
2740