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