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