blob: 73fad086c611f552ee3ef66587321ac84e4a8030 [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;
Jaekyun Seokbfdad8e2013-07-08 13:53:21 +0900632 /** Key code constant: Audio Track key
633 * Switches the audio tracks. */
634 public static final int KEYCODE_MEDIA_AUDIO_TRACK = 222;
Jeff Brown497a92c2010-09-12 17:55:08 -0700635
Jaekyun Seokbfdad8e2013-07-08 13:53:21 +0900636 private static final int LAST_KEYCODE = KEYCODE_MEDIA_AUDIO_TRACK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637
638 // NOTE: If you add a new keycode here you must also add it to:
639 // isSystem()
Chirayu Desai61c37ae2013-04-15 20:11:37 +0530640 // frameworks/native/include/android/keycodes.h
641 // frameworks/base/include/androidfw/KeycodeLabels.h
Jeff Brownfd035822010-06-30 16:10:35 -0700642 // external/webkit/WebKit/android/plugins/ANPKeyCodes.h
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 // frameworks/base/core/res/res/values/attrs.xml
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 // emulator?
Jeff Brown6651a632011-11-28 12:59:11 -0800645 // LAST_KEYCODE
646 // KEYCODE_SYMBOLIC_NAMES
Dianne Hackborn935ae462009-04-13 16:11:55 -0700647 //
648 // Also Android currently does not reserve code ranges for vendor-
649 // specific key codes. If you have new key codes to have, you
650 // MUST contribute a patch to the open source project to define
651 // those new codes. This is intended to maintain a consistent
652 // set of key code definitions across all Android devices.
Jeff Brown497a92c2010-09-12 17:55:08 -0700653
Jeff Brown6f2fba42011-02-19 01:08:02 -0800654 // Symbolic names of all key codes.
655 private static final SparseArray<String> KEYCODE_SYMBOLIC_NAMES = new SparseArray<String>();
656 private static void populateKeycodeSymbolicNames() {
657 SparseArray<String> names = KEYCODE_SYMBOLIC_NAMES;
658 names.append(KEYCODE_UNKNOWN, "KEYCODE_UNKNOWN");
659 names.append(KEYCODE_SOFT_LEFT, "KEYCODE_SOFT_LEFT");
660 names.append(KEYCODE_SOFT_RIGHT, "KEYCODE_SOFT_RIGHT");
661 names.append(KEYCODE_HOME, "KEYCODE_HOME");
662 names.append(KEYCODE_BACK, "KEYCODE_BACK");
663 names.append(KEYCODE_CALL, "KEYCODE_CALL");
664 names.append(KEYCODE_ENDCALL, "KEYCODE_ENDCALL");
665 names.append(KEYCODE_0, "KEYCODE_0");
666 names.append(KEYCODE_1, "KEYCODE_1");
667 names.append(KEYCODE_2, "KEYCODE_2");
668 names.append(KEYCODE_3, "KEYCODE_3");
669 names.append(KEYCODE_4, "KEYCODE_4");
670 names.append(KEYCODE_5, "KEYCODE_5");
671 names.append(KEYCODE_6, "KEYCODE_6");
672 names.append(KEYCODE_7, "KEYCODE_7");
673 names.append(KEYCODE_8, "KEYCODE_8");
674 names.append(KEYCODE_9, "KEYCODE_9");
675 names.append(KEYCODE_STAR, "KEYCODE_STAR");
676 names.append(KEYCODE_POUND, "KEYCODE_POUND");
677 names.append(KEYCODE_DPAD_UP, "KEYCODE_DPAD_UP");
678 names.append(KEYCODE_DPAD_DOWN, "KEYCODE_DPAD_DOWN");
679 names.append(KEYCODE_DPAD_LEFT, "KEYCODE_DPAD_LEFT");
680 names.append(KEYCODE_DPAD_RIGHT, "KEYCODE_DPAD_RIGHT");
681 names.append(KEYCODE_DPAD_CENTER, "KEYCODE_DPAD_CENTER");
682 names.append(KEYCODE_VOLUME_UP, "KEYCODE_VOLUME_UP");
683 names.append(KEYCODE_VOLUME_DOWN, "KEYCODE_VOLUME_DOWN");
684 names.append(KEYCODE_POWER, "KEYCODE_POWER");
685 names.append(KEYCODE_CAMERA, "KEYCODE_CAMERA");
686 names.append(KEYCODE_CLEAR, "KEYCODE_CLEAR");
687 names.append(KEYCODE_A, "KEYCODE_A");
688 names.append(KEYCODE_B, "KEYCODE_B");
689 names.append(KEYCODE_C, "KEYCODE_C");
690 names.append(KEYCODE_D, "KEYCODE_D");
691 names.append(KEYCODE_E, "KEYCODE_E");
692 names.append(KEYCODE_F, "KEYCODE_F");
693 names.append(KEYCODE_G, "KEYCODE_G");
694 names.append(KEYCODE_H, "KEYCODE_H");
695 names.append(KEYCODE_I, "KEYCODE_I");
696 names.append(KEYCODE_J, "KEYCODE_J");
697 names.append(KEYCODE_K, "KEYCODE_K");
698 names.append(KEYCODE_L, "KEYCODE_L");
699 names.append(KEYCODE_M, "KEYCODE_M");
700 names.append(KEYCODE_N, "KEYCODE_N");
701 names.append(KEYCODE_O, "KEYCODE_O");
702 names.append(KEYCODE_P, "KEYCODE_P");
703 names.append(KEYCODE_Q, "KEYCODE_Q");
704 names.append(KEYCODE_R, "KEYCODE_R");
705 names.append(KEYCODE_S, "KEYCODE_S");
706 names.append(KEYCODE_T, "KEYCODE_T");
707 names.append(KEYCODE_U, "KEYCODE_U");
708 names.append(KEYCODE_V, "KEYCODE_V");
709 names.append(KEYCODE_W, "KEYCODE_W");
710 names.append(KEYCODE_X, "KEYCODE_X");
711 names.append(KEYCODE_Y, "KEYCODE_Y");
712 names.append(KEYCODE_Z, "KEYCODE_Z");
713 names.append(KEYCODE_COMMA, "KEYCODE_COMMA");
714 names.append(KEYCODE_PERIOD, "KEYCODE_PERIOD");
715 names.append(KEYCODE_ALT_LEFT, "KEYCODE_ALT_LEFT");
716 names.append(KEYCODE_ALT_RIGHT, "KEYCODE_ALT_RIGHT");
717 names.append(KEYCODE_SHIFT_LEFT, "KEYCODE_SHIFT_LEFT");
718 names.append(KEYCODE_SHIFT_RIGHT, "KEYCODE_SHIFT_RIGHT");
719 names.append(KEYCODE_TAB, "KEYCODE_TAB");
720 names.append(KEYCODE_SPACE, "KEYCODE_SPACE");
721 names.append(KEYCODE_SYM, "KEYCODE_SYM");
722 names.append(KEYCODE_EXPLORER, "KEYCODE_EXPLORER");
723 names.append(KEYCODE_ENVELOPE, "KEYCODE_ENVELOPE");
724 names.append(KEYCODE_ENTER, "KEYCODE_ENTER");
725 names.append(KEYCODE_DEL, "KEYCODE_DEL");
726 names.append(KEYCODE_GRAVE, "KEYCODE_GRAVE");
727 names.append(KEYCODE_MINUS, "KEYCODE_MINUS");
728 names.append(KEYCODE_EQUALS, "KEYCODE_EQUALS");
729 names.append(KEYCODE_LEFT_BRACKET, "KEYCODE_LEFT_BRACKET");
730 names.append(KEYCODE_RIGHT_BRACKET, "KEYCODE_RIGHT_BRACKET");
731 names.append(KEYCODE_BACKSLASH, "KEYCODE_BACKSLASH");
732 names.append(KEYCODE_SEMICOLON, "KEYCODE_SEMICOLON");
733 names.append(KEYCODE_APOSTROPHE, "KEYCODE_APOSTROPHE");
734 names.append(KEYCODE_SLASH, "KEYCODE_SLASH");
735 names.append(KEYCODE_AT, "KEYCODE_AT");
736 names.append(KEYCODE_NUM, "KEYCODE_NUM");
737 names.append(KEYCODE_HEADSETHOOK, "KEYCODE_HEADSETHOOK");
738 names.append(KEYCODE_FOCUS, "KEYCODE_FOCUS");
739 names.append(KEYCODE_PLUS, "KEYCODE_PLUS");
740 names.append(KEYCODE_MENU, "KEYCODE_MENU");
741 names.append(KEYCODE_NOTIFICATION, "KEYCODE_NOTIFICATION");
742 names.append(KEYCODE_SEARCH, "KEYCODE_SEARCH");
743 names.append(KEYCODE_MEDIA_PLAY_PAUSE, "KEYCODE_MEDIA_PLAY_PAUSE");
744 names.append(KEYCODE_MEDIA_STOP, "KEYCODE_MEDIA_STOP");
745 names.append(KEYCODE_MEDIA_NEXT, "KEYCODE_MEDIA_NEXT");
746 names.append(KEYCODE_MEDIA_PREVIOUS, "KEYCODE_MEDIA_PREVIOUS");
747 names.append(KEYCODE_MEDIA_REWIND, "KEYCODE_MEDIA_REWIND");
748 names.append(KEYCODE_MEDIA_FAST_FORWARD, "KEYCODE_MEDIA_FAST_FORWARD");
749 names.append(KEYCODE_MUTE, "KEYCODE_MUTE");
750 names.append(KEYCODE_PAGE_UP, "KEYCODE_PAGE_UP");
751 names.append(KEYCODE_PAGE_DOWN, "KEYCODE_PAGE_DOWN");
752 names.append(KEYCODE_PICTSYMBOLS, "KEYCODE_PICTSYMBOLS");
753 names.append(KEYCODE_SWITCH_CHARSET, "KEYCODE_SWITCH_CHARSET");
754 names.append(KEYCODE_BUTTON_A, "KEYCODE_BUTTON_A");
755 names.append(KEYCODE_BUTTON_B, "KEYCODE_BUTTON_B");
756 names.append(KEYCODE_BUTTON_C, "KEYCODE_BUTTON_C");
757 names.append(KEYCODE_BUTTON_X, "KEYCODE_BUTTON_X");
758 names.append(KEYCODE_BUTTON_Y, "KEYCODE_BUTTON_Y");
759 names.append(KEYCODE_BUTTON_Z, "KEYCODE_BUTTON_Z");
760 names.append(KEYCODE_BUTTON_L1, "KEYCODE_BUTTON_L1");
761 names.append(KEYCODE_BUTTON_R1, "KEYCODE_BUTTON_R1");
762 names.append(KEYCODE_BUTTON_L2, "KEYCODE_BUTTON_L2");
763 names.append(KEYCODE_BUTTON_R2, "KEYCODE_BUTTON_R2");
764 names.append(KEYCODE_BUTTON_THUMBL, "KEYCODE_BUTTON_THUMBL");
765 names.append(KEYCODE_BUTTON_THUMBR, "KEYCODE_BUTTON_THUMBR");
766 names.append(KEYCODE_BUTTON_START, "KEYCODE_BUTTON_START");
767 names.append(KEYCODE_BUTTON_SELECT, "KEYCODE_BUTTON_SELECT");
768 names.append(KEYCODE_BUTTON_MODE, "KEYCODE_BUTTON_MODE");
769 names.append(KEYCODE_ESCAPE, "KEYCODE_ESCAPE");
770 names.append(KEYCODE_FORWARD_DEL, "KEYCODE_FORWARD_DEL");
771 names.append(KEYCODE_CTRL_LEFT, "KEYCODE_CTRL_LEFT");
772 names.append(KEYCODE_CTRL_RIGHT, "KEYCODE_CTRL_RIGHT");
773 names.append(KEYCODE_CAPS_LOCK, "KEYCODE_CAPS_LOCK");
774 names.append(KEYCODE_SCROLL_LOCK, "KEYCODE_SCROLL_LOCK");
775 names.append(KEYCODE_META_LEFT, "KEYCODE_META_LEFT");
776 names.append(KEYCODE_META_RIGHT, "KEYCODE_META_RIGHT");
777 names.append(KEYCODE_FUNCTION, "KEYCODE_FUNCTION");
778 names.append(KEYCODE_SYSRQ, "KEYCODE_SYSRQ");
779 names.append(KEYCODE_BREAK, "KEYCODE_BREAK");
780 names.append(KEYCODE_MOVE_HOME, "KEYCODE_MOVE_HOME");
781 names.append(KEYCODE_MOVE_END, "KEYCODE_MOVE_END");
782 names.append(KEYCODE_INSERT, "KEYCODE_INSERT");
783 names.append(KEYCODE_FORWARD, "KEYCODE_FORWARD");
784 names.append(KEYCODE_MEDIA_PLAY, "KEYCODE_MEDIA_PLAY");
785 names.append(KEYCODE_MEDIA_PAUSE, "KEYCODE_MEDIA_PAUSE");
786 names.append(KEYCODE_MEDIA_CLOSE, "KEYCODE_MEDIA_CLOSE");
787 names.append(KEYCODE_MEDIA_EJECT, "KEYCODE_MEDIA_EJECT");
788 names.append(KEYCODE_MEDIA_RECORD, "KEYCODE_MEDIA_RECORD");
789 names.append(KEYCODE_F1, "KEYCODE_F1");
790 names.append(KEYCODE_F2, "KEYCODE_F2");
791 names.append(KEYCODE_F3, "KEYCODE_F3");
792 names.append(KEYCODE_F4, "KEYCODE_F4");
793 names.append(KEYCODE_F5, "KEYCODE_F5");
794 names.append(KEYCODE_F6, "KEYCODE_F6");
795 names.append(KEYCODE_F7, "KEYCODE_F7");
796 names.append(KEYCODE_F8, "KEYCODE_F8");
797 names.append(KEYCODE_F9, "KEYCODE_F9");
798 names.append(KEYCODE_F10, "KEYCODE_F10");
799 names.append(KEYCODE_F11, "KEYCODE_F11");
800 names.append(KEYCODE_F12, "KEYCODE_F12");
801 names.append(KEYCODE_NUM_LOCK, "KEYCODE_NUM_LOCK");
802 names.append(KEYCODE_NUMPAD_0, "KEYCODE_NUMPAD_0");
803 names.append(KEYCODE_NUMPAD_1, "KEYCODE_NUMPAD_1");
804 names.append(KEYCODE_NUMPAD_2, "KEYCODE_NUMPAD_2");
805 names.append(KEYCODE_NUMPAD_3, "KEYCODE_NUMPAD_3");
806 names.append(KEYCODE_NUMPAD_4, "KEYCODE_NUMPAD_4");
807 names.append(KEYCODE_NUMPAD_5, "KEYCODE_NUMPAD_5");
808 names.append(KEYCODE_NUMPAD_6, "KEYCODE_NUMPAD_6");
809 names.append(KEYCODE_NUMPAD_7, "KEYCODE_NUMPAD_7");
810 names.append(KEYCODE_NUMPAD_8, "KEYCODE_NUMPAD_8");
811 names.append(KEYCODE_NUMPAD_9, "KEYCODE_NUMPAD_9");
812 names.append(KEYCODE_NUMPAD_DIVIDE, "KEYCODE_NUMPAD_DIVIDE");
813 names.append(KEYCODE_NUMPAD_MULTIPLY, "KEYCODE_NUMPAD_MULTIPLY");
814 names.append(KEYCODE_NUMPAD_SUBTRACT, "KEYCODE_NUMPAD_SUBTRACT");
815 names.append(KEYCODE_NUMPAD_ADD, "KEYCODE_NUMPAD_ADD");
816 names.append(KEYCODE_NUMPAD_DOT, "KEYCODE_NUMPAD_DOT");
817 names.append(KEYCODE_NUMPAD_COMMA, "KEYCODE_NUMPAD_COMMA");
818 names.append(KEYCODE_NUMPAD_ENTER, "KEYCODE_NUMPAD_ENTER");
819 names.append(KEYCODE_NUMPAD_EQUALS, "KEYCODE_NUMPAD_EQUALS");
820 names.append(KEYCODE_NUMPAD_LEFT_PAREN, "KEYCODE_NUMPAD_LEFT_PAREN");
821 names.append(KEYCODE_NUMPAD_RIGHT_PAREN, "KEYCODE_NUMPAD_RIGHT_PAREN");
822 names.append(KEYCODE_VOLUME_MUTE, "KEYCODE_VOLUME_MUTE");
823 names.append(KEYCODE_INFO, "KEYCODE_INFO");
824 names.append(KEYCODE_CHANNEL_UP, "KEYCODE_CHANNEL_UP");
825 names.append(KEYCODE_CHANNEL_DOWN, "KEYCODE_CHANNEL_DOWN");
826 names.append(KEYCODE_ZOOM_IN, "KEYCODE_ZOOM_IN");
827 names.append(KEYCODE_ZOOM_OUT, "KEYCODE_ZOOM_OUT");
828 names.append(KEYCODE_TV, "KEYCODE_TV");
829 names.append(KEYCODE_WINDOW, "KEYCODE_WINDOW");
830 names.append(KEYCODE_GUIDE, "KEYCODE_GUIDE");
831 names.append(KEYCODE_DVR, "KEYCODE_DVR");
832 names.append(KEYCODE_BOOKMARK, "KEYCODE_BOOKMARK");
833 names.append(KEYCODE_CAPTIONS, "KEYCODE_CAPTIONS");
834 names.append(KEYCODE_SETTINGS, "KEYCODE_SETTINGS");
835 names.append(KEYCODE_TV_POWER, "KEYCODE_TV_POWER");
836 names.append(KEYCODE_TV_INPUT, "KEYCODE_TV_INPUT");
837 names.append(KEYCODE_STB_INPUT, "KEYCODE_STB_INPUT");
838 names.append(KEYCODE_STB_POWER, "KEYCODE_STB_POWER");
839 names.append(KEYCODE_AVR_POWER, "KEYCODE_AVR_POWER");
840 names.append(KEYCODE_AVR_INPUT, "KEYCODE_AVR_INPUT");
841 names.append(KEYCODE_PROG_RED, "KEYCODE_PROG_RED");
842 names.append(KEYCODE_PROG_GREEN, "KEYCODE_PROG_GREEN");
843 names.append(KEYCODE_PROG_YELLOW, "KEYCODE_PROG_YELLOW");
844 names.append(KEYCODE_PROG_BLUE, "KEYCODE_PROG_BLUE");
845 names.append(KEYCODE_APP_SWITCH, "KEYCODE_APP_SWITCH");
846 names.append(KEYCODE_BUTTON_1, "KEYCODE_BUTTON_1");
847 names.append(KEYCODE_BUTTON_2, "KEYCODE_BUTTON_2");
848 names.append(KEYCODE_BUTTON_3, "KEYCODE_BUTTON_3");
849 names.append(KEYCODE_BUTTON_4, "KEYCODE_BUTTON_4");
850 names.append(KEYCODE_BUTTON_5, "KEYCODE_BUTTON_5");
851 names.append(KEYCODE_BUTTON_6, "KEYCODE_BUTTON_6");
852 names.append(KEYCODE_BUTTON_7, "KEYCODE_BUTTON_7");
853 names.append(KEYCODE_BUTTON_8, "KEYCODE_BUTTON_8");
854 names.append(KEYCODE_BUTTON_9, "KEYCODE_BUTTON_9");
855 names.append(KEYCODE_BUTTON_10, "KEYCODE_BUTTON_10");
856 names.append(KEYCODE_BUTTON_11, "KEYCODE_BUTTON_11");
857 names.append(KEYCODE_BUTTON_12, "KEYCODE_BUTTON_12");
858 names.append(KEYCODE_BUTTON_13, "KEYCODE_BUTTON_13");
859 names.append(KEYCODE_BUTTON_14, "KEYCODE_BUTTON_14");
860 names.append(KEYCODE_BUTTON_15, "KEYCODE_BUTTON_15");
861 names.append(KEYCODE_BUTTON_16, "KEYCODE_BUTTON_16");
Jeff Brown9812aed2011-03-07 17:09:51 -0800862 names.append(KEYCODE_LANGUAGE_SWITCH, "KEYCODE_LANGUAGE_SWITCH");
863 names.append(KEYCODE_MANNER_MODE, "KEYCODE_MANNER_MODE");
864 names.append(KEYCODE_3D_MODE, "KEYCODE_3D_MODE");
Jeff Brown6651a632011-11-28 12:59:11 -0800865 names.append(KEYCODE_CONTACTS, "KEYCODE_CONTACTS");
866 names.append(KEYCODE_CALENDAR, "KEYCODE_CALENDAR");
867 names.append(KEYCODE_MUSIC, "KEYCODE_MUSIC");
868 names.append(KEYCODE_CALCULATOR, "KEYCODE_CALCULATOR");
Yang Chuang7511f9c2012-02-10 15:18:26 +0800869 names.append(KEYCODE_ZENKAKU_HANKAKU, "KEYCODE_ZENKAKU_HANKAKU");
870 names.append(KEYCODE_EISU, "KEYCODE_EISU");
871 names.append(KEYCODE_MUHENKAN, "KEYCODE_MUHENKAN");
872 names.append(KEYCODE_HENKAN, "KEYCODE_HENKAN");
873 names.append(KEYCODE_KATAKANA_HIRAGANA, "KEYCODE_KATAKANA_HIRAGANA");
874 names.append(KEYCODE_YEN, "KEYCODE_YEN");
875 names.append(KEYCODE_RO, "KEYCODE_RO");
876 names.append(KEYCODE_KANA, "KEYCODE_KANA");
Jeff Brownde7a8ea2012-06-13 18:28:57 -0700877 names.append(KEYCODE_ASSIST, "KEYCODE_ASSIST");
Michael Wright1df477a2013-01-31 16:19:18 -0800878 names.append(KEYCODE_BRIGHTNESS_DOWN, "KEYCODE_BRIGHTNESS_DOWN");
879 names.append(KEYCODE_BRIGHTNESS_UP, "KEYCODE_BRIGHTNESS_UP");
Jaekyun Seokbfdad8e2013-07-08 13:53:21 +0900880 names.append(KEYCODE_MEDIA_AUDIO_TRACK, "KEYCODE_MEDIA_AUDIO_TRACK");
Jeff Brown497a92c2010-09-12 17:55:08 -0700881 };
882
883 // Symbolic names of all metakeys in bit order from least significant to most significant.
884 // Accordingly there are exactly 32 values in this table.
885 private static final String[] META_SYMBOLIC_NAMES = new String[] {
886 "META_SHIFT_ON",
887 "META_ALT_ON",
888 "META_SYM_ON",
889 "META_FUNCTION_ON",
890 "META_ALT_LEFT_ON",
891 "META_ALT_RIGHT_ON",
892 "META_SHIFT_LEFT_ON",
893 "META_SHIFT_RIGHT_ON",
894 "META_CAP_LOCKED",
895 "META_ALT_LOCKED",
896 "META_SYM_LOCKED",
897 "0x00000800",
898 "META_CTRL_ON",
899 "META_CTRL_LEFT_ON",
900 "META_CTRL_RIGHT_ON",
901 "0x00008000",
902 "META_META_ON",
903 "META_META_LEFT_ON",
904 "META_META_RIGHT_ON",
905 "0x00080000",
Jeff Brown51e7fe72010-10-29 22:19:53 -0700906 "META_CAPS_LOCK_ON",
907 "META_NUM_LOCK_ON",
908 "META_SCROLL_LOCK_ON",
Jeff Brown497a92c2010-09-12 17:55:08 -0700909 "0x00800000",
910 "0x01000000",
911 "0x02000000",
912 "0x04000000",
913 "0x08000000",
914 "0x10000000",
915 "0x20000000",
916 "0x40000000",
917 "0x80000000",
918 };
919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 /**
921 * @deprecated There are now more than MAX_KEYCODE keycodes.
922 * Use {@link #getMaxKeyCode()} instead.
923 */
924 @Deprecated
925 public static final int MAX_KEYCODE = 84;
926
927 /**
928 * {@link #getAction} value: the key has been pressed down.
929 */
930 public static final int ACTION_DOWN = 0;
931 /**
932 * {@link #getAction} value: the key has been released.
933 */
934 public static final int ACTION_UP = 1;
935 /**
936 * {@link #getAction} value: multiple duplicate key events have
937 * occurred in a row, or a complex string is being delivered. If the
938 * key code is not {#link {@link #KEYCODE_UNKNOWN} then the
939 * {#link {@link #getRepeatCount()} method returns the number of times
940 * the given key code should be executed.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700941 * Otherwise, if the key code is {@link #KEYCODE_UNKNOWN}, then
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 * this is a sequence of characters as returned by {@link #getCharacters}.
943 */
944 public static final int ACTION_MULTIPLE = 2;
945
946 /**
Jeff Brown497a92c2010-09-12 17:55:08 -0700947 * SHIFT key locked in CAPS mode.
948 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
949 * @hide
950 */
951 public static final int META_CAP_LOCKED = 0x100;
952
953 /**
954 * ALT key locked.
955 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
956 * @hide
957 */
958 public static final int META_ALT_LOCKED = 0x200;
959
960 /**
961 * SYM key locked.
962 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
963 * @hide
964 */
965 public static final int META_SYM_LOCKED = 0x400;
966
967 /**
968 * Text is in selection mode.
969 * Reserved for use by {@link MetaKeyKeyListener} for a private unpublished constant
970 * in its API that is currently being retained for legacy reasons.
971 * @hide
972 */
973 public static final int META_SELECTING = 0x800;
974
975 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 * <p>This mask is used to check whether one of the ALT meta keys is pressed.</p>
977 *
978 * @see #isAltPressed()
979 * @see #getMetaState()
980 * @see #KEYCODE_ALT_LEFT
981 * @see #KEYCODE_ALT_RIGHT
982 */
983 public static final int META_ALT_ON = 0x02;
984
985 /**
986 * <p>This mask is used to check whether the left ALT meta key is pressed.</p>
987 *
988 * @see #isAltPressed()
989 * @see #getMetaState()
990 * @see #KEYCODE_ALT_LEFT
991 */
992 public static final int META_ALT_LEFT_ON = 0x10;
993
994 /**
995 * <p>This mask is used to check whether the right the ALT meta key is pressed.</p>
996 *
997 * @see #isAltPressed()
998 * @see #getMetaState()
999 * @see #KEYCODE_ALT_RIGHT
1000 */
1001 public static final int META_ALT_RIGHT_ON = 0x20;
1002
1003 /**
1004 * <p>This mask is used to check whether one of the SHIFT meta keys is pressed.</p>
1005 *
1006 * @see #isShiftPressed()
1007 * @see #getMetaState()
1008 * @see #KEYCODE_SHIFT_LEFT
1009 * @see #KEYCODE_SHIFT_RIGHT
1010 */
1011 public static final int META_SHIFT_ON = 0x1;
1012
1013 /**
1014 * <p>This mask is used to check whether the left SHIFT meta key is pressed.</p>
1015 *
1016 * @see #isShiftPressed()
1017 * @see #getMetaState()
1018 * @see #KEYCODE_SHIFT_LEFT
1019 */
1020 public static final int META_SHIFT_LEFT_ON = 0x40;
1021
1022 /**
1023 * <p>This mask is used to check whether the right SHIFT meta key is pressed.</p>
1024 *
1025 * @see #isShiftPressed()
1026 * @see #getMetaState()
1027 * @see #KEYCODE_SHIFT_RIGHT
1028 */
1029 public static final int META_SHIFT_RIGHT_ON = 0x80;
1030
1031 /**
1032 * <p>This mask is used to check whether the SYM meta key is pressed.</p>
1033 *
1034 * @see #isSymPressed()
1035 * @see #getMetaState()
1036 */
1037 public static final int META_SYM_ON = 0x4;
1038
1039 /**
Jeff Brown497a92c2010-09-12 17:55:08 -07001040 * <p>This mask is used to check whether the FUNCTION meta key is pressed.</p>
1041 *
1042 * @see #isFunctionPressed()
1043 * @see #getMetaState()
1044 */
1045 public static final int META_FUNCTION_ON = 0x8;
1046
1047 /**
1048 * <p>This mask is used to check whether one of the CTRL meta keys is pressed.</p>
1049 *
1050 * @see #isCtrlPressed()
1051 * @see #getMetaState()
1052 * @see #KEYCODE_CTRL_LEFT
1053 * @see #KEYCODE_CTRL_RIGHT
1054 */
1055 public static final int META_CTRL_ON = 0x1000;
1056
1057 /**
1058 * <p>This mask is used to check whether the left CTRL meta key is pressed.</p>
1059 *
1060 * @see #isCtrlPressed()
1061 * @see #getMetaState()
1062 * @see #KEYCODE_CTRL_LEFT
1063 */
1064 public static final int META_CTRL_LEFT_ON = 0x2000;
1065
1066 /**
1067 * <p>This mask is used to check whether the right CTRL meta key is pressed.</p>
1068 *
1069 * @see #isCtrlPressed()
1070 * @see #getMetaState()
1071 * @see #KEYCODE_CTRL_RIGHT
1072 */
1073 public static final int META_CTRL_RIGHT_ON = 0x4000;
1074
1075 /**
1076 * <p>This mask is used to check whether one of the META meta keys is pressed.</p>
1077 *
1078 * @see #isMetaPressed()
1079 * @see #getMetaState()
1080 * @see #KEYCODE_META_LEFT
1081 * @see #KEYCODE_META_RIGHT
1082 */
1083 public static final int META_META_ON = 0x10000;
1084
1085 /**
1086 * <p>This mask is used to check whether the left META meta key is pressed.</p>
1087 *
1088 * @see #isMetaPressed()
1089 * @see #getMetaState()
1090 * @see #KEYCODE_META_LEFT
1091 */
1092 public static final int META_META_LEFT_ON = 0x20000;
1093
1094 /**
1095 * <p>This mask is used to check whether the right META meta key is pressed.</p>
1096 *
1097 * @see #isMetaPressed()
1098 * @see #getMetaState()
1099 * @see #KEYCODE_META_RIGHT
1100 */
1101 public static final int META_META_RIGHT_ON = 0x40000;
1102
1103 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07001104 * <p>This mask is used to check whether the CAPS LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07001105 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07001106 * @see #isCapsLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -07001107 * @see #getMetaState()
1108 * @see #KEYCODE_CAPS_LOCK
1109 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07001110 public static final int META_CAPS_LOCK_ON = 0x100000;
Jeff Brown497a92c2010-09-12 17:55:08 -07001111
1112 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07001113 * <p>This mask is used to check whether the NUM LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07001114 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07001115 * @see #isNumLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -07001116 * @see #getMetaState()
1117 * @see #KEYCODE_NUM_LOCK
1118 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07001119 public static final int META_NUM_LOCK_ON = 0x200000;
Jeff Brown497a92c2010-09-12 17:55:08 -07001120
1121 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07001122 * <p>This mask is used to check whether the SCROLL LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07001123 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07001124 * @see #isScrollLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -07001125 * @see #getMetaState()
1126 * @see #KEYCODE_SCROLL_LOCK
1127 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07001128 public static final int META_SCROLL_LOCK_ON = 0x400000;
Jeff Brown497a92c2010-09-12 17:55:08 -07001129
Jeff Brown64da12a2011-01-04 19:57:47 -08001130 /**
1131 * This mask is a combination of {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON}
1132 * and {@link #META_SHIFT_RIGHT_ON}.
1133 */
Jeff Brownc1df9072010-12-21 16:38:50 -08001134 public static final int META_SHIFT_MASK = META_SHIFT_ON
1135 | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON;
1136
Jeff Brown64da12a2011-01-04 19:57:47 -08001137 /**
1138 * This mask is a combination of {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON}
1139 * and {@link #META_ALT_RIGHT_ON}.
1140 */
Jeff Brownc1df9072010-12-21 16:38:50 -08001141 public static final int META_ALT_MASK = META_ALT_ON
1142 | META_ALT_LEFT_ON | META_ALT_RIGHT_ON;
1143
Jeff Brown64da12a2011-01-04 19:57:47 -08001144 /**
1145 * This mask is a combination of {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON}
1146 * and {@link #META_CTRL_RIGHT_ON}.
1147 */
Jeff Brownc1df9072010-12-21 16:38:50 -08001148 public static final int META_CTRL_MASK = META_CTRL_ON
1149 | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON;
1150
Jeff Brown64da12a2011-01-04 19:57:47 -08001151 /**
1152 * This mask is a combination of {@link #META_META_ON}, {@link #META_META_LEFT_ON}
1153 * and {@link #META_META_RIGHT_ON}.
1154 */
1155 public static final int META_META_MASK = META_META_ON
Jeff Brownc1df9072010-12-21 16:38:50 -08001156 | META_META_LEFT_ON | META_META_RIGHT_ON;
1157
Jeff Brown497a92c2010-09-12 17:55:08 -07001158 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 * This mask is set if the device woke because of this key event.
1160 */
1161 public static final int FLAG_WOKE_HERE = 0x1;
1162
1163 /**
1164 * This mask is set if the key event was generated by a software keyboard.
1165 */
1166 public static final int FLAG_SOFT_KEYBOARD = 0x2;
1167
1168 /**
1169 * This mask is set if we don't want the key event to cause us to leave
1170 * touch mode.
1171 */
1172 public static final int FLAG_KEEP_TOUCH_MODE = 0x4;
1173
1174 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001175 * This mask is set if an event was known to come from a trusted part
1176 * of the system. That is, the event is known to come from the user,
1177 * and could not have been spoofed by a third party component.
1178 */
1179 public static final int FLAG_FROM_SYSTEM = 0x8;
1180
1181 /**
1182 * This mask is used for compatibility, to identify enter keys that are
1183 * coming from an IME whose enter key has been auto-labelled "next" or
1184 * "done". This allows TextView to dispatch these as normal enter keys
1185 * for old applications, but still do the appropriate action when
1186 * receiving them.
1187 */
1188 public static final int FLAG_EDITOR_ACTION = 0x10;
1189
1190 /**
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07001191 * When associated with up key events, this indicates that the key press
1192 * has been canceled. Typically this is used with virtual touch screen
1193 * keys, where the user can slide from the virtual key area on to the
1194 * display: in that case, the application will receive a canceled up
1195 * event and should not perform the action normally associated with the
1196 * key. Note that for this to work, the application can not perform an
1197 * action for a key until it receives an up or the long press timeout has
1198 * expired.
1199 */
1200 public static final int FLAG_CANCELED = 0x20;
1201
1202 /**
1203 * This key event was generated by a virtual (on-screen) hard key area.
1204 * Typically this is an area of the touchscreen, outside of the regular
1205 * display, dedicated to "hardware" buttons.
1206 */
1207 public static final int FLAG_VIRTUAL_HARD_KEY = 0x40;
1208
1209 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001210 * This flag is set for the first key repeat that occurs after the
1211 * long press timeout.
1212 */
1213 public static final int FLAG_LONG_PRESS = 0x80;
1214
1215 /**
1216 * Set when a key event has {@link #FLAG_CANCELED} set because a long
1217 * press action was executed while it was down.
1218 */
1219 public static final int FLAG_CANCELED_LONG_PRESS = 0x100;
1220
1221 /**
1222 * Set for {@link #ACTION_UP} when this event's key code is still being
1223 * tracked from its initial down. That is, somebody requested that tracking
1224 * started on the key down and a long press has not caused
1225 * the tracking to be canceled.
1226 */
1227 public static final int FLAG_TRACKING = 0x200;
Jeff Brown49ed71d2010-12-06 17:13:33 -08001228
1229 /**
1230 * Set when a key event has been synthesized to implement default behavior
1231 * for an event that the application did not handle.
1232 * Fallback key events are generated by unhandled trackball motions
1233 * (to emulate a directional keypad) and by certain unhandled key presses
1234 * that are declared in the key map (such as special function numeric keypad
1235 * keys when numlock is off).
1236 */
1237 public static final int FLAG_FALLBACK = 0x400;
1238
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001239 /**
Michael Wrighta44dd262013-04-10 21:12:00 -07001240 * Signifies that the key is being predispatched.
1241 * @hide
1242 */
1243 public static final int FLAG_PREDISPATCH = 0x20000000;
1244
1245 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001246 * Private control to determine when an app is tracking a key sequence.
1247 * @hide
1248 */
1249 public static final int FLAG_START_TRACKING = 0x40000000;
Jeff Brown21bc5c92011-02-28 18:27:14 -08001250
1251 /**
1252 * Private flag that indicates when the system has detected that this key event
1253 * may be inconsistent with respect to the sequence of previously delivered key events,
1254 * such as when a key up event is sent but the key was not down.
1255 *
1256 * @hide
1257 * @see #isTainted
1258 * @see #setTainted
1259 */
1260 public static final int FLAG_TAINTED = 0x80000000;
1261
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001262 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 * Returns the maximum keycode.
1264 */
1265 public static int getMaxKeyCode() {
1266 return LAST_KEYCODE;
1267 }
1268
1269 /**
1270 * Get the character that is produced by putting accent on the character
1271 * c.
1272 * For example, getDeadChar('`', 'e') returns &egrave;.
1273 */
1274 public static int getDeadChar(int accent, int c) {
1275 return KeyCharacterMap.getDeadChar(accent, c);
1276 }
1277
Dianne Hackborn8d374262009-09-14 21:21:52 -07001278 static final boolean DEBUG = false;
1279 static final String TAG = "KeyEvent";
Jeff Brown1f245102010-11-18 20:53:46 -08001280
1281 private static final int MAX_RECYCLED = 10;
1282 private static final Object gRecyclerLock = new Object();
1283 private static int gRecyclerUsed;
1284 private static KeyEvent gRecyclerTop;
1285
1286 private KeyEvent mNext;
Jeff Brown1f245102010-11-18 20:53:46 -08001287
Jeff Brown91c69ab2011-02-14 17:03:18 -08001288 private int mDeviceId;
1289 private int mSource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 private int mMetaState;
1291 private int mAction;
1292 private int mKeyCode;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001293 private int mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 private int mRepeatCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 private int mFlags;
1296 private long mDownTime;
1297 private long mEventTime;
1298 private String mCharacters;
1299
1300 public interface Callback {
1301 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001302 * Called when a key down event has occurred. If you return true,
1303 * you can first call {@link KeyEvent#startTracking()
1304 * KeyEvent.startTracking()} to have the framework track the event
1305 * through its {@link #onKeyUp(int, KeyEvent)} and also call your
1306 * {@link #onKeyLongPress(int, KeyEvent)} if it occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 *
1308 * @param keyCode The value in event.getKeyCode().
1309 * @param event Description of the key event.
1310 *
1311 * @return If you handled the event, return true. If you want to allow
1312 * the event to be handled by the next receiver, return false.
1313 */
1314 boolean onKeyDown(int keyCode, KeyEvent event);
1315
1316 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001317 * Called when a long press has occurred. If you return true,
1318 * the final key up will have {@link KeyEvent#FLAG_CANCELED} and
1319 * {@link KeyEvent#FLAG_CANCELED_LONG_PRESS} set. Note that in
1320 * order to receive this callback, someone in the event change
1321 * <em>must</em> return true from {@link #onKeyDown} <em>and</em>
1322 * call {@link KeyEvent#startTracking()} on the event.
1323 *
1324 * @param keyCode The value in event.getKeyCode().
1325 * @param event Description of the key event.
1326 *
1327 * @return If you handled the event, return true. If you want to allow
1328 * the event to be handled by the next receiver, return false.
1329 */
1330 boolean onKeyLongPress(int keyCode, KeyEvent event);
1331
1332 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 * Called when a key up event has occurred.
1334 *
1335 * @param keyCode The value in event.getKeyCode().
1336 * @param event Description of the key event.
1337 *
1338 * @return If you handled the event, return true. If you want to allow
1339 * the event to be handled by the next receiver, return false.
1340 */
1341 boolean onKeyUp(int keyCode, KeyEvent event);
1342
1343 /**
1344 * Called when multiple down/up pairs of the same key have occurred
1345 * in a row.
1346 *
1347 * @param keyCode The value in event.getKeyCode().
1348 * @param count Number of pairs as returned by event.getRepeatCount().
1349 * @param event Description of the key event.
1350 *
1351 * @return If you handled the event, return true. If you want to allow
1352 * the event to be handled by the next receiver, return false.
1353 */
1354 boolean onKeyMultiple(int keyCode, int count, KeyEvent event);
1355 }
1356
Jeff Brown497a92c2010-09-12 17:55:08 -07001357 static {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001358 populateKeycodeSymbolicNames();
Jeff Brown497a92c2010-09-12 17:55:08 -07001359 }
1360
Jeff Brown1f245102010-11-18 20:53:46 -08001361 private KeyEvent() {
1362 }
1363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 /**
1365 * Create a new key event.
1366 *
1367 * @param action Action code: either {@link #ACTION_DOWN},
1368 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1369 * @param code The key code.
1370 */
1371 public KeyEvent(int action, int code) {
1372 mAction = action;
1373 mKeyCode = code;
1374 mRepeatCount = 0;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001375 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 }
1377
1378 /**
1379 * Create a new key event.
1380 *
1381 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1382 * at which this key code originally went down.
1383 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1384 * at which this event happened.
1385 * @param action Action code: either {@link #ACTION_DOWN},
1386 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1387 * @param code The key code.
1388 * @param repeat A repeat count for down events (> 0 if this is after the
1389 * initial down) or event count for multiple events.
1390 */
1391 public KeyEvent(long downTime, long eventTime, int action,
1392 int code, int repeat) {
1393 mDownTime = downTime;
1394 mEventTime = eventTime;
1395 mAction = action;
1396 mKeyCode = code;
1397 mRepeatCount = repeat;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001398 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 }
1400
1401 /**
1402 * Create a new key event.
1403 *
1404 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1405 * at which this key code originally went down.
1406 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1407 * at which this event happened.
1408 * @param action Action code: either {@link #ACTION_DOWN},
1409 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1410 * @param code The key code.
1411 * @param repeat A repeat count for down events (> 0 if this is after the
1412 * initial down) or event count for multiple events.
1413 * @param metaState Flags indicating which meta keys are currently pressed.
1414 */
1415 public KeyEvent(long downTime, long eventTime, int action,
1416 int code, int repeat, int metaState) {
1417 mDownTime = downTime;
1418 mEventTime = eventTime;
1419 mAction = action;
1420 mKeyCode = code;
1421 mRepeatCount = repeat;
1422 mMetaState = metaState;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001423 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 }
1425
1426 /**
1427 * Create a new key event.
1428 *
1429 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1430 * at which this key code originally went down.
1431 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1432 * at which this event happened.
1433 * @param action Action code: either {@link #ACTION_DOWN},
1434 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1435 * @param code The key code.
1436 * @param repeat A repeat count for down events (> 0 if this is after the
1437 * initial down) or event count for multiple events.
1438 * @param metaState Flags indicating which meta keys are currently pressed.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001439 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 * @param scancode Raw device scan code of the event.
1441 */
1442 public KeyEvent(long downTime, long eventTime, int action,
1443 int code, int repeat, int metaState,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001444 int deviceId, int scancode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 mDownTime = downTime;
1446 mEventTime = eventTime;
1447 mAction = action;
1448 mKeyCode = code;
1449 mRepeatCount = repeat;
1450 mMetaState = metaState;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001451 mDeviceId = deviceId;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001452 mScanCode = scancode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 }
1454
1455 /**
1456 * Create a new key event.
1457 *
1458 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1459 * at which this key code originally went down.
1460 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1461 * at which this event happened.
1462 * @param action Action code: either {@link #ACTION_DOWN},
1463 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1464 * @param code The key code.
1465 * @param repeat A repeat count for down events (> 0 if this is after the
1466 * initial down) or event count for multiple events.
1467 * @param metaState Flags indicating which meta keys are currently pressed.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001468 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 * @param scancode Raw device scan code of the event.
1470 * @param flags The flags for this key event
1471 */
1472 public KeyEvent(long downTime, long eventTime, int action,
1473 int code, int repeat, int metaState,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001474 int deviceId, int scancode, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 mDownTime = downTime;
1476 mEventTime = eventTime;
1477 mAction = action;
1478 mKeyCode = code;
1479 mRepeatCount = repeat;
1480 mMetaState = metaState;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001481 mDeviceId = deviceId;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001482 mScanCode = scancode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 mFlags = flags;
1484 }
1485
1486 /**
Jeff Brownc5ed5912010-07-14 18:48:53 -07001487 * Create a new key event.
1488 *
1489 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1490 * at which this key code originally went down.
1491 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1492 * at which this event happened.
1493 * @param action Action code: either {@link #ACTION_DOWN},
1494 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1495 * @param code The key code.
1496 * @param repeat A repeat count for down events (> 0 if this is after the
1497 * initial down) or event count for multiple events.
1498 * @param metaState Flags indicating which meta keys are currently pressed.
1499 * @param deviceId The device ID that generated the key event.
1500 * @param scancode Raw device scan code of the event.
1501 * @param flags The flags for this key event
1502 * @param source The input source such as {@link InputDevice#SOURCE_KEYBOARD}.
1503 */
1504 public KeyEvent(long downTime, long eventTime, int action,
1505 int code, int repeat, int metaState,
1506 int deviceId, int scancode, int flags, int source) {
1507 mDownTime = downTime;
1508 mEventTime = eventTime;
1509 mAction = action;
1510 mKeyCode = code;
1511 mRepeatCount = repeat;
1512 mMetaState = metaState;
1513 mDeviceId = deviceId;
1514 mScanCode = scancode;
1515 mFlags = flags;
1516 mSource = source;
1517 }
1518
1519 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 * Create a new key event for a string of characters. The key code,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001521 * action, repeat count and source will automatically be set to
1522 * {@link #KEYCODE_UNKNOWN}, {@link #ACTION_MULTIPLE}, 0, and
1523 * {@link InputDevice#SOURCE_KEYBOARD} for you.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 *
1525 * @param time The time (in {@link android.os.SystemClock#uptimeMillis})
1526 * at which this event occured.
1527 * @param characters The string of characters.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001528 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 * @param flags The flags for this key event
1530 */
Jeff Brownc5ed5912010-07-14 18:48:53 -07001531 public KeyEvent(long time, String characters, int deviceId, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532 mDownTime = time;
1533 mEventTime = time;
1534 mCharacters = characters;
1535 mAction = ACTION_MULTIPLE;
1536 mKeyCode = KEYCODE_UNKNOWN;
1537 mRepeatCount = 0;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001538 mDeviceId = deviceId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 mFlags = flags;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001540 mSource = InputDevice.SOURCE_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 }
1542
1543 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001544 * Make an exact copy of an existing key event.
1545 */
1546 public KeyEvent(KeyEvent origEvent) {
1547 mDownTime = origEvent.mDownTime;
1548 mEventTime = origEvent.mEventTime;
1549 mAction = origEvent.mAction;
1550 mKeyCode = origEvent.mKeyCode;
1551 mRepeatCount = origEvent.mRepeatCount;
1552 mMetaState = origEvent.mMetaState;
1553 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001554 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001555 mScanCode = origEvent.mScanCode;
The Android Open Source Project10592532009-03-18 17:39:46 -07001556 mFlags = origEvent.mFlags;
1557 mCharacters = origEvent.mCharacters;
1558 }
1559
1560 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 * Copy an existing key event, modifying its time and repeat count.
1562 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001563 * @deprecated Use {@link #changeTimeRepeat(KeyEvent, long, int)}
1564 * instead.
1565 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 * @param origEvent The existing event to be copied.
1567 * @param eventTime The new event time
1568 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1569 * @param newRepeat The new repeat count of the event.
1570 */
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001571 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 public KeyEvent(KeyEvent origEvent, long eventTime, int newRepeat) {
1573 mDownTime = origEvent.mDownTime;
1574 mEventTime = eventTime;
1575 mAction = origEvent.mAction;
1576 mKeyCode = origEvent.mKeyCode;
1577 mRepeatCount = newRepeat;
1578 mMetaState = origEvent.mMetaState;
1579 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001580 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001581 mScanCode = origEvent.mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 mFlags = origEvent.mFlags;
1583 mCharacters = origEvent.mCharacters;
1584 }
1585
Jeff Brown1f245102010-11-18 20:53:46 -08001586 private static KeyEvent obtain() {
1587 final KeyEvent ev;
1588 synchronized (gRecyclerLock) {
1589 ev = gRecyclerTop;
1590 if (ev == null) {
1591 return new KeyEvent();
1592 }
1593 gRecyclerTop = ev.mNext;
1594 gRecyclerUsed -= 1;
1595 }
Jeff Brown1f245102010-11-18 20:53:46 -08001596 ev.mNext = null;
Jeff Brown32cbc38552011-12-01 14:01:49 -08001597 ev.prepareForReuse();
Jeff Brown1f245102010-11-18 20:53:46 -08001598 return ev;
1599 }
1600
1601 /**
1602 * Obtains a (potentially recycled) key event.
1603 *
1604 * @hide
1605 */
1606 public static KeyEvent obtain(long downTime, long eventTime, int action,
1607 int code, int repeat, int metaState,
1608 int deviceId, int scancode, int flags, int source, String characters) {
1609 KeyEvent ev = obtain();
1610 ev.mDownTime = downTime;
1611 ev.mEventTime = eventTime;
1612 ev.mAction = action;
1613 ev.mKeyCode = code;
1614 ev.mRepeatCount = repeat;
1615 ev.mMetaState = metaState;
1616 ev.mDeviceId = deviceId;
1617 ev.mScanCode = scancode;
1618 ev.mFlags = flags;
1619 ev.mSource = source;
1620 ev.mCharacters = characters;
1621 return ev;
1622 }
1623
1624 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08001625 * Obtains a (potentially recycled) copy of another key event.
1626 *
1627 * @hide
1628 */
1629 public static KeyEvent obtain(KeyEvent other) {
1630 KeyEvent ev = obtain();
1631 ev.mDownTime = other.mDownTime;
1632 ev.mEventTime = other.mEventTime;
1633 ev.mAction = other.mAction;
1634 ev.mKeyCode = other.mKeyCode;
1635 ev.mRepeatCount = other.mRepeatCount;
1636 ev.mMetaState = other.mMetaState;
1637 ev.mDeviceId = other.mDeviceId;
1638 ev.mScanCode = other.mScanCode;
1639 ev.mFlags = other.mFlags;
1640 ev.mSource = other.mSource;
1641 ev.mCharacters = other.mCharacters;
1642 return ev;
1643 }
1644
1645 /** @hide */
1646 @Override
1647 public KeyEvent copy() {
1648 return obtain(this);
1649 }
1650
1651 /**
Jeff Brown1f245102010-11-18 20:53:46 -08001652 * Recycles a key event.
1653 * Key events should only be recycled if they are owned by the system since user
1654 * code expects them to be essentially immutable, "tracking" notwithstanding.
1655 *
1656 * @hide
1657 */
Jeff Brown92cc2d82011-12-02 01:19:47 -08001658 @Override
Jeff Brown1f245102010-11-18 20:53:46 -08001659 public final void recycle() {
Jeff Brown32cbc38552011-12-01 14:01:49 -08001660 super.recycle();
Jeff Brown1f245102010-11-18 20:53:46 -08001661 mCharacters = null;
1662
1663 synchronized (gRecyclerLock) {
1664 if (gRecyclerUsed < MAX_RECYCLED) {
1665 gRecyclerUsed++;
1666 mNext = gRecyclerTop;
1667 gRecyclerTop = this;
1668 }
1669 }
1670 }
1671
Jeff Brown92cc2d82011-12-02 01:19:47 -08001672 /** @hide */
1673 @Override
1674 public final void recycleIfNeededAfterDispatch() {
1675 // Do nothing.
1676 }
1677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001679 * Create a new key event that is the same as the given one, but whose
1680 * event time and repeat count are replaced with the given value.
1681 *
1682 * @param event The existing event to be copied. This is not modified.
1683 * @param eventTime The new event time
1684 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1685 * @param newRepeat The new repeat count of the event.
1686 */
1687 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime,
1688 int newRepeat) {
1689 return new KeyEvent(event, eventTime, newRepeat);
1690 }
1691
1692 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001693 * Create a new key event that is the same as the given one, but whose
1694 * event time and repeat count are replaced with the given value.
1695 *
1696 * @param event The existing event to be copied. This is not modified.
1697 * @param eventTime The new event time
1698 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1699 * @param newRepeat The new repeat count of the event.
1700 * @param newFlags New flags for the event, replacing the entire value
1701 * in the original event.
1702 */
1703 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime,
1704 int newRepeat, int newFlags) {
1705 KeyEvent ret = new KeyEvent(event);
1706 ret.mEventTime = eventTime;
1707 ret.mRepeatCount = newRepeat;
1708 ret.mFlags = newFlags;
1709 return ret;
1710 }
1711
1712 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 * Copy an existing key event, modifying its action.
1714 *
1715 * @param origEvent The existing event to be copied.
1716 * @param action The new action code of the event.
1717 */
The Android Open Source Project10592532009-03-18 17:39:46 -07001718 private KeyEvent(KeyEvent origEvent, int action) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001719 mDownTime = origEvent.mDownTime;
1720 mEventTime = origEvent.mEventTime;
1721 mAction = action;
1722 mKeyCode = origEvent.mKeyCode;
1723 mRepeatCount = origEvent.mRepeatCount;
1724 mMetaState = origEvent.mMetaState;
1725 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001726 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001727 mScanCode = origEvent.mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 mFlags = origEvent.mFlags;
1729 // Don't copy mCharacters, since one way or the other we'll lose it
1730 // when changing the action.
1731 }
1732
1733 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001734 * Create a new key event that is the same as the given one, but whose
1735 * action is replaced with the given value.
1736 *
1737 * @param event The existing event to be copied. This is not modified.
1738 * @param action The new action code of the event.
1739 */
1740 public static KeyEvent changeAction(KeyEvent event, int action) {
1741 return new KeyEvent(event, action);
1742 }
1743
1744 /**
1745 * Create a new key event that is the same as the given one, but whose
1746 * flags are replaced with the given value.
1747 *
1748 * @param event The existing event to be copied. This is not modified.
1749 * @param flags The new flags constant.
1750 */
1751 public static KeyEvent changeFlags(KeyEvent event, int flags) {
1752 event = new KeyEvent(event);
1753 event.mFlags = flags;
1754 return event;
1755 }
Jeff Brown21bc5c92011-02-28 18:27:14 -08001756
1757 /** @hide */
1758 @Override
1759 public final boolean isTainted() {
1760 return (mFlags & FLAG_TAINTED) != 0;
1761 }
1762
1763 /** @hide */
1764 @Override
1765 public final void setTainted(boolean tainted) {
1766 mFlags = tainted ? mFlags | FLAG_TAINTED : mFlags & ~FLAG_TAINTED;
1767 }
1768
The Android Open Source Project10592532009-03-18 17:39:46 -07001769 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 * Don't use in new code, instead explicitly check
1771 * {@link #getAction()}.
1772 *
1773 * @return If the action is ACTION_DOWN, returns true; else false.
1774 *
1775 * @deprecated
1776 * @hide
1777 */
1778 @Deprecated public final boolean isDown() {
1779 return mAction == ACTION_DOWN;
1780 }
1781
1782 /**
1783 * Is this a system key? System keys can not be used for menu shortcuts.
1784 *
1785 * TODO: this information should come from a table somewhere.
1786 * TODO: should the dpad keys be here? arguably, because they also shouldn't be menu shortcuts
1787 */
1788 public final boolean isSystem() {
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -07001789 return native_isSystemKey(mKeyCode);
1790 }
1791
1792 /** @hide */
1793 public final boolean hasDefaultAction() {
1794 return native_hasDefaultAction(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 }
1796
Jeff Brown6f2fba42011-02-19 01:08:02 -08001797 /**
1798 * Returns true if the specified keycode is a gamepad button.
1799 * @return True if the keycode is a gamepad button, such as {@link #KEYCODE_BUTTON_A}.
1800 */
1801 public static final boolean isGamepadButton(int keyCode) {
1802 switch (keyCode) {
1803 case KeyEvent.KEYCODE_BUTTON_A:
1804 case KeyEvent.KEYCODE_BUTTON_B:
1805 case KeyEvent.KEYCODE_BUTTON_C:
1806 case KeyEvent.KEYCODE_BUTTON_X:
1807 case KeyEvent.KEYCODE_BUTTON_Y:
1808 case KeyEvent.KEYCODE_BUTTON_Z:
1809 case KeyEvent.KEYCODE_BUTTON_L1:
1810 case KeyEvent.KEYCODE_BUTTON_R1:
1811 case KeyEvent.KEYCODE_BUTTON_L2:
1812 case KeyEvent.KEYCODE_BUTTON_R2:
1813 case KeyEvent.KEYCODE_BUTTON_THUMBL:
1814 case KeyEvent.KEYCODE_BUTTON_THUMBR:
1815 case KeyEvent.KEYCODE_BUTTON_START:
1816 case KeyEvent.KEYCODE_BUTTON_SELECT:
1817 case KeyEvent.KEYCODE_BUTTON_MODE:
1818 case KeyEvent.KEYCODE_BUTTON_1:
1819 case KeyEvent.KEYCODE_BUTTON_2:
1820 case KeyEvent.KEYCODE_BUTTON_3:
1821 case KeyEvent.KEYCODE_BUTTON_4:
1822 case KeyEvent.KEYCODE_BUTTON_5:
1823 case KeyEvent.KEYCODE_BUTTON_6:
1824 case KeyEvent.KEYCODE_BUTTON_7:
1825 case KeyEvent.KEYCODE_BUTTON_8:
1826 case KeyEvent.KEYCODE_BUTTON_9:
1827 case KeyEvent.KEYCODE_BUTTON_10:
1828 case KeyEvent.KEYCODE_BUTTON_11:
1829 case KeyEvent.KEYCODE_BUTTON_12:
1830 case KeyEvent.KEYCODE_BUTTON_13:
1831 case KeyEvent.KEYCODE_BUTTON_14:
1832 case KeyEvent.KEYCODE_BUTTON_15:
1833 case KeyEvent.KEYCODE_BUTTON_16:
1834 return true;
1835 default:
1836 return false;
1837 }
1838 }
1839
Jeff Brown91c69ab2011-02-14 17:03:18 -08001840 /** {@inheritDoc} */
1841 @Override
1842 public final int getDeviceId() {
1843 return mDeviceId;
1844 }
1845
1846 /** {@inheritDoc} */
1847 @Override
1848 public final int getSource() {
1849 return mSource;
1850 }
1851
1852 /** {@inheritDoc} */
1853 @Override
1854 public final void setSource(int source) {
1855 mSource = source;
1856 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857
1858 /**
1859 * <p>Returns the state of the meta keys.</p>
1860 *
1861 * @return an integer in which each bit set to 1 represents a pressed
1862 * meta key
1863 *
1864 * @see #isAltPressed()
1865 * @see #isShiftPressed()
1866 * @see #isSymPressed()
Jeff Brown497a92c2010-09-12 17:55:08 -07001867 * @see #isCtrlPressed()
1868 * @see #isMetaPressed()
1869 * @see #isFunctionPressed()
Jeff Brown51e7fe72010-10-29 22:19:53 -07001870 * @see #isCapsLockOn()
1871 * @see #isNumLockOn()
1872 * @see #isScrollLockOn()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 * @see #META_ALT_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001874 * @see #META_ALT_LEFT_ON
1875 * @see #META_ALT_RIGHT_ON
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001876 * @see #META_SHIFT_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001877 * @see #META_SHIFT_LEFT_ON
1878 * @see #META_SHIFT_RIGHT_ON
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001879 * @see #META_SYM_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001880 * @see #META_FUNCTION_ON
1881 * @see #META_CTRL_ON
1882 * @see #META_CTRL_LEFT_ON
1883 * @see #META_CTRL_RIGHT_ON
1884 * @see #META_META_ON
1885 * @see #META_META_LEFT_ON
1886 * @see #META_META_RIGHT_ON
Jeff Brown51e7fe72010-10-29 22:19:53 -07001887 * @see #META_CAPS_LOCK_ON
1888 * @see #META_NUM_LOCK_ON
1889 * @see #META_SCROLL_LOCK_ON
Jeff Brown54875002011-04-06 15:33:01 -07001890 * @see #getModifiers
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 */
1892 public final int getMetaState() {
1893 return mMetaState;
1894 }
1895
1896 /**
Jeff Brown54875002011-04-06 15:33:01 -07001897 * Returns the state of the modifier keys.
1898 * <p>
1899 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1900 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1901 * not considered modifier keys. Consequently, this function specifically masks out
1902 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1903 * </p><p>
1904 * The value returned consists of the meta state (from {@link #getMetaState})
1905 * normalized using {@link #normalizeMetaState(int)} and then masked with
1906 * {@link #getModifierMetaStateMask} so that only valid modifier bits are retained.
1907 * </p>
1908 *
1909 * @return An integer in which each bit set to 1 represents a pressed modifier key.
1910 * @see #getMetaState
1911 */
1912 public final int getModifiers() {
1913 return normalizeMetaState(mMetaState) & META_MODIFIER_MASK;
1914 }
1915
1916 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 * Returns the flags for this key event.
1918 *
1919 * @see #FLAG_WOKE_HERE
1920 */
1921 public final int getFlags() {
1922 return mFlags;
1923 }
1924
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001925 // Mask of all modifier key meta states. Specifically excludes locked keys like caps lock.
1926 private static final int META_MODIFIER_MASK =
1927 META_SHIFT_ON | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON
1928 | META_ALT_ON | META_ALT_LEFT_ON | META_ALT_RIGHT_ON
1929 | META_CTRL_ON | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON
1930 | META_META_ON | META_META_LEFT_ON | META_META_RIGHT_ON
1931 | META_SYM_ON | META_FUNCTION_ON;
1932
1933 // Mask of all lock key meta states.
1934 private static final int META_LOCK_MASK =
1935 META_CAPS_LOCK_ON | META_NUM_LOCK_ON | META_SCROLL_LOCK_ON;
1936
1937 // Mask of all valid meta states.
1938 private static final int META_ALL_MASK = META_MODIFIER_MASK | META_LOCK_MASK;
1939
1940 // Mask of all synthetic meta states that are reserved for API compatibility with
1941 // historical uses in MetaKeyKeyListener.
1942 private static final int META_SYNTHETIC_MASK =
1943 META_CAP_LOCKED | META_ALT_LOCKED | META_SYM_LOCKED | META_SELECTING;
1944
1945 // Mask of all meta states that are not valid use in specifying a modifier key.
1946 // These bits are known to be used for purposes other than specifying modifiers.
1947 private static final int META_INVALID_MODIFIER_MASK =
1948 META_LOCK_MASK | META_SYNTHETIC_MASK;
1949
1950 /**
1951 * Gets a mask that includes all valid modifier key meta state bits.
1952 * <p>
1953 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1954 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1955 * not considered modifier keys. Consequently, the mask specifically excludes
1956 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1957 * </p>
1958 *
1959 * @return The modifier meta state mask which is a combination of
1960 * {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON}, {@link #META_SHIFT_RIGHT_ON},
1961 * {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON}, {@link #META_ALT_RIGHT_ON},
1962 * {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON}, {@link #META_CTRL_RIGHT_ON},
1963 * {@link #META_META_ON}, {@link #META_META_LEFT_ON}, {@link #META_META_RIGHT_ON},
1964 * {@link #META_SYM_ON}, {@link #META_FUNCTION_ON}.
1965 */
1966 public static int getModifierMetaStateMask() {
1967 return META_MODIFIER_MASK;
1968 }
1969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 /**
1971 * Returns true if this key code is a modifier key.
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001972 * <p>
1973 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1974 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1975 * not considered modifier keys. Consequently, this function return false
1976 * for those keys.
1977 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 *
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001979 * @return True if the key code is one of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 * {@link #KEYCODE_SHIFT_LEFT} {@link #KEYCODE_SHIFT_RIGHT},
Jeff Brown497a92c2010-09-12 17:55:08 -07001981 * {@link #KEYCODE_ALT_LEFT}, {@link #KEYCODE_ALT_RIGHT},
Jeff Brown497a92c2010-09-12 17:55:08 -07001982 * {@link #KEYCODE_CTRL_LEFT}, {@link #KEYCODE_CTRL_RIGHT},
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001983 * {@link #KEYCODE_META_LEFT}, or {@link #KEYCODE_META_RIGHT},
1984 * {@link #KEYCODE_SYM}, {@link #KEYCODE_NUM}, {@link #KEYCODE_FUNCTION}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 */
1986 public static boolean isModifierKey(int keyCode) {
Jeff Brown497a92c2010-09-12 17:55:08 -07001987 switch (keyCode) {
1988 case KEYCODE_SHIFT_LEFT:
1989 case KEYCODE_SHIFT_RIGHT:
1990 case KEYCODE_ALT_LEFT:
1991 case KEYCODE_ALT_RIGHT:
Jeff Brown497a92c2010-09-12 17:55:08 -07001992 case KEYCODE_CTRL_LEFT:
1993 case KEYCODE_CTRL_RIGHT:
1994 case KEYCODE_META_LEFT:
1995 case KEYCODE_META_RIGHT:
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001996 case KEYCODE_SYM:
1997 case KEYCODE_NUM:
1998 case KEYCODE_FUNCTION:
Jeff Brown497a92c2010-09-12 17:55:08 -07001999 return true;
2000 default:
2001 return false;
2002 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 }
2004
2005 /**
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002006 * Normalizes the specified meta state.
2007 * <p>
2008 * The meta state is normalized such that if either the left or right modifier meta state
2009 * bits are set then the result will also include the universal bit for that modifier.
2010 * </p><p>
2011 * If the specified meta state contains {@link #META_ALT_LEFT_ON} then
2012 * the result will also contain {@link #META_ALT_ON} in addition to {@link #META_ALT_LEFT_ON}
2013 * and the other bits that were specified in the input. The same is process is
2014 * performed for shift, control and meta.
2015 * </p><p>
2016 * If the specified meta state contains synthetic meta states defined by
2017 * {@link MetaKeyKeyListener}, then those states are translated here and the original
2018 * synthetic meta states are removed from the result.
2019 * {@link MetaKeyKeyListener#META_CAP_LOCKED} is translated to {@link #META_CAPS_LOCK_ON}.
2020 * {@link MetaKeyKeyListener#META_ALT_LOCKED} is translated to {@link #META_ALT_ON}.
2021 * {@link MetaKeyKeyListener#META_SYM_LOCKED} is translated to {@link #META_SYM_ON}.
2022 * </p><p>
2023 * Undefined meta state bits are removed.
2024 * </p>
2025 *
2026 * @param metaState The meta state.
2027 * @return The normalized meta state.
2028 */
2029 public static int normalizeMetaState(int metaState) {
2030 if ((metaState & (META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON)) != 0) {
2031 metaState |= META_SHIFT_ON;
2032 }
2033 if ((metaState & (META_ALT_LEFT_ON | META_ALT_RIGHT_ON)) != 0) {
2034 metaState |= META_ALT_ON;
2035 }
2036 if ((metaState & (META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON)) != 0) {
2037 metaState |= META_CTRL_ON;
2038 }
2039 if ((metaState & (META_META_LEFT_ON | META_META_RIGHT_ON)) != 0) {
2040 metaState |= META_META_ON;
2041 }
2042 if ((metaState & MetaKeyKeyListener.META_CAP_LOCKED) != 0) {
2043 metaState |= META_CAPS_LOCK_ON;
2044 }
2045 if ((metaState & MetaKeyKeyListener.META_ALT_LOCKED) != 0) {
2046 metaState |= META_ALT_ON;
2047 }
2048 if ((metaState & MetaKeyKeyListener.META_SYM_LOCKED) != 0) {
2049 metaState |= META_SYM_ON;
2050 }
2051 return metaState & META_ALL_MASK;
2052 }
2053
2054 /**
2055 * Returns true if no modifiers keys are pressed according to the specified meta state.
2056 * <p>
2057 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2058 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2059 * not considered modifier keys. Consequently, this function ignores
2060 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2061 * </p><p>
2062 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}.
2063 * </p>
2064 *
2065 * @param metaState The meta state to consider.
2066 * @return True if no modifier keys are pressed.
2067 * @see #hasNoModifiers()
2068 */
2069 public static boolean metaStateHasNoModifiers(int metaState) {
2070 return (normalizeMetaState(metaState) & META_MODIFIER_MASK) == 0;
2071 }
2072
2073 /**
2074 * Returns true if only the specified modifier keys are pressed according to
2075 * the specified meta state. Returns false if a different combination of modifier
2076 * keys are pressed.
2077 * <p>
2078 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2079 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2080 * not considered modifier keys. Consequently, this function ignores
2081 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2082 * </p><p>
2083 * If the specified modifier mask includes directional modifiers, such as
2084 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the
2085 * modifier is pressed on that side.
2086 * If the specified modifier mask includes non-directional modifiers, such as
2087 * {@link #META_SHIFT_ON}, then this method ensures that the modifier
2088 * is pressed on either side.
2089 * If the specified modifier mask includes both directional and non-directional modifiers
2090 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON},
2091 * then this method throws an illegal argument exception.
2092 * </p>
2093 *
2094 * @param metaState The meta state to consider.
2095 * @param modifiers The meta state of the modifier keys to check. May be a combination
2096 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to
2097 * ensure that no modifier keys are pressed.
2098 * @return True if only the specified modifier keys are pressed.
2099 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers
2100 * @see #hasModifiers
2101 */
2102 public static boolean metaStateHasModifiers(int metaState, int modifiers) {
2103 // Note: For forward compatibility, we allow the parameter to contain meta states
2104 // that we do not recognize but we explicitly disallow meta states that
2105 // are not valid modifiers.
2106 if ((modifiers & META_INVALID_MODIFIER_MASK) != 0) {
2107 throw new IllegalArgumentException("modifiers must not contain "
2108 + "META_CAPS_LOCK_ON, META_NUM_LOCK_ON, META_SCROLL_LOCK_ON, "
2109 + "META_CAP_LOCKED, META_ALT_LOCKED, META_SYM_LOCKED, "
2110 + "or META_SELECTING");
2111 }
2112
2113 metaState = normalizeMetaState(metaState) & META_MODIFIER_MASK;
2114 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2115 META_SHIFT_ON, META_SHIFT_LEFT_ON, META_SHIFT_RIGHT_ON);
2116 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2117 META_ALT_ON, META_ALT_LEFT_ON, META_ALT_RIGHT_ON);
2118 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2119 META_CTRL_ON, META_CTRL_LEFT_ON, META_CTRL_RIGHT_ON);
2120 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2121 META_META_ON, META_META_LEFT_ON, META_META_RIGHT_ON);
2122 return metaState == modifiers;
2123 }
2124
2125 private static int metaStateFilterDirectionalModifiers(int metaState,
2126 int modifiers, int basic, int left, int right) {
2127 final boolean wantBasic = (modifiers & basic) != 0;
2128 final int directional = left | right;
2129 final boolean wantLeftOrRight = (modifiers & directional) != 0;
2130
2131 if (wantBasic) {
2132 if (wantLeftOrRight) {
2133 throw new IllegalArgumentException("modifiers must not contain "
2134 + metaStateToString(basic) + " combined with "
2135 + metaStateToString(left) + " or " + metaStateToString(right));
2136 }
2137 return metaState & ~directional;
2138 } else if (wantLeftOrRight) {
2139 return metaState & ~basic;
2140 } else {
2141 return metaState;
2142 }
2143 }
2144
2145 /**
2146 * Returns true if no modifier keys are pressed.
2147 * <p>
2148 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2149 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2150 * not considered modifier keys. Consequently, this function ignores
2151 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2152 * </p><p>
2153 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}.
2154 * </p>
2155 *
2156 * @return True if no modifier keys are pressed.
2157 * @see #metaStateHasNoModifiers
2158 */
2159 public final boolean hasNoModifiers() {
2160 return metaStateHasNoModifiers(mMetaState);
2161 }
2162
2163 /**
2164 * Returns true if only the specified modifiers keys are pressed.
2165 * Returns false if a different combination of modifier keys are pressed.
2166 * <p>
2167 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2168 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2169 * not considered modifier keys. Consequently, this function ignores
2170 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2171 * </p><p>
2172 * If the specified modifier mask includes directional modifiers, such as
2173 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the
2174 * modifier is pressed on that side.
2175 * If the specified modifier mask includes non-directional modifiers, such as
2176 * {@link #META_SHIFT_ON}, then this method ensures that the modifier
2177 * is pressed on either side.
2178 * If the specified modifier mask includes both directional and non-directional modifiers
2179 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON},
2180 * then this method throws an illegal argument exception.
2181 * </p>
2182 *
2183 * @param modifiers The meta state of the modifier keys to check. May be a combination
2184 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to
2185 * ensure that no modifier keys are pressed.
2186 * @return True if only the specified modifier keys are pressed.
2187 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers
2188 * @see #metaStateHasModifiers
2189 */
2190 public final boolean hasModifiers(int modifiers) {
2191 return metaStateHasModifiers(mMetaState, modifiers);
2192 }
2193
2194 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002195 * <p>Returns the pressed state of the ALT meta key.</p>
2196 *
2197 * @return true if the ALT key is pressed, false otherwise
2198 *
2199 * @see #KEYCODE_ALT_LEFT
2200 * @see #KEYCODE_ALT_RIGHT
2201 * @see #META_ALT_ON
2202 */
2203 public final boolean isAltPressed() {
2204 return (mMetaState & META_ALT_ON) != 0;
2205 }
2206
2207 /**
2208 * <p>Returns the pressed state of the SHIFT meta key.</p>
2209 *
2210 * @return true if the SHIFT key is pressed, false otherwise
2211 *
2212 * @see #KEYCODE_SHIFT_LEFT
2213 * @see #KEYCODE_SHIFT_RIGHT
2214 * @see #META_SHIFT_ON
2215 */
2216 public final boolean isShiftPressed() {
2217 return (mMetaState & META_SHIFT_ON) != 0;
2218 }
2219
2220 /**
2221 * <p>Returns the pressed state of the SYM meta key.</p>
2222 *
2223 * @return true if the SYM key is pressed, false otherwise
2224 *
2225 * @see #KEYCODE_SYM
2226 * @see #META_SYM_ON
2227 */
2228 public final boolean isSymPressed() {
2229 return (mMetaState & META_SYM_ON) != 0;
2230 }
2231
2232 /**
Jeff Brown497a92c2010-09-12 17:55:08 -07002233 * <p>Returns the pressed state of the CTRL meta key.</p>
2234 *
2235 * @return true if the CTRL key is pressed, false otherwise
2236 *
2237 * @see #KEYCODE_CTRL_LEFT
2238 * @see #KEYCODE_CTRL_RIGHT
2239 * @see #META_CTRL_ON
2240 */
2241 public final boolean isCtrlPressed() {
2242 return (mMetaState & META_CTRL_ON) != 0;
2243 }
2244
2245 /**
2246 * <p>Returns the pressed state of the META meta key.</p>
2247 *
2248 * @return true if the META key is pressed, false otherwise
2249 *
2250 * @see #KEYCODE_META_LEFT
2251 * @see #KEYCODE_META_RIGHT
2252 * @see #META_META_ON
2253 */
2254 public final boolean isMetaPressed() {
2255 return (mMetaState & META_META_ON) != 0;
2256 }
2257
2258 /**
2259 * <p>Returns the pressed state of the FUNCTION meta key.</p>
2260 *
2261 * @return true if the FUNCTION key is pressed, false otherwise
2262 *
2263 * @see #KEYCODE_FUNCTION
2264 * @see #META_FUNCTION_ON
2265 */
2266 public final boolean isFunctionPressed() {
2267 return (mMetaState & META_FUNCTION_ON) != 0;
2268 }
2269
2270 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07002271 * <p>Returns the locked state of the CAPS LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002272 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002273 * @return true if the CAPS LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002274 *
2275 * @see #KEYCODE_CAPS_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002276 * @see #META_CAPS_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002277 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002278 public final boolean isCapsLockOn() {
2279 return (mMetaState & META_CAPS_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002280 }
2281
2282 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07002283 * <p>Returns the locked state of the NUM LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002284 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002285 * @return true if the NUM LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002286 *
2287 * @see #KEYCODE_NUM_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002288 * @see #META_NUM_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002289 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002290 public final boolean isNumLockOn() {
2291 return (mMetaState & META_NUM_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002292 }
2293
2294 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07002295 * <p>Returns the locked state of the SCROLL LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002296 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002297 * @return true if the SCROLL LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002298 *
2299 * @see #KEYCODE_SCROLL_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002300 * @see #META_SCROLL_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002301 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002302 public final boolean isScrollLockOn() {
2303 return (mMetaState & META_SCROLL_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002304 }
2305
2306 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002307 * Retrieve the action of this key event. May be either
2308 * {@link #ACTION_DOWN}, {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
2309 *
2310 * @return The event action: ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.
2311 */
2312 public final int getAction() {
2313 return mAction;
2314 }
2315
2316 /**
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07002317 * For {@link #ACTION_UP} events, indicates that the event has been
2318 * canceled as per {@link #FLAG_CANCELED}.
2319 */
2320 public final boolean isCanceled() {
2321 return (mFlags&FLAG_CANCELED) != 0;
2322 }
2323
2324 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002325 * Call this during {@link Callback#onKeyDown} to have the system track
2326 * the key through its final up (possibly including a long press). Note
2327 * that only one key can be tracked at a time -- if another key down
2328 * event is received while a previous one is being tracked, tracking is
2329 * stopped on the previous event.
2330 */
2331 public final void startTracking() {
2332 mFlags |= FLAG_START_TRACKING;
2333 }
2334
2335 /**
2336 * For {@link #ACTION_UP} events, indicates that the event is still being
2337 * tracked from its initial down event as per
2338 * {@link #FLAG_TRACKING}.
2339 */
2340 public final boolean isTracking() {
2341 return (mFlags&FLAG_TRACKING) != 0;
2342 }
2343
2344 /**
2345 * For {@link #ACTION_DOWN} events, indicates that the event has been
2346 * canceled as per {@link #FLAG_LONG_PRESS}.
2347 */
2348 public final boolean isLongPress() {
2349 return (mFlags&FLAG_LONG_PRESS) != 0;
2350 }
2351
2352 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 * Retrieve the key code of the key event. This is the physical key that
2354 * was pressed, <em>not</em> the Unicode character.
2355 *
2356 * @return The key code of the event.
2357 */
2358 public final int getKeyCode() {
2359 return mKeyCode;
2360 }
2361
2362 /**
2363 * For the special case of a {@link #ACTION_MULTIPLE} event with key
2364 * code of {@link #KEYCODE_UNKNOWN}, this is a raw string of characters
2365 * associated with the event. In all other cases it is null.
2366 *
2367 * @return Returns a String of 1 or more characters associated with
2368 * the event.
2369 */
2370 public final String getCharacters() {
2371 return mCharacters;
2372 }
2373
2374 /**
2375 * Retrieve the hardware key id of this key event. These values are not
2376 * reliable and vary from device to device.
2377 *
2378 * {@more}
2379 * Mostly this is here for debugging purposes.
2380 */
2381 public final int getScanCode() {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002382 return mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383 }
2384
2385 /**
2386 * Retrieve the repeat count of the event. For both key up and key down
2387 * events, this is the number of times the key has repeated with the first
2388 * down starting at 0 and counting up from there. For multiple key
2389 * events, this is the number of down/up pairs that have occurred.
2390 *
2391 * @return The number of times the key has repeated.
2392 */
2393 public final int getRepeatCount() {
2394 return mRepeatCount;
2395 }
2396
2397 /**
2398 * Retrieve the time of the most recent key down event,
2399 * in the {@link android.os.SystemClock#uptimeMillis} time base. If this
2400 * is a down event, this will be the same as {@link #getEventTime()}.
2401 * Note that when chording keys, this value is the down time of the
2402 * most recently pressed key, which may <em>not</em> be the same physical
2403 * key of this event.
2404 *
2405 * @return Returns the most recent key down time, in the
2406 * {@link android.os.SystemClock#uptimeMillis} time base
2407 */
2408 public final long getDownTime() {
2409 return mDownTime;
2410 }
2411
2412 /**
Jeff Brownb11499d2012-04-20 19:54:22 -07002413 * Retrieve the time this event occurred,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002414 * in the {@link android.os.SystemClock#uptimeMillis} time base.
Jeff Brownb11499d2012-04-20 19:54:22 -07002415 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002416 * @return Returns the time this event occurred,
2417 * in the {@link android.os.SystemClock#uptimeMillis} time base.
2418 */
Jeff Brownb11499d2012-04-20 19:54:22 -07002419 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420 public final long getEventTime() {
2421 return mEventTime;
2422 }
2423
Jeff Brownb11499d2012-04-20 19:54:22 -07002424 /**
2425 * Retrieve the time this event occurred,
2426 * in the {@link android.os.SystemClock#uptimeMillis} time base but with
2427 * nanosecond (instead of millisecond) precision.
2428 * <p>
2429 * The value is in nanosecond precision but it may not have nanosecond accuracy.
2430 * </p>
2431 *
2432 * @return Returns the time this event occurred,
2433 * in the {@link android.os.SystemClock#uptimeMillis} time base but with
2434 * nanosecond (instead of millisecond) precision.
2435 *
2436 * @hide
2437 */
Jeff Brown4e91a182011-04-07 11:38:09 -07002438 @Override
2439 public final long getEventTimeNano() {
2440 return mEventTime * 1000000L;
2441 }
2442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002443 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 * Renamed to {@link #getDeviceId}.
2445 *
2446 * @hide
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002447 * @deprecated use {@link #getDeviceId()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002448 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002449 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002450 public final int getKeyboardDevice() {
2451 return mDeviceId;
2452 }
2453
2454 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002455 * Gets the {@link KeyCharacterMap} associated with the keyboard device.
2456 *
2457 * @return The associated key character map.
Andrew Sapperstein8ab3dc72011-06-23 18:56:44 -07002458 * @throws {@link KeyCharacterMap.UnavailableException} if the key character map
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002459 * could not be loaded because it was malformed or the default key character map
2460 * is missing from the system.
2461 *
Andrew Sapperstein8ab3dc72011-06-23 18:56:44 -07002462 * @see KeyCharacterMap#load
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002463 */
2464 public final KeyCharacterMap getKeyCharacterMap() {
2465 return KeyCharacterMap.load(mDeviceId);
2466 }
2467
2468 /**
2469 * Gets the primary character for this key.
2470 * In other words, the label that is physically printed on it.
2471 *
2472 * @return The display label character, or 0 if none (eg. for non-printing keys).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002473 */
2474 public char getDisplayLabel() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002475 return getKeyCharacterMap().getDisplayLabel(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002476 }
2477
2478 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002479 * Gets the Unicode character generated by the specified key and meta
2480 * key state combination.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 * <p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002482 * Returns the Unicode character that the specified key would produce
2483 * when the specified meta bits (see {@link MetaKeyKeyListener})
2484 * were active.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002485 * </p><p>
2486 * Returns 0 if the key is not one that is used to type Unicode
2487 * characters.
2488 * </p><p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002489 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the
2490 * key is a "dead key" that should be combined with another to
2491 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} --
2492 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 * </p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002494 *
2495 * @return The associated character or combining accent, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002496 */
2497 public int getUnicodeChar() {
2498 return getUnicodeChar(mMetaState);
2499 }
2500
2501 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002502 * Gets the Unicode character generated by the specified key and meta
2503 * key state combination.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 * <p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002505 * Returns the Unicode character that the specified key would produce
2506 * when the specified meta bits (see {@link MetaKeyKeyListener})
2507 * were active.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002508 * </p><p>
2509 * Returns 0 if the key is not one that is used to type Unicode
2510 * characters.
2511 * </p><p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002512 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the
2513 * key is a "dead key" that should be combined with another to
2514 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} --
2515 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516 * </p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002517 *
2518 * @param metaState The meta key modifier state.
2519 * @return The associated character or combining accent, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002521 public int getUnicodeChar(int metaState) {
2522 return getKeyCharacterMap().get(mKeyCode, metaState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002523 }
2524
2525 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002526 * Get the character conversion data for a given key code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002527 *
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002528 * @param results A {@link KeyCharacterMap.KeyData} instance that will be
2529 * filled with the results.
2530 * @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 -08002531 *
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002532 * @deprecated instead use {@link #getDisplayLabel()},
2533 * {@link #getNumber()} or {@link #getUnicodeChar(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002535 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002536 public boolean getKeyData(KeyData results) {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002537 return getKeyCharacterMap().getKeyData(mKeyCode, results);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538 }
2539
2540 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002541 * Gets the first character in the character array that can be generated
2542 * by the specified key code.
2543 * <p>
2544 * This is a convenience function that returns the same value as
2545 * {@link #getMatch(char[],int) getMatch(chars, 0)}.
2546 * </p>
2547 *
2548 * @param chars The array of matching characters to consider.
2549 * @return The matching associated character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 */
2551 public char getMatch(char[] chars) {
2552 return getMatch(chars, 0);
2553 }
2554
2555 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002556 * Gets the first character in the character array that can be generated
2557 * by the specified key code. If there are multiple choices, prefers
2558 * the one that would be generated with the specified meta key modifier state.
2559 *
2560 * @param chars The array of matching characters to consider.
2561 * @param metaState The preferred meta key modifier state.
2562 * @return The matching associated character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002563 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002564 public char getMatch(char[] chars, int metaState) {
2565 return getKeyCharacterMap().getMatch(mKeyCode, chars, metaState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002566 }
2567
2568 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002569 * Gets the number or symbol associated with the key.
2570 * <p>
2571 * The character value is returned, not the numeric value.
2572 * If the key is not a number, but is a symbol, the symbol is retuned.
2573 * </p><p>
2574 * This method is intended to to support dial pads and other numeric or
2575 * symbolic entry on keyboards where certain keys serve dual function
2576 * as alphabetic and symbolic keys. This method returns the number
2577 * or symbol associated with the key independent of whether the user
2578 * has pressed the required modifier.
2579 * </p><p>
2580 * For example, on one particular keyboard the keys on the top QWERTY row generate
2581 * numbers when ALT is pressed such that ALT-Q maps to '1'. So for that keyboard
2582 * when {@link #getNumber} is called with {@link KeyEvent#KEYCODE_Q} it returns '1'
2583 * so that the user can type numbers without pressing ALT when it makes sense.
2584 * </p>
2585 *
2586 * @return The associated numeric or symbolic character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 */
2588 public char getNumber() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002589 return getKeyCharacterMap().getNumber(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002590 }
2591
2592 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002593 * Returns true if this key produces a glyph.
2594 *
2595 * @return True if the key is a printing key.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 */
2597 public boolean isPrintingKey() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002598 return getKeyCharacterMap().isPrintingKey(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002599 }
2600
2601 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002602 * @deprecated Use {@link #dispatch(Callback, DispatcherState, Object)} instead.
2603 */
2604 @Deprecated
2605 public final boolean dispatch(Callback receiver) {
2606 return dispatch(receiver, null, null);
2607 }
2608
2609 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002610 * Deliver this key event to a {@link Callback} interface. If this is
2611 * an ACTION_MULTIPLE event and it is not handled, then an attempt will
2612 * be made to deliver a single normal event.
2613 *
2614 * @param receiver The Callback that will be given the event.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002615 * @param state State information retained across events.
2616 * @param target The target of the dispatch, for use in tracking.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617 *
2618 * @return The return value from the Callback method that was called.
2619 */
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002620 public final boolean dispatch(Callback receiver, DispatcherState state,
2621 Object target) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002622 switch (mAction) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002623 case ACTION_DOWN: {
2624 mFlags &= ~FLAG_START_TRACKING;
Dianne Hackborn8d374262009-09-14 21:21:52 -07002625 if (DEBUG) Log.v(TAG, "Key down to " + target + " in " + state
2626 + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002627 boolean res = receiver.onKeyDown(mKeyCode, this);
2628 if (state != null) {
2629 if (res && mRepeatCount == 0 && (mFlags&FLAG_START_TRACKING) != 0) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002630 if (DEBUG) Log.v(TAG, " Start tracking!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002631 state.startTracking(this, target);
2632 } else if (isLongPress() && state.isTracking(this)) {
2633 try {
2634 if (receiver.onKeyLongPress(mKeyCode, this)) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002635 if (DEBUG) Log.v(TAG, " Clear from long press!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002636 state.performedLongPress(this);
2637 res = true;
2638 }
2639 } catch (AbstractMethodError e) {
2640 }
2641 }
2642 }
2643 return res;
2644 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 case ACTION_UP:
Dianne Hackborn8d374262009-09-14 21:21:52 -07002646 if (DEBUG) Log.v(TAG, "Key up to " + target + " in " + state
2647 + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002648 if (state != null) {
2649 state.handleUpEvent(this);
2650 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002651 return receiver.onKeyUp(mKeyCode, this);
2652 case ACTION_MULTIPLE:
2653 final int count = mRepeatCount;
2654 final int code = mKeyCode;
2655 if (receiver.onKeyMultiple(code, count, this)) {
2656 return true;
2657 }
2658 if (code != KeyEvent.KEYCODE_UNKNOWN) {
2659 mAction = ACTION_DOWN;
2660 mRepeatCount = 0;
2661 boolean handled = receiver.onKeyDown(code, this);
2662 if (handled) {
2663 mAction = ACTION_UP;
2664 receiver.onKeyUp(code, this);
2665 }
2666 mAction = ACTION_MULTIPLE;
2667 mRepeatCount = count;
2668 return handled;
2669 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002670 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002671 }
2672 return false;
2673 }
2674
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002675 /**
2676 * Use with {@link KeyEvent#dispatch(Callback, DispatcherState, Object)}
2677 * for more advanced key dispatching, such as long presses.
2678 */
2679 public static class DispatcherState {
2680 int mDownKeyCode;
2681 Object mDownTarget;
2682 SparseIntArray mActiveLongPresses = new SparseIntArray();
2683
2684 /**
2685 * Reset back to initial state.
2686 */
2687 public void reset() {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002688 if (DEBUG) Log.v(TAG, "Reset: " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002689 mDownKeyCode = 0;
2690 mDownTarget = null;
2691 mActiveLongPresses.clear();
2692 }
2693
2694 /**
2695 * Stop any tracking associated with this target.
2696 */
2697 public void reset(Object target) {
2698 if (mDownTarget == target) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002699 if (DEBUG) Log.v(TAG, "Reset in " + target + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002700 mDownKeyCode = 0;
2701 mDownTarget = null;
2702 }
2703 }
2704
2705 /**
2706 * Start tracking the key code associated with the given event. This
2707 * can only be called on a key down. It will allow you to see any
2708 * long press associated with the key, and will result in
2709 * {@link KeyEvent#isTracking} return true on the long press and up
2710 * events.
2711 *
2712 * <p>This is only needed if you are directly dispatching events, rather
2713 * than handling them in {@link Callback#onKeyDown}.
2714 */
2715 public void startTracking(KeyEvent event, Object target) {
2716 if (event.getAction() != ACTION_DOWN) {
2717 throw new IllegalArgumentException(
2718 "Can only start tracking on a down event");
2719 }
Dianne Hackborn8d374262009-09-14 21:21:52 -07002720 if (DEBUG) Log.v(TAG, "Start trackingt in " + target + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002721 mDownKeyCode = event.getKeyCode();
2722 mDownTarget = target;
2723 }
2724
2725 /**
2726 * Return true if the key event is for a key code that is currently
2727 * being tracked by the dispatcher.
2728 */
2729 public boolean isTracking(KeyEvent event) {
2730 return mDownKeyCode == event.getKeyCode();
2731 }
2732
2733 /**
2734 * Keep track of the given event's key code as having performed an
2735 * action with a long press, so no action should occur on the up.
2736 * <p>This is only needed if you are directly dispatching events, rather
2737 * than handling them in {@link Callback#onKeyLongPress}.
2738 */
2739 public void performedLongPress(KeyEvent event) {
2740 mActiveLongPresses.put(event.getKeyCode(), 1);
2741 }
2742
2743 /**
2744 * Handle key up event to stop tracking. This resets the dispatcher state,
2745 * and updates the key event state based on it.
2746 * <p>This is only needed if you are directly dispatching events, rather
2747 * than handling them in {@link Callback#onKeyUp}.
2748 */
2749 public void handleUpEvent(KeyEvent event) {
2750 final int keyCode = event.getKeyCode();
Dianne Hackborn8d374262009-09-14 21:21:52 -07002751 if (DEBUG) Log.v(TAG, "Handle key up " + event + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002752 int index = mActiveLongPresses.indexOfKey(keyCode);
2753 if (index >= 0) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002754 if (DEBUG) Log.v(TAG, " Index: " + index);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002755 event.mFlags |= FLAG_CANCELED | FLAG_CANCELED_LONG_PRESS;
2756 mActiveLongPresses.removeAt(index);
2757 }
2758 if (mDownKeyCode == keyCode) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002759 if (DEBUG) Log.v(TAG, " Tracking!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002760 event.mFlags |= FLAG_TRACKING;
2761 mDownKeyCode = 0;
2762 mDownTarget = null;
2763 }
2764 }
2765 }
Jeff Brown497a92c2010-09-12 17:55:08 -07002766
2767 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002768 public String toString() {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002769 StringBuilder msg = new StringBuilder();
2770 msg.append("KeyEvent { action=").append(actionToString(mAction));
2771 msg.append(", keyCode=").append(keyCodeToString(mKeyCode));
2772 msg.append(", scanCode=").append(mScanCode);
2773 if (mCharacters != null) {
2774 msg.append(", characters=\"").append(mCharacters).append("\"");
2775 }
2776 msg.append(", metaState=").append(metaStateToString(mMetaState));
2777 msg.append(", flags=0x").append(Integer.toHexString(mFlags));
2778 msg.append(", repeatCount=").append(mRepeatCount);
2779 msg.append(", eventTime=").append(mEventTime);
2780 msg.append(", downTime=").append(mDownTime);
2781 msg.append(", deviceId=").append(mDeviceId);
2782 msg.append(", source=0x").append(Integer.toHexString(mSource));
2783 msg.append(" }");
2784 return msg.toString();
Jeff Brown497a92c2010-09-12 17:55:08 -07002785 }
2786
2787 /**
2788 * Returns a string that represents the symbolic name of the specified action
Jeff Brown6f2fba42011-02-19 01:08:02 -08002789 * such as "ACTION_DOWN", or an equivalent numeric constant such as "35" if unknown.
Jeff Brown497a92c2010-09-12 17:55:08 -07002790 *
2791 * @param action The action.
2792 * @return The symbolic name of the specified action.
2793 * @hide
2794 */
2795 public static String actionToString(int action) {
2796 switch (action) {
2797 case ACTION_DOWN:
2798 return "ACTION_DOWN";
2799 case ACTION_UP:
2800 return "ACTION_UP";
2801 case ACTION_MULTIPLE:
2802 return "ACTION_MULTIPLE";
2803 default:
2804 return Integer.toString(action);
2805 }
2806 }
2807
2808 /**
2809 * Returns a string that represents the symbolic name of the specified keycode
Jeff Brown6f2fba42011-02-19 01:08:02 -08002810 * such as "KEYCODE_A", "KEYCODE_DPAD_UP", or an equivalent numeric constant
2811 * such as "1001" if unknown.
Jeff Brown497a92c2010-09-12 17:55:08 -07002812 *
2813 * @param keyCode The key code.
2814 * @return The symbolic name of the specified keycode.
2815 *
2816 * @see KeyCharacterMap#getDisplayLabel
Jeff Brown497a92c2010-09-12 17:55:08 -07002817 */
2818 public static String keyCodeToString(int keyCode) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08002819 String symbolicName = KEYCODE_SYMBOLIC_NAMES.get(keyCode);
2820 return symbolicName != null ? symbolicName : Integer.toString(keyCode);
Jeff Brown497a92c2010-09-12 17:55:08 -07002821 }
2822
2823 /**
Jeff Brown6f2fba42011-02-19 01:08:02 -08002824 * Gets a keycode by its symbolic name such as "KEYCODE_A" or an equivalent
2825 * numeric constant such as "1001".
Jeff Brown497a92c2010-09-12 17:55:08 -07002826 *
2827 * @param symbolicName The symbolic name of the keycode.
Jeff Brown6f2fba42011-02-19 01:08:02 -08002828 * @return The keycode or {@link #KEYCODE_UNKNOWN} if not found.
Michael Wright072137c2013-04-24 20:41:20 -07002829 * @see #keycodeToString(int)
Jeff Brown497a92c2010-09-12 17:55:08 -07002830 */
2831 public static int keyCodeFromString(String symbolicName) {
2832 if (symbolicName == null) {
2833 throw new IllegalArgumentException("symbolicName must not be null");
2834 }
2835
Jeff Brown6f2fba42011-02-19 01:08:02 -08002836 final int count = KEYCODE_SYMBOLIC_NAMES.size();
Jeff Brown497a92c2010-09-12 17:55:08 -07002837 for (int i = 0; i < count; i++) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08002838 if (symbolicName.equals(KEYCODE_SYMBOLIC_NAMES.valueAt(i))) {
Jeff Brown497a92c2010-09-12 17:55:08 -07002839 return i;
2840 }
2841 }
2842
2843 try {
Jeff Brown6f2fba42011-02-19 01:08:02 -08002844 return Integer.parseInt(symbolicName, 10);
Jeff Brown497a92c2010-09-12 17:55:08 -07002845 } catch (NumberFormatException ex) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08002846 return KEYCODE_UNKNOWN;
Jeff Brown497a92c2010-09-12 17:55:08 -07002847 }
2848 }
2849
2850 /**
2851 * Returns a string that represents the symbolic name of the specified combined meta
2852 * key modifier state flags such as "0", "META_SHIFT_ON",
Jeff Brown6f2fba42011-02-19 01:08:02 -08002853 * "META_ALT_ON|META_SHIFT_ON" or an equivalent numeric constant such as "0x10000000"
2854 * if unknown.
Jeff Brown497a92c2010-09-12 17:55:08 -07002855 *
2856 * @param metaState The meta state.
2857 * @return The symbolic name of the specified combined meta state flags.
2858 * @hide
2859 */
2860 public static String metaStateToString(int metaState) {
2861 if (metaState == 0) {
2862 return "0";
2863 }
2864 StringBuilder result = null;
2865 int i = 0;
2866 while (metaState != 0) {
2867 final boolean isSet = (metaState & 1) != 0;
2868 metaState >>>= 1; // unsigned shift!
2869 if (isSet) {
2870 final String name = META_SYMBOLIC_NAMES[i];
2871 if (result == null) {
2872 if (metaState == 0) {
2873 return name;
2874 }
2875 result = new StringBuilder(name);
2876 } else {
2877 result.append('|');
2878 result.append(name);
2879 }
2880 }
2881 i += 1;
2882 }
2883 return result.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002884 }
2885
2886 public static final Parcelable.Creator<KeyEvent> CREATOR
2887 = new Parcelable.Creator<KeyEvent>() {
2888 public KeyEvent createFromParcel(Parcel in) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07002889 in.readInt(); // skip token, we already know this is a KeyEvent
2890 return KeyEvent.createFromParcelBody(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002891 }
2892
2893 public KeyEvent[] newArray(int size) {
2894 return new KeyEvent[size];
2895 }
2896 };
Jeff Brown6ec402b2010-07-28 15:48:59 -07002897
2898 /** @hide */
2899 public static KeyEvent createFromParcelBody(Parcel in) {
2900 return new KeyEvent(in);
2901 }
2902
2903 private KeyEvent(Parcel in) {
Jeff Brown91c69ab2011-02-14 17:03:18 -08002904 mDeviceId = in.readInt();
2905 mSource = in.readInt();
Jeff Brown6ec402b2010-07-28 15:48:59 -07002906 mAction = in.readInt();
2907 mKeyCode = in.readInt();
2908 mRepeatCount = in.readInt();
2909 mMetaState = in.readInt();
2910 mScanCode = in.readInt();
2911 mFlags = in.readInt();
2912 mDownTime = in.readLong();
2913 mEventTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 }
2915
2916 public void writeToParcel(Parcel out, int flags) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07002917 out.writeInt(PARCEL_TOKEN_KEY_EVENT);
Jeff Brown91c69ab2011-02-14 17:03:18 -08002918
2919 out.writeInt(mDeviceId);
2920 out.writeInt(mSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002921 out.writeInt(mAction);
2922 out.writeInt(mKeyCode);
2923 out.writeInt(mRepeatCount);
2924 out.writeInt(mMetaState);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002925 out.writeInt(mScanCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002926 out.writeInt(mFlags);
2927 out.writeLong(mDownTime);
2928 out.writeLong(mEventTime);
2929 }
2930
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -07002931 private native boolean native_isSystemKey(int keyCode);
2932 private native boolean native_hasDefaultAction(int keyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002933}