blob: e0c6770ecb38cc188ba0b58fa9b82601f7595ea5 [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;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070023import android.util.SparseIntArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.view.KeyCharacterMap;
25import android.view.KeyCharacterMap.KeyData;
26
27/**
Jeff Browndc1ab4b2010-09-14 18:03:38 -070028 * Object used to report key and button events.
29 * <p>
30 * Each key press is described by a sequence of key events. A key press
31 * starts with a key event with {@link #ACTION_DOWN}. If the key is held
32 * sufficiently long that it repeats, then the initial down is followed
33 * additional key events with {@link #ACTION_DOWN} and a non-zero value for
34 * {@link #getRepeatCount()}. The last key event is a {@link #ACTION_UP}
35 * for the key up. If the key press is canceled, the key up event will have the
36 * {@link #FLAG_CANCELED} flag set.
37 * </p><p>
38 * Key events are generally accompanied by a key code ({@link #getKeyCode()}),
39 * scan code ({@link #getScanCode()}) and meta state ({@link #getMetaState()}).
40 * Key code constants are defined in this class. Scan code constants are raw
41 * device-specific codes obtained from the OS and so are not generally meaningful
42 * to applications unless interpreted using the {@link KeyCharacterMap}.
43 * Meta states describe the pressed state of key modifiers
44 * such as {@link #META_SHIFT_ON} or {@link #META_ALT_ON}.
45 * </p><p>
Jeff Brown497a92c2010-09-12 17:55:08 -070046 * Key codes typically correspond one-to-one with individual keys on an input device.
47 * Many keys and key combinations serve quite different functions on different
48 * input devices so care must be taken when interpreting them. Always use the
49 * {@link KeyCharacterMap} associated with the input device when mapping keys
50 * to characters. Be aware that there may be multiple key input devices active
51 * at the same time and each will have its own key character map.
52 * </p><p>
Jean Chalard405bc512012-05-29 19:12:34 +090053 * As soft input methods can use multiple and inventive ways of inputting text,
54 * there is no guarantee that any key press on a soft keyboard will generate a key
55 * event: this is left to the IME's discretion, and in fact sending such events is
56 * discouraged. You should never rely on receiving KeyEvents for any key on a soft
57 * input method. In particular, the default software keyboard will never send any
58 * key event to any application targetting Jelly Bean or later, and will only send
59 * events for some presses of the delete and return keys to applications targetting
60 * Ice Cream Sandwich or earlier. Be aware that other software input methods may
61 * never send key events regardless of the version. Consider using editor actions
62 * like {@link android.view.inputmethod.EditorInfo#IME_ACTION_DONE} if you need
63 * specific interaction with the software keyboard, as it gives more visibility to
64 * the user as to how your application will react to key presses.
65 * </p><p>
Jeff Browndc1ab4b2010-09-14 18:03:38 -070066 * When interacting with an IME, the framework may deliver key events
67 * with the special action {@link #ACTION_MULTIPLE} that either specifies
68 * that single repeated key code or a sequence of characters to insert.
69 * </p><p>
Jeff Brownb6997262010-10-08 22:31:17 -070070 * In general, the framework cannot guarantee that the key events it delivers
71 * to a view always constitute complete key sequences since some events may be dropped
72 * or modified by containing views before they are delivered. The view implementation
73 * should be prepared to handle {@link #FLAG_CANCELED} and should tolerate anomalous
74 * situations such as receiving a new {@link #ACTION_DOWN} without first having
75 * received an {@link #ACTION_UP} for the prior key press.
Jeff Browndc1ab4b2010-09-14 18:03:38 -070076 * </p><p>
77 * Refer to {@link InputDevice} for more information about how different kinds of
78 * input devices and sources represent keys and buttons.
79 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 */
Jeff Brownc5ed5912010-07-14 18:48:53 -070081public class KeyEvent extends InputEvent implements Parcelable {
Jeff Browndc1ab4b2010-09-14 18:03:38 -070082 /** Key code constant: Unknown key code. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 public static final int KEYCODE_UNKNOWN = 0;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070084 /** Key code constant: Soft Left key.
85 * Usually situated below the display on phones and used as a multi-function
86 * feature key for selecting a software defined function shown on the bottom left
87 * of the display. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 public static final int KEYCODE_SOFT_LEFT = 1;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070089 /** Key code constant: Soft Right key.
90 * Usually situated below the display on phones and used as a multi-function
91 * feature key for selecting a software defined function shown on the bottom right
92 * of the display. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 public static final int KEYCODE_SOFT_RIGHT = 2;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070094 /** Key code constant: Home key.
95 * This key is handled by the framework and is never delivered to applications. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 public static final int KEYCODE_HOME = 3;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070097 /** Key code constant: Back key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 public static final int KEYCODE_BACK = 4;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070099 /** Key code constant: Call key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 public static final int KEYCODE_CALL = 5;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700101 /** Key code constant: End Call key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 public static final int KEYCODE_ENDCALL = 6;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700103 /** Key code constant: '0' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 public static final int KEYCODE_0 = 7;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700105 /** Key code constant: '1' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 public static final int KEYCODE_1 = 8;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700107 /** Key code constant: '2' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 public static final int KEYCODE_2 = 9;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700109 /** Key code constant: '3' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 public static final int KEYCODE_3 = 10;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700111 /** Key code constant: '4' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 public static final int KEYCODE_4 = 11;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700113 /** Key code constant: '5' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 public static final int KEYCODE_5 = 12;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700115 /** Key code constant: '6' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 public static final int KEYCODE_6 = 13;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700117 /** Key code constant: '7' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 public static final int KEYCODE_7 = 14;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700119 /** Key code constant: '8' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 public static final int KEYCODE_8 = 15;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700121 /** Key code constant: '9' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 public static final int KEYCODE_9 = 16;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700123 /** Key code constant: '*' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 public static final int KEYCODE_STAR = 17;
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_POUND = 18;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700127 /** Key code constant: Directional Pad Up key.
128 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 public static final int KEYCODE_DPAD_UP = 19;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700130 /** Key code constant: Directional Pad Down key.
131 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 public static final int KEYCODE_DPAD_DOWN = 20;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700133 /** Key code constant: Directional Pad Left key.
134 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 public static final int KEYCODE_DPAD_LEFT = 21;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700136 /** Key code constant: Directional Pad Right key.
137 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 public static final int KEYCODE_DPAD_RIGHT = 22;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700139 /** Key code constant: Directional Pad Center key.
140 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 public static final int KEYCODE_DPAD_CENTER = 23;
Jeff Brownb0418da2010-11-01 15:24:01 -0700142 /** Key code constant: Volume Up key.
143 * Adjusts the speaker volume up. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 public static final int KEYCODE_VOLUME_UP = 24;
Jeff Brownb0418da2010-11-01 15:24:01 -0700145 /** Key code constant: Volume Down key.
146 * Adjusts the speaker volume down. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 public static final int KEYCODE_VOLUME_DOWN = 25;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700148 /** Key code constant: Power key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 public static final int KEYCODE_POWER = 26;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700150 /** Key code constant: Camera key.
151 * Used to launch a camera application or take pictures. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 public static final int KEYCODE_CAMERA = 27;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700153 /** Key code constant: Clear key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 public static final int KEYCODE_CLEAR = 28;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700155 /** Key code constant: 'A' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 public static final int KEYCODE_A = 29;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700157 /** Key code constant: 'B' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 public static final int KEYCODE_B = 30;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700159 /** Key code constant: 'C' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 public static final int KEYCODE_C = 31;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700161 /** Key code constant: 'D' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 public static final int KEYCODE_D = 32;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700163 /** Key code constant: 'E' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 public static final int KEYCODE_E = 33;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700165 /** Key code constant: 'F' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 public static final int KEYCODE_F = 34;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700167 /** Key code constant: 'G' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 public static final int KEYCODE_G = 35;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700169 /** Key code constant: 'H' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 public static final int KEYCODE_H = 36;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700171 /** Key code constant: 'I' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 public static final int KEYCODE_I = 37;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700173 /** Key code constant: 'J' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 public static final int KEYCODE_J = 38;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700175 /** Key code constant: 'K' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 public static final int KEYCODE_K = 39;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700177 /** Key code constant: 'L' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 public static final int KEYCODE_L = 40;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700179 /** Key code constant: 'M' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 public static final int KEYCODE_M = 41;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700181 /** Key code constant: 'N' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 public static final int KEYCODE_N = 42;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700183 /** Key code constant: 'O' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 public static final int KEYCODE_O = 43;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700185 /** Key code constant: 'P' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 public static final int KEYCODE_P = 44;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700187 /** Key code constant: 'Q' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 public static final int KEYCODE_Q = 45;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700189 /** Key code constant: 'R' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 public static final int KEYCODE_R = 46;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700191 /** Key code constant: 'S' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 public static final int KEYCODE_S = 47;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700193 /** Key code constant: 'T' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 public static final int KEYCODE_T = 48;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700195 /** Key code constant: 'U' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 public static final int KEYCODE_U = 49;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700197 /** Key code constant: 'V' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 public static final int KEYCODE_V = 50;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700199 /** Key code constant: 'W' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 public static final int KEYCODE_W = 51;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700201 /** Key code constant: 'X' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 public static final int KEYCODE_X = 52;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700203 /** Key code constant: 'Y' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 public static final int KEYCODE_Y = 53;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700205 /** Key code constant: 'Z' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 public static final int KEYCODE_Z = 54;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700207 /** Key code constant: ',' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 public static final int KEYCODE_COMMA = 55;
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_PERIOD = 56;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700211 /** Key code constant: Left Alt modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 public static final int KEYCODE_ALT_LEFT = 57;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700213 /** Key code constant: Right Alt modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 public static final int KEYCODE_ALT_RIGHT = 58;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700215 /** Key code constant: Left Shift modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 public static final int KEYCODE_SHIFT_LEFT = 59;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700217 /** Key code constant: Right Shift modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 public static final int KEYCODE_SHIFT_RIGHT = 60;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700219 /** Key code constant: Tab key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 public static final int KEYCODE_TAB = 61;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700221 /** Key code constant: Space key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 public static final int KEYCODE_SPACE = 62;
Jeff Brown224d4a12010-10-07 20:28:53 -0700223 /** Key code constant: Symbol modifier key.
224 * Used to enter alternate symbols. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 public static final int KEYCODE_SYM = 63;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700226 /** Key code constant: Explorer special function key.
227 * Used to launch a browser application. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 public static final int KEYCODE_EXPLORER = 64;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700229 /** Key code constant: Envelope special function key.
230 * Used to launch a mail application. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 public static final int KEYCODE_ENVELOPE = 65;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700232 /** Key code constant: Enter key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 public static final int KEYCODE_ENTER = 66;
Jeff Brown224d4a12010-10-07 20:28:53 -0700234 /** Key code constant: Backspace key.
Jeff Brown497a92c2010-09-12 17:55:08 -0700235 * Deletes characters before the insertion point, unlike {@link #KEYCODE_FORWARD_DEL}. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 public static final int KEYCODE_DEL = 67;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700237 /** Key code constant: '`' (backtick) key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 public static final int KEYCODE_GRAVE = 68;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700239 /** Key code constant: '-'. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 public static final int KEYCODE_MINUS = 69;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700241 /** Key code constant: '=' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 public static final int KEYCODE_EQUALS = 70;
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_LEFT_BRACKET = 71;
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_RIGHT_BRACKET = 72;
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_BACKSLASH = 73;
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_SEMICOLON = 74;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700251 /** Key code constant: ''' (apostrophe) key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 public static final int KEYCODE_APOSTROPHE = 75;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700253 /** Key code constant: '/' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 public static final int KEYCODE_SLASH = 76;
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_AT = 77;
Jeff Brown224d4a12010-10-07 20:28:53 -0700257 /** Key code constant: Number modifier key.
258 * Used to enter numeric symbols.
259 * This key is not Num Lock; it is more like {@link #KEYCODE_ALT_LEFT} and is
260 * interpreted as an ALT key by {@link android.text.method.MetaKeyKeyListener}. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 public static final int KEYCODE_NUM = 78;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700262 /** Key code constant: Headset Hook key.
263 * Used to hang up calls and stop media. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 public static final int KEYCODE_HEADSETHOOK = 79;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700265 /** Key code constant: Camera Focus key.
266 * Used to focus the camera. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 public static final int KEYCODE_FOCUS = 80; // *Camera* focus
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700268 /** Key code constant: '+' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 public static final int KEYCODE_PLUS = 81;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700270 /** Key code constant: Menu key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 public static final int KEYCODE_MENU = 82;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700272 /** Key code constant: Notification key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 public static final int KEYCODE_NOTIFICATION = 83;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700274 /** Key code constant: Search key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 public static final int KEYCODE_SEARCH = 84;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700276 /** Key code constant: Play/Pause media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700277 public static final int KEYCODE_MEDIA_PLAY_PAUSE= 85;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700278 /** Key code constant: Stop media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700279 public static final int KEYCODE_MEDIA_STOP = 86;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700280 /** Key code constant: Play Next media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700281 public static final int KEYCODE_MEDIA_NEXT = 87;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700282 /** Key code constant: Play Previous media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700283 public static final int KEYCODE_MEDIA_PREVIOUS = 88;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700284 /** Key code constant: Rewind media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700285 public static final int KEYCODE_MEDIA_REWIND = 89;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700286 /** Key code constant: Fast Forward media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700287 public static final int KEYCODE_MEDIA_FAST_FORWARD = 90;
Jeff Brownb0418da2010-11-01 15:24:01 -0700288 /** Key code constant: Mute key.
289 * Mutes the microphone, unlike {@link #KEYCODE_VOLUME_MUTE}. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 public static final int KEYCODE_MUTE = 91;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700291 /** Key code constant: Page Up key. */
Chih-Wei Huang4fedd802009-05-27 15:52:50 +0800292 public static final int KEYCODE_PAGE_UP = 92;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700293 /** Key code constant: Page Down key. */
Chih-Wei Huang4fedd802009-05-27 15:52:50 +0800294 public static final int KEYCODE_PAGE_DOWN = 93;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700295 /** Key code constant: Picture Symbols modifier key.
296 * Used to switch symbol sets (Emoji, Kao-moji). */
mogimob032bc02009-10-03 03:13:56 +0900297 public static final int KEYCODE_PICTSYMBOLS = 94; // switch symbol-sets (Emoji,Kao-moji)
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700298 /** Key code constant: Switch Charset modifier key.
299 * Used to switch character sets (Kanji, Katakana). */
mogimob032bc02009-10-03 03:13:56 +0900300 public static final int KEYCODE_SWITCH_CHARSET = 95; // switch char-sets (Kanji,Katakana)
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700301 /** Key code constant: A Button key.
302 * On a game controller, the A button should be either the button labeled A
Michael Wright6b57bde2013-01-28 20:35:58 -0800303 * or the first button on the bottom row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700304 public static final int KEYCODE_BUTTON_A = 96;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700305 /** Key code constant: B Button key.
306 * On a game controller, the B button should be either the button labeled B
Michael Wright6b57bde2013-01-28 20:35:58 -0800307 * or the second button on the bottom row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700308 public static final int KEYCODE_BUTTON_B = 97;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700309 /** Key code constant: C Button key.
310 * On a game controller, the C button should be either the button labeled C
Michael Wright6b57bde2013-01-28 20:35:58 -0800311 * or the third button on the bottom row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700312 public static final int KEYCODE_BUTTON_C = 98;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700313 /** Key code constant: X Button key.
314 * On a game controller, the X button should be either the button labeled X
Michael Wright6b57bde2013-01-28 20:35:58 -0800315 * or the first button on the upper row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700316 public static final int KEYCODE_BUTTON_X = 99;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700317 /** Key code constant: Y Button key.
318 * On a game controller, the Y button should be either the button labeled Y
Michael Wright6b57bde2013-01-28 20:35:58 -0800319 * or the second button on the upper row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700320 public static final int KEYCODE_BUTTON_Y = 100;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700321 /** Key code constant: Z Button key.
322 * On a game controller, the Z button should be either the button labeled Z
Michael Wright6b57bde2013-01-28 20:35:58 -0800323 * or the third button on the upper row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700324 public static final int KEYCODE_BUTTON_Z = 101;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700325 /** Key code constant: L1 Button key.
326 * On a game controller, the L1 button should be either the button labeled L1 (or L)
327 * or the top left trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700328 public static final int KEYCODE_BUTTON_L1 = 102;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700329 /** Key code constant: R1 Button key.
330 * On a game controller, the R1 button should be either the button labeled R1 (or R)
331 * or the top right trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700332 public static final int KEYCODE_BUTTON_R1 = 103;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700333 /** Key code constant: L2 Button key.
334 * On a game controller, the L2 button should be either the button labeled L2
335 * or the bottom left trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700336 public static final int KEYCODE_BUTTON_L2 = 104;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700337 /** Key code constant: R2 Button key.
338 * On a game controller, the R2 button should be either the button labeled R2
339 * or the bottom right trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700340 public static final int KEYCODE_BUTTON_R2 = 105;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700341 /** Key code constant: Left Thumb Button key.
342 * On a game controller, the left thumb button indicates that the left (or only)
343 * joystick is pressed. */
Jeff Brownfd035822010-06-30 16:10:35 -0700344 public static final int KEYCODE_BUTTON_THUMBL = 106;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700345 /** Key code constant: Right Thumb Button key.
346 * On a game controller, the right thumb button indicates that the right
347 * joystick is pressed. */
Jeff Brownfd035822010-06-30 16:10:35 -0700348 public static final int KEYCODE_BUTTON_THUMBR = 107;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700349 /** Key code constant: Start Button key.
350 * On a game controller, the button labeled Start. */
Jeff Brownfd035822010-06-30 16:10:35 -0700351 public static final int KEYCODE_BUTTON_START = 108;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700352 /** Key code constant: Select Button key.
353 * On a game controller, the button labeled Select. */
Jeff Brownfd035822010-06-30 16:10:35 -0700354 public static final int KEYCODE_BUTTON_SELECT = 109;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700355 /** Key code constant: Mode Button key.
356 * On a game controller, the button labeled Mode. */
Jeff Brownfd035822010-06-30 16:10:35 -0700357 public static final int KEYCODE_BUTTON_MODE = 110;
Jeff Brown497a92c2010-09-12 17:55:08 -0700358 /** Key code constant: Escape key. */
359 public static final int KEYCODE_ESCAPE = 111;
360 /** Key code constant: Forward Delete key.
361 * Deletes characters ahead of the insertion point, unlike {@link #KEYCODE_DEL}. */
362 public static final int KEYCODE_FORWARD_DEL = 112;
363 /** Key code constant: Left Control modifier key. */
364 public static final int KEYCODE_CTRL_LEFT = 113;
365 /** Key code constant: Right Control modifier key. */
366 public static final int KEYCODE_CTRL_RIGHT = 114;
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800367 /** Key code constant: Caps Lock key. */
Jeff Brown497a92c2010-09-12 17:55:08 -0700368 public static final int KEYCODE_CAPS_LOCK = 115;
369 /** Key code constant: Scroll Lock key. */
370 public static final int KEYCODE_SCROLL_LOCK = 116;
371 /** Key code constant: Left Meta modifier key. */
372 public static final int KEYCODE_META_LEFT = 117;
373 /** Key code constant: Right Meta modifier key. */
374 public static final int KEYCODE_META_RIGHT = 118;
375 /** Key code constant: Function modifier key. */
376 public static final int KEYCODE_FUNCTION = 119;
377 /** Key code constant: System Request / Print Screen key. */
378 public static final int KEYCODE_SYSRQ = 120;
379 /** Key code constant: Break / Pause key. */
380 public static final int KEYCODE_BREAK = 121;
381 /** Key code constant: Home Movement key.
382 * Used for scrolling or moving the cursor around to the start of a line
383 * or to the top of a list. */
384 public static final int KEYCODE_MOVE_HOME = 122;
385 /** Key code constant: End Movement key.
386 * Used for scrolling or moving the cursor around to the end of a line
387 * or to the bottom of a list. */
388 public static final int KEYCODE_MOVE_END = 123;
389 /** Key code constant: Insert key.
390 * Toggles insert / overwrite edit mode. */
391 public static final int KEYCODE_INSERT = 124;
392 /** Key code constant: Forward key.
393 * Navigates forward in the history stack. Complement of {@link #KEYCODE_BACK}. */
394 public static final int KEYCODE_FORWARD = 125;
395 /** Key code constant: Play media key. */
396 public static final int KEYCODE_MEDIA_PLAY = 126;
397 /** Key code constant: Pause media key. */
398 public static final int KEYCODE_MEDIA_PAUSE = 127;
399 /** Key code constant: Close media key.
400 * May be used to close a CD tray, for example. */
401 public static final int KEYCODE_MEDIA_CLOSE = 128;
402 /** Key code constant: Eject media key.
403 * May be used to eject a CD tray, for example. */
404 public static final int KEYCODE_MEDIA_EJECT = 129;
405 /** Key code constant: Record media key. */
406 public static final int KEYCODE_MEDIA_RECORD = 130;
407 /** Key code constant: F1 key. */
408 public static final int KEYCODE_F1 = 131;
409 /** Key code constant: F2 key. */
410 public static final int KEYCODE_F2 = 132;
411 /** Key code constant: F3 key. */
412 public static final int KEYCODE_F3 = 133;
413 /** Key code constant: F4 key. */
414 public static final int KEYCODE_F4 = 134;
415 /** Key code constant: F5 key. */
416 public static final int KEYCODE_F5 = 135;
417 /** Key code constant: F6 key. */
418 public static final int KEYCODE_F6 = 136;
419 /** Key code constant: F7 key. */
420 public static final int KEYCODE_F7 = 137;
421 /** Key code constant: F8 key. */
422 public static final int KEYCODE_F8 = 138;
423 /** Key code constant: F9 key. */
424 public static final int KEYCODE_F9 = 139;
425 /** Key code constant: F10 key. */
426 public static final int KEYCODE_F10 = 140;
427 /** Key code constant: F11 key. */
428 public static final int KEYCODE_F11 = 141;
429 /** Key code constant: F12 key. */
430 public static final int KEYCODE_F12 = 142;
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800431 /** Key code constant: Num Lock key.
Jeff Brown497a92c2010-09-12 17:55:08 -0700432 * This is the Num Lock key; it is different from {@link #KEYCODE_NUM}.
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800433 * This key alters the behavior of other keys on the numeric keypad. */
Jeff Brown497a92c2010-09-12 17:55:08 -0700434 public static final int KEYCODE_NUM_LOCK = 143;
435 /** Key code constant: Numeric keypad '0' key. */
436 public static final int KEYCODE_NUMPAD_0 = 144;
437 /** Key code constant: Numeric keypad '1' key. */
438 public static final int KEYCODE_NUMPAD_1 = 145;
439 /** Key code constant: Numeric keypad '2' key. */
440 public static final int KEYCODE_NUMPAD_2 = 146;
441 /** Key code constant: Numeric keypad '3' key. */
442 public static final int KEYCODE_NUMPAD_3 = 147;
443 /** Key code constant: Numeric keypad '4' key. */
444 public static final int KEYCODE_NUMPAD_4 = 148;
445 /** Key code constant: Numeric keypad '5' key. */
446 public static final int KEYCODE_NUMPAD_5 = 149;
447 /** Key code constant: Numeric keypad '6' key. */
448 public static final int KEYCODE_NUMPAD_6 = 150;
449 /** Key code constant: Numeric keypad '7' key. */
450 public static final int KEYCODE_NUMPAD_7 = 151;
451 /** Key code constant: Numeric keypad '8' key. */
452 public static final int KEYCODE_NUMPAD_8 = 152;
453 /** Key code constant: Numeric keypad '9' key. */
454 public static final int KEYCODE_NUMPAD_9 = 153;
455 /** Key code constant: Numeric keypad '/' key (for division). */
456 public static final int KEYCODE_NUMPAD_DIVIDE = 154;
457 /** Key code constant: Numeric keypad '*' key (for multiplication). */
458 public static final int KEYCODE_NUMPAD_MULTIPLY = 155;
459 /** Key code constant: Numeric keypad '-' key (for subtraction). */
460 public static final int KEYCODE_NUMPAD_SUBTRACT = 156;
461 /** Key code constant: Numeric keypad '+' key (for addition). */
462 public static final int KEYCODE_NUMPAD_ADD = 157;
463 /** Key code constant: Numeric keypad '.' key (for decimals or digit grouping). */
464 public static final int KEYCODE_NUMPAD_DOT = 158;
465 /** Key code constant: Numeric keypad ',' key (for decimals or digit grouping). */
466 public static final int KEYCODE_NUMPAD_COMMA = 159;
467 /** Key code constant: Numeric keypad Enter key. */
468 public static final int KEYCODE_NUMPAD_ENTER = 160;
469 /** Key code constant: Numeric keypad '=' key. */
470 public static final int KEYCODE_NUMPAD_EQUALS = 161;
471 /** Key code constant: Numeric keypad '(' key. */
472 public static final int KEYCODE_NUMPAD_LEFT_PAREN = 162;
473 /** Key code constant: Numeric keypad ')' key. */
474 public static final int KEYCODE_NUMPAD_RIGHT_PAREN = 163;
Jeff Brownb0418da2010-11-01 15:24:01 -0700475 /** Key code constant: Volume Mute key.
476 * Mutes the speaker, unlike {@link #KEYCODE_MUTE}.
477 * This key should normally be implemented as a toggle such that the first press
478 * mutes the speaker and the second press restores the original volume. */
479 public static final int KEYCODE_VOLUME_MUTE = 164;
Jason Bayer3adf4902010-11-09 14:54:55 -0800480 /** Key code constant: Info key.
481 * Common on TV remotes to show additional information related to what is
482 * currently being viewed. */
483 public static final int KEYCODE_INFO = 165;
484 /** Key code constant: Channel up key.
485 * On TV remotes, increments the television channel. */
486 public static final int KEYCODE_CHANNEL_UP = 166;
487 /** Key code constant: Channel down key.
488 * On TV remotes, decrements the television channel. */
489 public static final int KEYCODE_CHANNEL_DOWN = 167;
490 /** Key code constant: Zoom in key. */
491 public static final int KEYCODE_ZOOM_IN = 168;
492 /** Key code constant: Zoom out key. */
493 public static final int KEYCODE_ZOOM_OUT = 169;
494 /** Key code constant: TV key.
495 * On TV remotes, switches to viewing live TV. */
496 public static final int KEYCODE_TV = 170;
497 /** Key code constant: Window key.
498 * On TV remotes, toggles picture-in-picture mode or other windowing functions. */
499 public static final int KEYCODE_WINDOW = 171;
500 /** Key code constant: Guide key.
501 * On TV remotes, shows a programming guide. */
502 public static final int KEYCODE_GUIDE = 172;
503 /** Key code constant: DVR key.
504 * On some TV remotes, switches to a DVR mode for recorded shows. */
505 public static final int KEYCODE_DVR = 173;
506 /** Key code constant: Bookmark key.
507 * On some TV remotes, bookmarks content or web pages. */
508 public static final int KEYCODE_BOOKMARK = 174;
509 /** Key code constant: Toggle captions key.
510 * Switches the mode for closed-captioning text, for example during television shows. */
511 public static final int KEYCODE_CAPTIONS = 175;
512 /** Key code constant: Settings key.
513 * Starts the system settings activity. */
514 public static final int KEYCODE_SETTINGS = 176;
515 /** Key code constant: TV power key.
516 * On TV remotes, toggles the power on a television screen. */
517 public static final int KEYCODE_TV_POWER = 177;
518 /** Key code constant: TV input key.
519 * On TV remotes, switches the input on a television screen. */
520 public static final int KEYCODE_TV_INPUT = 178;
521 /** Key code constant: Set-top-box power key.
522 * On TV remotes, toggles the power on an external Set-top-box. */
523 public static final int KEYCODE_STB_POWER = 179;
524 /** Key code constant: Set-top-box input key.
525 * On TV remotes, switches the input mode on an external Set-top-box. */
526 public static final int KEYCODE_STB_INPUT = 180;
527 /** Key code constant: A/V Receiver power key.
528 * On TV remotes, toggles the power on an external A/V Receiver. */
529 public static final int KEYCODE_AVR_POWER = 181;
530 /** Key code constant: A/V Receiver input key.
531 * On TV remotes, switches the input mode on an external A/V Receiver. */
532 public static final int KEYCODE_AVR_INPUT = 182;
533 /** Key code constant: Red "programmable" key.
534 * On TV remotes, acts as a contextual/programmable key. */
535 public static final int KEYCODE_PROG_RED = 183;
536 /** Key code constant: Green "programmable" key.
537 * On TV remotes, actsas a contextual/programmable key. */
538 public static final int KEYCODE_PROG_GREEN = 184;
539 /** Key code constant: Yellow "programmable" key.
540 * On TV remotes, acts as a contextual/programmable key. */
541 public static final int KEYCODE_PROG_YELLOW = 185;
542 /** Key code constant: Blue "programmable" key.
543 * On TV remotes, acts as a contextual/programmable key. */
544 public static final int KEYCODE_PROG_BLUE = 186;
Jeff Brown49ed71d2010-12-06 17:13:33 -0800545 /** Key code constant: App switch key.
546 * Should bring up the application switcher dialog. */
547 public static final int KEYCODE_APP_SWITCH = 187;
Jeff Browncb1404e2011-01-15 18:14:15 -0800548 /** Key code constant: Generic Game Pad Button #1.*/
549 public static final int KEYCODE_BUTTON_1 = 188;
550 /** Key code constant: Generic Game Pad Button #2.*/
551 public static final int KEYCODE_BUTTON_2 = 189;
552 /** Key code constant: Generic Game Pad Button #3.*/
553 public static final int KEYCODE_BUTTON_3 = 190;
554 /** Key code constant: Generic Game Pad Button #4.*/
555 public static final int KEYCODE_BUTTON_4 = 191;
556 /** Key code constant: Generic Game Pad Button #5.*/
557 public static final int KEYCODE_BUTTON_5 = 192;
558 /** Key code constant: Generic Game Pad Button #6.*/
559 public static final int KEYCODE_BUTTON_6 = 193;
560 /** Key code constant: Generic Game Pad Button #7.*/
561 public static final int KEYCODE_BUTTON_7 = 194;
562 /** Key code constant: Generic Game Pad Button #8.*/
563 public static final int KEYCODE_BUTTON_8 = 195;
564 /** Key code constant: Generic Game Pad Button #9.*/
565 public static final int KEYCODE_BUTTON_9 = 196;
566 /** Key code constant: Generic Game Pad Button #10.*/
567 public static final int KEYCODE_BUTTON_10 = 197;
568 /** Key code constant: Generic Game Pad Button #11.*/
569 public static final int KEYCODE_BUTTON_11 = 198;
570 /** Key code constant: Generic Game Pad Button #12.*/
571 public static final int KEYCODE_BUTTON_12 = 199;
572 /** Key code constant: Generic Game Pad Button #13.*/
573 public static final int KEYCODE_BUTTON_13 = 200;
574 /** Key code constant: Generic Game Pad Button #14.*/
575 public static final int KEYCODE_BUTTON_14 = 201;
576 /** Key code constant: Generic Game Pad Button #15.*/
577 public static final int KEYCODE_BUTTON_15 = 202;
578 /** Key code constant: Generic Game Pad Button #16.*/
579 public static final int KEYCODE_BUTTON_16 = 203;
Jeff Brown9812aed2011-03-07 17:09:51 -0800580 /** Key code constant: Language Switch key.
581 * Toggles the current input language such as switching between English and Japanese on
582 * a QWERTY keyboard. On some devices, the same function may be performed by
583 * pressing Shift+Spacebar. */
584 public static final int KEYCODE_LANGUAGE_SWITCH = 204;
585 /** Key code constant: Manner Mode key.
586 * Toggles silent or vibrate mode on and off to make the device behave more politely
587 * in certain settings such as on a crowded train. On some devices, the key may only
588 * operate when long-pressed. */
589 public static final int KEYCODE_MANNER_MODE = 205;
590 /** Key code constant: 3D Mode key.
591 * Toggles the display between 2D and 3D mode. */
592 public static final int KEYCODE_3D_MODE = 206;
Jeff Brown6651a632011-11-28 12:59:11 -0800593 /** Key code constant: Contacts special function key.
594 * Used to launch an address book application. */
595 public static final int KEYCODE_CONTACTS = 207;
596 /** Key code constant: Calendar special function key.
597 * Used to launch a calendar application. */
598 public static final int KEYCODE_CALENDAR = 208;
599 /** Key code constant: Music special function key.
600 * Used to launch a music player application. */
601 public static final int KEYCODE_MUSIC = 209;
602 /** Key code constant: Calculator special function key.
603 * Used to launch a calculator application. */
604 public static final int KEYCODE_CALCULATOR = 210;
Yang Chuang7511f9c2012-02-10 15:18:26 +0800605 /** Key code constant: Japanese full-width / half-width key. */
606 public static final int KEYCODE_ZENKAKU_HANKAKU = 211;
607 /** Key code constant: Japanese alphanumeric key. */
608 public static final int KEYCODE_EISU = 212;
609 /** Key code constant: Japanese non-conversion key. */
610 public static final int KEYCODE_MUHENKAN = 213;
611 /** Key code constant: Japanese conversion key. */
612 public static final int KEYCODE_HENKAN = 214;
613 /** Key code constant: Japanese katakana / hiragana key. */
614 public static final int KEYCODE_KATAKANA_HIRAGANA = 215;
615 /** Key code constant: Japanese Yen key. */
616 public static final int KEYCODE_YEN = 216;
617 /** Key code constant: Japanese Ro key. */
618 public static final int KEYCODE_RO = 217;
619 /** Key code constant: Japanese kana key. */
620 public static final int KEYCODE_KANA = 218;
Jeff Brownde7a8ea2012-06-13 18:28:57 -0700621 /** Key code constant: Assist key.
622 * Launches the global assist activity. Not delivered to applications. */
623 public static final int KEYCODE_ASSIST = 219;
Michael Wright1df477a2013-01-31 16:19:18 -0800624 /** Key code constant: Brightness Down key.
625 * Adjusts the screen brightness down. */
626 public static final int KEYCODE_BRIGHTNESS_DOWN = 220;
627 /** Key code constant: Brightness Up key.
628 * Adjusts the screen brightness up. */
629 public static final int KEYCODE_BRIGHTNESS_UP = 221;
Jeff Brown6212a492014-03-07 13:58:47 -0800630 /** Key code constant: Audio Track key.
Jaekyun Seokbfdad8e2013-07-08 13:53:21 +0900631 * Switches the audio tracks. */
632 public static final int KEYCODE_MEDIA_AUDIO_TRACK = 222;
Jeff Brown6212a492014-03-07 13:58:47 -0800633 /** Key code constant: Sleep key.
634 * Puts the device to sleep. Behaves somewhat like {@link #KEYCODE_POWER} but it
635 * has no effect if the device is already asleep. */
636 public static final int KEYCODE_SLEEP = 223;
637 /** Key code constant: Wakeup key.
638 * Wakes up the device. Behaves somewhat like {@link #KEYCODE_POWER} but it
639 * has no effect if the device is already awake. */
640 public static final int KEYCODE_WAKEUP = 224;
Tim Kilbourn87cd0dc2014-04-14 15:37:51 -0700641 /** Key code constant: Pairing key.
642 * Initiates peripheral pairing mode. Useful for pairing remote control
643 * devices or game controllers, especially if no other input mode is
644 * available. */
645 public static final int KEYCODE_PAIRING = 225;
Jinsuk Kim96658f72014-05-14 15:33:43 +0900646 /** Key code constant: Media Top Menu key.
647 * Goes to the top of media menu. */
648 public static final int KEYCODE_MEDIA_TOP_MENU = 226;
649 /** Key code constant: '11' key. */
650 public static final int KEYCODE_11 = 227;
651 /** Key code constant: '12' key. */
652 public static final int KEYCODE_12 = 228;
653 /** Key code constant: Last Channel key.
654 * Goes to the last viewed channel. */
655 public static final int KEYCODE_LAST_CHANNEL = 229;
656 /** Key code constant: TV data service key.
657 * Displays data services like weather, sports. */
658 public static final int KEYCODE_TV_DATA_SERVICE = 230;
Michael Wrightdc63f7b2014-08-21 19:05:21 -0700659 /** Key code constant: Voice Assist key.
660 * Launches the global voice assist activity. Not delivered to applications. */
661 public static final int KEYCODE_VOICE_ASSIST = 231;
ASAZU, Hidekidbd6aba2014-08-27 18:03:30 +0900662 /** Key code constant: Radio key.
663 * Toggles TV service / Radio service. */
664 public static final int KEYCODE_TV_RADIO_SERVICE = 232;
665 /** Key code constant: Teletext key.
666 * Displays Teletext service. */
667 public static final int KEYCODE_TV_TELETEXT = 233;
668 /** Key code constant: Number entry key.
669 * Initiates to enter multi-digit channel nubmber when each digit key is assigned
670 * for selecting separate channel. Corresponds to Number Entry Mode (0x1D) of CEC
671 * User Control Code. */
672 public static final int KEYCODE_TV_NUMBER_ENTRY = 234;
673 /** Key code constant: Analog Terrestrial key.
674 * Switches to analog terrestrial broadcast service. */
675 public static final int KEYCODE_TV_TERRESTRIAL_ANALOG = 235;
676 /** Key code constant: Digital Terrestrial key.
677 * Switches to digital terrestrial broadcast service. */
678 public static final int KEYCODE_TV_TERRESTRIAL_DIGITAL = 236;
679 /** Key code constant: Satellite key.
680 * Switches to digital satellite broadcast service. */
681 public static final int KEYCODE_TV_SATELLITE = 237;
682 /** Key code constant: BS key.
683 * Switches to BS digital satellite broadcasting service available in Japan. */
684 public static final int KEYCODE_TV_SATELLITE_BS = 238;
685 /** Key code constant: CS key.
686 * Switches to CS digital satellite broadcasting service available in Japan. */
687 public static final int KEYCODE_TV_SATELLITE_CS = 239;
688 /** Key code constant: BS/CS key.
689 * Toggles between BS and CS digital satellite services. */
690 public static final int KEYCODE_TV_SATELLITE_SERVICE = 240;
691 /** Key code constant: Toggle Network key.
692 * Toggles selecting broacast services. */
693 public static final int KEYCODE_TV_NETWORK = 241;
694 /** Key code constant: Antenna/Cable key.
695 * Toggles broadcast input source between antenna and cable. */
696 public static final int KEYCODE_TV_ANTENNA_CABLE = 242;
697 /** Key code constant: HDMI #1 key.
698 * Switches to HDMI input #1. */
699 public static final int KEYCODE_TV_INPUT_HDMI_1 = 243;
700 /** Key code constant: HDMI #2 key.
701 * Switches to HDMI input #2. */
702 public static final int KEYCODE_TV_INPUT_HDMI_2 = 244;
703 /** Key code constant: HDMI #3 key.
704 * Switches to HDMI input #3. */
705 public static final int KEYCODE_TV_INPUT_HDMI_3 = 245;
706 /** Key code constant: HDMI #4 key.
707 * Switches to HDMI input #4. */
708 public static final int KEYCODE_TV_INPUT_HDMI_4 = 246;
709 /** Key code constant: Composite #1 key.
710 * Switches to composite video input #1. */
711 public static final int KEYCODE_TV_INPUT_COMPOSITE_1 = 247;
712 /** Key code constant: Composite #2 key.
713 * Switches to composite video input #2. */
714 public static final int KEYCODE_TV_INPUT_COMPOSITE_2 = 248;
715 /** Key code constant: Component #1 key.
716 * Switches to component video input #1. */
717 public static final int KEYCODE_TV_INPUT_COMPONENT_1 = 249;
718 /** Key code constant: Component #2 key.
719 * Switches to component video input #2. */
720 public static final int KEYCODE_TV_INPUT_COMPONENT_2 = 250;
721 /** Key code constant: VGA #1 key.
722 * Switches to VGA (analog RGB) input #1. */
723 public static final int KEYCODE_TV_INPUT_VGA_1 = 251;
724 /** Key code constant: Audio description key.
725 * Toggles audio description off / on. */
726 public static final int KEYCODE_TV_AUDIO_DESCRIPTION = 252;
727 /** Key code constant: Audio description mixing volume up key.
728 * Louden audio description volume as compared with normal audio volume. */
729 public static final int KEYCODE_TV_AUDIO_DESCRIPTION_MIX_UP = 253;
730 /** Key code constant: Audio description mixing volume down key.
731 * Lessen audio description volume as compared with normal audio volume. */
732 public static final int KEYCODE_TV_AUDIO_DESCRIPTION_MIX_DOWN = 254;
733 /** Key code constant: Zoom mode key.
734 * Changes Zoom mode (Normal, Full, Zoom, Wide-zoom, etc.) */
735 public static final int KEYCODE_TV_ZOOM_MODE = 255;
736 /** Key code constant: Contents menu key.
737 * Goes to the title list. Corresponds to Contents Menu (0x0B) of CEC User Control
738 * Code */
739 public static final int KEYCODE_TV_CONTENTS_MENU = 256;
740 /** Key code constant: Media context menu key.
741 * Goes to the context menu of media contents. Corresponds to Media Context-sensitive
742 * Menu (0x11) of CEC User Control Code. */
743 public static final int KEYCODE_TV_MEDIA_CONTEXT_MENU = 257;
744 /** Key code constant: Timer programming key.
745 * Goes to the timer recording menu. Corresponds to Timer Programming (0x54) of
746 * CEC User Control Code. */
747 public static final int KEYCODE_TV_TIMER_PROGRAMMING = 258;
748 /** Key code constant: Help key. */
749 public static final int KEYCODE_HELP = 259;
Michael Wright962c9532015-08-06 15:16:22 +0100750 /** Key code constant: Navigate to previous key.
Joseph Cooper55b9ed42015-04-15 16:21:58 -0700751 * Goes backward by one item in an ordered collection of items. */
752 public static final int KEYCODE_NAVIGATE_PREVIOUS = 260;
Michael Wright962c9532015-08-06 15:16:22 +0100753 /** Key code constant: Navigate to next key.
Joseph Cooper55b9ed42015-04-15 16:21:58 -0700754 * Advances to the next item in an ordered collection of items. */
755 public static final int KEYCODE_NAVIGATE_NEXT = 261;
756 /** Key code constant: Navigate in key.
Michael Wright962c9532015-08-06 15:16:22 +0100757 * Activates the item that currently has focus or expands to the next level of a navigation
Joseph Cooper55b9ed42015-04-15 16:21:58 -0700758 * hierarchy. */
759 public static final int KEYCODE_NAVIGATE_IN = 262;
760 /** Key code constant: Navigate out key.
Michael Wright962c9532015-08-06 15:16:22 +0100761 * Backs out one level of a navigation hierarchy or collapses the item that currently has
Joseph Cooper55b9ed42015-04-15 16:21:58 -0700762 * focus. */
763 public static final int KEYCODE_NAVIGATE_OUT = 263;
Anthony Hugh9d826682015-06-23 10:44:17 -0700764 /** Key code constant: Primary stem key for Wear
765 * Main power/reset button on watch. */
766 public static final int KEYCODE_STEM_PRIMARY = 264;
767 /** Key code constant: Generic stem key 1 for Wear */
768 public static final int KEYCODE_STEM_1 = 265;
769 /** Key code constant: Generic stem key 2 for Wear */
770 public static final int KEYCODE_STEM_2 = 266;
771 /** Key code constant: Generic stem key 3 for Wear */
772 public static final int KEYCODE_STEM_3 = 267;
David Stevensa487f0c2015-07-31 11:00:50 -0700773 /** Key code constant: Directional Pad Up-Left */
774 public static final int KEYCODE_DPAD_UP_LEFT = 268;
775 /** Key code constant: Directional Pad Down-Left */
776 public static final int KEYCODE_DPAD_DOWN_LEFT = 269;
777 /** Key code constant: Directional Pad Up-Right */
778 public static final int KEYCODE_DPAD_UP_RIGHT = 270;
779 /** Key code constant: Directional Pad Down-Right */
780 public static final int KEYCODE_DPAD_DOWN_RIGHT = 271;
Michael Wright962c9532015-08-06 15:16:22 +0100781 /** Key code constant: Skip forward media key. */
782 public static final int KEYCODE_MEDIA_SKIP_FORWARD = 272;
783 /** Key code constant: Skip backward media key. */
784 public static final int KEYCODE_MEDIA_SKIP_BACKWARD = 273;
785 /** Key code constant: Step forward media key.
786 * Steps media forward, one frame at a time. */
787 public static final int KEYCODE_MEDIA_STEP_FORWARD = 274;
788 /** Key code constant: Step backward media key.
789 * Steps media backward, one frame at a time. */
790 public static final int KEYCODE_MEDIA_STEP_BACKWARD = 275;
Nick Armstrong-Crews3a5a8c72015-09-09 09:25:03 -0700791 /** Key code constant: put device to sleep unless a wakelock is held. */
Nick Armstrong-Crews56ecfcc2015-09-07 21:46:50 -0700792 public static final int KEYCODE_SOFT_SLEEP = 276;
Michael Wrightea84cff2015-10-21 18:08:30 +0100793 /** Key code constant: Cut key. */
794 public static final int KEYCODE_CUT = 277;
795 /** Key code constant: Copy key. */
796 public static final int KEYCODE_COPY = 278;
797 /** Key code constant: Paste key. */
798 public static final int KEYCODE_PASTE = 279;
Jeff Brown497a92c2010-09-12 17:55:08 -0700799
Michael Wrightea84cff2015-10-21 18:08:30 +0100800 private static final int LAST_KEYCODE = KEYCODE_PASTE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801
802 // NOTE: If you add a new keycode here you must also add it to:
803 // isSystem()
Michael Wrightdc63f7b2014-08-21 19:05:21 -0700804 // isWakeKey()
Chirayu Desai61c37ae2013-04-15 20:11:37 +0530805 // frameworks/native/include/android/keycodes.h
Jinsuk Kim96658f72014-05-14 15:33:43 +0900806 // frameworks/native/include/input/InputEventLabels.h
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 // frameworks/base/core/res/res/values/attrs.xml
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 // emulator?
Jeff Brown6651a632011-11-28 12:59:11 -0800809 // LAST_KEYCODE
Dianne Hackborn935ae462009-04-13 16:11:55 -0700810 //
811 // Also Android currently does not reserve code ranges for vendor-
812 // specific key codes. If you have new key codes to have, you
813 // MUST contribute a patch to the open source project to define
814 // those new codes. This is intended to maintain a consistent
815 // set of key code definitions across all Android devices.
Jeff Brown497a92c2010-09-12 17:55:08 -0700816
Jeff Brown497a92c2010-09-12 17:55:08 -0700817 // Symbolic names of all metakeys in bit order from least significant to most significant.
818 // Accordingly there are exactly 32 values in this table.
819 private static final String[] META_SYMBOLIC_NAMES = new String[] {
820 "META_SHIFT_ON",
821 "META_ALT_ON",
822 "META_SYM_ON",
823 "META_FUNCTION_ON",
824 "META_ALT_LEFT_ON",
825 "META_ALT_RIGHT_ON",
826 "META_SHIFT_LEFT_ON",
827 "META_SHIFT_RIGHT_ON",
828 "META_CAP_LOCKED",
829 "META_ALT_LOCKED",
830 "META_SYM_LOCKED",
831 "0x00000800",
832 "META_CTRL_ON",
833 "META_CTRL_LEFT_ON",
834 "META_CTRL_RIGHT_ON",
835 "0x00008000",
836 "META_META_ON",
837 "META_META_LEFT_ON",
838 "META_META_RIGHT_ON",
839 "0x00080000",
Jeff Brown51e7fe72010-10-29 22:19:53 -0700840 "META_CAPS_LOCK_ON",
841 "META_NUM_LOCK_ON",
842 "META_SCROLL_LOCK_ON",
Jeff Brown497a92c2010-09-12 17:55:08 -0700843 "0x00800000",
844 "0x01000000",
845 "0x02000000",
846 "0x04000000",
847 "0x08000000",
848 "0x10000000",
849 "0x20000000",
850 "0x40000000",
851 "0x80000000",
852 };
853
Michael Wright337d9d22014-04-22 15:03:48 -0700854 private static final String LABEL_PREFIX = "KEYCODE_";
855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 /**
857 * @deprecated There are now more than MAX_KEYCODE keycodes.
858 * Use {@link #getMaxKeyCode()} instead.
859 */
860 @Deprecated
861 public static final int MAX_KEYCODE = 84;
862
863 /**
864 * {@link #getAction} value: the key has been pressed down.
865 */
866 public static final int ACTION_DOWN = 0;
867 /**
868 * {@link #getAction} value: the key has been released.
869 */
870 public static final int ACTION_UP = 1;
871 /**
872 * {@link #getAction} value: multiple duplicate key events have
873 * occurred in a row, or a complex string is being delivered. If the
874 * key code is not {#link {@link #KEYCODE_UNKNOWN} then the
875 * {#link {@link #getRepeatCount()} method returns the number of times
876 * the given key code should be executed.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700877 * Otherwise, if the key code is {@link #KEYCODE_UNKNOWN}, then
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 * this is a sequence of characters as returned by {@link #getCharacters}.
879 */
880 public static final int ACTION_MULTIPLE = 2;
881
882 /**
Jeff Brown497a92c2010-09-12 17:55:08 -0700883 * SHIFT key locked in CAPS mode.
884 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
885 * @hide
886 */
887 public static final int META_CAP_LOCKED = 0x100;
888
889 /**
890 * ALT key locked.
891 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
892 * @hide
893 */
894 public static final int META_ALT_LOCKED = 0x200;
895
896 /**
897 * SYM key locked.
898 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
899 * @hide
900 */
901 public static final int META_SYM_LOCKED = 0x400;
902
903 /**
904 * Text is in selection mode.
905 * Reserved for use by {@link MetaKeyKeyListener} for a private unpublished constant
906 * in its API that is currently being retained for legacy reasons.
907 * @hide
908 */
909 public static final int META_SELECTING = 0x800;
910
911 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 * <p>This mask is used to check whether one of the ALT meta keys is pressed.</p>
913 *
914 * @see #isAltPressed()
915 * @see #getMetaState()
916 * @see #KEYCODE_ALT_LEFT
917 * @see #KEYCODE_ALT_RIGHT
918 */
919 public static final int META_ALT_ON = 0x02;
920
921 /**
922 * <p>This mask is used to check whether the left ALT meta key is pressed.</p>
923 *
924 * @see #isAltPressed()
925 * @see #getMetaState()
926 * @see #KEYCODE_ALT_LEFT
927 */
928 public static final int META_ALT_LEFT_ON = 0x10;
929
930 /**
931 * <p>This mask is used to check whether the right the ALT meta key is pressed.</p>
932 *
933 * @see #isAltPressed()
934 * @see #getMetaState()
935 * @see #KEYCODE_ALT_RIGHT
936 */
937 public static final int META_ALT_RIGHT_ON = 0x20;
938
939 /**
940 * <p>This mask is used to check whether one of the SHIFT meta keys is pressed.</p>
941 *
942 * @see #isShiftPressed()
943 * @see #getMetaState()
944 * @see #KEYCODE_SHIFT_LEFT
945 * @see #KEYCODE_SHIFT_RIGHT
946 */
947 public static final int META_SHIFT_ON = 0x1;
948
949 /**
950 * <p>This mask is used to check whether the left SHIFT meta key is pressed.</p>
951 *
952 * @see #isShiftPressed()
953 * @see #getMetaState()
954 * @see #KEYCODE_SHIFT_LEFT
955 */
956 public static final int META_SHIFT_LEFT_ON = 0x40;
957
958 /**
959 * <p>This mask is used to check whether the right SHIFT meta key is pressed.</p>
960 *
961 * @see #isShiftPressed()
962 * @see #getMetaState()
963 * @see #KEYCODE_SHIFT_RIGHT
964 */
965 public static final int META_SHIFT_RIGHT_ON = 0x80;
966
967 /**
968 * <p>This mask is used to check whether the SYM meta key is pressed.</p>
969 *
970 * @see #isSymPressed()
971 * @see #getMetaState()
972 */
973 public static final int META_SYM_ON = 0x4;
974
975 /**
Jeff Brown497a92c2010-09-12 17:55:08 -0700976 * <p>This mask is used to check whether the FUNCTION meta key is pressed.</p>
977 *
978 * @see #isFunctionPressed()
979 * @see #getMetaState()
980 */
981 public static final int META_FUNCTION_ON = 0x8;
982
983 /**
984 * <p>This mask is used to check whether one of the CTRL meta keys is pressed.</p>
985 *
986 * @see #isCtrlPressed()
987 * @see #getMetaState()
988 * @see #KEYCODE_CTRL_LEFT
989 * @see #KEYCODE_CTRL_RIGHT
990 */
991 public static final int META_CTRL_ON = 0x1000;
992
993 /**
994 * <p>This mask is used to check whether the left CTRL meta key is pressed.</p>
995 *
996 * @see #isCtrlPressed()
997 * @see #getMetaState()
998 * @see #KEYCODE_CTRL_LEFT
999 */
1000 public static final int META_CTRL_LEFT_ON = 0x2000;
1001
1002 /**
1003 * <p>This mask is used to check whether the right CTRL meta key is pressed.</p>
1004 *
1005 * @see #isCtrlPressed()
1006 * @see #getMetaState()
1007 * @see #KEYCODE_CTRL_RIGHT
1008 */
1009 public static final int META_CTRL_RIGHT_ON = 0x4000;
1010
1011 /**
1012 * <p>This mask is used to check whether one of the META meta keys is pressed.</p>
1013 *
1014 * @see #isMetaPressed()
1015 * @see #getMetaState()
1016 * @see #KEYCODE_META_LEFT
1017 * @see #KEYCODE_META_RIGHT
1018 */
1019 public static final int META_META_ON = 0x10000;
1020
1021 /**
1022 * <p>This mask is used to check whether the left META meta key is pressed.</p>
1023 *
1024 * @see #isMetaPressed()
1025 * @see #getMetaState()
1026 * @see #KEYCODE_META_LEFT
1027 */
1028 public static final int META_META_LEFT_ON = 0x20000;
1029
1030 /**
1031 * <p>This mask is used to check whether the right META meta key is pressed.</p>
1032 *
1033 * @see #isMetaPressed()
1034 * @see #getMetaState()
1035 * @see #KEYCODE_META_RIGHT
1036 */
1037 public static final int META_META_RIGHT_ON = 0x40000;
1038
1039 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07001040 * <p>This mask is used to check whether the CAPS LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07001041 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07001042 * @see #isCapsLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -07001043 * @see #getMetaState()
1044 * @see #KEYCODE_CAPS_LOCK
1045 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07001046 public static final int META_CAPS_LOCK_ON = 0x100000;
Jeff Brown497a92c2010-09-12 17:55:08 -07001047
1048 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07001049 * <p>This mask is used to check whether the NUM LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07001050 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07001051 * @see #isNumLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -07001052 * @see #getMetaState()
1053 * @see #KEYCODE_NUM_LOCK
1054 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07001055 public static final int META_NUM_LOCK_ON = 0x200000;
Jeff Brown497a92c2010-09-12 17:55:08 -07001056
1057 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07001058 * <p>This mask is used to check whether the SCROLL LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07001059 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07001060 * @see #isScrollLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -07001061 * @see #getMetaState()
1062 * @see #KEYCODE_SCROLL_LOCK
1063 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07001064 public static final int META_SCROLL_LOCK_ON = 0x400000;
Jeff Brown497a92c2010-09-12 17:55:08 -07001065
Jeff Brown64da12a2011-01-04 19:57:47 -08001066 /**
1067 * This mask is a combination of {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON}
1068 * and {@link #META_SHIFT_RIGHT_ON}.
1069 */
Jeff Brownc1df9072010-12-21 16:38:50 -08001070 public static final int META_SHIFT_MASK = META_SHIFT_ON
1071 | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON;
1072
Jeff Brown64da12a2011-01-04 19:57:47 -08001073 /**
1074 * This mask is a combination of {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON}
1075 * and {@link #META_ALT_RIGHT_ON}.
1076 */
Jeff Brownc1df9072010-12-21 16:38:50 -08001077 public static final int META_ALT_MASK = META_ALT_ON
1078 | META_ALT_LEFT_ON | META_ALT_RIGHT_ON;
1079
Jeff Brown64da12a2011-01-04 19:57:47 -08001080 /**
1081 * This mask is a combination of {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON}
1082 * and {@link #META_CTRL_RIGHT_ON}.
1083 */
Jeff Brownc1df9072010-12-21 16:38:50 -08001084 public static final int META_CTRL_MASK = META_CTRL_ON
1085 | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON;
1086
Jeff Brown64da12a2011-01-04 19:57:47 -08001087 /**
1088 * This mask is a combination of {@link #META_META_ON}, {@link #META_META_LEFT_ON}
1089 * and {@link #META_META_RIGHT_ON}.
1090 */
1091 public static final int META_META_MASK = META_META_ON
Jeff Brownc1df9072010-12-21 16:38:50 -08001092 | META_META_LEFT_ON | META_META_RIGHT_ON;
1093
Jeff Brown497a92c2010-09-12 17:55:08 -07001094 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 * This mask is set if the device woke because of this key event.
Jeff Brown037c33e2014-04-09 00:31:55 -07001096 *
1097 * @deprecated This flag will never be set by the system since the system
1098 * consumes all wake keys itself.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 */
Jeff Brown037c33e2014-04-09 00:31:55 -07001100 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 public static final int FLAG_WOKE_HERE = 0x1;
RoboErik01fe6612014-02-13 14:19:04 -08001102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 /**
1104 * This mask is set if the key event was generated by a software keyboard.
1105 */
1106 public static final int FLAG_SOFT_KEYBOARD = 0x2;
RoboErik01fe6612014-02-13 14:19:04 -08001107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 /**
1109 * This mask is set if we don't want the key event to cause us to leave
1110 * touch mode.
1111 */
1112 public static final int FLAG_KEEP_TOUCH_MODE = 0x4;
RoboErik01fe6612014-02-13 14:19:04 -08001113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001115 * This mask is set if an event was known to come from a trusted part
1116 * of the system. That is, the event is known to come from the user,
1117 * and could not have been spoofed by a third party component.
1118 */
1119 public static final int FLAG_FROM_SYSTEM = 0x8;
RoboErik01fe6612014-02-13 14:19:04 -08001120
The Android Open Source Project10592532009-03-18 17:39:46 -07001121 /**
1122 * This mask is used for compatibility, to identify enter keys that are
1123 * coming from an IME whose enter key has been auto-labelled "next" or
1124 * "done". This allows TextView to dispatch these as normal enter keys
1125 * for old applications, but still do the appropriate action when
1126 * receiving them.
1127 */
1128 public static final int FLAG_EDITOR_ACTION = 0x10;
RoboErik01fe6612014-02-13 14:19:04 -08001129
The Android Open Source Project10592532009-03-18 17:39:46 -07001130 /**
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07001131 * When associated with up key events, this indicates that the key press
1132 * has been canceled. Typically this is used with virtual touch screen
1133 * keys, where the user can slide from the virtual key area on to the
1134 * display: in that case, the application will receive a canceled up
1135 * event and should not perform the action normally associated with the
1136 * key. Note that for this to work, the application can not perform an
1137 * action for a key until it receives an up or the long press timeout has
RoboErik01fe6612014-02-13 14:19:04 -08001138 * expired.
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07001139 */
1140 public static final int FLAG_CANCELED = 0x20;
RoboErik01fe6612014-02-13 14:19:04 -08001141
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07001142 /**
1143 * This key event was generated by a virtual (on-screen) hard key area.
1144 * Typically this is an area of the touchscreen, outside of the regular
1145 * display, dedicated to "hardware" buttons.
1146 */
1147 public static final int FLAG_VIRTUAL_HARD_KEY = 0x40;
RoboErik01fe6612014-02-13 14:19:04 -08001148
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07001149 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001150 * This flag is set for the first key repeat that occurs after the
1151 * long press timeout.
1152 */
1153 public static final int FLAG_LONG_PRESS = 0x80;
RoboErik01fe6612014-02-13 14:19:04 -08001154
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001155 /**
1156 * Set when a key event has {@link #FLAG_CANCELED} set because a long
RoboErik01fe6612014-02-13 14:19:04 -08001157 * press action was executed while it was down.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001158 */
1159 public static final int FLAG_CANCELED_LONG_PRESS = 0x100;
RoboErik01fe6612014-02-13 14:19:04 -08001160
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001161 /**
1162 * Set for {@link #ACTION_UP} when this event's key code is still being
1163 * tracked from its initial down. That is, somebody requested that tracking
1164 * started on the key down and a long press has not caused
1165 * the tracking to be canceled.
1166 */
1167 public static final int FLAG_TRACKING = 0x200;
Jeff Brown49ed71d2010-12-06 17:13:33 -08001168
1169 /**
1170 * Set when a key event has been synthesized to implement default behavior
1171 * for an event that the application did not handle.
1172 * Fallback key events are generated by unhandled trackball motions
1173 * (to emulate a directional keypad) and by certain unhandled key presses
1174 * that are declared in the key map (such as special function numeric keypad
1175 * keys when numlock is off).
1176 */
1177 public static final int FLAG_FALLBACK = 0x400;
1178
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001179 /**
Michael Wrighta44dd262013-04-10 21:12:00 -07001180 * Signifies that the key is being predispatched.
1181 * @hide
1182 */
1183 public static final int FLAG_PREDISPATCH = 0x20000000;
1184
1185 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001186 * Private control to determine when an app is tracking a key sequence.
1187 * @hide
1188 */
1189 public static final int FLAG_START_TRACKING = 0x40000000;
Jeff Brown21bc5c92011-02-28 18:27:14 -08001190
1191 /**
1192 * Private flag that indicates when the system has detected that this key event
1193 * may be inconsistent with respect to the sequence of previously delivered key events,
1194 * such as when a key up event is sent but the key was not down.
1195 *
1196 * @hide
1197 * @see #isTainted
1198 * @see #setTainted
1199 */
1200 public static final int FLAG_TAINTED = 0x80000000;
1201
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001202 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 * Returns the maximum keycode.
1204 */
1205 public static int getMaxKeyCode() {
1206 return LAST_KEYCODE;
1207 }
1208
1209 /**
1210 * Get the character that is produced by putting accent on the character
1211 * c.
1212 * For example, getDeadChar('`', 'e') returns &egrave;.
1213 */
1214 public static int getDeadChar(int accent, int c) {
1215 return KeyCharacterMap.getDeadChar(accent, c);
1216 }
RoboErik01fe6612014-02-13 14:19:04 -08001217
Dianne Hackborn8d374262009-09-14 21:21:52 -07001218 static final boolean DEBUG = false;
1219 static final String TAG = "KeyEvent";
Jeff Brown1f245102010-11-18 20:53:46 -08001220
1221 private static final int MAX_RECYCLED = 10;
1222 private static final Object gRecyclerLock = new Object();
1223 private static int gRecyclerUsed;
1224 private static KeyEvent gRecyclerTop;
1225
1226 private KeyEvent mNext;
Jeff Brown1f245102010-11-18 20:53:46 -08001227
Jeff Brown91c69ab2011-02-14 17:03:18 -08001228 private int mDeviceId;
1229 private int mSource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 private int mMetaState;
1231 private int mAction;
1232 private int mKeyCode;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001233 private int mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 private int mRepeatCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 private int mFlags;
1236 private long mDownTime;
1237 private long mEventTime;
1238 private String mCharacters;
1239
1240 public interface Callback {
1241 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001242 * Called when a key down event has occurred. If you return true,
1243 * you can first call {@link KeyEvent#startTracking()
1244 * KeyEvent.startTracking()} to have the framework track the event
1245 * through its {@link #onKeyUp(int, KeyEvent)} and also call your
1246 * {@link #onKeyLongPress(int, KeyEvent)} if it occurs.
RoboErik01fe6612014-02-13 14:19:04 -08001247 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 * @param keyCode The value in event.getKeyCode().
1249 * @param event Description of the key event.
RoboErik01fe6612014-02-13 14:19:04 -08001250 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 * @return If you handled the event, return true. If you want to allow
1252 * the event to be handled by the next receiver, return false.
1253 */
1254 boolean onKeyDown(int keyCode, KeyEvent event);
1255
1256 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001257 * Called when a long press has occurred. If you return true,
1258 * the final key up will have {@link KeyEvent#FLAG_CANCELED} and
1259 * {@link KeyEvent#FLAG_CANCELED_LONG_PRESS} set. Note that in
1260 * order to receive this callback, someone in the event change
1261 * <em>must</em> return true from {@link #onKeyDown} <em>and</em>
1262 * call {@link KeyEvent#startTracking()} on the event.
RoboErik01fe6612014-02-13 14:19:04 -08001263 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001264 * @param keyCode The value in event.getKeyCode().
1265 * @param event Description of the key event.
RoboErik01fe6612014-02-13 14:19:04 -08001266 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001267 * @return If you handled the event, return true. If you want to allow
1268 * the event to be handled by the next receiver, return false.
1269 */
1270 boolean onKeyLongPress(int keyCode, KeyEvent event);
1271
1272 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 * Called when a key up event has occurred.
RoboErik01fe6612014-02-13 14:19:04 -08001274 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 * @param keyCode The value in event.getKeyCode().
1276 * @param event Description of the key event.
RoboErik01fe6612014-02-13 14:19:04 -08001277 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 * @return If you handled the event, return true. If you want to allow
1279 * the event to be handled by the next receiver, return false.
1280 */
1281 boolean onKeyUp(int keyCode, KeyEvent event);
1282
1283 /**
1284 * Called when multiple down/up pairs of the same key have occurred
1285 * in a row.
RoboErik01fe6612014-02-13 14:19:04 -08001286 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 * @param keyCode The value in event.getKeyCode().
1288 * @param count Number of pairs as returned by event.getRepeatCount().
1289 * @param event Description of the key event.
RoboErik01fe6612014-02-13 14:19:04 -08001290 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 * @return If you handled the event, return true. If you want to allow
1292 * the event to be handled by the next receiver, return false.
1293 */
1294 boolean onKeyMultiple(int keyCode, int count, KeyEvent event);
1295 }
1296
Michael Wright337d9d22014-04-22 15:03:48 -07001297 private static native String nativeKeyCodeToString(int keyCode);
1298 private static native int nativeKeyCodeFromString(String keyCode);
Jeff Brown497a92c2010-09-12 17:55:08 -07001299
Jeff Brown1f245102010-11-18 20:53:46 -08001300 private KeyEvent() {
1301 }
1302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 /**
1304 * Create a new key event.
RoboErik01fe6612014-02-13 14:19:04 -08001305 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 * @param action Action code: either {@link #ACTION_DOWN},
1307 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1308 * @param code The key code.
1309 */
1310 public KeyEvent(int action, int code) {
1311 mAction = action;
1312 mKeyCode = code;
1313 mRepeatCount = 0;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001314 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 }
1316
1317 /**
1318 * Create a new key event.
RoboErik01fe6612014-02-13 14:19:04 -08001319 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1321 * at which this key code originally went down.
1322 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1323 * at which this event happened.
1324 * @param action Action code: either {@link #ACTION_DOWN},
1325 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1326 * @param code The key code.
1327 * @param repeat A repeat count for down events (> 0 if this is after the
1328 * initial down) or event count for multiple events.
1329 */
1330 public KeyEvent(long downTime, long eventTime, int action,
1331 int code, int repeat) {
1332 mDownTime = downTime;
1333 mEventTime = eventTime;
1334 mAction = action;
1335 mKeyCode = code;
1336 mRepeatCount = repeat;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001337 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 }
1339
1340 /**
1341 * Create a new key event.
RoboErik01fe6612014-02-13 14:19:04 -08001342 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1344 * at which this key code originally went down.
1345 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1346 * at which this event happened.
1347 * @param action Action code: either {@link #ACTION_DOWN},
1348 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1349 * @param code The key code.
1350 * @param repeat A repeat count for down events (> 0 if this is after the
1351 * initial down) or event count for multiple events.
1352 * @param metaState Flags indicating which meta keys are currently pressed.
1353 */
1354 public KeyEvent(long downTime, long eventTime, int action,
1355 int code, int repeat, int metaState) {
1356 mDownTime = downTime;
1357 mEventTime = eventTime;
1358 mAction = action;
1359 mKeyCode = code;
1360 mRepeatCount = repeat;
1361 mMetaState = metaState;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001362 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 }
1364
1365 /**
1366 * Create a new key event.
RoboErik01fe6612014-02-13 14:19:04 -08001367 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1369 * at which this key code originally went down.
1370 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1371 * at which this event happened.
1372 * @param action Action code: either {@link #ACTION_DOWN},
1373 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1374 * @param code The key code.
1375 * @param repeat A repeat count for down events (> 0 if this is after the
1376 * initial down) or event count for multiple events.
1377 * @param metaState Flags indicating which meta keys are currently pressed.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001378 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 * @param scancode Raw device scan code of the event.
1380 */
1381 public KeyEvent(long downTime, long eventTime, int action,
1382 int code, int repeat, int metaState,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001383 int deviceId, int scancode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 mDownTime = downTime;
1385 mEventTime = eventTime;
1386 mAction = action;
1387 mKeyCode = code;
1388 mRepeatCount = repeat;
1389 mMetaState = metaState;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001390 mDeviceId = deviceId;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001391 mScanCode = scancode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 }
1393
1394 /**
1395 * Create a new key event.
RoboErik01fe6612014-02-13 14:19:04 -08001396 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1398 * at which this key code originally went down.
1399 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1400 * at which this event happened.
1401 * @param action Action code: either {@link #ACTION_DOWN},
1402 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1403 * @param code The key code.
1404 * @param repeat A repeat count for down events (> 0 if this is after the
1405 * initial down) or event count for multiple events.
1406 * @param metaState Flags indicating which meta keys are currently pressed.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001407 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 * @param scancode Raw device scan code of the event.
1409 * @param flags The flags for this key event
1410 */
1411 public KeyEvent(long downTime, long eventTime, int action,
1412 int code, int repeat, int metaState,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001413 int deviceId, int scancode, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 mDownTime = downTime;
1415 mEventTime = eventTime;
1416 mAction = action;
1417 mKeyCode = code;
1418 mRepeatCount = repeat;
1419 mMetaState = metaState;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001420 mDeviceId = deviceId;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001421 mScanCode = scancode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 mFlags = flags;
1423 }
1424
1425 /**
Jeff Brownc5ed5912010-07-14 18:48:53 -07001426 * Create a new key event.
RoboErik01fe6612014-02-13 14:19:04 -08001427 *
Jeff Brownc5ed5912010-07-14 18:48:53 -07001428 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1429 * at which this key code originally went down.
1430 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1431 * at which this event happened.
1432 * @param action Action code: either {@link #ACTION_DOWN},
1433 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1434 * @param code The key code.
1435 * @param repeat A repeat count for down events (> 0 if this is after the
1436 * initial down) or event count for multiple events.
1437 * @param metaState Flags indicating which meta keys are currently pressed.
1438 * @param deviceId The device ID that generated the key event.
1439 * @param scancode Raw device scan code of the event.
1440 * @param flags The flags for this key event
1441 * @param source The input source such as {@link InputDevice#SOURCE_KEYBOARD}.
1442 */
1443 public KeyEvent(long downTime, long eventTime, int action,
1444 int code, int repeat, int metaState,
1445 int deviceId, int scancode, int flags, int source) {
1446 mDownTime = downTime;
1447 mEventTime = eventTime;
1448 mAction = action;
1449 mKeyCode = code;
1450 mRepeatCount = repeat;
1451 mMetaState = metaState;
1452 mDeviceId = deviceId;
1453 mScanCode = scancode;
1454 mFlags = flags;
1455 mSource = source;
1456 }
1457
1458 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 * Create a new key event for a string of characters. The key code,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001460 * action, repeat count and source will automatically be set to
1461 * {@link #KEYCODE_UNKNOWN}, {@link #ACTION_MULTIPLE}, 0, and
1462 * {@link InputDevice#SOURCE_KEYBOARD} for you.
RoboErik01fe6612014-02-13 14:19:04 -08001463 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 * @param time The time (in {@link android.os.SystemClock#uptimeMillis})
1465 * at which this event occured.
1466 * @param characters The string of characters.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001467 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 * @param flags The flags for this key event
1469 */
Jeff Brownc5ed5912010-07-14 18:48:53 -07001470 public KeyEvent(long time, String characters, int deviceId, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 mDownTime = time;
1472 mEventTime = time;
1473 mCharacters = characters;
1474 mAction = ACTION_MULTIPLE;
1475 mKeyCode = KEYCODE_UNKNOWN;
1476 mRepeatCount = 0;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001477 mDeviceId = deviceId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 mFlags = flags;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001479 mSource = InputDevice.SOURCE_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 }
1481
1482 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001483 * Make an exact copy of an existing key event.
1484 */
1485 public KeyEvent(KeyEvent origEvent) {
1486 mDownTime = origEvent.mDownTime;
1487 mEventTime = origEvent.mEventTime;
1488 mAction = origEvent.mAction;
1489 mKeyCode = origEvent.mKeyCode;
1490 mRepeatCount = origEvent.mRepeatCount;
1491 mMetaState = origEvent.mMetaState;
1492 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001493 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001494 mScanCode = origEvent.mScanCode;
The Android Open Source Project10592532009-03-18 17:39:46 -07001495 mFlags = origEvent.mFlags;
1496 mCharacters = origEvent.mCharacters;
1497 }
1498
1499 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 * Copy an existing key event, modifying its time and repeat count.
RoboErik01fe6612014-02-13 14:19:04 -08001501 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001502 * @deprecated Use {@link #changeTimeRepeat(KeyEvent, long, int)}
1503 * instead.
RoboErik01fe6612014-02-13 14:19:04 -08001504 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 * @param origEvent The existing event to be copied.
1506 * @param eventTime The new event time
1507 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1508 * @param newRepeat The new repeat count of the event.
1509 */
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001510 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 public KeyEvent(KeyEvent origEvent, long eventTime, int newRepeat) {
1512 mDownTime = origEvent.mDownTime;
1513 mEventTime = eventTime;
1514 mAction = origEvent.mAction;
1515 mKeyCode = origEvent.mKeyCode;
1516 mRepeatCount = newRepeat;
1517 mMetaState = origEvent.mMetaState;
1518 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001519 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001520 mScanCode = origEvent.mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 mFlags = origEvent.mFlags;
1522 mCharacters = origEvent.mCharacters;
1523 }
1524
Jeff Brown1f245102010-11-18 20:53:46 -08001525 private static KeyEvent obtain() {
1526 final KeyEvent ev;
1527 synchronized (gRecyclerLock) {
1528 ev = gRecyclerTop;
1529 if (ev == null) {
1530 return new KeyEvent();
1531 }
1532 gRecyclerTop = ev.mNext;
1533 gRecyclerUsed -= 1;
1534 }
Jeff Brown1f245102010-11-18 20:53:46 -08001535 ev.mNext = null;
Jeff Brown32cbc38552011-12-01 14:01:49 -08001536 ev.prepareForReuse();
Jeff Brown1f245102010-11-18 20:53:46 -08001537 return ev;
1538 }
1539
1540 /**
1541 * Obtains a (potentially recycled) key event.
1542 *
1543 * @hide
1544 */
1545 public static KeyEvent obtain(long downTime, long eventTime, int action,
1546 int code, int repeat, int metaState,
1547 int deviceId, int scancode, int flags, int source, String characters) {
1548 KeyEvent ev = obtain();
1549 ev.mDownTime = downTime;
1550 ev.mEventTime = eventTime;
1551 ev.mAction = action;
1552 ev.mKeyCode = code;
1553 ev.mRepeatCount = repeat;
1554 ev.mMetaState = metaState;
1555 ev.mDeviceId = deviceId;
1556 ev.mScanCode = scancode;
1557 ev.mFlags = flags;
1558 ev.mSource = source;
1559 ev.mCharacters = characters;
1560 return ev;
1561 }
1562
1563 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08001564 * Obtains a (potentially recycled) copy of another key event.
1565 *
1566 * @hide
1567 */
1568 public static KeyEvent obtain(KeyEvent other) {
1569 KeyEvent ev = obtain();
1570 ev.mDownTime = other.mDownTime;
1571 ev.mEventTime = other.mEventTime;
1572 ev.mAction = other.mAction;
1573 ev.mKeyCode = other.mKeyCode;
1574 ev.mRepeatCount = other.mRepeatCount;
1575 ev.mMetaState = other.mMetaState;
1576 ev.mDeviceId = other.mDeviceId;
1577 ev.mScanCode = other.mScanCode;
1578 ev.mFlags = other.mFlags;
1579 ev.mSource = other.mSource;
1580 ev.mCharacters = other.mCharacters;
1581 return ev;
1582 }
1583
1584 /** @hide */
1585 @Override
1586 public KeyEvent copy() {
1587 return obtain(this);
1588 }
1589
1590 /**
Jeff Brown1f245102010-11-18 20:53:46 -08001591 * Recycles a key event.
1592 * Key events should only be recycled if they are owned by the system since user
1593 * code expects them to be essentially immutable, "tracking" notwithstanding.
1594 *
1595 * @hide
1596 */
Jeff Brown92cc2d82011-12-02 01:19:47 -08001597 @Override
Jeff Brown1f245102010-11-18 20:53:46 -08001598 public final void recycle() {
Jeff Brown32cbc38552011-12-01 14:01:49 -08001599 super.recycle();
Jeff Brown1f245102010-11-18 20:53:46 -08001600 mCharacters = null;
1601
1602 synchronized (gRecyclerLock) {
1603 if (gRecyclerUsed < MAX_RECYCLED) {
1604 gRecyclerUsed++;
1605 mNext = gRecyclerTop;
1606 gRecyclerTop = this;
1607 }
1608 }
1609 }
1610
Jeff Brown92cc2d82011-12-02 01:19:47 -08001611 /** @hide */
1612 @Override
1613 public final void recycleIfNeededAfterDispatch() {
1614 // Do nothing.
1615 }
1616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001618 * Create a new key event that is the same as the given one, but whose
1619 * event time and repeat count are replaced with the given value.
RoboErik01fe6612014-02-13 14:19:04 -08001620 *
The Android Open Source Project10592532009-03-18 17:39:46 -07001621 * @param event The existing event to be copied. This is not modified.
1622 * @param eventTime The new event time
1623 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1624 * @param newRepeat The new repeat count of the event.
1625 */
1626 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime,
1627 int newRepeat) {
1628 return new KeyEvent(event, eventTime, newRepeat);
1629 }
RoboErik01fe6612014-02-13 14:19:04 -08001630
The Android Open Source Project10592532009-03-18 17:39:46 -07001631 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001632 * Create a new key event that is the same as the given one, but whose
1633 * event time and repeat count are replaced with the given value.
RoboErik01fe6612014-02-13 14:19:04 -08001634 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001635 * @param event The existing event to be copied. This is not modified.
1636 * @param eventTime The new event time
1637 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1638 * @param newRepeat The new repeat count of the event.
1639 * @param newFlags New flags for the event, replacing the entire value
1640 * in the original event.
1641 */
1642 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime,
1643 int newRepeat, int newFlags) {
1644 KeyEvent ret = new KeyEvent(event);
1645 ret.mEventTime = eventTime;
1646 ret.mRepeatCount = newRepeat;
1647 ret.mFlags = newFlags;
1648 return ret;
1649 }
RoboErik01fe6612014-02-13 14:19:04 -08001650
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001651 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 * Copy an existing key event, modifying its action.
RoboErik01fe6612014-02-13 14:19:04 -08001653 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 * @param origEvent The existing event to be copied.
1655 * @param action The new action code of the event.
1656 */
The Android Open Source Project10592532009-03-18 17:39:46 -07001657 private KeyEvent(KeyEvent origEvent, int action) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 mDownTime = origEvent.mDownTime;
1659 mEventTime = origEvent.mEventTime;
1660 mAction = action;
1661 mKeyCode = origEvent.mKeyCode;
1662 mRepeatCount = origEvent.mRepeatCount;
1663 mMetaState = origEvent.mMetaState;
1664 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001665 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001666 mScanCode = origEvent.mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 mFlags = origEvent.mFlags;
1668 // Don't copy mCharacters, since one way or the other we'll lose it
1669 // when changing the action.
1670 }
1671
1672 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001673 * Create a new key event that is the same as the given one, but whose
1674 * action is replaced with the given value.
RoboErik01fe6612014-02-13 14:19:04 -08001675 *
The Android Open Source Project10592532009-03-18 17:39:46 -07001676 * @param event The existing event to be copied. This is not modified.
1677 * @param action The new action code of the event.
1678 */
1679 public static KeyEvent changeAction(KeyEvent event, int action) {
1680 return new KeyEvent(event, action);
1681 }
RoboErik01fe6612014-02-13 14:19:04 -08001682
The Android Open Source Project10592532009-03-18 17:39:46 -07001683 /**
1684 * Create a new key event that is the same as the given one, but whose
1685 * flags are replaced with the given value.
RoboErik01fe6612014-02-13 14:19:04 -08001686 *
The Android Open Source Project10592532009-03-18 17:39:46 -07001687 * @param event The existing event to be copied. This is not modified.
1688 * @param flags The new flags constant.
1689 */
1690 public static KeyEvent changeFlags(KeyEvent event, int flags) {
1691 event = new KeyEvent(event);
1692 event.mFlags = flags;
1693 return event;
1694 }
Jeff Brown21bc5c92011-02-28 18:27:14 -08001695
1696 /** @hide */
1697 @Override
1698 public final boolean isTainted() {
1699 return (mFlags & FLAG_TAINTED) != 0;
1700 }
1701
1702 /** @hide */
1703 @Override
1704 public final void setTainted(boolean tainted) {
1705 mFlags = tainted ? mFlags | FLAG_TAINTED : mFlags & ~FLAG_TAINTED;
1706 }
1707
The Android Open Source Project10592532009-03-18 17:39:46 -07001708 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 * Don't use in new code, instead explicitly check
1710 * {@link #getAction()}.
RoboErik01fe6612014-02-13 14:19:04 -08001711 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 * @return If the action is ACTION_DOWN, returns true; else false.
1713 *
1714 * @deprecated
1715 * @hide
1716 */
1717 @Deprecated public final boolean isDown() {
1718 return mAction == ACTION_DOWN;
1719 }
1720
Michael Wright337d9d22014-04-22 15:03:48 -07001721 /** Is this a system key? System keys can not be used for menu shortcuts.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001722 */
1723 public final boolean isSystem() {
Michael Wright337d9d22014-04-22 15:03:48 -07001724 return isSystemKey(mKeyCode);
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -07001725 }
1726
1727 /** @hide */
Michael Wright337d9d22014-04-22 15:03:48 -07001728 public final boolean isWakeKey() {
1729 return isWakeKey(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001730 }
1731
Jeff Brown6f2fba42011-02-19 01:08:02 -08001732 /**
1733 * Returns true if the specified keycode is a gamepad button.
1734 * @return True if the keycode is a gamepad button, such as {@link #KEYCODE_BUTTON_A}.
1735 */
1736 public static final boolean isGamepadButton(int keyCode) {
1737 switch (keyCode) {
1738 case KeyEvent.KEYCODE_BUTTON_A:
1739 case KeyEvent.KEYCODE_BUTTON_B:
1740 case KeyEvent.KEYCODE_BUTTON_C:
1741 case KeyEvent.KEYCODE_BUTTON_X:
1742 case KeyEvent.KEYCODE_BUTTON_Y:
1743 case KeyEvent.KEYCODE_BUTTON_Z:
1744 case KeyEvent.KEYCODE_BUTTON_L1:
1745 case KeyEvent.KEYCODE_BUTTON_R1:
1746 case KeyEvent.KEYCODE_BUTTON_L2:
1747 case KeyEvent.KEYCODE_BUTTON_R2:
1748 case KeyEvent.KEYCODE_BUTTON_THUMBL:
1749 case KeyEvent.KEYCODE_BUTTON_THUMBR:
1750 case KeyEvent.KEYCODE_BUTTON_START:
1751 case KeyEvent.KEYCODE_BUTTON_SELECT:
1752 case KeyEvent.KEYCODE_BUTTON_MODE:
1753 case KeyEvent.KEYCODE_BUTTON_1:
1754 case KeyEvent.KEYCODE_BUTTON_2:
1755 case KeyEvent.KEYCODE_BUTTON_3:
1756 case KeyEvent.KEYCODE_BUTTON_4:
1757 case KeyEvent.KEYCODE_BUTTON_5:
1758 case KeyEvent.KEYCODE_BUTTON_6:
1759 case KeyEvent.KEYCODE_BUTTON_7:
1760 case KeyEvent.KEYCODE_BUTTON_8:
1761 case KeyEvent.KEYCODE_BUTTON_9:
1762 case KeyEvent.KEYCODE_BUTTON_10:
1763 case KeyEvent.KEYCODE_BUTTON_11:
1764 case KeyEvent.KEYCODE_BUTTON_12:
1765 case KeyEvent.KEYCODE_BUTTON_13:
1766 case KeyEvent.KEYCODE_BUTTON_14:
1767 case KeyEvent.KEYCODE_BUTTON_15:
1768 case KeyEvent.KEYCODE_BUTTON_16:
1769 return true;
1770 default:
1771 return false;
1772 }
1773 }
1774
Michael Wright25b0c302013-07-10 12:54:06 -07001775 /** Whether key will, by default, trigger a click on the focused view.
1776 * @hide
1777 */
1778 public static final boolean isConfirmKey(int keyCode) {
1779 switch (keyCode) {
1780 case KeyEvent.KEYCODE_DPAD_CENTER:
1781 case KeyEvent.KEYCODE_ENTER:
Michael Wrightaa1a94d2015-11-26 16:04:54 +00001782 case KeyEvent.KEYCODE_SPACE:
Selim Cinekce2bd0f2016-02-19 16:16:54 -08001783 case KeyEvent.KEYCODE_NUMPAD_ENTER:
Michael Wright25b0c302013-07-10 12:54:06 -07001784 return true;
1785 default:
1786 return false;
1787 }
1788 }
1789
RoboErik01fe6612014-02-13 14:19:04 -08001790 /**
1791 * Whether this key is a media key, which can be send to apps that are
1792 * interested in media key events.
1793 *
1794 * @hide
1795 */
1796 public static final boolean isMediaKey(int keyCode) {
1797 switch (keyCode) {
1798 case KeyEvent.KEYCODE_MEDIA_PLAY:
1799 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1800 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1801 case KeyEvent.KEYCODE_MUTE:
1802 case KeyEvent.KEYCODE_HEADSETHOOK:
1803 case KeyEvent.KEYCODE_MEDIA_STOP:
1804 case KeyEvent.KEYCODE_MEDIA_NEXT:
1805 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1806 case KeyEvent.KEYCODE_MEDIA_REWIND:
1807 case KeyEvent.KEYCODE_MEDIA_RECORD:
1808 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1809 return true;
1810 }
1811 return false;
1812 }
1813
Michael Wright337d9d22014-04-22 15:03:48 -07001814
1815 /** Is this a system key? System keys can not be used for menu shortcuts.
1816 * @hide
1817 */
1818 public static final boolean isSystemKey(int keyCode) {
1819 switch (keyCode) {
1820 case KeyEvent.KEYCODE_MENU:
1821 case KeyEvent.KEYCODE_SOFT_RIGHT:
1822 case KeyEvent.KEYCODE_HOME:
1823 case KeyEvent.KEYCODE_BACK:
1824 case KeyEvent.KEYCODE_CALL:
1825 case KeyEvent.KEYCODE_ENDCALL:
1826 case KeyEvent.KEYCODE_VOLUME_UP:
1827 case KeyEvent.KEYCODE_VOLUME_DOWN:
1828 case KeyEvent.KEYCODE_VOLUME_MUTE:
1829 case KeyEvent.KEYCODE_MUTE:
1830 case KeyEvent.KEYCODE_POWER:
1831 case KeyEvent.KEYCODE_HEADSETHOOK:
1832 case KeyEvent.KEYCODE_MEDIA_PLAY:
1833 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1834 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1835 case KeyEvent.KEYCODE_MEDIA_STOP:
1836 case KeyEvent.KEYCODE_MEDIA_NEXT:
1837 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1838 case KeyEvent.KEYCODE_MEDIA_REWIND:
1839 case KeyEvent.KEYCODE_MEDIA_RECORD:
1840 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1841 case KeyEvent.KEYCODE_CAMERA:
1842 case KeyEvent.KEYCODE_FOCUS:
1843 case KeyEvent.KEYCODE_SEARCH:
1844 case KeyEvent.KEYCODE_BRIGHTNESS_DOWN:
1845 case KeyEvent.KEYCODE_BRIGHTNESS_UP:
1846 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
Michael Wright337d9d22014-04-22 15:03:48 -07001847 return true;
1848 }
1849
1850 return false;
1851 }
1852
1853 /** @hide */
1854 public static final boolean isWakeKey(int keyCode) {
1855 switch (keyCode) {
1856 case KeyEvent.KEYCODE_BACK:
Michael Wright337d9d22014-04-22 15:03:48 -07001857 case KeyEvent.KEYCODE_MENU:
Michael Wright337d9d22014-04-22 15:03:48 -07001858 case KeyEvent.KEYCODE_WAKEUP:
Michael Wright8ac485a2014-06-04 12:38:18 -07001859 case KeyEvent.KEYCODE_PAIRING:
Chenjie Luobad498f2016-01-13 11:01:59 -08001860 case KeyEvent.KEYCODE_STEM_1:
1861 case KeyEvent.KEYCODE_STEM_2:
1862 case KeyEvent.KEYCODE_STEM_3:
Michael Wright337d9d22014-04-22 15:03:48 -07001863 return true;
1864 }
1865 return false;
1866 }
1867
Michael Wrightce0c13a2014-07-30 10:49:21 -07001868 /** @hide */
1869 public static final boolean isMetaKey(int keyCode) {
1870 return keyCode == KeyEvent.KEYCODE_META_LEFT || keyCode == KeyEvent.KEYCODE_META_RIGHT;
1871 }
1872
Jeff Brown91c69ab2011-02-14 17:03:18 -08001873 /** {@inheritDoc} */
1874 @Override
1875 public final int getDeviceId() {
1876 return mDeviceId;
1877 }
1878
1879 /** {@inheritDoc} */
1880 @Override
1881 public final int getSource() {
1882 return mSource;
1883 }
1884
1885 /** {@inheritDoc} */
1886 @Override
1887 public final void setSource(int source) {
1888 mSource = source;
1889 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890
1891 /**
1892 * <p>Returns the state of the meta keys.</p>
1893 *
1894 * @return an integer in which each bit set to 1 represents a pressed
1895 * meta key
1896 *
1897 * @see #isAltPressed()
1898 * @see #isShiftPressed()
1899 * @see #isSymPressed()
Jeff Brown497a92c2010-09-12 17:55:08 -07001900 * @see #isCtrlPressed()
1901 * @see #isMetaPressed()
1902 * @see #isFunctionPressed()
Jeff Brown51e7fe72010-10-29 22:19:53 -07001903 * @see #isCapsLockOn()
1904 * @see #isNumLockOn()
1905 * @see #isScrollLockOn()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001906 * @see #META_ALT_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001907 * @see #META_ALT_LEFT_ON
1908 * @see #META_ALT_RIGHT_ON
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 * @see #META_SHIFT_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001910 * @see #META_SHIFT_LEFT_ON
1911 * @see #META_SHIFT_RIGHT_ON
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912 * @see #META_SYM_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001913 * @see #META_FUNCTION_ON
1914 * @see #META_CTRL_ON
1915 * @see #META_CTRL_LEFT_ON
1916 * @see #META_CTRL_RIGHT_ON
1917 * @see #META_META_ON
1918 * @see #META_META_LEFT_ON
1919 * @see #META_META_RIGHT_ON
Jeff Brown51e7fe72010-10-29 22:19:53 -07001920 * @see #META_CAPS_LOCK_ON
1921 * @see #META_NUM_LOCK_ON
1922 * @see #META_SCROLL_LOCK_ON
Jeff Brown54875002011-04-06 15:33:01 -07001923 * @see #getModifiers
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 */
1925 public final int getMetaState() {
1926 return mMetaState;
1927 }
1928
1929 /**
Jeff Brown54875002011-04-06 15:33:01 -07001930 * Returns the state of the modifier keys.
1931 * <p>
1932 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1933 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1934 * not considered modifier keys. Consequently, this function specifically masks out
1935 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1936 * </p><p>
1937 * The value returned consists of the meta state (from {@link #getMetaState})
1938 * normalized using {@link #normalizeMetaState(int)} and then masked with
1939 * {@link #getModifierMetaStateMask} so that only valid modifier bits are retained.
1940 * </p>
1941 *
1942 * @return An integer in which each bit set to 1 represents a pressed modifier key.
1943 * @see #getMetaState
1944 */
1945 public final int getModifiers() {
1946 return normalizeMetaState(mMetaState) & META_MODIFIER_MASK;
1947 }
1948
1949 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 * Returns the flags for this key event.
1951 *
1952 * @see #FLAG_WOKE_HERE
1953 */
1954 public final int getFlags() {
1955 return mFlags;
1956 }
1957
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001958 // Mask of all modifier key meta states. Specifically excludes locked keys like caps lock.
1959 private static final int META_MODIFIER_MASK =
1960 META_SHIFT_ON | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON
1961 | META_ALT_ON | META_ALT_LEFT_ON | META_ALT_RIGHT_ON
1962 | META_CTRL_ON | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON
1963 | META_META_ON | META_META_LEFT_ON | META_META_RIGHT_ON
1964 | META_SYM_ON | META_FUNCTION_ON;
1965
1966 // Mask of all lock key meta states.
1967 private static final int META_LOCK_MASK =
1968 META_CAPS_LOCK_ON | META_NUM_LOCK_ON | META_SCROLL_LOCK_ON;
1969
1970 // Mask of all valid meta states.
1971 private static final int META_ALL_MASK = META_MODIFIER_MASK | META_LOCK_MASK;
1972
1973 // Mask of all synthetic meta states that are reserved for API compatibility with
1974 // historical uses in MetaKeyKeyListener.
1975 private static final int META_SYNTHETIC_MASK =
1976 META_CAP_LOCKED | META_ALT_LOCKED | META_SYM_LOCKED | META_SELECTING;
1977
1978 // Mask of all meta states that are not valid use in specifying a modifier key.
1979 // These bits are known to be used for purposes other than specifying modifiers.
1980 private static final int META_INVALID_MODIFIER_MASK =
1981 META_LOCK_MASK | META_SYNTHETIC_MASK;
1982
1983 /**
1984 * Gets a mask that includes all valid modifier key meta state bits.
1985 * <p>
1986 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1987 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1988 * not considered modifier keys. Consequently, the mask specifically excludes
1989 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1990 * </p>
1991 *
1992 * @return The modifier meta state mask which is a combination of
1993 * {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON}, {@link #META_SHIFT_RIGHT_ON},
1994 * {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON}, {@link #META_ALT_RIGHT_ON},
1995 * {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON}, {@link #META_CTRL_RIGHT_ON},
1996 * {@link #META_META_ON}, {@link #META_META_LEFT_ON}, {@link #META_META_RIGHT_ON},
1997 * {@link #META_SYM_ON}, {@link #META_FUNCTION_ON}.
1998 */
1999 public static int getModifierMetaStateMask() {
2000 return META_MODIFIER_MASK;
2001 }
2002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 /**
2004 * Returns true if this key code is a modifier key.
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002005 * <p>
2006 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2007 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2008 * not considered modifier keys. Consequently, this function return false
2009 * for those keys.
2010 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 *
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002012 * @return True if the key code is one of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002013 * {@link #KEYCODE_SHIFT_LEFT} {@link #KEYCODE_SHIFT_RIGHT},
Jeff Brown497a92c2010-09-12 17:55:08 -07002014 * {@link #KEYCODE_ALT_LEFT}, {@link #KEYCODE_ALT_RIGHT},
Jeff Brown497a92c2010-09-12 17:55:08 -07002015 * {@link #KEYCODE_CTRL_LEFT}, {@link #KEYCODE_CTRL_RIGHT},
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002016 * {@link #KEYCODE_META_LEFT}, or {@link #KEYCODE_META_RIGHT},
2017 * {@link #KEYCODE_SYM}, {@link #KEYCODE_NUM}, {@link #KEYCODE_FUNCTION}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 */
2019 public static boolean isModifierKey(int keyCode) {
Jeff Brown497a92c2010-09-12 17:55:08 -07002020 switch (keyCode) {
2021 case KEYCODE_SHIFT_LEFT:
2022 case KEYCODE_SHIFT_RIGHT:
2023 case KEYCODE_ALT_LEFT:
2024 case KEYCODE_ALT_RIGHT:
Jeff Brown497a92c2010-09-12 17:55:08 -07002025 case KEYCODE_CTRL_LEFT:
2026 case KEYCODE_CTRL_RIGHT:
2027 case KEYCODE_META_LEFT:
2028 case KEYCODE_META_RIGHT:
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002029 case KEYCODE_SYM:
2030 case KEYCODE_NUM:
2031 case KEYCODE_FUNCTION:
Jeff Brown497a92c2010-09-12 17:55:08 -07002032 return true;
2033 default:
2034 return false;
2035 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 }
2037
2038 /**
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002039 * Normalizes the specified meta state.
2040 * <p>
2041 * The meta state is normalized such that if either the left or right modifier meta state
2042 * bits are set then the result will also include the universal bit for that modifier.
2043 * </p><p>
2044 * If the specified meta state contains {@link #META_ALT_LEFT_ON} then
2045 * the result will also contain {@link #META_ALT_ON} in addition to {@link #META_ALT_LEFT_ON}
2046 * and the other bits that were specified in the input. The same is process is
2047 * performed for shift, control and meta.
2048 * </p><p>
2049 * If the specified meta state contains synthetic meta states defined by
2050 * {@link MetaKeyKeyListener}, then those states are translated here and the original
2051 * synthetic meta states are removed from the result.
2052 * {@link MetaKeyKeyListener#META_CAP_LOCKED} is translated to {@link #META_CAPS_LOCK_ON}.
2053 * {@link MetaKeyKeyListener#META_ALT_LOCKED} is translated to {@link #META_ALT_ON}.
2054 * {@link MetaKeyKeyListener#META_SYM_LOCKED} is translated to {@link #META_SYM_ON}.
2055 * </p><p>
2056 * Undefined meta state bits are removed.
2057 * </p>
2058 *
2059 * @param metaState The meta state.
2060 * @return The normalized meta state.
2061 */
2062 public static int normalizeMetaState(int metaState) {
2063 if ((metaState & (META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON)) != 0) {
2064 metaState |= META_SHIFT_ON;
2065 }
2066 if ((metaState & (META_ALT_LEFT_ON | META_ALT_RIGHT_ON)) != 0) {
2067 metaState |= META_ALT_ON;
2068 }
2069 if ((metaState & (META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON)) != 0) {
2070 metaState |= META_CTRL_ON;
2071 }
2072 if ((metaState & (META_META_LEFT_ON | META_META_RIGHT_ON)) != 0) {
2073 metaState |= META_META_ON;
2074 }
2075 if ((metaState & MetaKeyKeyListener.META_CAP_LOCKED) != 0) {
2076 metaState |= META_CAPS_LOCK_ON;
2077 }
2078 if ((metaState & MetaKeyKeyListener.META_ALT_LOCKED) != 0) {
2079 metaState |= META_ALT_ON;
2080 }
2081 if ((metaState & MetaKeyKeyListener.META_SYM_LOCKED) != 0) {
2082 metaState |= META_SYM_ON;
2083 }
2084 return metaState & META_ALL_MASK;
2085 }
2086
2087 /**
2088 * Returns true if no modifiers keys are pressed according to the specified meta state.
2089 * <p>
2090 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2091 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2092 * not considered modifier keys. Consequently, this function ignores
2093 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2094 * </p><p>
2095 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}.
2096 * </p>
2097 *
2098 * @param metaState The meta state to consider.
2099 * @return True if no modifier keys are pressed.
2100 * @see #hasNoModifiers()
2101 */
2102 public static boolean metaStateHasNoModifiers(int metaState) {
2103 return (normalizeMetaState(metaState) & META_MODIFIER_MASK) == 0;
2104 }
2105
2106 /**
2107 * Returns true if only the specified modifier keys are pressed according to
2108 * the specified meta state. Returns false if a different combination of modifier
2109 * keys are pressed.
2110 * <p>
2111 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2112 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2113 * not considered modifier keys. Consequently, this function ignores
2114 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2115 * </p><p>
2116 * If the specified modifier mask includes directional modifiers, such as
2117 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the
2118 * modifier is pressed on that side.
2119 * If the specified modifier mask includes non-directional modifiers, such as
2120 * {@link #META_SHIFT_ON}, then this method ensures that the modifier
2121 * is pressed on either side.
2122 * If the specified modifier mask includes both directional and non-directional modifiers
2123 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON},
2124 * then this method throws an illegal argument exception.
2125 * </p>
2126 *
2127 * @param metaState The meta state to consider.
2128 * @param modifiers The meta state of the modifier keys to check. May be a combination
2129 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to
2130 * ensure that no modifier keys are pressed.
2131 * @return True if only the specified modifier keys are pressed.
2132 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers
2133 * @see #hasModifiers
2134 */
2135 public static boolean metaStateHasModifiers(int metaState, int modifiers) {
2136 // Note: For forward compatibility, we allow the parameter to contain meta states
2137 // that we do not recognize but we explicitly disallow meta states that
2138 // are not valid modifiers.
2139 if ((modifiers & META_INVALID_MODIFIER_MASK) != 0) {
2140 throw new IllegalArgumentException("modifiers must not contain "
2141 + "META_CAPS_LOCK_ON, META_NUM_LOCK_ON, META_SCROLL_LOCK_ON, "
2142 + "META_CAP_LOCKED, META_ALT_LOCKED, META_SYM_LOCKED, "
2143 + "or META_SELECTING");
2144 }
2145
2146 metaState = normalizeMetaState(metaState) & META_MODIFIER_MASK;
2147 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2148 META_SHIFT_ON, META_SHIFT_LEFT_ON, META_SHIFT_RIGHT_ON);
2149 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2150 META_ALT_ON, META_ALT_LEFT_ON, META_ALT_RIGHT_ON);
2151 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2152 META_CTRL_ON, META_CTRL_LEFT_ON, META_CTRL_RIGHT_ON);
2153 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2154 META_META_ON, META_META_LEFT_ON, META_META_RIGHT_ON);
2155 return metaState == modifiers;
2156 }
2157
2158 private static int metaStateFilterDirectionalModifiers(int metaState,
2159 int modifiers, int basic, int left, int right) {
2160 final boolean wantBasic = (modifiers & basic) != 0;
2161 final int directional = left | right;
2162 final boolean wantLeftOrRight = (modifiers & directional) != 0;
2163
2164 if (wantBasic) {
2165 if (wantLeftOrRight) {
2166 throw new IllegalArgumentException("modifiers must not contain "
2167 + metaStateToString(basic) + " combined with "
2168 + metaStateToString(left) + " or " + metaStateToString(right));
2169 }
2170 return metaState & ~directional;
2171 } else if (wantLeftOrRight) {
2172 return metaState & ~basic;
2173 } else {
2174 return metaState;
2175 }
2176 }
2177
2178 /**
2179 * Returns true if no modifier keys are pressed.
2180 * <p>
2181 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2182 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2183 * not considered modifier keys. Consequently, this function ignores
2184 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2185 * </p><p>
2186 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}.
2187 * </p>
2188 *
2189 * @return True if no modifier keys are pressed.
2190 * @see #metaStateHasNoModifiers
2191 */
2192 public final boolean hasNoModifiers() {
2193 return metaStateHasNoModifiers(mMetaState);
2194 }
2195
2196 /**
2197 * Returns true if only the specified modifiers keys are pressed.
2198 * Returns false if a different combination of modifier keys are pressed.
2199 * <p>
2200 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2201 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2202 * not considered modifier keys. Consequently, this function ignores
2203 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2204 * </p><p>
2205 * If the specified modifier mask includes directional modifiers, such as
2206 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the
2207 * modifier is pressed on that side.
2208 * If the specified modifier mask includes non-directional modifiers, such as
2209 * {@link #META_SHIFT_ON}, then this method ensures that the modifier
2210 * is pressed on either side.
2211 * If the specified modifier mask includes both directional and non-directional modifiers
2212 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON},
2213 * then this method throws an illegal argument exception.
2214 * </p>
2215 *
2216 * @param modifiers The meta state of the modifier keys to check. May be a combination
2217 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to
2218 * ensure that no modifier keys are pressed.
2219 * @return True if only the specified modifier keys are pressed.
2220 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers
2221 * @see #metaStateHasModifiers
2222 */
2223 public final boolean hasModifiers(int modifiers) {
2224 return metaStateHasModifiers(mMetaState, modifiers);
2225 }
2226
2227 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002228 * <p>Returns the pressed state of the ALT meta key.</p>
2229 *
2230 * @return true if the ALT key is pressed, false otherwise
2231 *
2232 * @see #KEYCODE_ALT_LEFT
2233 * @see #KEYCODE_ALT_RIGHT
2234 * @see #META_ALT_ON
2235 */
2236 public final boolean isAltPressed() {
2237 return (mMetaState & META_ALT_ON) != 0;
2238 }
2239
2240 /**
2241 * <p>Returns the pressed state of the SHIFT meta key.</p>
2242 *
2243 * @return true if the SHIFT key is pressed, false otherwise
2244 *
2245 * @see #KEYCODE_SHIFT_LEFT
2246 * @see #KEYCODE_SHIFT_RIGHT
2247 * @see #META_SHIFT_ON
2248 */
2249 public final boolean isShiftPressed() {
2250 return (mMetaState & META_SHIFT_ON) != 0;
2251 }
2252
2253 /**
2254 * <p>Returns the pressed state of the SYM meta key.</p>
2255 *
2256 * @return true if the SYM key is pressed, false otherwise
2257 *
2258 * @see #KEYCODE_SYM
2259 * @see #META_SYM_ON
2260 */
2261 public final boolean isSymPressed() {
2262 return (mMetaState & META_SYM_ON) != 0;
2263 }
2264
2265 /**
Jeff Brown497a92c2010-09-12 17:55:08 -07002266 * <p>Returns the pressed state of the CTRL meta key.</p>
2267 *
2268 * @return true if the CTRL key is pressed, false otherwise
2269 *
2270 * @see #KEYCODE_CTRL_LEFT
2271 * @see #KEYCODE_CTRL_RIGHT
2272 * @see #META_CTRL_ON
2273 */
2274 public final boolean isCtrlPressed() {
2275 return (mMetaState & META_CTRL_ON) != 0;
2276 }
2277
2278 /**
2279 * <p>Returns the pressed state of the META meta key.</p>
2280 *
2281 * @return true if the META key is pressed, false otherwise
2282 *
2283 * @see #KEYCODE_META_LEFT
2284 * @see #KEYCODE_META_RIGHT
2285 * @see #META_META_ON
2286 */
2287 public final boolean isMetaPressed() {
2288 return (mMetaState & META_META_ON) != 0;
2289 }
2290
2291 /**
2292 * <p>Returns the pressed state of the FUNCTION meta key.</p>
2293 *
2294 * @return true if the FUNCTION key is pressed, false otherwise
2295 *
2296 * @see #KEYCODE_FUNCTION
2297 * @see #META_FUNCTION_ON
2298 */
2299 public final boolean isFunctionPressed() {
2300 return (mMetaState & META_FUNCTION_ON) != 0;
2301 }
2302
2303 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07002304 * <p>Returns the locked state of the CAPS LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002305 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002306 * @return true if the CAPS LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002307 *
2308 * @see #KEYCODE_CAPS_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002309 * @see #META_CAPS_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002310 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002311 public final boolean isCapsLockOn() {
2312 return (mMetaState & META_CAPS_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002313 }
2314
2315 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07002316 * <p>Returns the locked state of the NUM LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002317 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002318 * @return true if the NUM LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002319 *
2320 * @see #KEYCODE_NUM_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002321 * @see #META_NUM_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002322 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002323 public final boolean isNumLockOn() {
2324 return (mMetaState & META_NUM_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002325 }
2326
2327 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07002328 * <p>Returns the locked state of the SCROLL LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002329 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002330 * @return true if the SCROLL LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002331 *
2332 * @see #KEYCODE_SCROLL_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002333 * @see #META_SCROLL_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002334 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002335 public final boolean isScrollLockOn() {
2336 return (mMetaState & META_SCROLL_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002337 }
2338
2339 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002340 * Retrieve the action of this key event. May be either
2341 * {@link #ACTION_DOWN}, {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
RoboErik01fe6612014-02-13 14:19:04 -08002342 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 * @return The event action: ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.
2344 */
2345 public final int getAction() {
2346 return mAction;
2347 }
2348
2349 /**
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07002350 * For {@link #ACTION_UP} events, indicates that the event has been
2351 * canceled as per {@link #FLAG_CANCELED}.
2352 */
2353 public final boolean isCanceled() {
2354 return (mFlags&FLAG_CANCELED) != 0;
2355 }
RoboErik01fe6612014-02-13 14:19:04 -08002356
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07002357 /**
Wale Ogunwalec3672cd2014-11-05 15:17:35 -08002358 * Set {@link #FLAG_CANCELED} flag for the key event.
2359 *
2360 * @hide
2361 */
2362 @Override
2363 public final void cancel() {
2364 mFlags |= FLAG_CANCELED;
2365 }
2366
2367 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002368 * Call this during {@link Callback#onKeyDown} to have the system track
2369 * the key through its final up (possibly including a long press). Note
2370 * that only one key can be tracked at a time -- if another key down
2371 * event is received while a previous one is being tracked, tracking is
2372 * stopped on the previous event.
2373 */
2374 public final void startTracking() {
2375 mFlags |= FLAG_START_TRACKING;
2376 }
RoboErik01fe6612014-02-13 14:19:04 -08002377
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002378 /**
2379 * For {@link #ACTION_UP} events, indicates that the event is still being
2380 * tracked from its initial down event as per
2381 * {@link #FLAG_TRACKING}.
2382 */
2383 public final boolean isTracking() {
2384 return (mFlags&FLAG_TRACKING) != 0;
2385 }
RoboErik01fe6612014-02-13 14:19:04 -08002386
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002387 /**
2388 * For {@link #ACTION_DOWN} events, indicates that the event has been
2389 * canceled as per {@link #FLAG_LONG_PRESS}.
2390 */
2391 public final boolean isLongPress() {
2392 return (mFlags&FLAG_LONG_PRESS) != 0;
2393 }
RoboErik01fe6612014-02-13 14:19:04 -08002394
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002395 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396 * Retrieve the key code of the key event. This is the physical key that
2397 * was pressed, <em>not</em> the Unicode character.
RoboErik01fe6612014-02-13 14:19:04 -08002398 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399 * @return The key code of the event.
2400 */
2401 public final int getKeyCode() {
2402 return mKeyCode;
2403 }
2404
2405 /**
2406 * For the special case of a {@link #ACTION_MULTIPLE} event with key
2407 * code of {@link #KEYCODE_UNKNOWN}, this is a raw string of characters
2408 * associated with the event. In all other cases it is null.
RoboErik01fe6612014-02-13 14:19:04 -08002409 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 * @return Returns a String of 1 or more characters associated with
2411 * the event.
2412 */
2413 public final String getCharacters() {
2414 return mCharacters;
2415 }
RoboErik01fe6612014-02-13 14:19:04 -08002416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002417 /**
2418 * Retrieve the hardware key id of this key event. These values are not
2419 * reliable and vary from device to device.
2420 *
2421 * {@more}
2422 * Mostly this is here for debugging purposes.
2423 */
2424 public final int getScanCode() {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002425 return mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002426 }
2427
2428 /**
2429 * Retrieve the repeat count of the event. For both key up and key down
2430 * events, this is the number of times the key has repeated with the first
2431 * down starting at 0 and counting up from there. For multiple key
2432 * events, this is the number of down/up pairs that have occurred.
RoboErik01fe6612014-02-13 14:19:04 -08002433 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002434 * @return The number of times the key has repeated.
2435 */
2436 public final int getRepeatCount() {
2437 return mRepeatCount;
2438 }
2439
2440 /**
2441 * Retrieve the time of the most recent key down event,
2442 * in the {@link android.os.SystemClock#uptimeMillis} time base. If this
2443 * is a down event, this will be the same as {@link #getEventTime()}.
2444 * Note that when chording keys, this value is the down time of the
2445 * most recently pressed key, which may <em>not</em> be the same physical
2446 * key of this event.
RoboErik01fe6612014-02-13 14:19:04 -08002447 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002448 * @return Returns the most recent key down time, in the
2449 * {@link android.os.SystemClock#uptimeMillis} time base
2450 */
2451 public final long getDownTime() {
2452 return mDownTime;
2453 }
2454
2455 /**
Jeff Brownb11499d2012-04-20 19:54:22 -07002456 * Retrieve the time this event occurred,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 * in the {@link android.os.SystemClock#uptimeMillis} time base.
Jeff Brownb11499d2012-04-20 19:54:22 -07002458 *
RoboErik01fe6612014-02-13 14:19:04 -08002459 * @return Returns the time this event occurred,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002460 * in the {@link android.os.SystemClock#uptimeMillis} time base.
2461 */
Jeff Brownb11499d2012-04-20 19:54:22 -07002462 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002463 public final long getEventTime() {
2464 return mEventTime;
2465 }
2466
Jeff Brownb11499d2012-04-20 19:54:22 -07002467 /**
2468 * Retrieve the time this event occurred,
2469 * in the {@link android.os.SystemClock#uptimeMillis} time base but with
2470 * nanosecond (instead of millisecond) precision.
2471 * <p>
2472 * The value is in nanosecond precision but it may not have nanosecond accuracy.
2473 * </p>
2474 *
2475 * @return Returns the time this event occurred,
2476 * in the {@link android.os.SystemClock#uptimeMillis} time base but with
2477 * nanosecond (instead of millisecond) precision.
2478 *
2479 * @hide
2480 */
Jeff Brown4e91a182011-04-07 11:38:09 -07002481 @Override
2482 public final long getEventTimeNano() {
2483 return mEventTime * 1000000L;
2484 }
2485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487 * Renamed to {@link #getDeviceId}.
RoboErik01fe6612014-02-13 14:19:04 -08002488 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002489 * @hide
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002490 * @deprecated use {@link #getDeviceId()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002491 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002492 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 public final int getKeyboardDevice() {
2494 return mDeviceId;
2495 }
2496
2497 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002498 * Gets the {@link KeyCharacterMap} associated with the keyboard device.
2499 *
2500 * @return The associated key character map.
Andrew Sapperstein8ab3dc72011-06-23 18:56:44 -07002501 * @throws {@link KeyCharacterMap.UnavailableException} if the key character map
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002502 * could not be loaded because it was malformed or the default key character map
2503 * is missing from the system.
2504 *
Andrew Sapperstein8ab3dc72011-06-23 18:56:44 -07002505 * @see KeyCharacterMap#load
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002506 */
2507 public final KeyCharacterMap getKeyCharacterMap() {
2508 return KeyCharacterMap.load(mDeviceId);
2509 }
2510
2511 /**
2512 * Gets the primary character for this key.
2513 * In other words, the label that is physically printed on it.
2514 *
2515 * @return The display label character, or 0 if none (eg. for non-printing keys).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516 */
2517 public char getDisplayLabel() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002518 return getKeyCharacterMap().getDisplayLabel(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002519 }
RoboErik01fe6612014-02-13 14:19:04 -08002520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002522 * Gets the Unicode character generated by the specified key and meta
2523 * key state combination.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002524 * <p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002525 * Returns the Unicode character that the specified key would produce
2526 * when the specified meta bits (see {@link MetaKeyKeyListener})
2527 * were active.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 * </p><p>
2529 * Returns 0 if the key is not one that is used to type Unicode
2530 * characters.
2531 * </p><p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002532 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the
2533 * key is a "dead key" that should be combined with another to
2534 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} --
2535 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002536 * </p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002537 *
2538 * @return The associated character or combining accent, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002539 */
2540 public int getUnicodeChar() {
2541 return getUnicodeChar(mMetaState);
2542 }
RoboErik01fe6612014-02-13 14:19:04 -08002543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002545 * Gets the Unicode character generated by the specified key and meta
2546 * key state combination.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547 * <p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002548 * Returns the Unicode character that the specified key would produce
2549 * when the specified meta bits (see {@link MetaKeyKeyListener})
2550 * were active.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551 * </p><p>
2552 * Returns 0 if the key is not one that is used to type Unicode
2553 * characters.
2554 * </p><p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002555 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the
2556 * key is a "dead key" that should be combined with another to
2557 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} --
2558 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002559 * </p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002560 *
2561 * @param metaState The meta key modifier state.
2562 * @return The associated character or combining accent, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002563 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002564 public int getUnicodeChar(int metaState) {
2565 return getKeyCharacterMap().get(mKeyCode, metaState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002566 }
RoboErik01fe6612014-02-13 14:19:04 -08002567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002568 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002569 * Get the character conversion data for a given key code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570 *
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002571 * @param results A {@link KeyCharacterMap.KeyData} instance that will be
2572 * filled with the results.
2573 * @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 -08002574 *
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002575 * @deprecated instead use {@link #getDisplayLabel()},
2576 * {@link #getNumber()} or {@link #getUnicodeChar(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002577 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002578 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002579 public boolean getKeyData(KeyData results) {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002580 return getKeyCharacterMap().getKeyData(mKeyCode, results);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 }
RoboErik01fe6612014-02-13 14:19:04 -08002582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002584 * Gets the first character in the character array that can be generated
2585 * by the specified key code.
2586 * <p>
2587 * This is a convenience function that returns the same value as
2588 * {@link #getMatch(char[],int) getMatch(chars, 0)}.
2589 * </p>
2590 *
2591 * @param chars The array of matching characters to consider.
2592 * @return The matching associated character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002593 */
2594 public char getMatch(char[] chars) {
2595 return getMatch(chars, 0);
2596 }
RoboErik01fe6612014-02-13 14:19:04 -08002597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002599 * Gets the first character in the character array that can be generated
2600 * by the specified key code. If there are multiple choices, prefers
2601 * the one that would be generated with the specified meta key modifier state.
2602 *
2603 * @param chars The array of matching characters to consider.
2604 * @param metaState The preferred meta key modifier state.
2605 * @return The matching associated character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002606 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002607 public char getMatch(char[] chars, int metaState) {
2608 return getKeyCharacterMap().getMatch(mKeyCode, chars, metaState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 }
RoboErik01fe6612014-02-13 14:19:04 -08002610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002611 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002612 * Gets the number or symbol associated with the key.
2613 * <p>
2614 * The character value is returned, not the numeric value.
2615 * If the key is not a number, but is a symbol, the symbol is retuned.
2616 * </p><p>
2617 * This method is intended to to support dial pads and other numeric or
2618 * symbolic entry on keyboards where certain keys serve dual function
2619 * as alphabetic and symbolic keys. This method returns the number
2620 * or symbol associated with the key independent of whether the user
2621 * has pressed the required modifier.
2622 * </p><p>
2623 * For example, on one particular keyboard the keys on the top QWERTY row generate
2624 * numbers when ALT is pressed such that ALT-Q maps to '1'. So for that keyboard
2625 * when {@link #getNumber} is called with {@link KeyEvent#KEYCODE_Q} it returns '1'
2626 * so that the user can type numbers without pressing ALT when it makes sense.
2627 * </p>
2628 *
2629 * @return The associated numeric or symbolic character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002630 */
2631 public char getNumber() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002632 return getKeyCharacterMap().getNumber(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002633 }
RoboErik01fe6612014-02-13 14:19:04 -08002634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002635 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002636 * Returns true if this key produces a glyph.
2637 *
2638 * @return True if the key is a printing key.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002639 */
2640 public boolean isPrintingKey() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002641 return getKeyCharacterMap().isPrintingKey(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642 }
RoboErik01fe6612014-02-13 14:19:04 -08002643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002644 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002645 * @deprecated Use {@link #dispatch(Callback, DispatcherState, Object)} instead.
2646 */
2647 @Deprecated
2648 public final boolean dispatch(Callback receiver) {
2649 return dispatch(receiver, null, null);
2650 }
RoboErik01fe6612014-02-13 14:19:04 -08002651
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002652 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002653 * Deliver this key event to a {@link Callback} interface. If this is
2654 * an ACTION_MULTIPLE event and it is not handled, then an attempt will
2655 * be made to deliver a single normal event.
RoboErik01fe6612014-02-13 14:19:04 -08002656 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002657 * @param receiver The Callback that will be given the event.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002658 * @param state State information retained across events.
2659 * @param target The target of the dispatch, for use in tracking.
RoboErik01fe6612014-02-13 14:19:04 -08002660 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002661 * @return The return value from the Callback method that was called.
2662 */
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002663 public final boolean dispatch(Callback receiver, DispatcherState state,
2664 Object target) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 switch (mAction) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002666 case ACTION_DOWN: {
2667 mFlags &= ~FLAG_START_TRACKING;
Dianne Hackborn8d374262009-09-14 21:21:52 -07002668 if (DEBUG) Log.v(TAG, "Key down to " + target + " in " + state
2669 + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002670 boolean res = receiver.onKeyDown(mKeyCode, this);
2671 if (state != null) {
2672 if (res && mRepeatCount == 0 && (mFlags&FLAG_START_TRACKING) != 0) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002673 if (DEBUG) Log.v(TAG, " Start tracking!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002674 state.startTracking(this, target);
2675 } else if (isLongPress() && state.isTracking(this)) {
2676 try {
2677 if (receiver.onKeyLongPress(mKeyCode, this)) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002678 if (DEBUG) Log.v(TAG, " Clear from long press!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002679 state.performedLongPress(this);
2680 res = true;
2681 }
2682 } catch (AbstractMethodError e) {
2683 }
2684 }
2685 }
2686 return res;
2687 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002688 case ACTION_UP:
Dianne Hackborn8d374262009-09-14 21:21:52 -07002689 if (DEBUG) Log.v(TAG, "Key up to " + target + " in " + state
2690 + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002691 if (state != null) {
2692 state.handleUpEvent(this);
2693 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694 return receiver.onKeyUp(mKeyCode, this);
2695 case ACTION_MULTIPLE:
2696 final int count = mRepeatCount;
2697 final int code = mKeyCode;
2698 if (receiver.onKeyMultiple(code, count, this)) {
2699 return true;
2700 }
2701 if (code != KeyEvent.KEYCODE_UNKNOWN) {
2702 mAction = ACTION_DOWN;
2703 mRepeatCount = 0;
2704 boolean handled = receiver.onKeyDown(code, this);
2705 if (handled) {
2706 mAction = ACTION_UP;
2707 receiver.onKeyUp(code, this);
2708 }
2709 mAction = ACTION_MULTIPLE;
2710 mRepeatCount = count;
2711 return handled;
2712 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002713 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002714 }
2715 return false;
2716 }
2717
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002718 /**
2719 * Use with {@link KeyEvent#dispatch(Callback, DispatcherState, Object)}
2720 * for more advanced key dispatching, such as long presses.
2721 */
2722 public static class DispatcherState {
2723 int mDownKeyCode;
2724 Object mDownTarget;
2725 SparseIntArray mActiveLongPresses = new SparseIntArray();
RoboErik01fe6612014-02-13 14:19:04 -08002726
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002727 /**
2728 * Reset back to initial state.
2729 */
2730 public void reset() {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002731 if (DEBUG) Log.v(TAG, "Reset: " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002732 mDownKeyCode = 0;
2733 mDownTarget = null;
2734 mActiveLongPresses.clear();
2735 }
RoboErik01fe6612014-02-13 14:19:04 -08002736
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002737 /**
2738 * Stop any tracking associated with this target.
2739 */
2740 public void reset(Object target) {
2741 if (mDownTarget == target) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002742 if (DEBUG) Log.v(TAG, "Reset in " + target + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002743 mDownKeyCode = 0;
2744 mDownTarget = null;
2745 }
2746 }
RoboErik01fe6612014-02-13 14:19:04 -08002747
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002748 /**
2749 * Start tracking the key code associated with the given event. This
2750 * can only be called on a key down. It will allow you to see any
2751 * long press associated with the key, and will result in
2752 * {@link KeyEvent#isTracking} return true on the long press and up
2753 * events.
RoboErik01fe6612014-02-13 14:19:04 -08002754 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002755 * <p>This is only needed if you are directly dispatching events, rather
2756 * than handling them in {@link Callback#onKeyDown}.
2757 */
2758 public void startTracking(KeyEvent event, Object target) {
2759 if (event.getAction() != ACTION_DOWN) {
2760 throw new IllegalArgumentException(
2761 "Can only start tracking on a down event");
2762 }
Dianne Hackborn8d374262009-09-14 21:21:52 -07002763 if (DEBUG) Log.v(TAG, "Start trackingt in " + target + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002764 mDownKeyCode = event.getKeyCode();
2765 mDownTarget = target;
2766 }
RoboErik01fe6612014-02-13 14:19:04 -08002767
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002768 /**
2769 * Return true if the key event is for a key code that is currently
2770 * being tracked by the dispatcher.
2771 */
2772 public boolean isTracking(KeyEvent event) {
2773 return mDownKeyCode == event.getKeyCode();
2774 }
RoboErik01fe6612014-02-13 14:19:04 -08002775
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002776 /**
2777 * Keep track of the given event's key code as having performed an
2778 * action with a long press, so no action should occur on the up.
2779 * <p>This is only needed if you are directly dispatching events, rather
2780 * than handling them in {@link Callback#onKeyLongPress}.
2781 */
2782 public void performedLongPress(KeyEvent event) {
2783 mActiveLongPresses.put(event.getKeyCode(), 1);
2784 }
RoboErik01fe6612014-02-13 14:19:04 -08002785
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002786 /**
2787 * Handle key up event to stop tracking. This resets the dispatcher state,
2788 * and updates the key event state based on it.
2789 * <p>This is only needed if you are directly dispatching events, rather
2790 * than handling them in {@link Callback#onKeyUp}.
2791 */
2792 public void handleUpEvent(KeyEvent event) {
2793 final int keyCode = event.getKeyCode();
Dianne Hackborn8d374262009-09-14 21:21:52 -07002794 if (DEBUG) Log.v(TAG, "Handle key up " + event + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002795 int index = mActiveLongPresses.indexOfKey(keyCode);
2796 if (index >= 0) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002797 if (DEBUG) Log.v(TAG, " Index: " + index);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002798 event.mFlags |= FLAG_CANCELED | FLAG_CANCELED_LONG_PRESS;
2799 mActiveLongPresses.removeAt(index);
2800 }
2801 if (mDownKeyCode == keyCode) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002802 if (DEBUG) Log.v(TAG, " Tracking!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002803 event.mFlags |= FLAG_TRACKING;
2804 mDownKeyCode = 0;
2805 mDownTarget = null;
2806 }
2807 }
2808 }
Jeff Brown497a92c2010-09-12 17:55:08 -07002809
2810 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 public String toString() {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002812 StringBuilder msg = new StringBuilder();
2813 msg.append("KeyEvent { action=").append(actionToString(mAction));
2814 msg.append(", keyCode=").append(keyCodeToString(mKeyCode));
2815 msg.append(", scanCode=").append(mScanCode);
2816 if (mCharacters != null) {
2817 msg.append(", characters=\"").append(mCharacters).append("\"");
2818 }
2819 msg.append(", metaState=").append(metaStateToString(mMetaState));
2820 msg.append(", flags=0x").append(Integer.toHexString(mFlags));
2821 msg.append(", repeatCount=").append(mRepeatCount);
2822 msg.append(", eventTime=").append(mEventTime);
2823 msg.append(", downTime=").append(mDownTime);
2824 msg.append(", deviceId=").append(mDeviceId);
2825 msg.append(", source=0x").append(Integer.toHexString(mSource));
2826 msg.append(" }");
2827 return msg.toString();
Jeff Brown497a92c2010-09-12 17:55:08 -07002828 }
2829
2830 /**
2831 * Returns a string that represents the symbolic name of the specified action
Jeff Brown6f2fba42011-02-19 01:08:02 -08002832 * such as "ACTION_DOWN", or an equivalent numeric constant such as "35" if unknown.
Jeff Brown497a92c2010-09-12 17:55:08 -07002833 *
2834 * @param action The action.
2835 * @return The symbolic name of the specified action.
2836 * @hide
2837 */
2838 public static String actionToString(int action) {
2839 switch (action) {
2840 case ACTION_DOWN:
2841 return "ACTION_DOWN";
2842 case ACTION_UP:
2843 return "ACTION_UP";
2844 case ACTION_MULTIPLE:
2845 return "ACTION_MULTIPLE";
2846 default:
2847 return Integer.toString(action);
2848 }
2849 }
2850
2851 /**
2852 * Returns a string that represents the symbolic name of the specified keycode
Jeff Brown6f2fba42011-02-19 01:08:02 -08002853 * such as "KEYCODE_A", "KEYCODE_DPAD_UP", or an equivalent numeric constant
2854 * such as "1001" if unknown.
Jeff Brown497a92c2010-09-12 17:55:08 -07002855 *
2856 * @param keyCode The key code.
2857 * @return The symbolic name of the specified keycode.
2858 *
2859 * @see KeyCharacterMap#getDisplayLabel
Jeff Brown497a92c2010-09-12 17:55:08 -07002860 */
2861 public static String keyCodeToString(int keyCode) {
Michael Wright337d9d22014-04-22 15:03:48 -07002862 String symbolicName = nativeKeyCodeToString(keyCode);
2863 return symbolicName != null ? LABEL_PREFIX + symbolicName : Integer.toString(keyCode);
Jeff Brown497a92c2010-09-12 17:55:08 -07002864 }
2865
2866 /**
Jeff Brown6f2fba42011-02-19 01:08:02 -08002867 * Gets a keycode by its symbolic name such as "KEYCODE_A" or an equivalent
2868 * numeric constant such as "1001".
Jeff Brown497a92c2010-09-12 17:55:08 -07002869 *
2870 * @param symbolicName The symbolic name of the keycode.
Jeff Brown6f2fba42011-02-19 01:08:02 -08002871 * @return The keycode or {@link #KEYCODE_UNKNOWN} if not found.
Michael Wright072137c2013-04-24 20:41:20 -07002872 * @see #keycodeToString(int)
Jeff Brown497a92c2010-09-12 17:55:08 -07002873 */
2874 public static int keyCodeFromString(String symbolicName) {
Michael Wright337d9d22014-04-22 15:03:48 -07002875 if (symbolicName.startsWith(LABEL_PREFIX)) {
2876 symbolicName = symbolicName.substring(LABEL_PREFIX.length());
Michael Wright973efa02014-05-13 15:38:56 -07002877 int keyCode = nativeKeyCodeFromString(symbolicName);
2878 if (keyCode > 0) {
2879 return keyCode;
2880 }
Jeff Brown497a92c2010-09-12 17:55:08 -07002881 }
Jeff Brown497a92c2010-09-12 17:55:08 -07002882 try {
Jeff Brown6f2fba42011-02-19 01:08:02 -08002883 return Integer.parseInt(symbolicName, 10);
Jeff Brown497a92c2010-09-12 17:55:08 -07002884 } catch (NumberFormatException ex) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08002885 return KEYCODE_UNKNOWN;
Jeff Brown497a92c2010-09-12 17:55:08 -07002886 }
2887 }
2888
2889 /**
2890 * Returns a string that represents the symbolic name of the specified combined meta
2891 * key modifier state flags such as "0", "META_SHIFT_ON",
Jeff Brown6f2fba42011-02-19 01:08:02 -08002892 * "META_ALT_ON|META_SHIFT_ON" or an equivalent numeric constant such as "0x10000000"
2893 * if unknown.
Jeff Brown497a92c2010-09-12 17:55:08 -07002894 *
2895 * @param metaState The meta state.
2896 * @return The symbolic name of the specified combined meta state flags.
2897 * @hide
2898 */
2899 public static String metaStateToString(int metaState) {
2900 if (metaState == 0) {
2901 return "0";
2902 }
2903 StringBuilder result = null;
2904 int i = 0;
2905 while (metaState != 0) {
2906 final boolean isSet = (metaState & 1) != 0;
2907 metaState >>>= 1; // unsigned shift!
2908 if (isSet) {
2909 final String name = META_SYMBOLIC_NAMES[i];
2910 if (result == null) {
2911 if (metaState == 0) {
2912 return name;
2913 }
2914 result = new StringBuilder(name);
2915 } else {
2916 result.append('|');
2917 result.append(name);
2918 }
2919 }
2920 i += 1;
2921 }
2922 return result.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002923 }
2924
2925 public static final Parcelable.Creator<KeyEvent> CREATOR
2926 = new Parcelable.Creator<KeyEvent>() {
2927 public KeyEvent createFromParcel(Parcel in) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07002928 in.readInt(); // skip token, we already know this is a KeyEvent
2929 return KeyEvent.createFromParcelBody(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002930 }
2931
2932 public KeyEvent[] newArray(int size) {
2933 return new KeyEvent[size];
2934 }
2935 };
RoboErik01fe6612014-02-13 14:19:04 -08002936
Jeff Brown6ec402b2010-07-28 15:48:59 -07002937 /** @hide */
2938 public static KeyEvent createFromParcelBody(Parcel in) {
2939 return new KeyEvent(in);
2940 }
RoboErik01fe6612014-02-13 14:19:04 -08002941
Jeff Brown6ec402b2010-07-28 15:48:59 -07002942 private KeyEvent(Parcel in) {
Jeff Brown91c69ab2011-02-14 17:03:18 -08002943 mDeviceId = in.readInt();
2944 mSource = in.readInt();
Jeff Brown6ec402b2010-07-28 15:48:59 -07002945 mAction = in.readInt();
2946 mKeyCode = in.readInt();
2947 mRepeatCount = in.readInt();
2948 mMetaState = in.readInt();
2949 mScanCode = in.readInt();
2950 mFlags = in.readInt();
2951 mDownTime = in.readLong();
2952 mEventTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953 }
2954
2955 public void writeToParcel(Parcel out, int flags) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07002956 out.writeInt(PARCEL_TOKEN_KEY_EVENT);
Jeff Brown91c69ab2011-02-14 17:03:18 -08002957
2958 out.writeInt(mDeviceId);
2959 out.writeInt(mSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002960 out.writeInt(mAction);
2961 out.writeInt(mKeyCode);
2962 out.writeInt(mRepeatCount);
2963 out.writeInt(mMetaState);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002964 out.writeInt(mScanCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002965 out.writeInt(mFlags);
2966 out.writeLong(mDownTime);
2967 out.writeLong(mEventTime);
2968 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969}