blob: 5db3909b0d0b2a198ed67e7475bb7f4a4170ae79 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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;
Jeff Brown6b53e8d2010-11-10 16:03:06 -080021import android.text.method.MetaKeyKeyListener;
Dianne Hackborn8d374262009-09-14 21:21:52 -070022import android.util.Log;
Jeff Brown28cbf4b2010-12-13 10:33:20 -080023import android.util.Slog;
Jeff Brown6f2fba42011-02-19 01:08:02 -080024import android.util.SparseArray;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070025import android.util.SparseIntArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.view.KeyCharacterMap;
27import android.view.KeyCharacterMap.KeyData;
28
29/**
Jeff Browndc1ab4b2010-09-14 18:03:38 -070030 * Object used to report key and button events.
31 * <p>
32 * Each key press is described by a sequence of key events. A key press
33 * starts with a key event with {@link #ACTION_DOWN}. If the key is held
34 * sufficiently long that it repeats, then the initial down is followed
35 * additional key events with {@link #ACTION_DOWN} and a non-zero value for
36 * {@link #getRepeatCount()}. The last key event is a {@link #ACTION_UP}
37 * for the key up. If the key press is canceled, the key up event will have the
38 * {@link #FLAG_CANCELED} flag set.
39 * </p><p>
40 * Key events are generally accompanied by a key code ({@link #getKeyCode()}),
41 * scan code ({@link #getScanCode()}) and meta state ({@link #getMetaState()}).
42 * Key code constants are defined in this class. Scan code constants are raw
43 * device-specific codes obtained from the OS and so are not generally meaningful
44 * to applications unless interpreted using the {@link KeyCharacterMap}.
45 * Meta states describe the pressed state of key modifiers
46 * such as {@link #META_SHIFT_ON} or {@link #META_ALT_ON}.
47 * </p><p>
Jeff Brown497a92c2010-09-12 17:55:08 -070048 * Key codes typically correspond one-to-one with individual keys on an input device.
49 * Many keys and key combinations serve quite different functions on different
50 * input devices so care must be taken when interpreting them. Always use the
51 * {@link KeyCharacterMap} associated with the input device when mapping keys
52 * to characters. Be aware that there may be multiple key input devices active
53 * at the same time and each will have its own key character map.
54 * </p><p>
Jean Chalard405bc512012-05-29 19:12:34 +090055 * As soft input methods can use multiple and inventive ways of inputting text,
56 * there is no guarantee that any key press on a soft keyboard will generate a key
57 * event: this is left to the IME's discretion, and in fact sending such events is
58 * discouraged. You should never rely on receiving KeyEvents for any key on a soft
59 * input method. In particular, the default software keyboard will never send any
60 * key event to any application targetting Jelly Bean or later, and will only send
61 * events for some presses of the delete and return keys to applications targetting
62 * Ice Cream Sandwich or earlier. Be aware that other software input methods may
63 * never send key events regardless of the version. Consider using editor actions
64 * like {@link android.view.inputmethod.EditorInfo#IME_ACTION_DONE} if you need
65 * specific interaction with the software keyboard, as it gives more visibility to
66 * the user as to how your application will react to key presses.
67 * </p><p>
Jeff Browndc1ab4b2010-09-14 18:03:38 -070068 * When interacting with an IME, the framework may deliver key events
69 * with the special action {@link #ACTION_MULTIPLE} that either specifies
70 * that single repeated key code or a sequence of characters to insert.
71 * </p><p>
Jeff Brownb6997262010-10-08 22:31:17 -070072 * In general, the framework cannot guarantee that the key events it delivers
73 * to a view always constitute complete key sequences since some events may be dropped
74 * or modified by containing views before they are delivered. The view implementation
75 * should be prepared to handle {@link #FLAG_CANCELED} and should tolerate anomalous
76 * situations such as receiving a new {@link #ACTION_DOWN} without first having
77 * received an {@link #ACTION_UP} for the prior key press.
Jeff Browndc1ab4b2010-09-14 18:03:38 -070078 * </p><p>
79 * Refer to {@link InputDevice} for more information about how different kinds of
80 * input devices and sources represent keys and buttons.
81 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 */
Jeff Brownc5ed5912010-07-14 18:48:53 -070083public class KeyEvent extends InputEvent implements Parcelable {
Jeff Browndc1ab4b2010-09-14 18:03:38 -070084 /** Key code constant: Unknown key code. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 public static final int KEYCODE_UNKNOWN = 0;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070086 /** Key code constant: Soft Left key.
87 * Usually situated below the display on phones and used as a multi-function
88 * feature key for selecting a software defined function shown on the bottom left
89 * of the display. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 public static final int KEYCODE_SOFT_LEFT = 1;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070091 /** Key code constant: Soft Right key.
92 * Usually situated below the display on phones and used as a multi-function
93 * feature key for selecting a software defined function shown on the bottom right
94 * of the display. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 public static final int KEYCODE_SOFT_RIGHT = 2;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070096 /** Key code constant: Home key.
97 * This key is handled by the framework and is never delivered to applications. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 public static final int KEYCODE_HOME = 3;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070099 /** Key code constant: Back key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 public static final int KEYCODE_BACK = 4;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700101 /** Key code constant: Call key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 public static final int KEYCODE_CALL = 5;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700103 /** Key code constant: End Call key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 public static final int KEYCODE_ENDCALL = 6;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700105 /** Key code constant: '0' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 public static final int KEYCODE_0 = 7;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700107 /** Key code constant: '1' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 public static final int KEYCODE_1 = 8;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700109 /** Key code constant: '2' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 public static final int KEYCODE_2 = 9;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700111 /** Key code constant: '3' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 public static final int KEYCODE_3 = 10;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700113 /** Key code constant: '4' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 public static final int KEYCODE_4 = 11;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700115 /** Key code constant: '5' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 public static final int KEYCODE_5 = 12;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700117 /** Key code constant: '6' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 public static final int KEYCODE_6 = 13;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700119 /** Key code constant: '7' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 public static final int KEYCODE_7 = 14;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700121 /** Key code constant: '8' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 public static final int KEYCODE_8 = 15;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700123 /** Key code constant: '9' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 public static final int KEYCODE_9 = 16;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700125 /** Key code constant: '*' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 public static final int KEYCODE_STAR = 17;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700127 /** Key code constant: '#' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 public static final int KEYCODE_POUND = 18;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700129 /** Key code constant: Directional Pad Up key.
130 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 public static final int KEYCODE_DPAD_UP = 19;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700132 /** Key code constant: Directional Pad Down key.
133 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 public static final int KEYCODE_DPAD_DOWN = 20;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700135 /** Key code constant: Directional Pad Left key.
136 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 public static final int KEYCODE_DPAD_LEFT = 21;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700138 /** Key code constant: Directional Pad Right key.
139 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 public static final int KEYCODE_DPAD_RIGHT = 22;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700141 /** Key code constant: Directional Pad Center key.
142 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 public static final int KEYCODE_DPAD_CENTER = 23;
Jeff Brownb0418da2010-11-01 15:24:01 -0700144 /** Key code constant: Volume Up key.
145 * Adjusts the speaker volume up. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 public static final int KEYCODE_VOLUME_UP = 24;
Jeff Brownb0418da2010-11-01 15:24:01 -0700147 /** Key code constant: Volume Down key.
148 * Adjusts the speaker volume down. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 public static final int KEYCODE_VOLUME_DOWN = 25;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700150 /** Key code constant: Power key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 public static final int KEYCODE_POWER = 26;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700152 /** Key code constant: Camera key.
153 * Used to launch a camera application or take pictures. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 public static final int KEYCODE_CAMERA = 27;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700155 /** Key code constant: Clear key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 public static final int KEYCODE_CLEAR = 28;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700157 /** Key code constant: 'A' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 public static final int KEYCODE_A = 29;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700159 /** Key code constant: 'B' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 public static final int KEYCODE_B = 30;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700161 /** Key code constant: 'C' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 public static final int KEYCODE_C = 31;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700163 /** Key code constant: 'D' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 public static final int KEYCODE_D = 32;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700165 /** Key code constant: 'E' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 public static final int KEYCODE_E = 33;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700167 /** Key code constant: 'F' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 public static final int KEYCODE_F = 34;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700169 /** Key code constant: 'G' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 public static final int KEYCODE_G = 35;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700171 /** Key code constant: 'H' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 public static final int KEYCODE_H = 36;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700173 /** Key code constant: 'I' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 public static final int KEYCODE_I = 37;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700175 /** Key code constant: 'J' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 public static final int KEYCODE_J = 38;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700177 /** Key code constant: 'K' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 public static final int KEYCODE_K = 39;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700179 /** Key code constant: 'L' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 public static final int KEYCODE_L = 40;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700181 /** Key code constant: 'M' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 public static final int KEYCODE_M = 41;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700183 /** Key code constant: 'N' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 public static final int KEYCODE_N = 42;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700185 /** Key code constant: 'O' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 public static final int KEYCODE_O = 43;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700187 /** Key code constant: 'P' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 public static final int KEYCODE_P = 44;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700189 /** Key code constant: 'Q' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 public static final int KEYCODE_Q = 45;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700191 /** Key code constant: 'R' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 public static final int KEYCODE_R = 46;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700193 /** Key code constant: 'S' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 public static final int KEYCODE_S = 47;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700195 /** Key code constant: 'T' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 public static final int KEYCODE_T = 48;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700197 /** Key code constant: 'U' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 public static final int KEYCODE_U = 49;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700199 /** Key code constant: 'V' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 public static final int KEYCODE_V = 50;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700201 /** Key code constant: 'W' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 public static final int KEYCODE_W = 51;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700203 /** Key code constant: 'X' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 public static final int KEYCODE_X = 52;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700205 /** Key code constant: 'Y' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 public static final int KEYCODE_Y = 53;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700207 /** Key code constant: 'Z' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 public static final int KEYCODE_Z = 54;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700209 /** Key code constant: ',' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 public static final int KEYCODE_COMMA = 55;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700211 /** Key code constant: '.' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 public static final int KEYCODE_PERIOD = 56;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700213 /** Key code constant: Left Alt modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 public static final int KEYCODE_ALT_LEFT = 57;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700215 /** Key code constant: Right Alt modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 public static final int KEYCODE_ALT_RIGHT = 58;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700217 /** Key code constant: Left Shift modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 public static final int KEYCODE_SHIFT_LEFT = 59;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700219 /** Key code constant: Right Shift modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 public static final int KEYCODE_SHIFT_RIGHT = 60;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700221 /** Key code constant: Tab key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 public static final int KEYCODE_TAB = 61;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700223 /** Key code constant: Space key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 public static final int KEYCODE_SPACE = 62;
Jeff Brown224d4a12010-10-07 20:28:53 -0700225 /** Key code constant: Symbol modifier key.
226 * Used to enter alternate symbols. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 public static final int KEYCODE_SYM = 63;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700228 /** Key code constant: Explorer special function key.
229 * Used to launch a browser application. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 public static final int KEYCODE_EXPLORER = 64;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700231 /** Key code constant: Envelope special function key.
232 * Used to launch a mail application. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 public static final int KEYCODE_ENVELOPE = 65;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700234 /** Key code constant: Enter key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 public static final int KEYCODE_ENTER = 66;
Jeff Brown224d4a12010-10-07 20:28:53 -0700236 /** Key code constant: Backspace key.
Jeff Brown497a92c2010-09-12 17:55:08 -0700237 * Deletes characters before the insertion point, unlike {@link #KEYCODE_FORWARD_DEL}. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 public static final int KEYCODE_DEL = 67;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700239 /** Key code constant: '`' (backtick) key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 public static final int KEYCODE_GRAVE = 68;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700241 /** Key code constant: '-'. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 public static final int KEYCODE_MINUS = 69;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700243 /** Key code constant: '=' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 public static final int KEYCODE_EQUALS = 70;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700245 /** Key code constant: '[' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 public static final int KEYCODE_LEFT_BRACKET = 71;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700247 /** Key code constant: ']' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 public static final int KEYCODE_RIGHT_BRACKET = 72;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700249 /** Key code constant: '\' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 public static final int KEYCODE_BACKSLASH = 73;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700251 /** Key code constant: ';' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 public static final int KEYCODE_SEMICOLON = 74;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700253 /** Key code constant: ''' (apostrophe) key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 public static final int KEYCODE_APOSTROPHE = 75;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700255 /** Key code constant: '/' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 public static final int KEYCODE_SLASH = 76;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700257 /** Key code constant: '@' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 public static final int KEYCODE_AT = 77;
Jeff Brown224d4a12010-10-07 20:28:53 -0700259 /** Key code constant: Number modifier key.
260 * Used to enter numeric symbols.
261 * This key is not Num Lock; it is more like {@link #KEYCODE_ALT_LEFT} and is
262 * interpreted as an ALT key by {@link android.text.method.MetaKeyKeyListener}. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 public static final int KEYCODE_NUM = 78;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700264 /** Key code constant: Headset Hook key.
265 * Used to hang up calls and stop media. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 public static final int KEYCODE_HEADSETHOOK = 79;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700267 /** Key code constant: Camera Focus key.
268 * Used to focus the camera. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 public static final int KEYCODE_FOCUS = 80; // *Camera* focus
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700270 /** Key code constant: '+' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 public static final int KEYCODE_PLUS = 81;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700272 /** Key code constant: Menu key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 public static final int KEYCODE_MENU = 82;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700274 /** Key code constant: Notification key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 public static final int KEYCODE_NOTIFICATION = 83;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700276 /** Key code constant: Search key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 public static final int KEYCODE_SEARCH = 84;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700278 /** Key code constant: Play/Pause media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700279 public static final int KEYCODE_MEDIA_PLAY_PAUSE= 85;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700280 /** Key code constant: Stop media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700281 public static final int KEYCODE_MEDIA_STOP = 86;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700282 /** Key code constant: Play Next media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700283 public static final int KEYCODE_MEDIA_NEXT = 87;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700284 /** Key code constant: Play Previous media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700285 public static final int KEYCODE_MEDIA_PREVIOUS = 88;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700286 /** Key code constant: Rewind media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700287 public static final int KEYCODE_MEDIA_REWIND = 89;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700288 /** Key code constant: Fast Forward media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700289 public static final int KEYCODE_MEDIA_FAST_FORWARD = 90;
Jeff Brownb0418da2010-11-01 15:24:01 -0700290 /** Key code constant: Mute key.
291 * Mutes the microphone, unlike {@link #KEYCODE_VOLUME_MUTE}. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 public static final int KEYCODE_MUTE = 91;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700293 /** Key code constant: Page Up key. */
Chih-Wei Huang4fedd802009-05-27 15:52:50 +0800294 public static final int KEYCODE_PAGE_UP = 92;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700295 /** Key code constant: Page Down key. */
Chih-Wei Huang4fedd802009-05-27 15:52:50 +0800296 public static final int KEYCODE_PAGE_DOWN = 93;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700297 /** Key code constant: Picture Symbols modifier key.
298 * Used to switch symbol sets (Emoji, Kao-moji). */
mogimob032bc02009-10-03 03:13:56 +0900299 public static final int KEYCODE_PICTSYMBOLS = 94; // switch symbol-sets (Emoji,Kao-moji)
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700300 /** Key code constant: Switch Charset modifier key.
301 * Used to switch character sets (Kanji, Katakana). */
mogimob032bc02009-10-03 03:13:56 +0900302 public static final int KEYCODE_SWITCH_CHARSET = 95; // switch char-sets (Kanji,Katakana)
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700303 /** Key code constant: A Button key.
304 * On a game controller, the A button should be either the button labeled A
Michael Wright6b57bde2013-01-28 20:35:58 -0800305 * or the first button on the bottom row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700306 public static final int KEYCODE_BUTTON_A = 96;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700307 /** Key code constant: B Button key.
308 * On a game controller, the B button should be either the button labeled B
Michael Wright6b57bde2013-01-28 20:35:58 -0800309 * or the second button on the bottom row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700310 public static final int KEYCODE_BUTTON_B = 97;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700311 /** Key code constant: C Button key.
312 * On a game controller, the C button should be either the button labeled C
Michael Wright6b57bde2013-01-28 20:35:58 -0800313 * or the third button on the bottom row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700314 public static final int KEYCODE_BUTTON_C = 98;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700315 /** Key code constant: X Button key.
316 * On a game controller, the X button should be either the button labeled X
Michael Wright6b57bde2013-01-28 20:35:58 -0800317 * or the first button on the upper row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700318 public static final int KEYCODE_BUTTON_X = 99;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700319 /** Key code constant: Y Button key.
320 * On a game controller, the Y button should be either the button labeled Y
Michael Wright6b57bde2013-01-28 20:35:58 -0800321 * or the second button on the upper row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700322 public static final int KEYCODE_BUTTON_Y = 100;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700323 /** Key code constant: Z Button key.
324 * On a game controller, the Z button should be either the button labeled Z
Michael Wright6b57bde2013-01-28 20:35:58 -0800325 * or the third button on the upper row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700326 public static final int KEYCODE_BUTTON_Z = 101;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700327 /** Key code constant: L1 Button key.
328 * On a game controller, the L1 button should be either the button labeled L1 (or L)
329 * or the top left trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700330 public static final int KEYCODE_BUTTON_L1 = 102;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700331 /** Key code constant: R1 Button key.
332 * On a game controller, the R1 button should be either the button labeled R1 (or R)
333 * or the top right trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700334 public static final int KEYCODE_BUTTON_R1 = 103;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700335 /** Key code constant: L2 Button key.
336 * On a game controller, the L2 button should be either the button labeled L2
337 * or the bottom left trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700338 public static final int KEYCODE_BUTTON_L2 = 104;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700339 /** Key code constant: R2 Button key.
340 * On a game controller, the R2 button should be either the button labeled R2
341 * or the bottom right trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700342 public static final int KEYCODE_BUTTON_R2 = 105;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700343 /** Key code constant: Left Thumb Button key.
344 * On a game controller, the left thumb button indicates that the left (or only)
345 * joystick is pressed. */
Jeff Brownfd035822010-06-30 16:10:35 -0700346 public static final int KEYCODE_BUTTON_THUMBL = 106;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700347 /** Key code constant: Right Thumb Button key.
348 * On a game controller, the right thumb button indicates that the right
349 * joystick is pressed. */
Jeff Brownfd035822010-06-30 16:10:35 -0700350 public static final int KEYCODE_BUTTON_THUMBR = 107;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700351 /** Key code constant: Start Button key.
352 * On a game controller, the button labeled Start. */
Jeff Brownfd035822010-06-30 16:10:35 -0700353 public static final int KEYCODE_BUTTON_START = 108;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700354 /** Key code constant: Select Button key.
355 * On a game controller, the button labeled Select. */
Jeff Brownfd035822010-06-30 16:10:35 -0700356 public static final int KEYCODE_BUTTON_SELECT = 109;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700357 /** Key code constant: Mode Button key.
358 * On a game controller, the button labeled Mode. */
Jeff Brownfd035822010-06-30 16:10:35 -0700359 public static final int KEYCODE_BUTTON_MODE = 110;
Jeff Brown497a92c2010-09-12 17:55:08 -0700360 /** Key code constant: Escape key. */
361 public static final int KEYCODE_ESCAPE = 111;
362 /** Key code constant: Forward Delete key.
363 * Deletes characters ahead of the insertion point, unlike {@link #KEYCODE_DEL}. */
364 public static final int KEYCODE_FORWARD_DEL = 112;
365 /** Key code constant: Left Control modifier key. */
366 public static final int KEYCODE_CTRL_LEFT = 113;
367 /** Key code constant: Right Control modifier key. */
368 public static final int KEYCODE_CTRL_RIGHT = 114;
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800369 /** Key code constant: Caps Lock key. */
Jeff Brown497a92c2010-09-12 17:55:08 -0700370 public static final int KEYCODE_CAPS_LOCK = 115;
371 /** Key code constant: Scroll Lock key. */
372 public static final int KEYCODE_SCROLL_LOCK = 116;
373 /** Key code constant: Left Meta modifier key. */
374 public static final int KEYCODE_META_LEFT = 117;
375 /** Key code constant: Right Meta modifier key. */
376 public static final int KEYCODE_META_RIGHT = 118;
377 /** Key code constant: Function modifier key. */
378 public static final int KEYCODE_FUNCTION = 119;
379 /** Key code constant: System Request / Print Screen key. */
380 public static final int KEYCODE_SYSRQ = 120;
381 /** Key code constant: Break / Pause key. */
382 public static final int KEYCODE_BREAK = 121;
383 /** Key code constant: Home Movement key.
384 * Used for scrolling or moving the cursor around to the start of a line
385 * or to the top of a list. */
386 public static final int KEYCODE_MOVE_HOME = 122;
387 /** Key code constant: End Movement key.
388 * Used for scrolling or moving the cursor around to the end of a line
389 * or to the bottom of a list. */
390 public static final int KEYCODE_MOVE_END = 123;
391 /** Key code constant: Insert key.
392 * Toggles insert / overwrite edit mode. */
393 public static final int KEYCODE_INSERT = 124;
394 /** Key code constant: Forward key.
395 * Navigates forward in the history stack. Complement of {@link #KEYCODE_BACK}. */
396 public static final int KEYCODE_FORWARD = 125;
397 /** Key code constant: Play media key. */
398 public static final int KEYCODE_MEDIA_PLAY = 126;
399 /** Key code constant: Pause media key. */
400 public static final int KEYCODE_MEDIA_PAUSE = 127;
401 /** Key code constant: Close media key.
402 * May be used to close a CD tray, for example. */
403 public static final int KEYCODE_MEDIA_CLOSE = 128;
404 /** Key code constant: Eject media key.
405 * May be used to eject a CD tray, for example. */
406 public static final int KEYCODE_MEDIA_EJECT = 129;
407 /** Key code constant: Record media key. */
408 public static final int KEYCODE_MEDIA_RECORD = 130;
409 /** Key code constant: F1 key. */
410 public static final int KEYCODE_F1 = 131;
411 /** Key code constant: F2 key. */
412 public static final int KEYCODE_F2 = 132;
413 /** Key code constant: F3 key. */
414 public static final int KEYCODE_F3 = 133;
415 /** Key code constant: F4 key. */
416 public static final int KEYCODE_F4 = 134;
417 /** Key code constant: F5 key. */
418 public static final int KEYCODE_F5 = 135;
419 /** Key code constant: F6 key. */
420 public static final int KEYCODE_F6 = 136;
421 /** Key code constant: F7 key. */
422 public static final int KEYCODE_F7 = 137;
423 /** Key code constant: F8 key. */
424 public static final int KEYCODE_F8 = 138;
425 /** Key code constant: F9 key. */
426 public static final int KEYCODE_F9 = 139;
427 /** Key code constant: F10 key. */
428 public static final int KEYCODE_F10 = 140;
429 /** Key code constant: F11 key. */
430 public static final int KEYCODE_F11 = 141;
431 /** Key code constant: F12 key. */
432 public static final int KEYCODE_F12 = 142;
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800433 /** Key code constant: Num Lock key.
Jeff Brown497a92c2010-09-12 17:55:08 -0700434 * This is the Num Lock key; it is different from {@link #KEYCODE_NUM}.
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800435 * This key alters the behavior of other keys on the numeric keypad. */
Jeff Brown497a92c2010-09-12 17:55:08 -0700436 public static final int KEYCODE_NUM_LOCK = 143;
437 /** Key code constant: Numeric keypad '0' key. */
438 public static final int KEYCODE_NUMPAD_0 = 144;
439 /** Key code constant: Numeric keypad '1' key. */
440 public static final int KEYCODE_NUMPAD_1 = 145;
441 /** Key code constant: Numeric keypad '2' key. */
442 public static final int KEYCODE_NUMPAD_2 = 146;
443 /** Key code constant: Numeric keypad '3' key. */
444 public static final int KEYCODE_NUMPAD_3 = 147;
445 /** Key code constant: Numeric keypad '4' key. */
446 public static final int KEYCODE_NUMPAD_4 = 148;
447 /** Key code constant: Numeric keypad '5' key. */
448 public static final int KEYCODE_NUMPAD_5 = 149;
449 /** Key code constant: Numeric keypad '6' key. */
450 public static final int KEYCODE_NUMPAD_6 = 150;
451 /** Key code constant: Numeric keypad '7' key. */
452 public static final int KEYCODE_NUMPAD_7 = 151;
453 /** Key code constant: Numeric keypad '8' key. */
454 public static final int KEYCODE_NUMPAD_8 = 152;
455 /** Key code constant: Numeric keypad '9' key. */
456 public static final int KEYCODE_NUMPAD_9 = 153;
457 /** Key code constant: Numeric keypad '/' key (for division). */
458 public static final int KEYCODE_NUMPAD_DIVIDE = 154;
459 /** Key code constant: Numeric keypad '*' key (for multiplication). */
460 public static final int KEYCODE_NUMPAD_MULTIPLY = 155;
461 /** Key code constant: Numeric keypad '-' key (for subtraction). */
462 public static final int KEYCODE_NUMPAD_SUBTRACT = 156;
463 /** Key code constant: Numeric keypad '+' key (for addition). */
464 public static final int KEYCODE_NUMPAD_ADD = 157;
465 /** Key code constant: Numeric keypad '.' key (for decimals or digit grouping). */
466 public static final int KEYCODE_NUMPAD_DOT = 158;
467 /** Key code constant: Numeric keypad ',' key (for decimals or digit grouping). */
468 public static final int KEYCODE_NUMPAD_COMMA = 159;
469 /** Key code constant: Numeric keypad Enter key. */
470 public static final int KEYCODE_NUMPAD_ENTER = 160;
471 /** Key code constant: Numeric keypad '=' key. */
472 public static final int KEYCODE_NUMPAD_EQUALS = 161;
473 /** Key code constant: Numeric keypad '(' key. */
474 public static final int KEYCODE_NUMPAD_LEFT_PAREN = 162;
475 /** Key code constant: Numeric keypad ')' key. */
476 public static final int KEYCODE_NUMPAD_RIGHT_PAREN = 163;
Jeff Brownb0418da2010-11-01 15:24:01 -0700477 /** Key code constant: Volume Mute key.
478 * Mutes the speaker, unlike {@link #KEYCODE_MUTE}.
479 * This key should normally be implemented as a toggle such that the first press
480 * mutes the speaker and the second press restores the original volume. */
481 public static final int KEYCODE_VOLUME_MUTE = 164;
Jason Bayer3adf4902010-11-09 14:54:55 -0800482 /** Key code constant: Info key.
483 * Common on TV remotes to show additional information related to what is
484 * currently being viewed. */
485 public static final int KEYCODE_INFO = 165;
486 /** Key code constant: Channel up key.
487 * On TV remotes, increments the television channel. */
488 public static final int KEYCODE_CHANNEL_UP = 166;
489 /** Key code constant: Channel down key.
490 * On TV remotes, decrements the television channel. */
491 public static final int KEYCODE_CHANNEL_DOWN = 167;
492 /** Key code constant: Zoom in key. */
493 public static final int KEYCODE_ZOOM_IN = 168;
494 /** Key code constant: Zoom out key. */
495 public static final int KEYCODE_ZOOM_OUT = 169;
496 /** Key code constant: TV key.
497 * On TV remotes, switches to viewing live TV. */
498 public static final int KEYCODE_TV = 170;
499 /** Key code constant: Window key.
500 * On TV remotes, toggles picture-in-picture mode or other windowing functions. */
501 public static final int KEYCODE_WINDOW = 171;
502 /** Key code constant: Guide key.
503 * On TV remotes, shows a programming guide. */
504 public static final int KEYCODE_GUIDE = 172;
505 /** Key code constant: DVR key.
506 * On some TV remotes, switches to a DVR mode for recorded shows. */
507 public static final int KEYCODE_DVR = 173;
508 /** Key code constant: Bookmark key.
509 * On some TV remotes, bookmarks content or web pages. */
510 public static final int KEYCODE_BOOKMARK = 174;
511 /** Key code constant: Toggle captions key.
512 * Switches the mode for closed-captioning text, for example during television shows. */
513 public static final int KEYCODE_CAPTIONS = 175;
514 /** Key code constant: Settings key.
515 * Starts the system settings activity. */
516 public static final int KEYCODE_SETTINGS = 176;
517 /** Key code constant: TV power key.
518 * On TV remotes, toggles the power on a television screen. */
519 public static final int KEYCODE_TV_POWER = 177;
520 /** Key code constant: TV input key.
521 * On TV remotes, switches the input on a television screen. */
522 public static final int KEYCODE_TV_INPUT = 178;
523 /** Key code constant: Set-top-box power key.
524 * On TV remotes, toggles the power on an external Set-top-box. */
525 public static final int KEYCODE_STB_POWER = 179;
526 /** Key code constant: Set-top-box input key.
527 * On TV remotes, switches the input mode on an external Set-top-box. */
528 public static final int KEYCODE_STB_INPUT = 180;
529 /** Key code constant: A/V Receiver power key.
530 * On TV remotes, toggles the power on an external A/V Receiver. */
531 public static final int KEYCODE_AVR_POWER = 181;
532 /** Key code constant: A/V Receiver input key.
533 * On TV remotes, switches the input mode on an external A/V Receiver. */
534 public static final int KEYCODE_AVR_INPUT = 182;
535 /** Key code constant: Red "programmable" key.
536 * On TV remotes, acts as a contextual/programmable key. */
537 public static final int KEYCODE_PROG_RED = 183;
538 /** Key code constant: Green "programmable" key.
539 * On TV remotes, actsas a contextual/programmable key. */
540 public static final int KEYCODE_PROG_GREEN = 184;
541 /** Key code constant: Yellow "programmable" key.
542 * On TV remotes, acts as a contextual/programmable key. */
543 public static final int KEYCODE_PROG_YELLOW = 185;
544 /** Key code constant: Blue "programmable" key.
545 * On TV remotes, acts as a contextual/programmable key. */
546 public static final int KEYCODE_PROG_BLUE = 186;
Jeff Brown49ed71d2010-12-06 17:13:33 -0800547 /** Key code constant: App switch key.
548 * Should bring up the application switcher dialog. */
549 public static final int KEYCODE_APP_SWITCH = 187;
Jeff Browncb1404e2011-01-15 18:14:15 -0800550 /** Key code constant: Generic Game Pad Button #1.*/
551 public static final int KEYCODE_BUTTON_1 = 188;
552 /** Key code constant: Generic Game Pad Button #2.*/
553 public static final int KEYCODE_BUTTON_2 = 189;
554 /** Key code constant: Generic Game Pad Button #3.*/
555 public static final int KEYCODE_BUTTON_3 = 190;
556 /** Key code constant: Generic Game Pad Button #4.*/
557 public static final int KEYCODE_BUTTON_4 = 191;
558 /** Key code constant: Generic Game Pad Button #5.*/
559 public static final int KEYCODE_BUTTON_5 = 192;
560 /** Key code constant: Generic Game Pad Button #6.*/
561 public static final int KEYCODE_BUTTON_6 = 193;
562 /** Key code constant: Generic Game Pad Button #7.*/
563 public static final int KEYCODE_BUTTON_7 = 194;
564 /** Key code constant: Generic Game Pad Button #8.*/
565 public static final int KEYCODE_BUTTON_8 = 195;
566 /** Key code constant: Generic Game Pad Button #9.*/
567 public static final int KEYCODE_BUTTON_9 = 196;
568 /** Key code constant: Generic Game Pad Button #10.*/
569 public static final int KEYCODE_BUTTON_10 = 197;
570 /** Key code constant: Generic Game Pad Button #11.*/
571 public static final int KEYCODE_BUTTON_11 = 198;
572 /** Key code constant: Generic Game Pad Button #12.*/
573 public static final int KEYCODE_BUTTON_12 = 199;
574 /** Key code constant: Generic Game Pad Button #13.*/
575 public static final int KEYCODE_BUTTON_13 = 200;
576 /** Key code constant: Generic Game Pad Button #14.*/
577 public static final int KEYCODE_BUTTON_14 = 201;
578 /** Key code constant: Generic Game Pad Button #15.*/
579 public static final int KEYCODE_BUTTON_15 = 202;
580 /** Key code constant: Generic Game Pad Button #16.*/
581 public static final int KEYCODE_BUTTON_16 = 203;
Jeff Brown9812aed2011-03-07 17:09:51 -0800582 /** Key code constant: Language Switch key.
583 * Toggles the current input language such as switching between English and Japanese on
584 * a QWERTY keyboard. On some devices, the same function may be performed by
585 * pressing Shift+Spacebar. */
586 public static final int KEYCODE_LANGUAGE_SWITCH = 204;
587 /** Key code constant: Manner Mode key.
588 * Toggles silent or vibrate mode on and off to make the device behave more politely
589 * in certain settings such as on a crowded train. On some devices, the key may only
590 * operate when long-pressed. */
591 public static final int KEYCODE_MANNER_MODE = 205;
592 /** Key code constant: 3D Mode key.
593 * Toggles the display between 2D and 3D mode. */
594 public static final int KEYCODE_3D_MODE = 206;
Jeff Brown6651a632011-11-28 12:59:11 -0800595 /** Key code constant: Contacts special function key.
596 * Used to launch an address book application. */
597 public static final int KEYCODE_CONTACTS = 207;
598 /** Key code constant: Calendar special function key.
599 * Used to launch a calendar application. */
600 public static final int KEYCODE_CALENDAR = 208;
601 /** Key code constant: Music special function key.
602 * Used to launch a music player application. */
603 public static final int KEYCODE_MUSIC = 209;
604 /** Key code constant: Calculator special function key.
605 * Used to launch a calculator application. */
606 public static final int KEYCODE_CALCULATOR = 210;
Yang Chuang7511f9c2012-02-10 15:18:26 +0800607 /** Key code constant: Japanese full-width / half-width key. */
608 public static final int KEYCODE_ZENKAKU_HANKAKU = 211;
609 /** Key code constant: Japanese alphanumeric key. */
610 public static final int KEYCODE_EISU = 212;
611 /** Key code constant: Japanese non-conversion key. */
612 public static final int KEYCODE_MUHENKAN = 213;
613 /** Key code constant: Japanese conversion key. */
614 public static final int KEYCODE_HENKAN = 214;
615 /** Key code constant: Japanese katakana / hiragana key. */
616 public static final int KEYCODE_KATAKANA_HIRAGANA = 215;
617 /** Key code constant: Japanese Yen key. */
618 public static final int KEYCODE_YEN = 216;
619 /** Key code constant: Japanese Ro key. */
620 public static final int KEYCODE_RO = 217;
621 /** Key code constant: Japanese kana key. */
622 public static final int KEYCODE_KANA = 218;
Jeff Brownde7a8ea2012-06-13 18:28:57 -0700623 /** Key code constant: Assist key.
624 * Launches the global assist activity. Not delivered to applications. */
625 public static final int KEYCODE_ASSIST = 219;
Michael Wright1df477a2013-01-31 16:19:18 -0800626 /** Key code constant: Brightness Down key.
627 * Adjusts the screen brightness down. */
628 public static final int KEYCODE_BRIGHTNESS_DOWN = 220;
629 /** Key code constant: Brightness Up key.
630 * Adjusts the screen brightness up. */
631 public static final int KEYCODE_BRIGHTNESS_UP = 221;
Jeff Brown497a92c2010-09-12 17:55:08 -0700632
Michael Wright1df477a2013-01-31 16:19:18 -0800633 private static final int LAST_KEYCODE = KEYCODE_BRIGHTNESS_UP;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634
635 // NOTE: If you add a new keycode here you must also add it to:
636 // isSystem()
Chirayu Desai61c37ae2013-04-15 20:11:37 +0530637 // frameworks/native/include/android/keycodes.h
638 // frameworks/base/include/androidfw/KeycodeLabels.h
Jeff Brownfd035822010-06-30 16:10:35 -0700639 // external/webkit/WebKit/android/plugins/ANPKeyCodes.h
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 // frameworks/base/core/res/res/values/attrs.xml
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 // emulator?
Jeff Brown6651a632011-11-28 12:59:11 -0800642 // LAST_KEYCODE
643 // KEYCODE_SYMBOLIC_NAMES
Dianne Hackborn935ae462009-04-13 16:11:55 -0700644 //
645 // Also Android currently does not reserve code ranges for vendor-
646 // specific key codes. If you have new key codes to have, you
647 // MUST contribute a patch to the open source project to define
648 // those new codes. This is intended to maintain a consistent
649 // set of key code definitions across all Android devices.
Jeff Brown497a92c2010-09-12 17:55:08 -0700650
Jeff Brown6f2fba42011-02-19 01:08:02 -0800651 // Symbolic names of all key codes.
652 private static final SparseArray<String> KEYCODE_SYMBOLIC_NAMES = new SparseArray<String>();
653 private static void populateKeycodeSymbolicNames() {
654 SparseArray<String> names = KEYCODE_SYMBOLIC_NAMES;
655 names.append(KEYCODE_UNKNOWN, "KEYCODE_UNKNOWN");
656 names.append(KEYCODE_SOFT_LEFT, "KEYCODE_SOFT_LEFT");
657 names.append(KEYCODE_SOFT_RIGHT, "KEYCODE_SOFT_RIGHT");
658 names.append(KEYCODE_HOME, "KEYCODE_HOME");
659 names.append(KEYCODE_BACK, "KEYCODE_BACK");
660 names.append(KEYCODE_CALL, "KEYCODE_CALL");
661 names.append(KEYCODE_ENDCALL, "KEYCODE_ENDCALL");
662 names.append(KEYCODE_0, "KEYCODE_0");
663 names.append(KEYCODE_1, "KEYCODE_1");
664 names.append(KEYCODE_2, "KEYCODE_2");
665 names.append(KEYCODE_3, "KEYCODE_3");
666 names.append(KEYCODE_4, "KEYCODE_4");
667 names.append(KEYCODE_5, "KEYCODE_5");
668 names.append(KEYCODE_6, "KEYCODE_6");
669 names.append(KEYCODE_7, "KEYCODE_7");
670 names.append(KEYCODE_8, "KEYCODE_8");
671 names.append(KEYCODE_9, "KEYCODE_9");
672 names.append(KEYCODE_STAR, "KEYCODE_STAR");
673 names.append(KEYCODE_POUND, "KEYCODE_POUND");
674 names.append(KEYCODE_DPAD_UP, "KEYCODE_DPAD_UP");
675 names.append(KEYCODE_DPAD_DOWN, "KEYCODE_DPAD_DOWN");
676 names.append(KEYCODE_DPAD_LEFT, "KEYCODE_DPAD_LEFT");
677 names.append(KEYCODE_DPAD_RIGHT, "KEYCODE_DPAD_RIGHT");
678 names.append(KEYCODE_DPAD_CENTER, "KEYCODE_DPAD_CENTER");
679 names.append(KEYCODE_VOLUME_UP, "KEYCODE_VOLUME_UP");
680 names.append(KEYCODE_VOLUME_DOWN, "KEYCODE_VOLUME_DOWN");
681 names.append(KEYCODE_POWER, "KEYCODE_POWER");
682 names.append(KEYCODE_CAMERA, "KEYCODE_CAMERA");
683 names.append(KEYCODE_CLEAR, "KEYCODE_CLEAR");
684 names.append(KEYCODE_A, "KEYCODE_A");
685 names.append(KEYCODE_B, "KEYCODE_B");
686 names.append(KEYCODE_C, "KEYCODE_C");
687 names.append(KEYCODE_D, "KEYCODE_D");
688 names.append(KEYCODE_E, "KEYCODE_E");
689 names.append(KEYCODE_F, "KEYCODE_F");
690 names.append(KEYCODE_G, "KEYCODE_G");
691 names.append(KEYCODE_H, "KEYCODE_H");
692 names.append(KEYCODE_I, "KEYCODE_I");
693 names.append(KEYCODE_J, "KEYCODE_J");
694 names.append(KEYCODE_K, "KEYCODE_K");
695 names.append(KEYCODE_L, "KEYCODE_L");
696 names.append(KEYCODE_M, "KEYCODE_M");
697 names.append(KEYCODE_N, "KEYCODE_N");
698 names.append(KEYCODE_O, "KEYCODE_O");
699 names.append(KEYCODE_P, "KEYCODE_P");
700 names.append(KEYCODE_Q, "KEYCODE_Q");
701 names.append(KEYCODE_R, "KEYCODE_R");
702 names.append(KEYCODE_S, "KEYCODE_S");
703 names.append(KEYCODE_T, "KEYCODE_T");
704 names.append(KEYCODE_U, "KEYCODE_U");
705 names.append(KEYCODE_V, "KEYCODE_V");
706 names.append(KEYCODE_W, "KEYCODE_W");
707 names.append(KEYCODE_X, "KEYCODE_X");
708 names.append(KEYCODE_Y, "KEYCODE_Y");
709 names.append(KEYCODE_Z, "KEYCODE_Z");
710 names.append(KEYCODE_COMMA, "KEYCODE_COMMA");
711 names.append(KEYCODE_PERIOD, "KEYCODE_PERIOD");
712 names.append(KEYCODE_ALT_LEFT, "KEYCODE_ALT_LEFT");
713 names.append(KEYCODE_ALT_RIGHT, "KEYCODE_ALT_RIGHT");
714 names.append(KEYCODE_SHIFT_LEFT, "KEYCODE_SHIFT_LEFT");
715 names.append(KEYCODE_SHIFT_RIGHT, "KEYCODE_SHIFT_RIGHT");
716 names.append(KEYCODE_TAB, "KEYCODE_TAB");
717 names.append(KEYCODE_SPACE, "KEYCODE_SPACE");
718 names.append(KEYCODE_SYM, "KEYCODE_SYM");
719 names.append(KEYCODE_EXPLORER, "KEYCODE_EXPLORER");
720 names.append(KEYCODE_ENVELOPE, "KEYCODE_ENVELOPE");
721 names.append(KEYCODE_ENTER, "KEYCODE_ENTER");
722 names.append(KEYCODE_DEL, "KEYCODE_DEL");
723 names.append(KEYCODE_GRAVE, "KEYCODE_GRAVE");
724 names.append(KEYCODE_MINUS, "KEYCODE_MINUS");
725 names.append(KEYCODE_EQUALS, "KEYCODE_EQUALS");
726 names.append(KEYCODE_LEFT_BRACKET, "KEYCODE_LEFT_BRACKET");
727 names.append(KEYCODE_RIGHT_BRACKET, "KEYCODE_RIGHT_BRACKET");
728 names.append(KEYCODE_BACKSLASH, "KEYCODE_BACKSLASH");
729 names.append(KEYCODE_SEMICOLON, "KEYCODE_SEMICOLON");
730 names.append(KEYCODE_APOSTROPHE, "KEYCODE_APOSTROPHE");
731 names.append(KEYCODE_SLASH, "KEYCODE_SLASH");
732 names.append(KEYCODE_AT, "KEYCODE_AT");
733 names.append(KEYCODE_NUM, "KEYCODE_NUM");
734 names.append(KEYCODE_HEADSETHOOK, "KEYCODE_HEADSETHOOK");
735 names.append(KEYCODE_FOCUS, "KEYCODE_FOCUS");
736 names.append(KEYCODE_PLUS, "KEYCODE_PLUS");
737 names.append(KEYCODE_MENU, "KEYCODE_MENU");
738 names.append(KEYCODE_NOTIFICATION, "KEYCODE_NOTIFICATION");
739 names.append(KEYCODE_SEARCH, "KEYCODE_SEARCH");
740 names.append(KEYCODE_MEDIA_PLAY_PAUSE, "KEYCODE_MEDIA_PLAY_PAUSE");
741 names.append(KEYCODE_MEDIA_STOP, "KEYCODE_MEDIA_STOP");
742 names.append(KEYCODE_MEDIA_NEXT, "KEYCODE_MEDIA_NEXT");
743 names.append(KEYCODE_MEDIA_PREVIOUS, "KEYCODE_MEDIA_PREVIOUS");
744 names.append(KEYCODE_MEDIA_REWIND, "KEYCODE_MEDIA_REWIND");
745 names.append(KEYCODE_MEDIA_FAST_FORWARD, "KEYCODE_MEDIA_FAST_FORWARD");
746 names.append(KEYCODE_MUTE, "KEYCODE_MUTE");
747 names.append(KEYCODE_PAGE_UP, "KEYCODE_PAGE_UP");
748 names.append(KEYCODE_PAGE_DOWN, "KEYCODE_PAGE_DOWN");
749 names.append(KEYCODE_PICTSYMBOLS, "KEYCODE_PICTSYMBOLS");
750 names.append(KEYCODE_SWITCH_CHARSET, "KEYCODE_SWITCH_CHARSET");
751 names.append(KEYCODE_BUTTON_A, "KEYCODE_BUTTON_A");
752 names.append(KEYCODE_BUTTON_B, "KEYCODE_BUTTON_B");
753 names.append(KEYCODE_BUTTON_C, "KEYCODE_BUTTON_C");
754 names.append(KEYCODE_BUTTON_X, "KEYCODE_BUTTON_X");
755 names.append(KEYCODE_BUTTON_Y, "KEYCODE_BUTTON_Y");
756 names.append(KEYCODE_BUTTON_Z, "KEYCODE_BUTTON_Z");
757 names.append(KEYCODE_BUTTON_L1, "KEYCODE_BUTTON_L1");
758 names.append(KEYCODE_BUTTON_R1, "KEYCODE_BUTTON_R1");
759 names.append(KEYCODE_BUTTON_L2, "KEYCODE_BUTTON_L2");
760 names.append(KEYCODE_BUTTON_R2, "KEYCODE_BUTTON_R2");
761 names.append(KEYCODE_BUTTON_THUMBL, "KEYCODE_BUTTON_THUMBL");
762 names.append(KEYCODE_BUTTON_THUMBR, "KEYCODE_BUTTON_THUMBR");
763 names.append(KEYCODE_BUTTON_START, "KEYCODE_BUTTON_START");
764 names.append(KEYCODE_BUTTON_SELECT, "KEYCODE_BUTTON_SELECT");
765 names.append(KEYCODE_BUTTON_MODE, "KEYCODE_BUTTON_MODE");
766 names.append(KEYCODE_ESCAPE, "KEYCODE_ESCAPE");
767 names.append(KEYCODE_FORWARD_DEL, "KEYCODE_FORWARD_DEL");
768 names.append(KEYCODE_CTRL_LEFT, "KEYCODE_CTRL_LEFT");
769 names.append(KEYCODE_CTRL_RIGHT, "KEYCODE_CTRL_RIGHT");
770 names.append(KEYCODE_CAPS_LOCK, "KEYCODE_CAPS_LOCK");
771 names.append(KEYCODE_SCROLL_LOCK, "KEYCODE_SCROLL_LOCK");
772 names.append(KEYCODE_META_LEFT, "KEYCODE_META_LEFT");
773 names.append(KEYCODE_META_RIGHT, "KEYCODE_META_RIGHT");
774 names.append(KEYCODE_FUNCTION, "KEYCODE_FUNCTION");
775 names.append(KEYCODE_SYSRQ, "KEYCODE_SYSRQ");
776 names.append(KEYCODE_BREAK, "KEYCODE_BREAK");
777 names.append(KEYCODE_MOVE_HOME, "KEYCODE_MOVE_HOME");
778 names.append(KEYCODE_MOVE_END, "KEYCODE_MOVE_END");
779 names.append(KEYCODE_INSERT, "KEYCODE_INSERT");
780 names.append(KEYCODE_FORWARD, "KEYCODE_FORWARD");
781 names.append(KEYCODE_MEDIA_PLAY, "KEYCODE_MEDIA_PLAY");
782 names.append(KEYCODE_MEDIA_PAUSE, "KEYCODE_MEDIA_PAUSE");
783 names.append(KEYCODE_MEDIA_CLOSE, "KEYCODE_MEDIA_CLOSE");
784 names.append(KEYCODE_MEDIA_EJECT, "KEYCODE_MEDIA_EJECT");
785 names.append(KEYCODE_MEDIA_RECORD, "KEYCODE_MEDIA_RECORD");
786 names.append(KEYCODE_F1, "KEYCODE_F1");
787 names.append(KEYCODE_F2, "KEYCODE_F2");
788 names.append(KEYCODE_F3, "KEYCODE_F3");
789 names.append(KEYCODE_F4, "KEYCODE_F4");
790 names.append(KEYCODE_F5, "KEYCODE_F5");
791 names.append(KEYCODE_F6, "KEYCODE_F6");
792 names.append(KEYCODE_F7, "KEYCODE_F7");
793 names.append(KEYCODE_F8, "KEYCODE_F8");
794 names.append(KEYCODE_F9, "KEYCODE_F9");
795 names.append(KEYCODE_F10, "KEYCODE_F10");
796 names.append(KEYCODE_F11, "KEYCODE_F11");
797 names.append(KEYCODE_F12, "KEYCODE_F12");
798 names.append(KEYCODE_NUM_LOCK, "KEYCODE_NUM_LOCK");
799 names.append(KEYCODE_NUMPAD_0, "KEYCODE_NUMPAD_0");
800 names.append(KEYCODE_NUMPAD_1, "KEYCODE_NUMPAD_1");
801 names.append(KEYCODE_NUMPAD_2, "KEYCODE_NUMPAD_2");
802 names.append(KEYCODE_NUMPAD_3, "KEYCODE_NUMPAD_3");
803 names.append(KEYCODE_NUMPAD_4, "KEYCODE_NUMPAD_4");
804 names.append(KEYCODE_NUMPAD_5, "KEYCODE_NUMPAD_5");
805 names.append(KEYCODE_NUMPAD_6, "KEYCODE_NUMPAD_6");
806 names.append(KEYCODE_NUMPAD_7, "KEYCODE_NUMPAD_7");
807 names.append(KEYCODE_NUMPAD_8, "KEYCODE_NUMPAD_8");
808 names.append(KEYCODE_NUMPAD_9, "KEYCODE_NUMPAD_9");
809 names.append(KEYCODE_NUMPAD_DIVIDE, "KEYCODE_NUMPAD_DIVIDE");
810 names.append(KEYCODE_NUMPAD_MULTIPLY, "KEYCODE_NUMPAD_MULTIPLY");
811 names.append(KEYCODE_NUMPAD_SUBTRACT, "KEYCODE_NUMPAD_SUBTRACT");
812 names.append(KEYCODE_NUMPAD_ADD, "KEYCODE_NUMPAD_ADD");
813 names.append(KEYCODE_NUMPAD_DOT, "KEYCODE_NUMPAD_DOT");
814 names.append(KEYCODE_NUMPAD_COMMA, "KEYCODE_NUMPAD_COMMA");
815 names.append(KEYCODE_NUMPAD_ENTER, "KEYCODE_NUMPAD_ENTER");
816 names.append(KEYCODE_NUMPAD_EQUALS, "KEYCODE_NUMPAD_EQUALS");
817 names.append(KEYCODE_NUMPAD_LEFT_PAREN, "KEYCODE_NUMPAD_LEFT_PAREN");
818 names.append(KEYCODE_NUMPAD_RIGHT_PAREN, "KEYCODE_NUMPAD_RIGHT_PAREN");
819 names.append(KEYCODE_VOLUME_MUTE, "KEYCODE_VOLUME_MUTE");
820 names.append(KEYCODE_INFO, "KEYCODE_INFO");
821 names.append(KEYCODE_CHANNEL_UP, "KEYCODE_CHANNEL_UP");
822 names.append(KEYCODE_CHANNEL_DOWN, "KEYCODE_CHANNEL_DOWN");
823 names.append(KEYCODE_ZOOM_IN, "KEYCODE_ZOOM_IN");
824 names.append(KEYCODE_ZOOM_OUT, "KEYCODE_ZOOM_OUT");
825 names.append(KEYCODE_TV, "KEYCODE_TV");
826 names.append(KEYCODE_WINDOW, "KEYCODE_WINDOW");
827 names.append(KEYCODE_GUIDE, "KEYCODE_GUIDE");
828 names.append(KEYCODE_DVR, "KEYCODE_DVR");
829 names.append(KEYCODE_BOOKMARK, "KEYCODE_BOOKMARK");
830 names.append(KEYCODE_CAPTIONS, "KEYCODE_CAPTIONS");
831 names.append(KEYCODE_SETTINGS, "KEYCODE_SETTINGS");
832 names.append(KEYCODE_TV_POWER, "KEYCODE_TV_POWER");
833 names.append(KEYCODE_TV_INPUT, "KEYCODE_TV_INPUT");
834 names.append(KEYCODE_STB_INPUT, "KEYCODE_STB_INPUT");
835 names.append(KEYCODE_STB_POWER, "KEYCODE_STB_POWER");
836 names.append(KEYCODE_AVR_POWER, "KEYCODE_AVR_POWER");
837 names.append(KEYCODE_AVR_INPUT, "KEYCODE_AVR_INPUT");
838 names.append(KEYCODE_PROG_RED, "KEYCODE_PROG_RED");
839 names.append(KEYCODE_PROG_GREEN, "KEYCODE_PROG_GREEN");
840 names.append(KEYCODE_PROG_YELLOW, "KEYCODE_PROG_YELLOW");
841 names.append(KEYCODE_PROG_BLUE, "KEYCODE_PROG_BLUE");
842 names.append(KEYCODE_APP_SWITCH, "KEYCODE_APP_SWITCH");
843 names.append(KEYCODE_BUTTON_1, "KEYCODE_BUTTON_1");
844 names.append(KEYCODE_BUTTON_2, "KEYCODE_BUTTON_2");
845 names.append(KEYCODE_BUTTON_3, "KEYCODE_BUTTON_3");
846 names.append(KEYCODE_BUTTON_4, "KEYCODE_BUTTON_4");
847 names.append(KEYCODE_BUTTON_5, "KEYCODE_BUTTON_5");
848 names.append(KEYCODE_BUTTON_6, "KEYCODE_BUTTON_6");
849 names.append(KEYCODE_BUTTON_7, "KEYCODE_BUTTON_7");
850 names.append(KEYCODE_BUTTON_8, "KEYCODE_BUTTON_8");
851 names.append(KEYCODE_BUTTON_9, "KEYCODE_BUTTON_9");
852 names.append(KEYCODE_BUTTON_10, "KEYCODE_BUTTON_10");
853 names.append(KEYCODE_BUTTON_11, "KEYCODE_BUTTON_11");
854 names.append(KEYCODE_BUTTON_12, "KEYCODE_BUTTON_12");
855 names.append(KEYCODE_BUTTON_13, "KEYCODE_BUTTON_13");
856 names.append(KEYCODE_BUTTON_14, "KEYCODE_BUTTON_14");
857 names.append(KEYCODE_BUTTON_15, "KEYCODE_BUTTON_15");
858 names.append(KEYCODE_BUTTON_16, "KEYCODE_BUTTON_16");
Jeff Brown9812aed2011-03-07 17:09:51 -0800859 names.append(KEYCODE_LANGUAGE_SWITCH, "KEYCODE_LANGUAGE_SWITCH");
860 names.append(KEYCODE_MANNER_MODE, "KEYCODE_MANNER_MODE");
861 names.append(KEYCODE_3D_MODE, "KEYCODE_3D_MODE");
Jeff Brown6651a632011-11-28 12:59:11 -0800862 names.append(KEYCODE_CONTACTS, "KEYCODE_CONTACTS");
863 names.append(KEYCODE_CALENDAR, "KEYCODE_CALENDAR");
864 names.append(KEYCODE_MUSIC, "KEYCODE_MUSIC");
865 names.append(KEYCODE_CALCULATOR, "KEYCODE_CALCULATOR");
Yang Chuang7511f9c2012-02-10 15:18:26 +0800866 names.append(KEYCODE_ZENKAKU_HANKAKU, "KEYCODE_ZENKAKU_HANKAKU");
867 names.append(KEYCODE_EISU, "KEYCODE_EISU");
868 names.append(KEYCODE_MUHENKAN, "KEYCODE_MUHENKAN");
869 names.append(KEYCODE_HENKAN, "KEYCODE_HENKAN");
870 names.append(KEYCODE_KATAKANA_HIRAGANA, "KEYCODE_KATAKANA_HIRAGANA");
871 names.append(KEYCODE_YEN, "KEYCODE_YEN");
872 names.append(KEYCODE_RO, "KEYCODE_RO");
873 names.append(KEYCODE_KANA, "KEYCODE_KANA");
Jeff Brownde7a8ea2012-06-13 18:28:57 -0700874 names.append(KEYCODE_ASSIST, "KEYCODE_ASSIST");
Michael Wright1df477a2013-01-31 16:19:18 -0800875 names.append(KEYCODE_BRIGHTNESS_DOWN, "KEYCODE_BRIGHTNESS_DOWN");
876 names.append(KEYCODE_BRIGHTNESS_UP, "KEYCODE_BRIGHTNESS_UP");
Jeff Brown497a92c2010-09-12 17:55:08 -0700877 };
878
879 // Symbolic names of all metakeys in bit order from least significant to most significant.
880 // Accordingly there are exactly 32 values in this table.
881 private static final String[] META_SYMBOLIC_NAMES = new String[] {
882 "META_SHIFT_ON",
883 "META_ALT_ON",
884 "META_SYM_ON",
885 "META_FUNCTION_ON",
886 "META_ALT_LEFT_ON",
887 "META_ALT_RIGHT_ON",
888 "META_SHIFT_LEFT_ON",
889 "META_SHIFT_RIGHT_ON",
890 "META_CAP_LOCKED",
891 "META_ALT_LOCKED",
892 "META_SYM_LOCKED",
893 "0x00000800",
894 "META_CTRL_ON",
895 "META_CTRL_LEFT_ON",
896 "META_CTRL_RIGHT_ON",
897 "0x00008000",
898 "META_META_ON",
899 "META_META_LEFT_ON",
900 "META_META_RIGHT_ON",
901 "0x00080000",
Jeff Brown51e7fe72010-10-29 22:19:53 -0700902 "META_CAPS_LOCK_ON",
903 "META_NUM_LOCK_ON",
904 "META_SCROLL_LOCK_ON",
Jeff Brown497a92c2010-09-12 17:55:08 -0700905 "0x00800000",
906 "0x01000000",
907 "0x02000000",
908 "0x04000000",
909 "0x08000000",
910 "0x10000000",
911 "0x20000000",
912 "0x40000000",
913 "0x80000000",
914 };
915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 /**
917 * @deprecated There are now more than MAX_KEYCODE keycodes.
918 * Use {@link #getMaxKeyCode()} instead.
919 */
920 @Deprecated
921 public static final int MAX_KEYCODE = 84;
922
923 /**
924 * {@link #getAction} value: the key has been pressed down.
925 */
926 public static final int ACTION_DOWN = 0;
927 /**
928 * {@link #getAction} value: the key has been released.
929 */
930 public static final int ACTION_UP = 1;
931 /**
932 * {@link #getAction} value: multiple duplicate key events have
933 * occurred in a row, or a complex string is being delivered. If the
934 * key code is not {#link {@link #KEYCODE_UNKNOWN} then the
935 * {#link {@link #getRepeatCount()} method returns the number of times
936 * the given key code should be executed.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700937 * Otherwise, if the key code is {@link #KEYCODE_UNKNOWN}, then
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 * this is a sequence of characters as returned by {@link #getCharacters}.
939 */
940 public static final int ACTION_MULTIPLE = 2;
941
942 /**
Jeff Brown497a92c2010-09-12 17:55:08 -0700943 * SHIFT key locked in CAPS mode.
944 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
945 * @hide
946 */
947 public static final int META_CAP_LOCKED = 0x100;
948
949 /**
950 * ALT key locked.
951 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
952 * @hide
953 */
954 public static final int META_ALT_LOCKED = 0x200;
955
956 /**
957 * SYM key locked.
958 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
959 * @hide
960 */
961 public static final int META_SYM_LOCKED = 0x400;
962
963 /**
964 * Text is in selection mode.
965 * Reserved for use by {@link MetaKeyKeyListener} for a private unpublished constant
966 * in its API that is currently being retained for legacy reasons.
967 * @hide
968 */
969 public static final int META_SELECTING = 0x800;
970
971 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 * <p>This mask is used to check whether one of the ALT meta keys is pressed.</p>
973 *
974 * @see #isAltPressed()
975 * @see #getMetaState()
976 * @see #KEYCODE_ALT_LEFT
977 * @see #KEYCODE_ALT_RIGHT
978 */
979 public static final int META_ALT_ON = 0x02;
980
981 /**
982 * <p>This mask is used to check whether the left ALT meta key is pressed.</p>
983 *
984 * @see #isAltPressed()
985 * @see #getMetaState()
986 * @see #KEYCODE_ALT_LEFT
987 */
988 public static final int META_ALT_LEFT_ON = 0x10;
989
990 /**
991 * <p>This mask is used to check whether the right the ALT meta key is pressed.</p>
992 *
993 * @see #isAltPressed()
994 * @see #getMetaState()
995 * @see #KEYCODE_ALT_RIGHT
996 */
997 public static final int META_ALT_RIGHT_ON = 0x20;
998
999 /**
1000 * <p>This mask is used to check whether one of the SHIFT meta keys is pressed.</p>
1001 *
1002 * @see #isShiftPressed()
1003 * @see #getMetaState()
1004 * @see #KEYCODE_SHIFT_LEFT
1005 * @see #KEYCODE_SHIFT_RIGHT
1006 */
1007 public static final int META_SHIFT_ON = 0x1;
1008
1009 /**
1010 * <p>This mask is used to check whether the left SHIFT meta key is pressed.</p>
1011 *
1012 * @see #isShiftPressed()
1013 * @see #getMetaState()
1014 * @see #KEYCODE_SHIFT_LEFT
1015 */
1016 public static final int META_SHIFT_LEFT_ON = 0x40;
1017
1018 /**
1019 * <p>This mask is used to check whether the right SHIFT meta key is pressed.</p>
1020 *
1021 * @see #isShiftPressed()
1022 * @see #getMetaState()
1023 * @see #KEYCODE_SHIFT_RIGHT
1024 */
1025 public static final int META_SHIFT_RIGHT_ON = 0x80;
1026
1027 /**
1028 * <p>This mask is used to check whether the SYM meta key is pressed.</p>
1029 *
1030 * @see #isSymPressed()
1031 * @see #getMetaState()
1032 */
1033 public static final int META_SYM_ON = 0x4;
1034
1035 /**
Jeff Brown497a92c2010-09-12 17:55:08 -07001036 * <p>This mask is used to check whether the FUNCTION meta key is pressed.</p>
1037 *
1038 * @see #isFunctionPressed()
1039 * @see #getMetaState()
1040 */
1041 public static final int META_FUNCTION_ON = 0x8;
1042
1043 /**
1044 * <p>This mask is used to check whether one of the CTRL meta keys is pressed.</p>
1045 *
1046 * @see #isCtrlPressed()
1047 * @see #getMetaState()
1048 * @see #KEYCODE_CTRL_LEFT
1049 * @see #KEYCODE_CTRL_RIGHT
1050 */
1051 public static final int META_CTRL_ON = 0x1000;
1052
1053 /**
1054 * <p>This mask is used to check whether the left CTRL meta key is pressed.</p>
1055 *
1056 * @see #isCtrlPressed()
1057 * @see #getMetaState()
1058 * @see #KEYCODE_CTRL_LEFT
1059 */
1060 public static final int META_CTRL_LEFT_ON = 0x2000;
1061
1062 /**
1063 * <p>This mask is used to check whether the right CTRL meta key is pressed.</p>
1064 *
1065 * @see #isCtrlPressed()
1066 * @see #getMetaState()
1067 * @see #KEYCODE_CTRL_RIGHT
1068 */
1069 public static final int META_CTRL_RIGHT_ON = 0x4000;
1070
1071 /**
1072 * <p>This mask is used to check whether one of the META meta keys is pressed.</p>
1073 *
1074 * @see #isMetaPressed()
1075 * @see #getMetaState()
1076 * @see #KEYCODE_META_LEFT
1077 * @see #KEYCODE_META_RIGHT
1078 */
1079 public static final int META_META_ON = 0x10000;
1080
1081 /**
1082 * <p>This mask is used to check whether the left META meta key is pressed.</p>
1083 *
1084 * @see #isMetaPressed()
1085 * @see #getMetaState()
1086 * @see #KEYCODE_META_LEFT
1087 */
1088 public static final int META_META_LEFT_ON = 0x20000;
1089
1090 /**
1091 * <p>This mask is used to check whether the right META meta key is pressed.</p>
1092 *
1093 * @see #isMetaPressed()
1094 * @see #getMetaState()
1095 * @see #KEYCODE_META_RIGHT
1096 */
1097 public static final int META_META_RIGHT_ON = 0x40000;
1098
1099 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07001100 * <p>This mask is used to check whether the CAPS LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07001101 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07001102 * @see #isCapsLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -07001103 * @see #getMetaState()
1104 * @see #KEYCODE_CAPS_LOCK
1105 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07001106 public static final int META_CAPS_LOCK_ON = 0x100000;
Jeff Brown497a92c2010-09-12 17:55:08 -07001107
1108 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07001109 * <p>This mask is used to check whether the NUM LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07001110 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07001111 * @see #isNumLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -07001112 * @see #getMetaState()
1113 * @see #KEYCODE_NUM_LOCK
1114 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07001115 public static final int META_NUM_LOCK_ON = 0x200000;
Jeff Brown497a92c2010-09-12 17:55:08 -07001116
1117 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07001118 * <p>This mask is used to check whether the SCROLL LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07001119 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07001120 * @see #isScrollLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -07001121 * @see #getMetaState()
1122 * @see #KEYCODE_SCROLL_LOCK
1123 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07001124 public static final int META_SCROLL_LOCK_ON = 0x400000;
Jeff Brown497a92c2010-09-12 17:55:08 -07001125
Jeff Brown64da12a2011-01-04 19:57:47 -08001126 /**
1127 * This mask is a combination of {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON}
1128 * and {@link #META_SHIFT_RIGHT_ON}.
1129 */
Jeff Brownc1df9072010-12-21 16:38:50 -08001130 public static final int META_SHIFT_MASK = META_SHIFT_ON
1131 | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON;
1132
Jeff Brown64da12a2011-01-04 19:57:47 -08001133 /**
1134 * This mask is a combination of {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON}
1135 * and {@link #META_ALT_RIGHT_ON}.
1136 */
Jeff Brownc1df9072010-12-21 16:38:50 -08001137 public static final int META_ALT_MASK = META_ALT_ON
1138 | META_ALT_LEFT_ON | META_ALT_RIGHT_ON;
1139
Jeff Brown64da12a2011-01-04 19:57:47 -08001140 /**
1141 * This mask is a combination of {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON}
1142 * and {@link #META_CTRL_RIGHT_ON}.
1143 */
Jeff Brownc1df9072010-12-21 16:38:50 -08001144 public static final int META_CTRL_MASK = META_CTRL_ON
1145 | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON;
1146
Jeff Brown64da12a2011-01-04 19:57:47 -08001147 /**
1148 * This mask is a combination of {@link #META_META_ON}, {@link #META_META_LEFT_ON}
1149 * and {@link #META_META_RIGHT_ON}.
1150 */
1151 public static final int META_META_MASK = META_META_ON
Jeff Brownc1df9072010-12-21 16:38:50 -08001152 | META_META_LEFT_ON | META_META_RIGHT_ON;
1153
Jeff Brown497a92c2010-09-12 17:55:08 -07001154 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 * This mask is set if the device woke because of this key event.
1156 */
1157 public static final int FLAG_WOKE_HERE = 0x1;
1158
1159 /**
1160 * This mask is set if the key event was generated by a software keyboard.
1161 */
1162 public static final int FLAG_SOFT_KEYBOARD = 0x2;
1163
1164 /**
1165 * This mask is set if we don't want the key event to cause us to leave
1166 * touch mode.
1167 */
1168 public static final int FLAG_KEEP_TOUCH_MODE = 0x4;
1169
1170 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001171 * This mask is set if an event was known to come from a trusted part
1172 * of the system. That is, the event is known to come from the user,
1173 * and could not have been spoofed by a third party component.
1174 */
1175 public static final int FLAG_FROM_SYSTEM = 0x8;
1176
1177 /**
1178 * This mask is used for compatibility, to identify enter keys that are
1179 * coming from an IME whose enter key has been auto-labelled "next" or
1180 * "done". This allows TextView to dispatch these as normal enter keys
1181 * for old applications, but still do the appropriate action when
1182 * receiving them.
1183 */
1184 public static final int FLAG_EDITOR_ACTION = 0x10;
1185
1186 /**
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07001187 * When associated with up key events, this indicates that the key press
1188 * has been canceled. Typically this is used with virtual touch screen
1189 * keys, where the user can slide from the virtual key area on to the
1190 * display: in that case, the application will receive a canceled up
1191 * event and should not perform the action normally associated with the
1192 * key. Note that for this to work, the application can not perform an
1193 * action for a key until it receives an up or the long press timeout has
1194 * expired.
1195 */
1196 public static final int FLAG_CANCELED = 0x20;
1197
1198 /**
1199 * This key event was generated by a virtual (on-screen) hard key area.
1200 * Typically this is an area of the touchscreen, outside of the regular
1201 * display, dedicated to "hardware" buttons.
1202 */
1203 public static final int FLAG_VIRTUAL_HARD_KEY = 0x40;
1204
1205 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001206 * This flag is set for the first key repeat that occurs after the
1207 * long press timeout.
1208 */
1209 public static final int FLAG_LONG_PRESS = 0x80;
1210
1211 /**
1212 * Set when a key event has {@link #FLAG_CANCELED} set because a long
1213 * press action was executed while it was down.
1214 */
1215 public static final int FLAG_CANCELED_LONG_PRESS = 0x100;
1216
1217 /**
1218 * Set for {@link #ACTION_UP} when this event's key code is still being
1219 * tracked from its initial down. That is, somebody requested that tracking
1220 * started on the key down and a long press has not caused
1221 * the tracking to be canceled.
1222 */
1223 public static final int FLAG_TRACKING = 0x200;
Jeff Brown49ed71d2010-12-06 17:13:33 -08001224
1225 /**
1226 * Set when a key event has been synthesized to implement default behavior
1227 * for an event that the application did not handle.
1228 * Fallback key events are generated by unhandled trackball motions
1229 * (to emulate a directional keypad) and by certain unhandled key presses
1230 * that are declared in the key map (such as special function numeric keypad
1231 * keys when numlock is off).
1232 */
1233 public static final int FLAG_FALLBACK = 0x400;
1234
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001235 /**
Michael Wrighta44dd262013-04-10 21:12:00 -07001236 * Signifies that the key is being predispatched.
1237 * @hide
1238 */
1239 public static final int FLAG_PREDISPATCH = 0x20000000;
1240
1241 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001242 * Private control to determine when an app is tracking a key sequence.
1243 * @hide
1244 */
1245 public static final int FLAG_START_TRACKING = 0x40000000;
Jeff Brown21bc5c92011-02-28 18:27:14 -08001246
1247 /**
1248 * Private flag that indicates when the system has detected that this key event
1249 * may be inconsistent with respect to the sequence of previously delivered key events,
1250 * such as when a key up event is sent but the key was not down.
1251 *
1252 * @hide
1253 * @see #isTainted
1254 * @see #setTainted
1255 */
1256 public static final int FLAG_TAINTED = 0x80000000;
1257
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001258 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 * Returns the maximum keycode.
1260 */
1261 public static int getMaxKeyCode() {
1262 return LAST_KEYCODE;
1263 }
1264
1265 /**
1266 * Get the character that is produced by putting accent on the character
1267 * c.
1268 * For example, getDeadChar('`', 'e') returns &egrave;.
1269 */
1270 public static int getDeadChar(int accent, int c) {
1271 return KeyCharacterMap.getDeadChar(accent, c);
1272 }
1273
Dianne Hackborn8d374262009-09-14 21:21:52 -07001274 static final boolean DEBUG = false;
1275 static final String TAG = "KeyEvent";
Jeff Brown1f245102010-11-18 20:53:46 -08001276
1277 private static final int MAX_RECYCLED = 10;
1278 private static final Object gRecyclerLock = new Object();
1279 private static int gRecyclerUsed;
1280 private static KeyEvent gRecyclerTop;
1281
1282 private KeyEvent mNext;
Jeff Brown1f245102010-11-18 20:53:46 -08001283
Jeff Brown91c69ab2011-02-14 17:03:18 -08001284 private int mDeviceId;
1285 private int mSource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 private int mMetaState;
1287 private int mAction;
1288 private int mKeyCode;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001289 private int mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 private int mRepeatCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 private int mFlags;
1292 private long mDownTime;
1293 private long mEventTime;
1294 private String mCharacters;
1295
1296 public interface Callback {
1297 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001298 * Called when a key down event has occurred. If you return true,
1299 * you can first call {@link KeyEvent#startTracking()
1300 * KeyEvent.startTracking()} to have the framework track the event
1301 * through its {@link #onKeyUp(int, KeyEvent)} and also call your
1302 * {@link #onKeyLongPress(int, KeyEvent)} if it occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 *
1304 * @param keyCode The value in event.getKeyCode().
1305 * @param event Description of the key event.
1306 *
1307 * @return If you handled the event, return true. If you want to allow
1308 * the event to be handled by the next receiver, return false.
1309 */
1310 boolean onKeyDown(int keyCode, KeyEvent event);
1311
1312 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001313 * Called when a long press has occurred. If you return true,
1314 * the final key up will have {@link KeyEvent#FLAG_CANCELED} and
1315 * {@link KeyEvent#FLAG_CANCELED_LONG_PRESS} set. Note that in
1316 * order to receive this callback, someone in the event change
1317 * <em>must</em> return true from {@link #onKeyDown} <em>and</em>
1318 * call {@link KeyEvent#startTracking()} on the event.
1319 *
1320 * @param keyCode The value in event.getKeyCode().
1321 * @param event Description of the key event.
1322 *
1323 * @return If you handled the event, return true. If you want to allow
1324 * the event to be handled by the next receiver, return false.
1325 */
1326 boolean onKeyLongPress(int keyCode, KeyEvent event);
1327
1328 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 * Called when a key up event has occurred.
1330 *
1331 * @param keyCode The value in event.getKeyCode().
1332 * @param event Description of the key event.
1333 *
1334 * @return If you handled the event, return true. If you want to allow
1335 * the event to be handled by the next receiver, return false.
1336 */
1337 boolean onKeyUp(int keyCode, KeyEvent event);
1338
1339 /**
1340 * Called when multiple down/up pairs of the same key have occurred
1341 * in a row.
1342 *
1343 * @param keyCode The value in event.getKeyCode().
1344 * @param count Number of pairs as returned by event.getRepeatCount().
1345 * @param event Description of the key event.
1346 *
1347 * @return If you handled the event, return true. If you want to allow
1348 * the event to be handled by the next receiver, return false.
1349 */
1350 boolean onKeyMultiple(int keyCode, int count, KeyEvent event);
1351 }
1352
Jeff Brown497a92c2010-09-12 17:55:08 -07001353 static {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001354 populateKeycodeSymbolicNames();
Jeff Brown497a92c2010-09-12 17:55:08 -07001355 }
1356
Jeff Brown1f245102010-11-18 20:53:46 -08001357 private KeyEvent() {
1358 }
1359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 /**
1361 * Create a new key event.
1362 *
1363 * @param action Action code: either {@link #ACTION_DOWN},
1364 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1365 * @param code The key code.
1366 */
1367 public KeyEvent(int action, int code) {
1368 mAction = action;
1369 mKeyCode = code;
1370 mRepeatCount = 0;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001371 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 }
1373
1374 /**
1375 * Create a new key event.
1376 *
1377 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1378 * at which this key code originally went down.
1379 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1380 * at which this event happened.
1381 * @param action Action code: either {@link #ACTION_DOWN},
1382 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1383 * @param code The key code.
1384 * @param repeat A repeat count for down events (> 0 if this is after the
1385 * initial down) or event count for multiple events.
1386 */
1387 public KeyEvent(long downTime, long eventTime, int action,
1388 int code, int repeat) {
1389 mDownTime = downTime;
1390 mEventTime = eventTime;
1391 mAction = action;
1392 mKeyCode = code;
1393 mRepeatCount = repeat;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001394 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 }
1396
1397 /**
1398 * Create a new key event.
1399 *
1400 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1401 * at which this key code originally went down.
1402 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1403 * at which this event happened.
1404 * @param action Action code: either {@link #ACTION_DOWN},
1405 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1406 * @param code The key code.
1407 * @param repeat A repeat count for down events (> 0 if this is after the
1408 * initial down) or event count for multiple events.
1409 * @param metaState Flags indicating which meta keys are currently pressed.
1410 */
1411 public KeyEvent(long downTime, long eventTime, int action,
1412 int code, int repeat, int metaState) {
1413 mDownTime = downTime;
1414 mEventTime = eventTime;
1415 mAction = action;
1416 mKeyCode = code;
1417 mRepeatCount = repeat;
1418 mMetaState = metaState;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001419 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 }
1421
1422 /**
1423 * Create a new key event.
1424 *
1425 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1426 * at which this key code originally went down.
1427 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1428 * at which this event happened.
1429 * @param action Action code: either {@link #ACTION_DOWN},
1430 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1431 * @param code The key code.
1432 * @param repeat A repeat count for down events (> 0 if this is after the
1433 * initial down) or event count for multiple events.
1434 * @param metaState Flags indicating which meta keys are currently pressed.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001435 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 * @param scancode Raw device scan code of the event.
1437 */
1438 public KeyEvent(long downTime, long eventTime, int action,
1439 int code, int repeat, int metaState,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001440 int deviceId, int scancode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 mDownTime = downTime;
1442 mEventTime = eventTime;
1443 mAction = action;
1444 mKeyCode = code;
1445 mRepeatCount = repeat;
1446 mMetaState = metaState;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001447 mDeviceId = deviceId;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001448 mScanCode = scancode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 }
1450
1451 /**
1452 * Create a new key event.
1453 *
1454 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1455 * at which this key code originally went down.
1456 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1457 * at which this event happened.
1458 * @param action Action code: either {@link #ACTION_DOWN},
1459 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1460 * @param code The key code.
1461 * @param repeat A repeat count for down events (> 0 if this is after the
1462 * initial down) or event count for multiple events.
1463 * @param metaState Flags indicating which meta keys are currently pressed.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001464 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 * @param scancode Raw device scan code of the event.
1466 * @param flags The flags for this key event
1467 */
1468 public KeyEvent(long downTime, long eventTime, int action,
1469 int code, int repeat, int metaState,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001470 int deviceId, int scancode, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 mDownTime = downTime;
1472 mEventTime = eventTime;
1473 mAction = action;
1474 mKeyCode = code;
1475 mRepeatCount = repeat;
1476 mMetaState = metaState;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001477 mDeviceId = deviceId;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001478 mScanCode = scancode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 mFlags = flags;
1480 }
1481
1482 /**
Jeff Brownc5ed5912010-07-14 18:48:53 -07001483 * Create a new key event.
1484 *
1485 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1486 * at which this key code originally went down.
1487 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1488 * at which this event happened.
1489 * @param action Action code: either {@link #ACTION_DOWN},
1490 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1491 * @param code The key code.
1492 * @param repeat A repeat count for down events (> 0 if this is after the
1493 * initial down) or event count for multiple events.
1494 * @param metaState Flags indicating which meta keys are currently pressed.
1495 * @param deviceId The device ID that generated the key event.
1496 * @param scancode Raw device scan code of the event.
1497 * @param flags The flags for this key event
1498 * @param source The input source such as {@link InputDevice#SOURCE_KEYBOARD}.
1499 */
1500 public KeyEvent(long downTime, long eventTime, int action,
1501 int code, int repeat, int metaState,
1502 int deviceId, int scancode, int flags, int source) {
1503 mDownTime = downTime;
1504 mEventTime = eventTime;
1505 mAction = action;
1506 mKeyCode = code;
1507 mRepeatCount = repeat;
1508 mMetaState = metaState;
1509 mDeviceId = deviceId;
1510 mScanCode = scancode;
1511 mFlags = flags;
1512 mSource = source;
1513 }
1514
1515 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 * Create a new key event for a string of characters. The key code,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001517 * action, repeat count and source will automatically be set to
1518 * {@link #KEYCODE_UNKNOWN}, {@link #ACTION_MULTIPLE}, 0, and
1519 * {@link InputDevice#SOURCE_KEYBOARD} for you.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 *
1521 * @param time The time (in {@link android.os.SystemClock#uptimeMillis})
1522 * at which this event occured.
1523 * @param characters The string of characters.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001524 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 * @param flags The flags for this key event
1526 */
Jeff Brownc5ed5912010-07-14 18:48:53 -07001527 public KeyEvent(long time, String characters, int deviceId, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 mDownTime = time;
1529 mEventTime = time;
1530 mCharacters = characters;
1531 mAction = ACTION_MULTIPLE;
1532 mKeyCode = KEYCODE_UNKNOWN;
1533 mRepeatCount = 0;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001534 mDeviceId = deviceId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 mFlags = flags;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001536 mSource = InputDevice.SOURCE_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 }
1538
1539 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001540 * Make an exact copy of an existing key event.
1541 */
1542 public KeyEvent(KeyEvent origEvent) {
1543 mDownTime = origEvent.mDownTime;
1544 mEventTime = origEvent.mEventTime;
1545 mAction = origEvent.mAction;
1546 mKeyCode = origEvent.mKeyCode;
1547 mRepeatCount = origEvent.mRepeatCount;
1548 mMetaState = origEvent.mMetaState;
1549 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001550 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001551 mScanCode = origEvent.mScanCode;
The Android Open Source Project10592532009-03-18 17:39:46 -07001552 mFlags = origEvent.mFlags;
1553 mCharacters = origEvent.mCharacters;
1554 }
1555
1556 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557 * Copy an existing key event, modifying its time and repeat count.
1558 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001559 * @deprecated Use {@link #changeTimeRepeat(KeyEvent, long, int)}
1560 * instead.
1561 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 * @param origEvent The existing event to be copied.
1563 * @param eventTime The new event time
1564 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1565 * @param newRepeat The new repeat count of the event.
1566 */
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001567 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 public KeyEvent(KeyEvent origEvent, long eventTime, int newRepeat) {
1569 mDownTime = origEvent.mDownTime;
1570 mEventTime = eventTime;
1571 mAction = origEvent.mAction;
1572 mKeyCode = origEvent.mKeyCode;
1573 mRepeatCount = newRepeat;
1574 mMetaState = origEvent.mMetaState;
1575 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001576 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001577 mScanCode = origEvent.mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 mFlags = origEvent.mFlags;
1579 mCharacters = origEvent.mCharacters;
1580 }
1581
Jeff Brown1f245102010-11-18 20:53:46 -08001582 private static KeyEvent obtain() {
1583 final KeyEvent ev;
1584 synchronized (gRecyclerLock) {
1585 ev = gRecyclerTop;
1586 if (ev == null) {
1587 return new KeyEvent();
1588 }
1589 gRecyclerTop = ev.mNext;
1590 gRecyclerUsed -= 1;
1591 }
Jeff Brown1f245102010-11-18 20:53:46 -08001592 ev.mNext = null;
Jeff Brown32cbc38552011-12-01 14:01:49 -08001593 ev.prepareForReuse();
Jeff Brown1f245102010-11-18 20:53:46 -08001594 return ev;
1595 }
1596
1597 /**
1598 * Obtains a (potentially recycled) key event.
1599 *
1600 * @hide
1601 */
1602 public static KeyEvent obtain(long downTime, long eventTime, int action,
1603 int code, int repeat, int metaState,
1604 int deviceId, int scancode, int flags, int source, String characters) {
1605 KeyEvent ev = obtain();
1606 ev.mDownTime = downTime;
1607 ev.mEventTime = eventTime;
1608 ev.mAction = action;
1609 ev.mKeyCode = code;
1610 ev.mRepeatCount = repeat;
1611 ev.mMetaState = metaState;
1612 ev.mDeviceId = deviceId;
1613 ev.mScanCode = scancode;
1614 ev.mFlags = flags;
1615 ev.mSource = source;
1616 ev.mCharacters = characters;
1617 return ev;
1618 }
1619
1620 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08001621 * Obtains a (potentially recycled) copy of another key event.
1622 *
1623 * @hide
1624 */
1625 public static KeyEvent obtain(KeyEvent other) {
1626 KeyEvent ev = obtain();
1627 ev.mDownTime = other.mDownTime;
1628 ev.mEventTime = other.mEventTime;
1629 ev.mAction = other.mAction;
1630 ev.mKeyCode = other.mKeyCode;
1631 ev.mRepeatCount = other.mRepeatCount;
1632 ev.mMetaState = other.mMetaState;
1633 ev.mDeviceId = other.mDeviceId;
1634 ev.mScanCode = other.mScanCode;
1635 ev.mFlags = other.mFlags;
1636 ev.mSource = other.mSource;
1637 ev.mCharacters = other.mCharacters;
1638 return ev;
1639 }
1640
1641 /** @hide */
1642 @Override
1643 public KeyEvent copy() {
1644 return obtain(this);
1645 }
1646
1647 /**
Jeff Brown1f245102010-11-18 20:53:46 -08001648 * Recycles a key event.
1649 * Key events should only be recycled if they are owned by the system since user
1650 * code expects them to be essentially immutable, "tracking" notwithstanding.
1651 *
1652 * @hide
1653 */
Jeff Brown92cc2d82011-12-02 01:19:47 -08001654 @Override
Jeff Brown1f245102010-11-18 20:53:46 -08001655 public final void recycle() {
Jeff Brown32cbc38552011-12-01 14:01:49 -08001656 super.recycle();
Jeff Brown1f245102010-11-18 20:53:46 -08001657 mCharacters = null;
1658
1659 synchronized (gRecyclerLock) {
1660 if (gRecyclerUsed < MAX_RECYCLED) {
1661 gRecyclerUsed++;
1662 mNext = gRecyclerTop;
1663 gRecyclerTop = this;
1664 }
1665 }
1666 }
1667
Jeff Brown92cc2d82011-12-02 01:19:47 -08001668 /** @hide */
1669 @Override
1670 public final void recycleIfNeededAfterDispatch() {
1671 // Do nothing.
1672 }
1673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001675 * Create a new key event that is the same as the given one, but whose
1676 * event time and repeat count are replaced with the given value.
1677 *
1678 * @param event The existing event to be copied. This is not modified.
1679 * @param eventTime The new event time
1680 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1681 * @param newRepeat The new repeat count of the event.
1682 */
1683 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime,
1684 int newRepeat) {
1685 return new KeyEvent(event, eventTime, newRepeat);
1686 }
1687
1688 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001689 * Create a new key event that is the same as the given one, but whose
1690 * event time and repeat count are replaced with the given value.
1691 *
1692 * @param event The existing event to be copied. This is not modified.
1693 * @param eventTime The new event time
1694 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1695 * @param newRepeat The new repeat count of the event.
1696 * @param newFlags New flags for the event, replacing the entire value
1697 * in the original event.
1698 */
1699 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime,
1700 int newRepeat, int newFlags) {
1701 KeyEvent ret = new KeyEvent(event);
1702 ret.mEventTime = eventTime;
1703 ret.mRepeatCount = newRepeat;
1704 ret.mFlags = newFlags;
1705 return ret;
1706 }
1707
1708 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 * Copy an existing key event, modifying its action.
1710 *
1711 * @param origEvent The existing event to be copied.
1712 * @param action The new action code of the event.
1713 */
The Android Open Source Project10592532009-03-18 17:39:46 -07001714 private KeyEvent(KeyEvent origEvent, int action) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 mDownTime = origEvent.mDownTime;
1716 mEventTime = origEvent.mEventTime;
1717 mAction = action;
1718 mKeyCode = origEvent.mKeyCode;
1719 mRepeatCount = origEvent.mRepeatCount;
1720 mMetaState = origEvent.mMetaState;
1721 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001722 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001723 mScanCode = origEvent.mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724 mFlags = origEvent.mFlags;
1725 // Don't copy mCharacters, since one way or the other we'll lose it
1726 // when changing the action.
1727 }
1728
1729 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001730 * Create a new key event that is the same as the given one, but whose
1731 * action is replaced with the given value.
1732 *
1733 * @param event The existing event to be copied. This is not modified.
1734 * @param action The new action code of the event.
1735 */
1736 public static KeyEvent changeAction(KeyEvent event, int action) {
1737 return new KeyEvent(event, action);
1738 }
1739
1740 /**
1741 * Create a new key event that is the same as the given one, but whose
1742 * flags are replaced with the given value.
1743 *
1744 * @param event The existing event to be copied. This is not modified.
1745 * @param flags The new flags constant.
1746 */
1747 public static KeyEvent changeFlags(KeyEvent event, int flags) {
1748 event = new KeyEvent(event);
1749 event.mFlags = flags;
1750 return event;
1751 }
Jeff Brown21bc5c92011-02-28 18:27:14 -08001752
1753 /** @hide */
1754 @Override
1755 public final boolean isTainted() {
1756 return (mFlags & FLAG_TAINTED) != 0;
1757 }
1758
1759 /** @hide */
1760 @Override
1761 public final void setTainted(boolean tainted) {
1762 mFlags = tainted ? mFlags | FLAG_TAINTED : mFlags & ~FLAG_TAINTED;
1763 }
1764
The Android Open Source Project10592532009-03-18 17:39:46 -07001765 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 * Don't use in new code, instead explicitly check
1767 * {@link #getAction()}.
1768 *
1769 * @return If the action is ACTION_DOWN, returns true; else false.
1770 *
1771 * @deprecated
1772 * @hide
1773 */
1774 @Deprecated public final boolean isDown() {
1775 return mAction == ACTION_DOWN;
1776 }
1777
1778 /**
1779 * Is this a system key? System keys can not be used for menu shortcuts.
1780 *
1781 * TODO: this information should come from a table somewhere.
1782 * TODO: should the dpad keys be here? arguably, because they also shouldn't be menu shortcuts
1783 */
1784 public final boolean isSystem() {
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -07001785 return native_isSystemKey(mKeyCode);
1786 }
1787
1788 /** @hide */
1789 public final boolean hasDefaultAction() {
1790 return native_hasDefaultAction(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791 }
1792
Jeff Brown6f2fba42011-02-19 01:08:02 -08001793 /**
1794 * Returns true if the specified keycode is a gamepad button.
1795 * @return True if the keycode is a gamepad button, such as {@link #KEYCODE_BUTTON_A}.
1796 */
1797 public static final boolean isGamepadButton(int keyCode) {
1798 switch (keyCode) {
1799 case KeyEvent.KEYCODE_BUTTON_A:
1800 case KeyEvent.KEYCODE_BUTTON_B:
1801 case KeyEvent.KEYCODE_BUTTON_C:
1802 case KeyEvent.KEYCODE_BUTTON_X:
1803 case KeyEvent.KEYCODE_BUTTON_Y:
1804 case KeyEvent.KEYCODE_BUTTON_Z:
1805 case KeyEvent.KEYCODE_BUTTON_L1:
1806 case KeyEvent.KEYCODE_BUTTON_R1:
1807 case KeyEvent.KEYCODE_BUTTON_L2:
1808 case KeyEvent.KEYCODE_BUTTON_R2:
1809 case KeyEvent.KEYCODE_BUTTON_THUMBL:
1810 case KeyEvent.KEYCODE_BUTTON_THUMBR:
1811 case KeyEvent.KEYCODE_BUTTON_START:
1812 case KeyEvent.KEYCODE_BUTTON_SELECT:
1813 case KeyEvent.KEYCODE_BUTTON_MODE:
1814 case KeyEvent.KEYCODE_BUTTON_1:
1815 case KeyEvent.KEYCODE_BUTTON_2:
1816 case KeyEvent.KEYCODE_BUTTON_3:
1817 case KeyEvent.KEYCODE_BUTTON_4:
1818 case KeyEvent.KEYCODE_BUTTON_5:
1819 case KeyEvent.KEYCODE_BUTTON_6:
1820 case KeyEvent.KEYCODE_BUTTON_7:
1821 case KeyEvent.KEYCODE_BUTTON_8:
1822 case KeyEvent.KEYCODE_BUTTON_9:
1823 case KeyEvent.KEYCODE_BUTTON_10:
1824 case KeyEvent.KEYCODE_BUTTON_11:
1825 case KeyEvent.KEYCODE_BUTTON_12:
1826 case KeyEvent.KEYCODE_BUTTON_13:
1827 case KeyEvent.KEYCODE_BUTTON_14:
1828 case KeyEvent.KEYCODE_BUTTON_15:
1829 case KeyEvent.KEYCODE_BUTTON_16:
1830 return true;
1831 default:
1832 return false;
1833 }
1834 }
1835
Jeff Brown91c69ab2011-02-14 17:03:18 -08001836 /** {@inheritDoc} */
1837 @Override
1838 public final int getDeviceId() {
1839 return mDeviceId;
1840 }
1841
1842 /** {@inheritDoc} */
1843 @Override
1844 public final int getSource() {
1845 return mSource;
1846 }
1847
1848 /** {@inheritDoc} */
1849 @Override
1850 public final void setSource(int source) {
1851 mSource = source;
1852 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853
1854 /**
1855 * <p>Returns the state of the meta keys.</p>
1856 *
1857 * @return an integer in which each bit set to 1 represents a pressed
1858 * meta key
1859 *
1860 * @see #isAltPressed()
1861 * @see #isShiftPressed()
1862 * @see #isSymPressed()
Jeff Brown497a92c2010-09-12 17:55:08 -07001863 * @see #isCtrlPressed()
1864 * @see #isMetaPressed()
1865 * @see #isFunctionPressed()
Jeff Brown51e7fe72010-10-29 22:19:53 -07001866 * @see #isCapsLockOn()
1867 * @see #isNumLockOn()
1868 * @see #isScrollLockOn()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 * @see #META_ALT_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001870 * @see #META_ALT_LEFT_ON
1871 * @see #META_ALT_RIGHT_ON
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 * @see #META_SHIFT_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001873 * @see #META_SHIFT_LEFT_ON
1874 * @see #META_SHIFT_RIGHT_ON
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 * @see #META_SYM_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001876 * @see #META_FUNCTION_ON
1877 * @see #META_CTRL_ON
1878 * @see #META_CTRL_LEFT_ON
1879 * @see #META_CTRL_RIGHT_ON
1880 * @see #META_META_ON
1881 * @see #META_META_LEFT_ON
1882 * @see #META_META_RIGHT_ON
Jeff Brown51e7fe72010-10-29 22:19:53 -07001883 * @see #META_CAPS_LOCK_ON
1884 * @see #META_NUM_LOCK_ON
1885 * @see #META_SCROLL_LOCK_ON
Jeff Brown54875002011-04-06 15:33:01 -07001886 * @see #getModifiers
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001887 */
1888 public final int getMetaState() {
1889 return mMetaState;
1890 }
1891
1892 /**
Jeff Brown54875002011-04-06 15:33:01 -07001893 * Returns the state of the modifier keys.
1894 * <p>
1895 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1896 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1897 * not considered modifier keys. Consequently, this function specifically masks out
1898 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1899 * </p><p>
1900 * The value returned consists of the meta state (from {@link #getMetaState})
1901 * normalized using {@link #normalizeMetaState(int)} and then masked with
1902 * {@link #getModifierMetaStateMask} so that only valid modifier bits are retained.
1903 * </p>
1904 *
1905 * @return An integer in which each bit set to 1 represents a pressed modifier key.
1906 * @see #getMetaState
1907 */
1908 public final int getModifiers() {
1909 return normalizeMetaState(mMetaState) & META_MODIFIER_MASK;
1910 }
1911
1912 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913 * Returns the flags for this key event.
1914 *
1915 * @see #FLAG_WOKE_HERE
1916 */
1917 public final int getFlags() {
1918 return mFlags;
1919 }
1920
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001921 // Mask of all modifier key meta states. Specifically excludes locked keys like caps lock.
1922 private static final int META_MODIFIER_MASK =
1923 META_SHIFT_ON | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON
1924 | META_ALT_ON | META_ALT_LEFT_ON | META_ALT_RIGHT_ON
1925 | META_CTRL_ON | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON
1926 | META_META_ON | META_META_LEFT_ON | META_META_RIGHT_ON
1927 | META_SYM_ON | META_FUNCTION_ON;
1928
1929 // Mask of all lock key meta states.
1930 private static final int META_LOCK_MASK =
1931 META_CAPS_LOCK_ON | META_NUM_LOCK_ON | META_SCROLL_LOCK_ON;
1932
1933 // Mask of all valid meta states.
1934 private static final int META_ALL_MASK = META_MODIFIER_MASK | META_LOCK_MASK;
1935
1936 // Mask of all synthetic meta states that are reserved for API compatibility with
1937 // historical uses in MetaKeyKeyListener.
1938 private static final int META_SYNTHETIC_MASK =
1939 META_CAP_LOCKED | META_ALT_LOCKED | META_SYM_LOCKED | META_SELECTING;
1940
1941 // Mask of all meta states that are not valid use in specifying a modifier key.
1942 // These bits are known to be used for purposes other than specifying modifiers.
1943 private static final int META_INVALID_MODIFIER_MASK =
1944 META_LOCK_MASK | META_SYNTHETIC_MASK;
1945
1946 /**
1947 * Gets a mask that includes all valid modifier key meta state bits.
1948 * <p>
1949 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1950 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1951 * not considered modifier keys. Consequently, the mask specifically excludes
1952 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1953 * </p>
1954 *
1955 * @return The modifier meta state mask which is a combination of
1956 * {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON}, {@link #META_SHIFT_RIGHT_ON},
1957 * {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON}, {@link #META_ALT_RIGHT_ON},
1958 * {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON}, {@link #META_CTRL_RIGHT_ON},
1959 * {@link #META_META_ON}, {@link #META_META_LEFT_ON}, {@link #META_META_RIGHT_ON},
1960 * {@link #META_SYM_ON}, {@link #META_FUNCTION_ON}.
1961 */
1962 public static int getModifierMetaStateMask() {
1963 return META_MODIFIER_MASK;
1964 }
1965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 /**
1967 * Returns true if this key code is a modifier key.
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001968 * <p>
1969 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1970 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1971 * not considered modifier keys. Consequently, this function return false
1972 * for those keys.
1973 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 *
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001975 * @return True if the key code is one of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976 * {@link #KEYCODE_SHIFT_LEFT} {@link #KEYCODE_SHIFT_RIGHT},
Jeff Brown497a92c2010-09-12 17:55:08 -07001977 * {@link #KEYCODE_ALT_LEFT}, {@link #KEYCODE_ALT_RIGHT},
Jeff Brown497a92c2010-09-12 17:55:08 -07001978 * {@link #KEYCODE_CTRL_LEFT}, {@link #KEYCODE_CTRL_RIGHT},
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001979 * {@link #KEYCODE_META_LEFT}, or {@link #KEYCODE_META_RIGHT},
1980 * {@link #KEYCODE_SYM}, {@link #KEYCODE_NUM}, {@link #KEYCODE_FUNCTION}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 */
1982 public static boolean isModifierKey(int keyCode) {
Jeff Brown497a92c2010-09-12 17:55:08 -07001983 switch (keyCode) {
1984 case KEYCODE_SHIFT_LEFT:
1985 case KEYCODE_SHIFT_RIGHT:
1986 case KEYCODE_ALT_LEFT:
1987 case KEYCODE_ALT_RIGHT:
Jeff Brown497a92c2010-09-12 17:55:08 -07001988 case KEYCODE_CTRL_LEFT:
1989 case KEYCODE_CTRL_RIGHT:
1990 case KEYCODE_META_LEFT:
1991 case KEYCODE_META_RIGHT:
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001992 case KEYCODE_SYM:
1993 case KEYCODE_NUM:
1994 case KEYCODE_FUNCTION:
Jeff Brown497a92c2010-09-12 17:55:08 -07001995 return true;
1996 default:
1997 return false;
1998 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 }
2000
2001 /**
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002002 * Normalizes the specified meta state.
2003 * <p>
2004 * The meta state is normalized such that if either the left or right modifier meta state
2005 * bits are set then the result will also include the universal bit for that modifier.
2006 * </p><p>
2007 * If the specified meta state contains {@link #META_ALT_LEFT_ON} then
2008 * the result will also contain {@link #META_ALT_ON} in addition to {@link #META_ALT_LEFT_ON}
2009 * and the other bits that were specified in the input. The same is process is
2010 * performed for shift, control and meta.
2011 * </p><p>
2012 * If the specified meta state contains synthetic meta states defined by
2013 * {@link MetaKeyKeyListener}, then those states are translated here and the original
2014 * synthetic meta states are removed from the result.
2015 * {@link MetaKeyKeyListener#META_CAP_LOCKED} is translated to {@link #META_CAPS_LOCK_ON}.
2016 * {@link MetaKeyKeyListener#META_ALT_LOCKED} is translated to {@link #META_ALT_ON}.
2017 * {@link MetaKeyKeyListener#META_SYM_LOCKED} is translated to {@link #META_SYM_ON}.
2018 * </p><p>
2019 * Undefined meta state bits are removed.
2020 * </p>
2021 *
2022 * @param metaState The meta state.
2023 * @return The normalized meta state.
2024 */
2025 public static int normalizeMetaState(int metaState) {
2026 if ((metaState & (META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON)) != 0) {
2027 metaState |= META_SHIFT_ON;
2028 }
2029 if ((metaState & (META_ALT_LEFT_ON | META_ALT_RIGHT_ON)) != 0) {
2030 metaState |= META_ALT_ON;
2031 }
2032 if ((metaState & (META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON)) != 0) {
2033 metaState |= META_CTRL_ON;
2034 }
2035 if ((metaState & (META_META_LEFT_ON | META_META_RIGHT_ON)) != 0) {
2036 metaState |= META_META_ON;
2037 }
2038 if ((metaState & MetaKeyKeyListener.META_CAP_LOCKED) != 0) {
2039 metaState |= META_CAPS_LOCK_ON;
2040 }
2041 if ((metaState & MetaKeyKeyListener.META_ALT_LOCKED) != 0) {
2042 metaState |= META_ALT_ON;
2043 }
2044 if ((metaState & MetaKeyKeyListener.META_SYM_LOCKED) != 0) {
2045 metaState |= META_SYM_ON;
2046 }
2047 return metaState & META_ALL_MASK;
2048 }
2049
2050 /**
2051 * Returns true if no modifiers keys are pressed according to the specified meta state.
2052 * <p>
2053 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2054 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2055 * not considered modifier keys. Consequently, this function ignores
2056 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2057 * </p><p>
2058 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}.
2059 * </p>
2060 *
2061 * @param metaState The meta state to consider.
2062 * @return True if no modifier keys are pressed.
2063 * @see #hasNoModifiers()
2064 */
2065 public static boolean metaStateHasNoModifiers(int metaState) {
2066 return (normalizeMetaState(metaState) & META_MODIFIER_MASK) == 0;
2067 }
2068
2069 /**
2070 * Returns true if only the specified modifier keys are pressed according to
2071 * the specified meta state. Returns false if a different combination of modifier
2072 * keys are pressed.
2073 * <p>
2074 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2075 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2076 * not considered modifier keys. Consequently, this function ignores
2077 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2078 * </p><p>
2079 * If the specified modifier mask includes directional modifiers, such as
2080 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the
2081 * modifier is pressed on that side.
2082 * If the specified modifier mask includes non-directional modifiers, such as
2083 * {@link #META_SHIFT_ON}, then this method ensures that the modifier
2084 * is pressed on either side.
2085 * If the specified modifier mask includes both directional and non-directional modifiers
2086 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON},
2087 * then this method throws an illegal argument exception.
2088 * </p>
2089 *
2090 * @param metaState The meta state to consider.
2091 * @param modifiers The meta state of the modifier keys to check. May be a combination
2092 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to
2093 * ensure that no modifier keys are pressed.
2094 * @return True if only the specified modifier keys are pressed.
2095 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers
2096 * @see #hasModifiers
2097 */
2098 public static boolean metaStateHasModifiers(int metaState, int modifiers) {
2099 // Note: For forward compatibility, we allow the parameter to contain meta states
2100 // that we do not recognize but we explicitly disallow meta states that
2101 // are not valid modifiers.
2102 if ((modifiers & META_INVALID_MODIFIER_MASK) != 0) {
2103 throw new IllegalArgumentException("modifiers must not contain "
2104 + "META_CAPS_LOCK_ON, META_NUM_LOCK_ON, META_SCROLL_LOCK_ON, "
2105 + "META_CAP_LOCKED, META_ALT_LOCKED, META_SYM_LOCKED, "
2106 + "or META_SELECTING");
2107 }
2108
2109 metaState = normalizeMetaState(metaState) & META_MODIFIER_MASK;
2110 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2111 META_SHIFT_ON, META_SHIFT_LEFT_ON, META_SHIFT_RIGHT_ON);
2112 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2113 META_ALT_ON, META_ALT_LEFT_ON, META_ALT_RIGHT_ON);
2114 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2115 META_CTRL_ON, META_CTRL_LEFT_ON, META_CTRL_RIGHT_ON);
2116 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2117 META_META_ON, META_META_LEFT_ON, META_META_RIGHT_ON);
2118 return metaState == modifiers;
2119 }
2120
2121 private static int metaStateFilterDirectionalModifiers(int metaState,
2122 int modifiers, int basic, int left, int right) {
2123 final boolean wantBasic = (modifiers & basic) != 0;
2124 final int directional = left | right;
2125 final boolean wantLeftOrRight = (modifiers & directional) != 0;
2126
2127 if (wantBasic) {
2128 if (wantLeftOrRight) {
2129 throw new IllegalArgumentException("modifiers must not contain "
2130 + metaStateToString(basic) + " combined with "
2131 + metaStateToString(left) + " or " + metaStateToString(right));
2132 }
2133 return metaState & ~directional;
2134 } else if (wantLeftOrRight) {
2135 return metaState & ~basic;
2136 } else {
2137 return metaState;
2138 }
2139 }
2140
2141 /**
2142 * Returns true if no modifier keys are pressed.
2143 * <p>
2144 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2145 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2146 * not considered modifier keys. Consequently, this function ignores
2147 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2148 * </p><p>
2149 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}.
2150 * </p>
2151 *
2152 * @return True if no modifier keys are pressed.
2153 * @see #metaStateHasNoModifiers
2154 */
2155 public final boolean hasNoModifiers() {
2156 return metaStateHasNoModifiers(mMetaState);
2157 }
2158
2159 /**
2160 * Returns true if only the specified modifiers keys are pressed.
2161 * Returns false if a different combination of modifier keys are pressed.
2162 * <p>
2163 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2164 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2165 * not considered modifier keys. Consequently, this function ignores
2166 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2167 * </p><p>
2168 * If the specified modifier mask includes directional modifiers, such as
2169 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the
2170 * modifier is pressed on that side.
2171 * If the specified modifier mask includes non-directional modifiers, such as
2172 * {@link #META_SHIFT_ON}, then this method ensures that the modifier
2173 * is pressed on either side.
2174 * If the specified modifier mask includes both directional and non-directional modifiers
2175 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON},
2176 * then this method throws an illegal argument exception.
2177 * </p>
2178 *
2179 * @param modifiers The meta state of the modifier keys to check. May be a combination
2180 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to
2181 * ensure that no modifier keys are pressed.
2182 * @return True if only the specified modifier keys are pressed.
2183 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers
2184 * @see #metaStateHasModifiers
2185 */
2186 public final boolean hasModifiers(int modifiers) {
2187 return metaStateHasModifiers(mMetaState, modifiers);
2188 }
2189
2190 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191 * <p>Returns the pressed state of the ALT meta key.</p>
2192 *
2193 * @return true if the ALT key is pressed, false otherwise
2194 *
2195 * @see #KEYCODE_ALT_LEFT
2196 * @see #KEYCODE_ALT_RIGHT
2197 * @see #META_ALT_ON
2198 */
2199 public final boolean isAltPressed() {
2200 return (mMetaState & META_ALT_ON) != 0;
2201 }
2202
2203 /**
2204 * <p>Returns the pressed state of the SHIFT meta key.</p>
2205 *
2206 * @return true if the SHIFT key is pressed, false otherwise
2207 *
2208 * @see #KEYCODE_SHIFT_LEFT
2209 * @see #KEYCODE_SHIFT_RIGHT
2210 * @see #META_SHIFT_ON
2211 */
2212 public final boolean isShiftPressed() {
2213 return (mMetaState & META_SHIFT_ON) != 0;
2214 }
2215
2216 /**
2217 * <p>Returns the pressed state of the SYM meta key.</p>
2218 *
2219 * @return true if the SYM key is pressed, false otherwise
2220 *
2221 * @see #KEYCODE_SYM
2222 * @see #META_SYM_ON
2223 */
2224 public final boolean isSymPressed() {
2225 return (mMetaState & META_SYM_ON) != 0;
2226 }
2227
2228 /**
Jeff Brown497a92c2010-09-12 17:55:08 -07002229 * <p>Returns the pressed state of the CTRL meta key.</p>
2230 *
2231 * @return true if the CTRL key is pressed, false otherwise
2232 *
2233 * @see #KEYCODE_CTRL_LEFT
2234 * @see #KEYCODE_CTRL_RIGHT
2235 * @see #META_CTRL_ON
2236 */
2237 public final boolean isCtrlPressed() {
2238 return (mMetaState & META_CTRL_ON) != 0;
2239 }
2240
2241 /**
2242 * <p>Returns the pressed state of the META meta key.</p>
2243 *
2244 * @return true if the META key is pressed, false otherwise
2245 *
2246 * @see #KEYCODE_META_LEFT
2247 * @see #KEYCODE_META_RIGHT
2248 * @see #META_META_ON
2249 */
2250 public final boolean isMetaPressed() {
2251 return (mMetaState & META_META_ON) != 0;
2252 }
2253
2254 /**
2255 * <p>Returns the pressed state of the FUNCTION meta key.</p>
2256 *
2257 * @return true if the FUNCTION key is pressed, false otherwise
2258 *
2259 * @see #KEYCODE_FUNCTION
2260 * @see #META_FUNCTION_ON
2261 */
2262 public final boolean isFunctionPressed() {
2263 return (mMetaState & META_FUNCTION_ON) != 0;
2264 }
2265
2266 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07002267 * <p>Returns the locked state of the CAPS LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002268 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002269 * @return true if the CAPS LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002270 *
2271 * @see #KEYCODE_CAPS_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002272 * @see #META_CAPS_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002273 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002274 public final boolean isCapsLockOn() {
2275 return (mMetaState & META_CAPS_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002276 }
2277
2278 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07002279 * <p>Returns the locked state of the NUM LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002280 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002281 * @return true if the NUM LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002282 *
2283 * @see #KEYCODE_NUM_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002284 * @see #META_NUM_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002285 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002286 public final boolean isNumLockOn() {
2287 return (mMetaState & META_NUM_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002288 }
2289
2290 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07002291 * <p>Returns the locked state of the SCROLL LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002292 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002293 * @return true if the SCROLL LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002294 *
2295 * @see #KEYCODE_SCROLL_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002296 * @see #META_SCROLL_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002297 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002298 public final boolean isScrollLockOn() {
2299 return (mMetaState & META_SCROLL_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002300 }
2301
2302 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002303 * Retrieve the action of this key event. May be either
2304 * {@link #ACTION_DOWN}, {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
2305 *
2306 * @return The event action: ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.
2307 */
2308 public final int getAction() {
2309 return mAction;
2310 }
2311
2312 /**
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07002313 * For {@link #ACTION_UP} events, indicates that the event has been
2314 * canceled as per {@link #FLAG_CANCELED}.
2315 */
2316 public final boolean isCanceled() {
2317 return (mFlags&FLAG_CANCELED) != 0;
2318 }
2319
2320 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002321 * Call this during {@link Callback#onKeyDown} to have the system track
2322 * the key through its final up (possibly including a long press). Note
2323 * that only one key can be tracked at a time -- if another key down
2324 * event is received while a previous one is being tracked, tracking is
2325 * stopped on the previous event.
2326 */
2327 public final void startTracking() {
2328 mFlags |= FLAG_START_TRACKING;
2329 }
2330
2331 /**
2332 * For {@link #ACTION_UP} events, indicates that the event is still being
2333 * tracked from its initial down event as per
2334 * {@link #FLAG_TRACKING}.
2335 */
2336 public final boolean isTracking() {
2337 return (mFlags&FLAG_TRACKING) != 0;
2338 }
2339
2340 /**
2341 * For {@link #ACTION_DOWN} events, indicates that the event has been
2342 * canceled as per {@link #FLAG_LONG_PRESS}.
2343 */
2344 public final boolean isLongPress() {
2345 return (mFlags&FLAG_LONG_PRESS) != 0;
2346 }
2347
2348 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349 * Retrieve the key code of the key event. This is the physical key that
2350 * was pressed, <em>not</em> the Unicode character.
2351 *
2352 * @return The key code of the event.
2353 */
2354 public final int getKeyCode() {
2355 return mKeyCode;
2356 }
2357
2358 /**
2359 * For the special case of a {@link #ACTION_MULTIPLE} event with key
2360 * code of {@link #KEYCODE_UNKNOWN}, this is a raw string of characters
2361 * associated with the event. In all other cases it is null.
2362 *
2363 * @return Returns a String of 1 or more characters associated with
2364 * the event.
2365 */
2366 public final String getCharacters() {
2367 return mCharacters;
2368 }
2369
2370 /**
2371 * Retrieve the hardware key id of this key event. These values are not
2372 * reliable and vary from device to device.
2373 *
2374 * {@more}
2375 * Mostly this is here for debugging purposes.
2376 */
2377 public final int getScanCode() {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002378 return mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379 }
2380
2381 /**
2382 * Retrieve the repeat count of the event. For both key up and key down
2383 * events, this is the number of times the key has repeated with the first
2384 * down starting at 0 and counting up from there. For multiple key
2385 * events, this is the number of down/up pairs that have occurred.
2386 *
2387 * @return The number of times the key has repeated.
2388 */
2389 public final int getRepeatCount() {
2390 return mRepeatCount;
2391 }
2392
2393 /**
2394 * Retrieve the time of the most recent key down event,
2395 * in the {@link android.os.SystemClock#uptimeMillis} time base. If this
2396 * is a down event, this will be the same as {@link #getEventTime()}.
2397 * Note that when chording keys, this value is the down time of the
2398 * most recently pressed key, which may <em>not</em> be the same physical
2399 * key of this event.
2400 *
2401 * @return Returns the most recent key down time, in the
2402 * {@link android.os.SystemClock#uptimeMillis} time base
2403 */
2404 public final long getDownTime() {
2405 return mDownTime;
2406 }
2407
2408 /**
Jeff Brownb11499d2012-04-20 19:54:22 -07002409 * Retrieve the time this event occurred,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 * in the {@link android.os.SystemClock#uptimeMillis} time base.
Jeff Brownb11499d2012-04-20 19:54:22 -07002411 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 * @return Returns the time this event occurred,
2413 * in the {@link android.os.SystemClock#uptimeMillis} time base.
2414 */
Jeff Brownb11499d2012-04-20 19:54:22 -07002415 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002416 public final long getEventTime() {
2417 return mEventTime;
2418 }
2419
Jeff Brownb11499d2012-04-20 19:54:22 -07002420 /**
2421 * Retrieve the time this event occurred,
2422 * in the {@link android.os.SystemClock#uptimeMillis} time base but with
2423 * nanosecond (instead of millisecond) precision.
2424 * <p>
2425 * The value is in nanosecond precision but it may not have nanosecond accuracy.
2426 * </p>
2427 *
2428 * @return Returns the time this event occurred,
2429 * in the {@link android.os.SystemClock#uptimeMillis} time base but with
2430 * nanosecond (instead of millisecond) precision.
2431 *
2432 * @hide
2433 */
Jeff Brown4e91a182011-04-07 11:38:09 -07002434 @Override
2435 public final long getEventTimeNano() {
2436 return mEventTime * 1000000L;
2437 }
2438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002439 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002440 * Renamed to {@link #getDeviceId}.
2441 *
2442 * @hide
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002443 * @deprecated use {@link #getDeviceId()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002445 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002446 public final int getKeyboardDevice() {
2447 return mDeviceId;
2448 }
2449
2450 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002451 * Gets the {@link KeyCharacterMap} associated with the keyboard device.
2452 *
2453 * @return The associated key character map.
Andrew Sapperstein8ab3dc72011-06-23 18:56:44 -07002454 * @throws {@link KeyCharacterMap.UnavailableException} if the key character map
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002455 * could not be loaded because it was malformed or the default key character map
2456 * is missing from the system.
2457 *
Andrew Sapperstein8ab3dc72011-06-23 18:56:44 -07002458 * @see KeyCharacterMap#load
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002459 */
2460 public final KeyCharacterMap getKeyCharacterMap() {
2461 return KeyCharacterMap.load(mDeviceId);
2462 }
2463
2464 /**
2465 * Gets the primary character for this key.
2466 * In other words, the label that is physically printed on it.
2467 *
2468 * @return The display label character, or 0 if none (eg. for non-printing keys).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002469 */
2470 public char getDisplayLabel() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002471 return getKeyCharacterMap().getDisplayLabel(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002472 }
2473
2474 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002475 * Gets the Unicode character generated by the specified key and meta
2476 * key state combination.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002477 * <p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002478 * Returns the Unicode character that the specified key would produce
2479 * when the specified meta bits (see {@link MetaKeyKeyListener})
2480 * were active.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 * </p><p>
2482 * Returns 0 if the key is not one that is used to type Unicode
2483 * characters.
2484 * </p><p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002485 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the
2486 * key is a "dead key" that should be combined with another to
2487 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} --
2488 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002489 * </p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002490 *
2491 * @return The associated character or combining accent, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002492 */
2493 public int getUnicodeChar() {
2494 return getUnicodeChar(mMetaState);
2495 }
2496
2497 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002498 * Gets the Unicode character generated by the specified key and meta
2499 * key state combination.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002500 * <p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002501 * Returns the Unicode character that the specified key would produce
2502 * when the specified meta bits (see {@link MetaKeyKeyListener})
2503 * were active.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 * </p><p>
2505 * Returns 0 if the key is not one that is used to type Unicode
2506 * characters.
2507 * </p><p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002508 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the
2509 * key is a "dead key" that should be combined with another to
2510 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} --
2511 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002512 * </p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002513 *
2514 * @param metaState The meta key modifier state.
2515 * @return The associated character or combining accent, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002517 public int getUnicodeChar(int metaState) {
2518 return getKeyCharacterMap().get(mKeyCode, metaState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002519 }
2520
2521 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002522 * Get the character conversion data for a given key code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002523 *
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002524 * @param results A {@link KeyCharacterMap.KeyData} instance that will be
2525 * filled with the results.
2526 * @return True if the key was mapped. If the key was not mapped, results is not modified.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002527 *
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002528 * @deprecated instead use {@link #getDisplayLabel()},
2529 * {@link #getNumber()} or {@link #getUnicodeChar(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002530 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002531 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002532 public boolean getKeyData(KeyData results) {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002533 return getKeyCharacterMap().getKeyData(mKeyCode, results);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 }
2535
2536 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002537 * Gets the first character in the character array that can be generated
2538 * by the specified key code.
2539 * <p>
2540 * This is a convenience function that returns the same value as
2541 * {@link #getMatch(char[],int) getMatch(chars, 0)}.
2542 * </p>
2543 *
2544 * @param chars The array of matching characters to consider.
2545 * @return The matching associated character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002546 */
2547 public char getMatch(char[] chars) {
2548 return getMatch(chars, 0);
2549 }
2550
2551 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002552 * Gets the first character in the character array that can be generated
2553 * by the specified key code. If there are multiple choices, prefers
2554 * the one that would be generated with the specified meta key modifier state.
2555 *
2556 * @param chars The array of matching characters to consider.
2557 * @param metaState The preferred meta key modifier state.
2558 * @return The matching associated character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002559 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002560 public char getMatch(char[] chars, int metaState) {
2561 return getKeyCharacterMap().getMatch(mKeyCode, chars, metaState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562 }
2563
2564 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002565 * Gets the number or symbol associated with the key.
2566 * <p>
2567 * The character value is returned, not the numeric value.
2568 * If the key is not a number, but is a symbol, the symbol is retuned.
2569 * </p><p>
2570 * This method is intended to to support dial pads and other numeric or
2571 * symbolic entry on keyboards where certain keys serve dual function
2572 * as alphabetic and symbolic keys. This method returns the number
2573 * or symbol associated with the key independent of whether the user
2574 * has pressed the required modifier.
2575 * </p><p>
2576 * For example, on one particular keyboard the keys on the top QWERTY row generate
2577 * numbers when ALT is pressed such that ALT-Q maps to '1'. So for that keyboard
2578 * when {@link #getNumber} is called with {@link KeyEvent#KEYCODE_Q} it returns '1'
2579 * so that the user can type numbers without pressing ALT when it makes sense.
2580 * </p>
2581 *
2582 * @return The associated numeric or symbolic character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 */
2584 public char getNumber() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002585 return getKeyCharacterMap().getNumber(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002586 }
2587
2588 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002589 * Returns true if this key produces a glyph.
2590 *
2591 * @return True if the key is a printing key.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592 */
2593 public boolean isPrintingKey() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002594 return getKeyCharacterMap().isPrintingKey(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002595 }
2596
2597 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002598 * @deprecated Use {@link #dispatch(Callback, DispatcherState, Object)} instead.
2599 */
2600 @Deprecated
2601 public final boolean dispatch(Callback receiver) {
2602 return dispatch(receiver, null, null);
2603 }
2604
2605 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002606 * Deliver this key event to a {@link Callback} interface. If this is
2607 * an ACTION_MULTIPLE event and it is not handled, then an attempt will
2608 * be made to deliver a single normal event.
2609 *
2610 * @param receiver The Callback that will be given the event.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002611 * @param state State information retained across events.
2612 * @param target The target of the dispatch, for use in tracking.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002613 *
2614 * @return The return value from the Callback method that was called.
2615 */
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002616 public final boolean dispatch(Callback receiver, DispatcherState state,
2617 Object target) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002618 switch (mAction) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002619 case ACTION_DOWN: {
2620 mFlags &= ~FLAG_START_TRACKING;
Dianne Hackborn8d374262009-09-14 21:21:52 -07002621 if (DEBUG) Log.v(TAG, "Key down to " + target + " in " + state
2622 + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002623 boolean res = receiver.onKeyDown(mKeyCode, this);
2624 if (state != null) {
2625 if (res && mRepeatCount == 0 && (mFlags&FLAG_START_TRACKING) != 0) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002626 if (DEBUG) Log.v(TAG, " Start tracking!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002627 state.startTracking(this, target);
2628 } else if (isLongPress() && state.isTracking(this)) {
2629 try {
2630 if (receiver.onKeyLongPress(mKeyCode, this)) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002631 if (DEBUG) Log.v(TAG, " Clear from long press!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002632 state.performedLongPress(this);
2633 res = true;
2634 }
2635 } catch (AbstractMethodError e) {
2636 }
2637 }
2638 }
2639 return res;
2640 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002641 case ACTION_UP:
Dianne Hackborn8d374262009-09-14 21:21:52 -07002642 if (DEBUG) Log.v(TAG, "Key up to " + target + " in " + state
2643 + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002644 if (state != null) {
2645 state.handleUpEvent(this);
2646 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002647 return receiver.onKeyUp(mKeyCode, this);
2648 case ACTION_MULTIPLE:
2649 final int count = mRepeatCount;
2650 final int code = mKeyCode;
2651 if (receiver.onKeyMultiple(code, count, this)) {
2652 return true;
2653 }
2654 if (code != KeyEvent.KEYCODE_UNKNOWN) {
2655 mAction = ACTION_DOWN;
2656 mRepeatCount = 0;
2657 boolean handled = receiver.onKeyDown(code, this);
2658 if (handled) {
2659 mAction = ACTION_UP;
2660 receiver.onKeyUp(code, this);
2661 }
2662 mAction = ACTION_MULTIPLE;
2663 mRepeatCount = count;
2664 return handled;
2665 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002666 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 }
2668 return false;
2669 }
2670
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002671 /**
2672 * Use with {@link KeyEvent#dispatch(Callback, DispatcherState, Object)}
2673 * for more advanced key dispatching, such as long presses.
2674 */
2675 public static class DispatcherState {
2676 int mDownKeyCode;
2677 Object mDownTarget;
2678 SparseIntArray mActiveLongPresses = new SparseIntArray();
2679
2680 /**
2681 * Reset back to initial state.
2682 */
2683 public void reset() {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002684 if (DEBUG) Log.v(TAG, "Reset: " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002685 mDownKeyCode = 0;
2686 mDownTarget = null;
2687 mActiveLongPresses.clear();
2688 }
2689
2690 /**
2691 * Stop any tracking associated with this target.
2692 */
2693 public void reset(Object target) {
2694 if (mDownTarget == target) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002695 if (DEBUG) Log.v(TAG, "Reset in " + target + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002696 mDownKeyCode = 0;
2697 mDownTarget = null;
2698 }
2699 }
2700
2701 /**
2702 * Start tracking the key code associated with the given event. This
2703 * can only be called on a key down. It will allow you to see any
2704 * long press associated with the key, and will result in
2705 * {@link KeyEvent#isTracking} return true on the long press and up
2706 * events.
2707 *
2708 * <p>This is only needed if you are directly dispatching events, rather
2709 * than handling them in {@link Callback#onKeyDown}.
2710 */
2711 public void startTracking(KeyEvent event, Object target) {
2712 if (event.getAction() != ACTION_DOWN) {
2713 throw new IllegalArgumentException(
2714 "Can only start tracking on a down event");
2715 }
Dianne Hackborn8d374262009-09-14 21:21:52 -07002716 if (DEBUG) Log.v(TAG, "Start trackingt in " + target + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002717 mDownKeyCode = event.getKeyCode();
2718 mDownTarget = target;
2719 }
2720
2721 /**
2722 * Return true if the key event is for a key code that is currently
2723 * being tracked by the dispatcher.
2724 */
2725 public boolean isTracking(KeyEvent event) {
2726 return mDownKeyCode == event.getKeyCode();
2727 }
2728
2729 /**
2730 * Keep track of the given event's key code as having performed an
2731 * action with a long press, so no action should occur on the up.
2732 * <p>This is only needed if you are directly dispatching events, rather
2733 * than handling them in {@link Callback#onKeyLongPress}.
2734 */
2735 public void performedLongPress(KeyEvent event) {
2736 mActiveLongPresses.put(event.getKeyCode(), 1);
2737 }
2738
2739 /**
2740 * Handle key up event to stop tracking. This resets the dispatcher state,
2741 * and updates the key event state based on it.
2742 * <p>This is only needed if you are directly dispatching events, rather
2743 * than handling them in {@link Callback#onKeyUp}.
2744 */
2745 public void handleUpEvent(KeyEvent event) {
2746 final int keyCode = event.getKeyCode();
Dianne Hackborn8d374262009-09-14 21:21:52 -07002747 if (DEBUG) Log.v(TAG, "Handle key up " + event + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002748 int index = mActiveLongPresses.indexOfKey(keyCode);
2749 if (index >= 0) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002750 if (DEBUG) Log.v(TAG, " Index: " + index);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002751 event.mFlags |= FLAG_CANCELED | FLAG_CANCELED_LONG_PRESS;
2752 mActiveLongPresses.removeAt(index);
2753 }
2754 if (mDownKeyCode == keyCode) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002755 if (DEBUG) Log.v(TAG, " Tracking!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002756 event.mFlags |= FLAG_TRACKING;
2757 mDownKeyCode = 0;
2758 mDownTarget = null;
2759 }
2760 }
2761 }
Jeff Brown497a92c2010-09-12 17:55:08 -07002762
2763 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 public String toString() {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002765 StringBuilder msg = new StringBuilder();
2766 msg.append("KeyEvent { action=").append(actionToString(mAction));
2767 msg.append(", keyCode=").append(keyCodeToString(mKeyCode));
2768 msg.append(", scanCode=").append(mScanCode);
2769 if (mCharacters != null) {
2770 msg.append(", characters=\"").append(mCharacters).append("\"");
2771 }
2772 msg.append(", metaState=").append(metaStateToString(mMetaState));
2773 msg.append(", flags=0x").append(Integer.toHexString(mFlags));
2774 msg.append(", repeatCount=").append(mRepeatCount);
2775 msg.append(", eventTime=").append(mEventTime);
2776 msg.append(", downTime=").append(mDownTime);
2777 msg.append(", deviceId=").append(mDeviceId);
2778 msg.append(", source=0x").append(Integer.toHexString(mSource));
2779 msg.append(" }");
2780 return msg.toString();
Jeff Brown497a92c2010-09-12 17:55:08 -07002781 }
2782
2783 /**
2784 * Returns a string that represents the symbolic name of the specified action
Jeff Brown6f2fba42011-02-19 01:08:02 -08002785 * such as "ACTION_DOWN", or an equivalent numeric constant such as "35" if unknown.
Jeff Brown497a92c2010-09-12 17:55:08 -07002786 *
2787 * @param action The action.
2788 * @return The symbolic name of the specified action.
2789 * @hide
2790 */
2791 public static String actionToString(int action) {
2792 switch (action) {
2793 case ACTION_DOWN:
2794 return "ACTION_DOWN";
2795 case ACTION_UP:
2796 return "ACTION_UP";
2797 case ACTION_MULTIPLE:
2798 return "ACTION_MULTIPLE";
2799 default:
2800 return Integer.toString(action);
2801 }
2802 }
2803
2804 /**
2805 * Returns a string that represents the symbolic name of the specified keycode
Jeff Brown6f2fba42011-02-19 01:08:02 -08002806 * such as "KEYCODE_A", "KEYCODE_DPAD_UP", or an equivalent numeric constant
2807 * such as "1001" if unknown.
Jeff Brown497a92c2010-09-12 17:55:08 -07002808 *
2809 * @param keyCode The key code.
2810 * @return The symbolic name of the specified keycode.
2811 *
2812 * @see KeyCharacterMap#getDisplayLabel
Jeff Brown497a92c2010-09-12 17:55:08 -07002813 */
2814 public static String keyCodeToString(int keyCode) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08002815 String symbolicName = KEYCODE_SYMBOLIC_NAMES.get(keyCode);
2816 return symbolicName != null ? symbolicName : Integer.toString(keyCode);
Jeff Brown497a92c2010-09-12 17:55:08 -07002817 }
2818
2819 /**
Jeff Brown6f2fba42011-02-19 01:08:02 -08002820 * Gets a keycode by its symbolic name such as "KEYCODE_A" or an equivalent
2821 * numeric constant such as "1001".
Jeff Brown497a92c2010-09-12 17:55:08 -07002822 *
2823 * @param symbolicName The symbolic name of the keycode.
Jeff Brown6f2fba42011-02-19 01:08:02 -08002824 * @return The keycode or {@link #KEYCODE_UNKNOWN} if not found.
Michael Wright072137c2013-04-24 20:41:20 -07002825 * @see #keycodeToString(int)
Jeff Brown497a92c2010-09-12 17:55:08 -07002826 */
2827 public static int keyCodeFromString(String symbolicName) {
2828 if (symbolicName == null) {
2829 throw new IllegalArgumentException("symbolicName must not be null");
2830 }
2831
Jeff Brown6f2fba42011-02-19 01:08:02 -08002832 final int count = KEYCODE_SYMBOLIC_NAMES.size();
Jeff Brown497a92c2010-09-12 17:55:08 -07002833 for (int i = 0; i < count; i++) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08002834 if (symbolicName.equals(KEYCODE_SYMBOLIC_NAMES.valueAt(i))) {
Jeff Brown497a92c2010-09-12 17:55:08 -07002835 return i;
2836 }
2837 }
2838
2839 try {
Jeff Brown6f2fba42011-02-19 01:08:02 -08002840 return Integer.parseInt(symbolicName, 10);
Jeff Brown497a92c2010-09-12 17:55:08 -07002841 } catch (NumberFormatException ex) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08002842 return KEYCODE_UNKNOWN;
Jeff Brown497a92c2010-09-12 17:55:08 -07002843 }
2844 }
2845
2846 /**
2847 * Returns a string that represents the symbolic name of the specified combined meta
2848 * key modifier state flags such as "0", "META_SHIFT_ON",
Jeff Brown6f2fba42011-02-19 01:08:02 -08002849 * "META_ALT_ON|META_SHIFT_ON" or an equivalent numeric constant such as "0x10000000"
2850 * if unknown.
Jeff Brown497a92c2010-09-12 17:55:08 -07002851 *
2852 * @param metaState The meta state.
2853 * @return The symbolic name of the specified combined meta state flags.
2854 * @hide
2855 */
2856 public static String metaStateToString(int metaState) {
2857 if (metaState == 0) {
2858 return "0";
2859 }
2860 StringBuilder result = null;
2861 int i = 0;
2862 while (metaState != 0) {
2863 final boolean isSet = (metaState & 1) != 0;
2864 metaState >>>= 1; // unsigned shift!
2865 if (isSet) {
2866 final String name = META_SYMBOLIC_NAMES[i];
2867 if (result == null) {
2868 if (metaState == 0) {
2869 return name;
2870 }
2871 result = new StringBuilder(name);
2872 } else {
2873 result.append('|');
2874 result.append(name);
2875 }
2876 }
2877 i += 1;
2878 }
2879 return result.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880 }
2881
2882 public static final Parcelable.Creator<KeyEvent> CREATOR
2883 = new Parcelable.Creator<KeyEvent>() {
2884 public KeyEvent createFromParcel(Parcel in) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07002885 in.readInt(); // skip token, we already know this is a KeyEvent
2886 return KeyEvent.createFromParcelBody(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887 }
2888
2889 public KeyEvent[] newArray(int size) {
2890 return new KeyEvent[size];
2891 }
2892 };
Jeff Brown6ec402b2010-07-28 15:48:59 -07002893
2894 /** @hide */
2895 public static KeyEvent createFromParcelBody(Parcel in) {
2896 return new KeyEvent(in);
2897 }
2898
2899 private KeyEvent(Parcel in) {
Jeff Brown91c69ab2011-02-14 17:03:18 -08002900 mDeviceId = in.readInt();
2901 mSource = in.readInt();
Jeff Brown6ec402b2010-07-28 15:48:59 -07002902 mAction = in.readInt();
2903 mKeyCode = in.readInt();
2904 mRepeatCount = in.readInt();
2905 mMetaState = in.readInt();
2906 mScanCode = in.readInt();
2907 mFlags = in.readInt();
2908 mDownTime = in.readLong();
2909 mEventTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910 }
2911
2912 public void writeToParcel(Parcel out, int flags) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07002913 out.writeInt(PARCEL_TOKEN_KEY_EVENT);
Jeff Brown91c69ab2011-02-14 17:03:18 -08002914
2915 out.writeInt(mDeviceId);
2916 out.writeInt(mSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002917 out.writeInt(mAction);
2918 out.writeInt(mKeyCode);
2919 out.writeInt(mRepeatCount);
2920 out.writeInt(mMetaState);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002921 out.writeInt(mScanCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922 out.writeInt(mFlags);
2923 out.writeLong(mDownTime);
2924 out.writeLong(mEventTime);
2925 }
2926
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -07002927 private native boolean native_isSystemKey(int keyCode);
2928 private native boolean native_hasDefaultAction(int keyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929}