blob: 5d8f336ef19330127c05cc3a9dcbcfb4365f22ea [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.KeyData;
25
26/**
Jeff Browndc1ab4b2010-09-14 18:03:38 -070027 * Object used to report key and button events.
28 * <p>
29 * Each key press is described by a sequence of key events. A key press
30 * starts with a key event with {@link #ACTION_DOWN}. If the key is held
31 * sufficiently long that it repeats, then the initial down is followed
32 * additional key events with {@link #ACTION_DOWN} and a non-zero value for
33 * {@link #getRepeatCount()}. The last key event is a {@link #ACTION_UP}
34 * for the key up. If the key press is canceled, the key up event will have the
35 * {@link #FLAG_CANCELED} flag set.
36 * </p><p>
37 * Key events are generally accompanied by a key code ({@link #getKeyCode()}),
38 * scan code ({@link #getScanCode()}) and meta state ({@link #getMetaState()}).
39 * Key code constants are defined in this class. Scan code constants are raw
40 * device-specific codes obtained from the OS and so are not generally meaningful
41 * to applications unless interpreted using the {@link KeyCharacterMap}.
42 * Meta states describe the pressed state of key modifiers
43 * such as {@link #META_SHIFT_ON} or {@link #META_ALT_ON}.
44 * </p><p>
Jeff Brown497a92c2010-09-12 17:55:08 -070045 * Key codes typically correspond one-to-one with individual keys on an input device.
46 * Many keys and key combinations serve quite different functions on different
47 * input devices so care must be taken when interpreting them. Always use the
48 * {@link KeyCharacterMap} associated with the input device when mapping keys
49 * to characters. Be aware that there may be multiple key input devices active
50 * at the same time and each will have its own key character map.
51 * </p><p>
Jean Chalard405bc512012-05-29 19:12:34 +090052 * As soft input methods can use multiple and inventive ways of inputting text,
53 * there is no guarantee that any key press on a soft keyboard will generate a key
54 * event: this is left to the IME's discretion, and in fact sending such events is
55 * discouraged. You should never rely on receiving KeyEvents for any key on a soft
56 * input method. In particular, the default software keyboard will never send any
57 * key event to any application targetting Jelly Bean or later, and will only send
58 * events for some presses of the delete and return keys to applications targetting
59 * Ice Cream Sandwich or earlier. Be aware that other software input methods may
60 * never send key events regardless of the version. Consider using editor actions
61 * like {@link android.view.inputmethod.EditorInfo#IME_ACTION_DONE} if you need
62 * specific interaction with the software keyboard, as it gives more visibility to
63 * the user as to how your application will react to key presses.
64 * </p><p>
Jeff Browndc1ab4b2010-09-14 18:03:38 -070065 * When interacting with an IME, the framework may deliver key events
66 * with the special action {@link #ACTION_MULTIPLE} that either specifies
67 * that single repeated key code or a sequence of characters to insert.
68 * </p><p>
Jeff Brownb6997262010-10-08 22:31:17 -070069 * In general, the framework cannot guarantee that the key events it delivers
70 * to a view always constitute complete key sequences since some events may be dropped
71 * or modified by containing views before they are delivered. The view implementation
72 * should be prepared to handle {@link #FLAG_CANCELED} and should tolerate anomalous
73 * situations such as receiving a new {@link #ACTION_DOWN} without first having
74 * received an {@link #ACTION_UP} for the prior key press.
Jeff Browndc1ab4b2010-09-14 18:03:38 -070075 * </p><p>
76 * Refer to {@link InputDevice} for more information about how different kinds of
77 * input devices and sources represent keys and buttons.
78 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 */
Jeff Brownc5ed5912010-07-14 18:48:53 -070080public class KeyEvent extends InputEvent implements Parcelable {
Jeff Browndc1ab4b2010-09-14 18:03:38 -070081 /** Key code constant: Unknown key code. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 public static final int KEYCODE_UNKNOWN = 0;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070083 /** Key code constant: Soft Left key.
84 * Usually situated below the display on phones and used as a multi-function
85 * feature key for selecting a software defined function shown on the bottom left
86 * of the display. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 public static final int KEYCODE_SOFT_LEFT = 1;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070088 /** Key code constant: Soft Right key.
89 * Usually situated below the display on phones and used as a multi-function
90 * feature key for selecting a software defined function shown on the bottom right
91 * of the display. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 public static final int KEYCODE_SOFT_RIGHT = 2;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070093 /** Key code constant: Home key.
94 * This key is handled by the framework and is never delivered to applications. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 public static final int KEYCODE_HOME = 3;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070096 /** Key code constant: Back key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 public static final int KEYCODE_BACK = 4;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070098 /** Key code constant: Call key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 public static final int KEYCODE_CALL = 5;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700100 /** Key code constant: End Call key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 public static final int KEYCODE_ENDCALL = 6;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700102 /** Key code constant: '0' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 public static final int KEYCODE_0 = 7;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700104 /** Key code constant: '1' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 public static final int KEYCODE_1 = 8;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700106 /** Key code constant: '2' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 public static final int KEYCODE_2 = 9;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700108 /** Key code constant: '3' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 public static final int KEYCODE_3 = 10;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700110 /** Key code constant: '4' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 public static final int KEYCODE_4 = 11;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700112 /** Key code constant: '5' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 public static final int KEYCODE_5 = 12;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700114 /** Key code constant: '6' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 public static final int KEYCODE_6 = 13;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700116 /** Key code constant: '7' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 public static final int KEYCODE_7 = 14;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700118 /** Key code constant: '8' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 public static final int KEYCODE_8 = 15;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700120 /** Key code constant: '9' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 public static final int KEYCODE_9 = 16;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700122 /** Key code constant: '*' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 public static final int KEYCODE_STAR = 17;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700124 /** Key code constant: '#' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 public static final int KEYCODE_POUND = 18;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700126 /** Key code constant: Directional Pad Up key.
127 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 public static final int KEYCODE_DPAD_UP = 19;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700129 /** Key code constant: Directional Pad Down key.
130 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 public static final int KEYCODE_DPAD_DOWN = 20;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700132 /** Key code constant: Directional Pad Left key.
133 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 public static final int KEYCODE_DPAD_LEFT = 21;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700135 /** Key code constant: Directional Pad Right key.
136 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 public static final int KEYCODE_DPAD_RIGHT = 22;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700138 /** Key code constant: Directional Pad Center key.
139 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 public static final int KEYCODE_DPAD_CENTER = 23;
Jeff Brownb0418da2010-11-01 15:24:01 -0700141 /** Key code constant: Volume Up key.
142 * Adjusts the speaker volume up. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 public static final int KEYCODE_VOLUME_UP = 24;
Jeff Brownb0418da2010-11-01 15:24:01 -0700144 /** Key code constant: Volume Down key.
145 * Adjusts the speaker volume down. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 public static final int KEYCODE_VOLUME_DOWN = 25;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700147 /** Key code constant: Power key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 public static final int KEYCODE_POWER = 26;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700149 /** Key code constant: Camera key.
150 * Used to launch a camera application or take pictures. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 public static final int KEYCODE_CAMERA = 27;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700152 /** Key code constant: Clear key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 public static final int KEYCODE_CLEAR = 28;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700154 /** Key code constant: 'A' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 public static final int KEYCODE_A = 29;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700156 /** Key code constant: 'B' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 public static final int KEYCODE_B = 30;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700158 /** Key code constant: 'C' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 public static final int KEYCODE_C = 31;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700160 /** Key code constant: 'D' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 public static final int KEYCODE_D = 32;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700162 /** Key code constant: 'E' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 public static final int KEYCODE_E = 33;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700164 /** Key code constant: 'F' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 public static final int KEYCODE_F = 34;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700166 /** Key code constant: 'G' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 public static final int KEYCODE_G = 35;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700168 /** Key code constant: 'H' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 public static final int KEYCODE_H = 36;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700170 /** Key code constant: 'I' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 public static final int KEYCODE_I = 37;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700172 /** Key code constant: 'J' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 public static final int KEYCODE_J = 38;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700174 /** Key code constant: 'K' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 public static final int KEYCODE_K = 39;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700176 /** Key code constant: 'L' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 public static final int KEYCODE_L = 40;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700178 /** Key code constant: 'M' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 public static final int KEYCODE_M = 41;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700180 /** Key code constant: 'N' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 public static final int KEYCODE_N = 42;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700182 /** Key code constant: 'O' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 public static final int KEYCODE_O = 43;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700184 /** Key code constant: 'P' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 public static final int KEYCODE_P = 44;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700186 /** Key code constant: 'Q' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 public static final int KEYCODE_Q = 45;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700188 /** Key code constant: 'R' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 public static final int KEYCODE_R = 46;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700190 /** Key code constant: 'S' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 public static final int KEYCODE_S = 47;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700192 /** Key code constant: 'T' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 public static final int KEYCODE_T = 48;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700194 /** Key code constant: 'U' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 public static final int KEYCODE_U = 49;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700196 /** Key code constant: 'V' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 public static final int KEYCODE_V = 50;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700198 /** Key code constant: 'W' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 public static final int KEYCODE_W = 51;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700200 /** Key code constant: 'X' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 public static final int KEYCODE_X = 52;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700202 /** Key code constant: 'Y' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 public static final int KEYCODE_Y = 53;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700204 /** Key code constant: 'Z' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 public static final int KEYCODE_Z = 54;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700206 /** Key code constant: ',' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 public static final int KEYCODE_COMMA = 55;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700208 /** Key code constant: '.' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 public static final int KEYCODE_PERIOD = 56;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700210 /** Key code constant: Left Alt modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 public static final int KEYCODE_ALT_LEFT = 57;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700212 /** Key code constant: Right Alt modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 public static final int KEYCODE_ALT_RIGHT = 58;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700214 /** Key code constant: Left Shift modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 public static final int KEYCODE_SHIFT_LEFT = 59;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700216 /** Key code constant: Right Shift modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 public static final int KEYCODE_SHIFT_RIGHT = 60;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700218 /** Key code constant: Tab key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 public static final int KEYCODE_TAB = 61;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700220 /** Key code constant: Space key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 public static final int KEYCODE_SPACE = 62;
Jeff Brown224d4a12010-10-07 20:28:53 -0700222 /** Key code constant: Symbol modifier key.
223 * Used to enter alternate symbols. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 public static final int KEYCODE_SYM = 63;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700225 /** Key code constant: Explorer special function key.
226 * Used to launch a browser application. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 public static final int KEYCODE_EXPLORER = 64;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700228 /** Key code constant: Envelope special function key.
229 * Used to launch a mail application. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 public static final int KEYCODE_ENVELOPE = 65;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700231 /** Key code constant: Enter key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 public static final int KEYCODE_ENTER = 66;
Jeff Brown224d4a12010-10-07 20:28:53 -0700233 /** Key code constant: Backspace key.
Jeff Brown497a92c2010-09-12 17:55:08 -0700234 * Deletes characters before the insertion point, unlike {@link #KEYCODE_FORWARD_DEL}. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 public static final int KEYCODE_DEL = 67;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700236 /** Key code constant: '`' (backtick) key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 public static final int KEYCODE_GRAVE = 68;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700238 /** Key code constant: '-'. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 public static final int KEYCODE_MINUS = 69;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700240 /** Key code constant: '=' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 public static final int KEYCODE_EQUALS = 70;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700242 /** Key code constant: '[' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 public static final int KEYCODE_LEFT_BRACKET = 71;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700244 /** Key code constant: ']' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 public static final int KEYCODE_RIGHT_BRACKET = 72;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700246 /** Key code constant: '\' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 public static final int KEYCODE_BACKSLASH = 73;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700248 /** Key code constant: ';' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 public static final int KEYCODE_SEMICOLON = 74;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700250 /** Key code constant: ''' (apostrophe) key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 public static final int KEYCODE_APOSTROPHE = 75;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700252 /** Key code constant: '/' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 public static final int KEYCODE_SLASH = 76;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700254 /** Key code constant: '@' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 public static final int KEYCODE_AT = 77;
Jeff Brown224d4a12010-10-07 20:28:53 -0700256 /** Key code constant: Number modifier key.
257 * Used to enter numeric symbols.
258 * This key is not Num Lock; it is more like {@link #KEYCODE_ALT_LEFT} and is
259 * interpreted as an ALT key by {@link android.text.method.MetaKeyKeyListener}. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 public static final int KEYCODE_NUM = 78;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700261 /** Key code constant: Headset Hook key.
262 * Used to hang up calls and stop media. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 public static final int KEYCODE_HEADSETHOOK = 79;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700264 /** Key code constant: Camera Focus key.
265 * Used to focus the camera. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 public static final int KEYCODE_FOCUS = 80; // *Camera* focus
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700267 /** Key code constant: '+' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 public static final int KEYCODE_PLUS = 81;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700269 /** Key code constant: Menu key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 public static final int KEYCODE_MENU = 82;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700271 /** Key code constant: Notification key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 public static final int KEYCODE_NOTIFICATION = 83;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700273 /** Key code constant: Search key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 public static final int KEYCODE_SEARCH = 84;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700275 /** Key code constant: Play/Pause media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700276 public static final int KEYCODE_MEDIA_PLAY_PAUSE= 85;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700277 /** Key code constant: Stop media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700278 public static final int KEYCODE_MEDIA_STOP = 86;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700279 /** Key code constant: Play Next media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700280 public static final int KEYCODE_MEDIA_NEXT = 87;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700281 /** Key code constant: Play Previous media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700282 public static final int KEYCODE_MEDIA_PREVIOUS = 88;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700283 /** Key code constant: Rewind media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700284 public static final int KEYCODE_MEDIA_REWIND = 89;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700285 /** Key code constant: Fast Forward media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700286 public static final int KEYCODE_MEDIA_FAST_FORWARD = 90;
Jeff Brownb0418da2010-11-01 15:24:01 -0700287 /** Key code constant: Mute key.
288 * Mutes the microphone, unlike {@link #KEYCODE_VOLUME_MUTE}. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 public static final int KEYCODE_MUTE = 91;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700290 /** Key code constant: Page Up key. */
Chih-Wei Huang4fedd802009-05-27 15:52:50 +0800291 public static final int KEYCODE_PAGE_UP = 92;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700292 /** Key code constant: Page Down key. */
Chih-Wei Huang4fedd802009-05-27 15:52:50 +0800293 public static final int KEYCODE_PAGE_DOWN = 93;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700294 /** Key code constant: Picture Symbols modifier key.
295 * Used to switch symbol sets (Emoji, Kao-moji). */
mogimob032bc02009-10-03 03:13:56 +0900296 public static final int KEYCODE_PICTSYMBOLS = 94; // switch symbol-sets (Emoji,Kao-moji)
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700297 /** Key code constant: Switch Charset modifier key.
298 * Used to switch character sets (Kanji, Katakana). */
mogimob032bc02009-10-03 03:13:56 +0900299 public static final int KEYCODE_SWITCH_CHARSET = 95; // switch char-sets (Kanji,Katakana)
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700300 /** Key code constant: A Button key.
301 * On a game controller, the A button should be either the button labeled A
Michael Wright6b57bde2013-01-28 20:35:58 -0800302 * or the first button on the bottom row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700303 public static final int KEYCODE_BUTTON_A = 96;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700304 /** Key code constant: B Button key.
305 * On a game controller, the B button should be either the button labeled B
Michael Wright6b57bde2013-01-28 20:35:58 -0800306 * or the second button on the bottom row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700307 public static final int KEYCODE_BUTTON_B = 97;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700308 /** Key code constant: C Button key.
309 * On a game controller, the C button should be either the button labeled C
Michael Wright6b57bde2013-01-28 20:35:58 -0800310 * or the third button on the bottom row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700311 public static final int KEYCODE_BUTTON_C = 98;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700312 /** Key code constant: X Button key.
313 * On a game controller, the X button should be either the button labeled X
Michael Wright6b57bde2013-01-28 20:35:58 -0800314 * or the first button on the upper row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700315 public static final int KEYCODE_BUTTON_X = 99;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700316 /** Key code constant: Y Button key.
317 * On a game controller, the Y button should be either the button labeled Y
Michael Wright6b57bde2013-01-28 20:35:58 -0800318 * or the second button on the upper row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700319 public static final int KEYCODE_BUTTON_Y = 100;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700320 /** Key code constant: Z Button key.
321 * On a game controller, the Z button should be either the button labeled Z
Michael Wright6b57bde2013-01-28 20:35:58 -0800322 * or the third button on the upper row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700323 public static final int KEYCODE_BUTTON_Z = 101;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700324 /** Key code constant: L1 Button key.
325 * On a game controller, the L1 button should be either the button labeled L1 (or L)
326 * or the top left trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700327 public static final int KEYCODE_BUTTON_L1 = 102;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700328 /** Key code constant: R1 Button key.
329 * On a game controller, the R1 button should be either the button labeled R1 (or R)
330 * or the top right trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700331 public static final int KEYCODE_BUTTON_R1 = 103;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700332 /** Key code constant: L2 Button key.
333 * On a game controller, the L2 button should be either the button labeled L2
334 * or the bottom left trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700335 public static final int KEYCODE_BUTTON_L2 = 104;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700336 /** Key code constant: R2 Button key.
337 * On a game controller, the R2 button should be either the button labeled R2
338 * or the bottom right trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700339 public static final int KEYCODE_BUTTON_R2 = 105;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700340 /** Key code constant: Left Thumb Button key.
341 * On a game controller, the left thumb button indicates that the left (or only)
342 * joystick is pressed. */
Jeff Brownfd035822010-06-30 16:10:35 -0700343 public static final int KEYCODE_BUTTON_THUMBL = 106;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700344 /** Key code constant: Right Thumb Button key.
345 * On a game controller, the right thumb button indicates that the right
346 * joystick is pressed. */
Jeff Brownfd035822010-06-30 16:10:35 -0700347 public static final int KEYCODE_BUTTON_THUMBR = 107;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700348 /** Key code constant: Start Button key.
349 * On a game controller, the button labeled Start. */
Jeff Brownfd035822010-06-30 16:10:35 -0700350 public static final int KEYCODE_BUTTON_START = 108;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700351 /** Key code constant: Select Button key.
352 * On a game controller, the button labeled Select. */
Jeff Brownfd035822010-06-30 16:10:35 -0700353 public static final int KEYCODE_BUTTON_SELECT = 109;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700354 /** Key code constant: Mode Button key.
355 * On a game controller, the button labeled Mode. */
Jeff Brownfd035822010-06-30 16:10:35 -0700356 public static final int KEYCODE_BUTTON_MODE = 110;
Jeff Brown497a92c2010-09-12 17:55:08 -0700357 /** Key code constant: Escape key. */
358 public static final int KEYCODE_ESCAPE = 111;
359 /** Key code constant: Forward Delete key.
360 * Deletes characters ahead of the insertion point, unlike {@link #KEYCODE_DEL}. */
361 public static final int KEYCODE_FORWARD_DEL = 112;
362 /** Key code constant: Left Control modifier key. */
363 public static final int KEYCODE_CTRL_LEFT = 113;
364 /** Key code constant: Right Control modifier key. */
365 public static final int KEYCODE_CTRL_RIGHT = 114;
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800366 /** Key code constant: Caps Lock key. */
Jeff Brown497a92c2010-09-12 17:55:08 -0700367 public static final int KEYCODE_CAPS_LOCK = 115;
368 /** Key code constant: Scroll Lock key. */
369 public static final int KEYCODE_SCROLL_LOCK = 116;
370 /** Key code constant: Left Meta modifier key. */
371 public static final int KEYCODE_META_LEFT = 117;
372 /** Key code constant: Right Meta modifier key. */
373 public static final int KEYCODE_META_RIGHT = 118;
374 /** Key code constant: Function modifier key. */
375 public static final int KEYCODE_FUNCTION = 119;
376 /** Key code constant: System Request / Print Screen key. */
377 public static final int KEYCODE_SYSRQ = 120;
378 /** Key code constant: Break / Pause key. */
379 public static final int KEYCODE_BREAK = 121;
380 /** Key code constant: Home Movement key.
381 * Used for scrolling or moving the cursor around to the start of a line
382 * or to the top of a list. */
383 public static final int KEYCODE_MOVE_HOME = 122;
384 /** Key code constant: End Movement key.
385 * Used for scrolling or moving the cursor around to the end of a line
386 * or to the bottom of a list. */
387 public static final int KEYCODE_MOVE_END = 123;
388 /** Key code constant: Insert key.
389 * Toggles insert / overwrite edit mode. */
390 public static final int KEYCODE_INSERT = 124;
391 /** Key code constant: Forward key.
392 * Navigates forward in the history stack. Complement of {@link #KEYCODE_BACK}. */
393 public static final int KEYCODE_FORWARD = 125;
394 /** Key code constant: Play media key. */
395 public static final int KEYCODE_MEDIA_PLAY = 126;
396 /** Key code constant: Pause media key. */
397 public static final int KEYCODE_MEDIA_PAUSE = 127;
398 /** Key code constant: Close media key.
399 * May be used to close a CD tray, for example. */
400 public static final int KEYCODE_MEDIA_CLOSE = 128;
401 /** Key code constant: Eject media key.
402 * May be used to eject a CD tray, for example. */
403 public static final int KEYCODE_MEDIA_EJECT = 129;
404 /** Key code constant: Record media key. */
405 public static final int KEYCODE_MEDIA_RECORD = 130;
406 /** Key code constant: F1 key. */
407 public static final int KEYCODE_F1 = 131;
408 /** Key code constant: F2 key. */
409 public static final int KEYCODE_F2 = 132;
410 /** Key code constant: F3 key. */
411 public static final int KEYCODE_F3 = 133;
412 /** Key code constant: F4 key. */
413 public static final int KEYCODE_F4 = 134;
414 /** Key code constant: F5 key. */
415 public static final int KEYCODE_F5 = 135;
416 /** Key code constant: F6 key. */
417 public static final int KEYCODE_F6 = 136;
418 /** Key code constant: F7 key. */
419 public static final int KEYCODE_F7 = 137;
420 /** Key code constant: F8 key. */
421 public static final int KEYCODE_F8 = 138;
422 /** Key code constant: F9 key. */
423 public static final int KEYCODE_F9 = 139;
424 /** Key code constant: F10 key. */
425 public static final int KEYCODE_F10 = 140;
426 /** Key code constant: F11 key. */
427 public static final int KEYCODE_F11 = 141;
428 /** Key code constant: F12 key. */
429 public static final int KEYCODE_F12 = 142;
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800430 /** Key code constant: Num Lock key.
Jeff Brown497a92c2010-09-12 17:55:08 -0700431 * This is the Num Lock key; it is different from {@link #KEYCODE_NUM}.
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800432 * This key alters the behavior of other keys on the numeric keypad. */
Jeff Brown497a92c2010-09-12 17:55:08 -0700433 public static final int KEYCODE_NUM_LOCK = 143;
434 /** Key code constant: Numeric keypad '0' key. */
435 public static final int KEYCODE_NUMPAD_0 = 144;
436 /** Key code constant: Numeric keypad '1' key. */
437 public static final int KEYCODE_NUMPAD_1 = 145;
438 /** Key code constant: Numeric keypad '2' key. */
439 public static final int KEYCODE_NUMPAD_2 = 146;
440 /** Key code constant: Numeric keypad '3' key. */
441 public static final int KEYCODE_NUMPAD_3 = 147;
442 /** Key code constant: Numeric keypad '4' key. */
443 public static final int KEYCODE_NUMPAD_4 = 148;
444 /** Key code constant: Numeric keypad '5' key. */
445 public static final int KEYCODE_NUMPAD_5 = 149;
446 /** Key code constant: Numeric keypad '6' key. */
447 public static final int KEYCODE_NUMPAD_6 = 150;
448 /** Key code constant: Numeric keypad '7' key. */
449 public static final int KEYCODE_NUMPAD_7 = 151;
450 /** Key code constant: Numeric keypad '8' key. */
451 public static final int KEYCODE_NUMPAD_8 = 152;
452 /** Key code constant: Numeric keypad '9' key. */
453 public static final int KEYCODE_NUMPAD_9 = 153;
454 /** Key code constant: Numeric keypad '/' key (for division). */
455 public static final int KEYCODE_NUMPAD_DIVIDE = 154;
456 /** Key code constant: Numeric keypad '*' key (for multiplication). */
457 public static final int KEYCODE_NUMPAD_MULTIPLY = 155;
458 /** Key code constant: Numeric keypad '-' key (for subtraction). */
459 public static final int KEYCODE_NUMPAD_SUBTRACT = 156;
460 /** Key code constant: Numeric keypad '+' key (for addition). */
461 public static final int KEYCODE_NUMPAD_ADD = 157;
462 /** Key code constant: Numeric keypad '.' key (for decimals or digit grouping). */
463 public static final int KEYCODE_NUMPAD_DOT = 158;
464 /** Key code constant: Numeric keypad ',' key (for decimals or digit grouping). */
465 public static final int KEYCODE_NUMPAD_COMMA = 159;
466 /** Key code constant: Numeric keypad Enter key. */
467 public static final int KEYCODE_NUMPAD_ENTER = 160;
468 /** Key code constant: Numeric keypad '=' key. */
469 public static final int KEYCODE_NUMPAD_EQUALS = 161;
470 /** Key code constant: Numeric keypad '(' key. */
471 public static final int KEYCODE_NUMPAD_LEFT_PAREN = 162;
472 /** Key code constant: Numeric keypad ')' key. */
473 public static final int KEYCODE_NUMPAD_RIGHT_PAREN = 163;
Jeff Brownb0418da2010-11-01 15:24:01 -0700474 /** Key code constant: Volume Mute key.
475 * Mutes the speaker, unlike {@link #KEYCODE_MUTE}.
476 * This key should normally be implemented as a toggle such that the first press
477 * mutes the speaker and the second press restores the original volume. */
478 public static final int KEYCODE_VOLUME_MUTE = 164;
Jason Bayer3adf4902010-11-09 14:54:55 -0800479 /** Key code constant: Info key.
480 * Common on TV remotes to show additional information related to what is
481 * currently being viewed. */
482 public static final int KEYCODE_INFO = 165;
483 /** Key code constant: Channel up key.
484 * On TV remotes, increments the television channel. */
485 public static final int KEYCODE_CHANNEL_UP = 166;
486 /** Key code constant: Channel down key.
487 * On TV remotes, decrements the television channel. */
488 public static final int KEYCODE_CHANNEL_DOWN = 167;
489 /** Key code constant: Zoom in key. */
490 public static final int KEYCODE_ZOOM_IN = 168;
491 /** Key code constant: Zoom out key. */
492 public static final int KEYCODE_ZOOM_OUT = 169;
493 /** Key code constant: TV key.
494 * On TV remotes, switches to viewing live TV. */
495 public static final int KEYCODE_TV = 170;
496 /** Key code constant: Window key.
497 * On TV remotes, toggles picture-in-picture mode or other windowing functions. */
498 public static final int KEYCODE_WINDOW = 171;
499 /** Key code constant: Guide key.
500 * On TV remotes, shows a programming guide. */
501 public static final int KEYCODE_GUIDE = 172;
502 /** Key code constant: DVR key.
503 * On some TV remotes, switches to a DVR mode for recorded shows. */
504 public static final int KEYCODE_DVR = 173;
505 /** Key code constant: Bookmark key.
506 * On some TV remotes, bookmarks content or web pages. */
507 public static final int KEYCODE_BOOKMARK = 174;
508 /** Key code constant: Toggle captions key.
509 * Switches the mode for closed-captioning text, for example during television shows. */
510 public static final int KEYCODE_CAPTIONS = 175;
511 /** Key code constant: Settings key.
512 * Starts the system settings activity. */
513 public static final int KEYCODE_SETTINGS = 176;
514 /** Key code constant: TV power key.
515 * On TV remotes, toggles the power on a television screen. */
516 public static final int KEYCODE_TV_POWER = 177;
517 /** Key code constant: TV input key.
518 * On TV remotes, switches the input on a television screen. */
519 public static final int KEYCODE_TV_INPUT = 178;
520 /** Key code constant: Set-top-box power key.
521 * On TV remotes, toggles the power on an external Set-top-box. */
522 public static final int KEYCODE_STB_POWER = 179;
523 /** Key code constant: Set-top-box input key.
524 * On TV remotes, switches the input mode on an external Set-top-box. */
525 public static final int KEYCODE_STB_INPUT = 180;
526 /** Key code constant: A/V Receiver power key.
527 * On TV remotes, toggles the power on an external A/V Receiver. */
528 public static final int KEYCODE_AVR_POWER = 181;
529 /** Key code constant: A/V Receiver input key.
530 * On TV remotes, switches the input mode on an external A/V Receiver. */
531 public static final int KEYCODE_AVR_INPUT = 182;
532 /** Key code constant: Red "programmable" key.
533 * On TV remotes, acts as a contextual/programmable key. */
534 public static final int KEYCODE_PROG_RED = 183;
535 /** Key code constant: Green "programmable" key.
536 * On TV remotes, actsas a contextual/programmable key. */
537 public static final int KEYCODE_PROG_GREEN = 184;
538 /** Key code constant: Yellow "programmable" key.
539 * On TV remotes, acts as a contextual/programmable key. */
540 public static final int KEYCODE_PROG_YELLOW = 185;
541 /** Key code constant: Blue "programmable" key.
542 * On TV remotes, acts as a contextual/programmable key. */
543 public static final int KEYCODE_PROG_BLUE = 186;
Jeff Brown49ed71d2010-12-06 17:13:33 -0800544 /** Key code constant: App switch key.
545 * Should bring up the application switcher dialog. */
546 public static final int KEYCODE_APP_SWITCH = 187;
Jeff Browncb1404e2011-01-15 18:14:15 -0800547 /** Key code constant: Generic Game Pad Button #1.*/
548 public static final int KEYCODE_BUTTON_1 = 188;
549 /** Key code constant: Generic Game Pad Button #2.*/
550 public static final int KEYCODE_BUTTON_2 = 189;
551 /** Key code constant: Generic Game Pad Button #3.*/
552 public static final int KEYCODE_BUTTON_3 = 190;
553 /** Key code constant: Generic Game Pad Button #4.*/
554 public static final int KEYCODE_BUTTON_4 = 191;
555 /** Key code constant: Generic Game Pad Button #5.*/
556 public static final int KEYCODE_BUTTON_5 = 192;
557 /** Key code constant: Generic Game Pad Button #6.*/
558 public static final int KEYCODE_BUTTON_6 = 193;
559 /** Key code constant: Generic Game Pad Button #7.*/
560 public static final int KEYCODE_BUTTON_7 = 194;
561 /** Key code constant: Generic Game Pad Button #8.*/
562 public static final int KEYCODE_BUTTON_8 = 195;
563 /** Key code constant: Generic Game Pad Button #9.*/
564 public static final int KEYCODE_BUTTON_9 = 196;
565 /** Key code constant: Generic Game Pad Button #10.*/
566 public static final int KEYCODE_BUTTON_10 = 197;
567 /** Key code constant: Generic Game Pad Button #11.*/
568 public static final int KEYCODE_BUTTON_11 = 198;
569 /** Key code constant: Generic Game Pad Button #12.*/
570 public static final int KEYCODE_BUTTON_12 = 199;
571 /** Key code constant: Generic Game Pad Button #13.*/
572 public static final int KEYCODE_BUTTON_13 = 200;
573 /** Key code constant: Generic Game Pad Button #14.*/
574 public static final int KEYCODE_BUTTON_14 = 201;
575 /** Key code constant: Generic Game Pad Button #15.*/
576 public static final int KEYCODE_BUTTON_15 = 202;
577 /** Key code constant: Generic Game Pad Button #16.*/
578 public static final int KEYCODE_BUTTON_16 = 203;
Jeff Brown9812aed2011-03-07 17:09:51 -0800579 /** Key code constant: Language Switch key.
580 * Toggles the current input language such as switching between English and Japanese on
581 * a QWERTY keyboard. On some devices, the same function may be performed by
582 * pressing Shift+Spacebar. */
583 public static final int KEYCODE_LANGUAGE_SWITCH = 204;
584 /** Key code constant: Manner Mode key.
585 * Toggles silent or vibrate mode on and off to make the device behave more politely
586 * in certain settings such as on a crowded train. On some devices, the key may only
587 * operate when long-pressed. */
588 public static final int KEYCODE_MANNER_MODE = 205;
589 /** Key code constant: 3D Mode key.
590 * Toggles the display between 2D and 3D mode. */
591 public static final int KEYCODE_3D_MODE = 206;
Jeff Brown6651a632011-11-28 12:59:11 -0800592 /** Key code constant: Contacts special function key.
593 * Used to launch an address book application. */
594 public static final int KEYCODE_CONTACTS = 207;
595 /** Key code constant: Calendar special function key.
596 * Used to launch a calendar application. */
597 public static final int KEYCODE_CALENDAR = 208;
598 /** Key code constant: Music special function key.
599 * Used to launch a music player application. */
600 public static final int KEYCODE_MUSIC = 209;
601 /** Key code constant: Calculator special function key.
602 * Used to launch a calculator application. */
603 public static final int KEYCODE_CALCULATOR = 210;
Yang Chuang7511f9c2012-02-10 15:18:26 +0800604 /** Key code constant: Japanese full-width / half-width key. */
605 public static final int KEYCODE_ZENKAKU_HANKAKU = 211;
606 /** Key code constant: Japanese alphanumeric key. */
607 public static final int KEYCODE_EISU = 212;
608 /** Key code constant: Japanese non-conversion key. */
609 public static final int KEYCODE_MUHENKAN = 213;
610 /** Key code constant: Japanese conversion key. */
611 public static final int KEYCODE_HENKAN = 214;
612 /** Key code constant: Japanese katakana / hiragana key. */
613 public static final int KEYCODE_KATAKANA_HIRAGANA = 215;
614 /** Key code constant: Japanese Yen key. */
615 public static final int KEYCODE_YEN = 216;
616 /** Key code constant: Japanese Ro key. */
617 public static final int KEYCODE_RO = 217;
618 /** Key code constant: Japanese kana key. */
619 public static final int KEYCODE_KANA = 218;
Jeff Brownde7a8ea2012-06-13 18:28:57 -0700620 /** Key code constant: Assist key.
621 * Launches the global assist activity. Not delivered to applications. */
622 public static final int KEYCODE_ASSIST = 219;
Michael Wright1df477a2013-01-31 16:19:18 -0800623 /** Key code constant: Brightness Down key.
624 * Adjusts the screen brightness down. */
625 public static final int KEYCODE_BRIGHTNESS_DOWN = 220;
626 /** Key code constant: Brightness Up key.
627 * Adjusts the screen brightness up. */
628 public static final int KEYCODE_BRIGHTNESS_UP = 221;
Jeff Brown6212a492014-03-07 13:58:47 -0800629 /** Key code constant: Audio Track key.
Jaekyun Seokbfdad8e2013-07-08 13:53:21 +0900630 * Switches the audio tracks. */
631 public static final int KEYCODE_MEDIA_AUDIO_TRACK = 222;
Jeff Brown6212a492014-03-07 13:58:47 -0800632 /** Key code constant: Sleep key.
633 * Puts the device to sleep. Behaves somewhat like {@link #KEYCODE_POWER} but it
634 * has no effect if the device is already asleep. */
635 public static final int KEYCODE_SLEEP = 223;
636 /** Key code constant: Wakeup key.
637 * Wakes up the device. Behaves somewhat like {@link #KEYCODE_POWER} but it
638 * has no effect if the device is already awake. */
639 public static final int KEYCODE_WAKEUP = 224;
Tim Kilbourn87cd0dc2014-04-14 15:37:51 -0700640 /** Key code constant: Pairing key.
641 * Initiates peripheral pairing mode. Useful for pairing remote control
642 * devices or game controllers, especially if no other input mode is
643 * available. */
644 public static final int KEYCODE_PAIRING = 225;
Jinsuk Kim96658f72014-05-14 15:33:43 +0900645 /** Key code constant: Media Top Menu key.
646 * Goes to the top of media menu. */
647 public static final int KEYCODE_MEDIA_TOP_MENU = 226;
648 /** Key code constant: '11' key. */
649 public static final int KEYCODE_11 = 227;
650 /** Key code constant: '12' key. */
651 public static final int KEYCODE_12 = 228;
652 /** Key code constant: Last Channel key.
653 * Goes to the last viewed channel. */
654 public static final int KEYCODE_LAST_CHANNEL = 229;
655 /** Key code constant: TV data service key.
656 * Displays data services like weather, sports. */
657 public static final int KEYCODE_TV_DATA_SERVICE = 230;
Michael Wrightdc63f7b2014-08-21 19:05:21 -0700658 /** Key code constant: Voice Assist key.
659 * Launches the global voice assist activity. Not delivered to applications. */
660 public static final int KEYCODE_VOICE_ASSIST = 231;
ASAZU, Hidekidbd6aba2014-08-27 18:03:30 +0900661 /** Key code constant: Radio key.
662 * Toggles TV service / Radio service. */
663 public static final int KEYCODE_TV_RADIO_SERVICE = 232;
664 /** Key code constant: Teletext key.
665 * Displays Teletext service. */
666 public static final int KEYCODE_TV_TELETEXT = 233;
667 /** Key code constant: Number entry key.
668 * Initiates to enter multi-digit channel nubmber when each digit key is assigned
669 * for selecting separate channel. Corresponds to Number Entry Mode (0x1D) of CEC
670 * User Control Code. */
671 public static final int KEYCODE_TV_NUMBER_ENTRY = 234;
672 /** Key code constant: Analog Terrestrial key.
673 * Switches to analog terrestrial broadcast service. */
674 public static final int KEYCODE_TV_TERRESTRIAL_ANALOG = 235;
675 /** Key code constant: Digital Terrestrial key.
676 * Switches to digital terrestrial broadcast service. */
677 public static final int KEYCODE_TV_TERRESTRIAL_DIGITAL = 236;
678 /** Key code constant: Satellite key.
679 * Switches to digital satellite broadcast service. */
680 public static final int KEYCODE_TV_SATELLITE = 237;
681 /** Key code constant: BS key.
682 * Switches to BS digital satellite broadcasting service available in Japan. */
683 public static final int KEYCODE_TV_SATELLITE_BS = 238;
684 /** Key code constant: CS key.
685 * Switches to CS digital satellite broadcasting service available in Japan. */
686 public static final int KEYCODE_TV_SATELLITE_CS = 239;
687 /** Key code constant: BS/CS key.
688 * Toggles between BS and CS digital satellite services. */
689 public static final int KEYCODE_TV_SATELLITE_SERVICE = 240;
690 /** Key code constant: Toggle Network key.
691 * Toggles selecting broacast services. */
692 public static final int KEYCODE_TV_NETWORK = 241;
693 /** Key code constant: Antenna/Cable key.
694 * Toggles broadcast input source between antenna and cable. */
695 public static final int KEYCODE_TV_ANTENNA_CABLE = 242;
696 /** Key code constant: HDMI #1 key.
697 * Switches to HDMI input #1. */
698 public static final int KEYCODE_TV_INPUT_HDMI_1 = 243;
699 /** Key code constant: HDMI #2 key.
700 * Switches to HDMI input #2. */
701 public static final int KEYCODE_TV_INPUT_HDMI_2 = 244;
702 /** Key code constant: HDMI #3 key.
703 * Switches to HDMI input #3. */
704 public static final int KEYCODE_TV_INPUT_HDMI_3 = 245;
705 /** Key code constant: HDMI #4 key.
706 * Switches to HDMI input #4. */
707 public static final int KEYCODE_TV_INPUT_HDMI_4 = 246;
708 /** Key code constant: Composite #1 key.
709 * Switches to composite video input #1. */
710 public static final int KEYCODE_TV_INPUT_COMPOSITE_1 = 247;
711 /** Key code constant: Composite #2 key.
712 * Switches to composite video input #2. */
713 public static final int KEYCODE_TV_INPUT_COMPOSITE_2 = 248;
714 /** Key code constant: Component #1 key.
715 * Switches to component video input #1. */
716 public static final int KEYCODE_TV_INPUT_COMPONENT_1 = 249;
717 /** Key code constant: Component #2 key.
718 * Switches to component video input #2. */
719 public static final int KEYCODE_TV_INPUT_COMPONENT_2 = 250;
720 /** Key code constant: VGA #1 key.
721 * Switches to VGA (analog RGB) input #1. */
722 public static final int KEYCODE_TV_INPUT_VGA_1 = 251;
723 /** Key code constant: Audio description key.
724 * Toggles audio description off / on. */
725 public static final int KEYCODE_TV_AUDIO_DESCRIPTION = 252;
726 /** Key code constant: Audio description mixing volume up key.
727 * Louden audio description volume as compared with normal audio volume. */
728 public static final int KEYCODE_TV_AUDIO_DESCRIPTION_MIX_UP = 253;
729 /** Key code constant: Audio description mixing volume down key.
730 * Lessen audio description volume as compared with normal audio volume. */
731 public static final int KEYCODE_TV_AUDIO_DESCRIPTION_MIX_DOWN = 254;
732 /** Key code constant: Zoom mode key.
733 * Changes Zoom mode (Normal, Full, Zoom, Wide-zoom, etc.) */
734 public static final int KEYCODE_TV_ZOOM_MODE = 255;
735 /** Key code constant: Contents menu key.
736 * Goes to the title list. Corresponds to Contents Menu (0x0B) of CEC User Control
737 * Code */
738 public static final int KEYCODE_TV_CONTENTS_MENU = 256;
739 /** Key code constant: Media context menu key.
740 * Goes to the context menu of media contents. Corresponds to Media Context-sensitive
741 * Menu (0x11) of CEC User Control Code. */
742 public static final int KEYCODE_TV_MEDIA_CONTEXT_MENU = 257;
743 /** Key code constant: Timer programming key.
744 * Goes to the timer recording menu. Corresponds to Timer Programming (0x54) of
745 * CEC User Control Code. */
746 public static final int KEYCODE_TV_TIMER_PROGRAMMING = 258;
747 /** Key code constant: Help key. */
748 public static final int KEYCODE_HELP = 259;
Michael Wright962c9532015-08-06 15:16:22 +0100749 /** Key code constant: Navigate to previous key.
Joseph Cooper55b9ed42015-04-15 16:21:58 -0700750 * Goes backward by one item in an ordered collection of items. */
751 public static final int KEYCODE_NAVIGATE_PREVIOUS = 260;
Michael Wright962c9532015-08-06 15:16:22 +0100752 /** Key code constant: Navigate to next key.
Joseph Cooper55b9ed42015-04-15 16:21:58 -0700753 * Advances to the next item in an ordered collection of items. */
754 public static final int KEYCODE_NAVIGATE_NEXT = 261;
755 /** Key code constant: Navigate in key.
Michael Wright962c9532015-08-06 15:16:22 +0100756 * Activates the item that currently has focus or expands to the next level of a navigation
Joseph Cooper55b9ed42015-04-15 16:21:58 -0700757 * hierarchy. */
758 public static final int KEYCODE_NAVIGATE_IN = 262;
759 /** Key code constant: Navigate out key.
Michael Wright962c9532015-08-06 15:16:22 +0100760 * Backs out one level of a navigation hierarchy or collapses the item that currently has
Joseph Cooper55b9ed42015-04-15 16:21:58 -0700761 * focus. */
762 public static final int KEYCODE_NAVIGATE_OUT = 263;
Anthony Hugh9d826682015-06-23 10:44:17 -0700763 /** Key code constant: Primary stem key for Wear
764 * Main power/reset button on watch. */
765 public static final int KEYCODE_STEM_PRIMARY = 264;
766 /** Key code constant: Generic stem key 1 for Wear */
767 public static final int KEYCODE_STEM_1 = 265;
768 /** Key code constant: Generic stem key 2 for Wear */
769 public static final int KEYCODE_STEM_2 = 266;
770 /** Key code constant: Generic stem key 3 for Wear */
771 public static final int KEYCODE_STEM_3 = 267;
David Stevensa487f0c2015-07-31 11:00:50 -0700772 /** Key code constant: Directional Pad Up-Left */
773 public static final int KEYCODE_DPAD_UP_LEFT = 268;
774 /** Key code constant: Directional Pad Down-Left */
775 public static final int KEYCODE_DPAD_DOWN_LEFT = 269;
776 /** Key code constant: Directional Pad Up-Right */
777 public static final int KEYCODE_DPAD_UP_RIGHT = 270;
778 /** Key code constant: Directional Pad Down-Right */
779 public static final int KEYCODE_DPAD_DOWN_RIGHT = 271;
Michael Wright962c9532015-08-06 15:16:22 +0100780 /** Key code constant: Skip forward media key. */
781 public static final int KEYCODE_MEDIA_SKIP_FORWARD = 272;
782 /** Key code constant: Skip backward media key. */
783 public static final int KEYCODE_MEDIA_SKIP_BACKWARD = 273;
784 /** Key code constant: Step forward media key.
785 * Steps media forward, one frame at a time. */
786 public static final int KEYCODE_MEDIA_STEP_FORWARD = 274;
787 /** Key code constant: Step backward media key.
788 * Steps media backward, one frame at a time. */
789 public static final int KEYCODE_MEDIA_STEP_BACKWARD = 275;
Nick Armstrong-Crews3a5a8c72015-09-09 09:25:03 -0700790 /** Key code constant: put device to sleep unless a wakelock is held. */
Nick Armstrong-Crews56ecfcc2015-09-07 21:46:50 -0700791 public static final int KEYCODE_SOFT_SLEEP = 276;
Michael Wrightea84cff2015-10-21 18:08:30 +0100792 /** Key code constant: Cut key. */
793 public static final int KEYCODE_CUT = 277;
794 /** Key code constant: Copy key. */
795 public static final int KEYCODE_COPY = 278;
796 /** Key code constant: Paste key. */
797 public static final int KEYCODE_PASTE = 279;
Jim Miller07e03842016-06-22 15:18:13 -0700798 /** Key code constant: Consumed by the system for navigation up */
799 public static final int KEYCODE_SYSTEM_NAVIGATION_UP = 280;
800 /** Key code constant: Consumed by the system for navigation down */
801 public static final int KEYCODE_SYSTEM_NAVIGATION_DOWN = 281;
802 /** Key code constant: Consumed by the system for navigation left*/
803 public static final int KEYCODE_SYSTEM_NAVIGATION_LEFT = 282;
804 /** Key code constant: Consumed by the system for navigation right */
805 public static final int KEYCODE_SYSTEM_NAVIGATION_RIGHT = 283;
Jeff Brown497a92c2010-09-12 17:55:08 -0700806
Jim Miller07e03842016-06-22 15:18:13 -0700807 private static final int LAST_KEYCODE = KEYCODE_SYSTEM_NAVIGATION_RIGHT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808
809 // NOTE: If you add a new keycode here you must also add it to:
810 // isSystem()
Michael Wrightdc63f7b2014-08-21 19:05:21 -0700811 // isWakeKey()
Chirayu Desai61c37ae2013-04-15 20:11:37 +0530812 // frameworks/native/include/android/keycodes.h
Jinsuk Kim96658f72014-05-14 15:33:43 +0900813 // frameworks/native/include/input/InputEventLabels.h
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 // frameworks/base/core/res/res/values/attrs.xml
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 // emulator?
Jeff Brown6651a632011-11-28 12:59:11 -0800816 // LAST_KEYCODE
Dianne Hackborn935ae462009-04-13 16:11:55 -0700817 //
818 // Also Android currently does not reserve code ranges for vendor-
819 // specific key codes. If you have new key codes to have, you
820 // MUST contribute a patch to the open source project to define
821 // those new codes. This is intended to maintain a consistent
822 // set of key code definitions across all Android devices.
Jeff Brown497a92c2010-09-12 17:55:08 -0700823
Jeff Brown497a92c2010-09-12 17:55:08 -0700824 // Symbolic names of all metakeys in bit order from least significant to most significant.
825 // Accordingly there are exactly 32 values in this table.
826 private static final String[] META_SYMBOLIC_NAMES = new String[] {
827 "META_SHIFT_ON",
828 "META_ALT_ON",
829 "META_SYM_ON",
830 "META_FUNCTION_ON",
831 "META_ALT_LEFT_ON",
832 "META_ALT_RIGHT_ON",
833 "META_SHIFT_LEFT_ON",
834 "META_SHIFT_RIGHT_ON",
835 "META_CAP_LOCKED",
836 "META_ALT_LOCKED",
837 "META_SYM_LOCKED",
838 "0x00000800",
839 "META_CTRL_ON",
840 "META_CTRL_LEFT_ON",
841 "META_CTRL_RIGHT_ON",
842 "0x00008000",
843 "META_META_ON",
844 "META_META_LEFT_ON",
845 "META_META_RIGHT_ON",
846 "0x00080000",
Jeff Brown51e7fe72010-10-29 22:19:53 -0700847 "META_CAPS_LOCK_ON",
848 "META_NUM_LOCK_ON",
849 "META_SCROLL_LOCK_ON",
Jeff Brown497a92c2010-09-12 17:55:08 -0700850 "0x00800000",
851 "0x01000000",
852 "0x02000000",
853 "0x04000000",
854 "0x08000000",
855 "0x10000000",
856 "0x20000000",
857 "0x40000000",
858 "0x80000000",
859 };
860
Michael Wright337d9d22014-04-22 15:03:48 -0700861 private static final String LABEL_PREFIX = "KEYCODE_";
862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 /**
864 * @deprecated There are now more than MAX_KEYCODE keycodes.
865 * Use {@link #getMaxKeyCode()} instead.
866 */
867 @Deprecated
868 public static final int MAX_KEYCODE = 84;
869
870 /**
871 * {@link #getAction} value: the key has been pressed down.
872 */
873 public static final int ACTION_DOWN = 0;
874 /**
875 * {@link #getAction} value: the key has been released.
876 */
877 public static final int ACTION_UP = 1;
878 /**
879 * {@link #getAction} value: multiple duplicate key events have
880 * occurred in a row, or a complex string is being delivered. If the
881 * key code is not {#link {@link #KEYCODE_UNKNOWN} then the
882 * {#link {@link #getRepeatCount()} method returns the number of times
883 * the given key code should be executed.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700884 * Otherwise, if the key code is {@link #KEYCODE_UNKNOWN}, then
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 * this is a sequence of characters as returned by {@link #getCharacters}.
886 */
887 public static final int ACTION_MULTIPLE = 2;
888
889 /**
Jeff Brown497a92c2010-09-12 17:55:08 -0700890 * SHIFT key locked in CAPS mode.
891 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
892 * @hide
893 */
894 public static final int META_CAP_LOCKED = 0x100;
895
896 /**
897 * ALT 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_ALT_LOCKED = 0x200;
902
903 /**
904 * SYM key locked.
905 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
906 * @hide
907 */
908 public static final int META_SYM_LOCKED = 0x400;
909
910 /**
911 * Text is in selection mode.
912 * Reserved for use by {@link MetaKeyKeyListener} for a private unpublished constant
913 * in its API that is currently being retained for legacy reasons.
914 * @hide
915 */
916 public static final int META_SELECTING = 0x800;
917
918 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 * <p>This mask is used to check whether one of the ALT meta keys is pressed.</p>
920 *
921 * @see #isAltPressed()
922 * @see #getMetaState()
923 * @see #KEYCODE_ALT_LEFT
924 * @see #KEYCODE_ALT_RIGHT
925 */
926 public static final int META_ALT_ON = 0x02;
927
928 /**
929 * <p>This mask is used to check whether the left ALT meta key is pressed.</p>
930 *
931 * @see #isAltPressed()
932 * @see #getMetaState()
933 * @see #KEYCODE_ALT_LEFT
934 */
935 public static final int META_ALT_LEFT_ON = 0x10;
936
937 /**
938 * <p>This mask is used to check whether the right the ALT meta key is pressed.</p>
939 *
940 * @see #isAltPressed()
941 * @see #getMetaState()
942 * @see #KEYCODE_ALT_RIGHT
943 */
944 public static final int META_ALT_RIGHT_ON = 0x20;
945
946 /**
947 * <p>This mask is used to check whether one of the SHIFT meta keys is pressed.</p>
948 *
949 * @see #isShiftPressed()
950 * @see #getMetaState()
951 * @see #KEYCODE_SHIFT_LEFT
952 * @see #KEYCODE_SHIFT_RIGHT
953 */
954 public static final int META_SHIFT_ON = 0x1;
955
956 /**
957 * <p>This mask is used to check whether the left SHIFT meta key is pressed.</p>
958 *
959 * @see #isShiftPressed()
960 * @see #getMetaState()
961 * @see #KEYCODE_SHIFT_LEFT
962 */
963 public static final int META_SHIFT_LEFT_ON = 0x40;
964
965 /**
966 * <p>This mask is used to check whether the right SHIFT meta key is pressed.</p>
967 *
968 * @see #isShiftPressed()
969 * @see #getMetaState()
970 * @see #KEYCODE_SHIFT_RIGHT
971 */
972 public static final int META_SHIFT_RIGHT_ON = 0x80;
973
974 /**
975 * <p>This mask is used to check whether the SYM meta key is pressed.</p>
976 *
977 * @see #isSymPressed()
978 * @see #getMetaState()
979 */
980 public static final int META_SYM_ON = 0x4;
981
982 /**
Jeff Brown497a92c2010-09-12 17:55:08 -0700983 * <p>This mask is used to check whether the FUNCTION meta key is pressed.</p>
984 *
985 * @see #isFunctionPressed()
986 * @see #getMetaState()
987 */
988 public static final int META_FUNCTION_ON = 0x8;
989
990 /**
991 * <p>This mask is used to check whether one of the CTRL meta keys is pressed.</p>
992 *
993 * @see #isCtrlPressed()
994 * @see #getMetaState()
995 * @see #KEYCODE_CTRL_LEFT
996 * @see #KEYCODE_CTRL_RIGHT
997 */
998 public static final int META_CTRL_ON = 0x1000;
999
1000 /**
1001 * <p>This mask is used to check whether the left CTRL meta key is pressed.</p>
1002 *
1003 * @see #isCtrlPressed()
1004 * @see #getMetaState()
1005 * @see #KEYCODE_CTRL_LEFT
1006 */
1007 public static final int META_CTRL_LEFT_ON = 0x2000;
1008
1009 /**
1010 * <p>This mask is used to check whether the right CTRL meta key is pressed.</p>
1011 *
1012 * @see #isCtrlPressed()
1013 * @see #getMetaState()
1014 * @see #KEYCODE_CTRL_RIGHT
1015 */
1016 public static final int META_CTRL_RIGHT_ON = 0x4000;
1017
1018 /**
1019 * <p>This mask is used to check whether one of the META meta keys is pressed.</p>
1020 *
1021 * @see #isMetaPressed()
1022 * @see #getMetaState()
1023 * @see #KEYCODE_META_LEFT
1024 * @see #KEYCODE_META_RIGHT
1025 */
1026 public static final int META_META_ON = 0x10000;
1027
1028 /**
1029 * <p>This mask is used to check whether the left META meta key is pressed.</p>
1030 *
1031 * @see #isMetaPressed()
1032 * @see #getMetaState()
1033 * @see #KEYCODE_META_LEFT
1034 */
1035 public static final int META_META_LEFT_ON = 0x20000;
1036
1037 /**
1038 * <p>This mask is used to check whether the right META meta key is pressed.</p>
1039 *
1040 * @see #isMetaPressed()
1041 * @see #getMetaState()
1042 * @see #KEYCODE_META_RIGHT
1043 */
1044 public static final int META_META_RIGHT_ON = 0x40000;
1045
1046 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07001047 * <p>This mask is used to check whether the CAPS LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07001048 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07001049 * @see #isCapsLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -07001050 * @see #getMetaState()
1051 * @see #KEYCODE_CAPS_LOCK
1052 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07001053 public static final int META_CAPS_LOCK_ON = 0x100000;
Jeff Brown497a92c2010-09-12 17:55:08 -07001054
1055 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07001056 * <p>This mask is used to check whether the NUM LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07001057 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07001058 * @see #isNumLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -07001059 * @see #getMetaState()
1060 * @see #KEYCODE_NUM_LOCK
1061 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07001062 public static final int META_NUM_LOCK_ON = 0x200000;
Jeff Brown497a92c2010-09-12 17:55:08 -07001063
1064 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07001065 * <p>This mask is used to check whether the SCROLL LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07001066 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07001067 * @see #isScrollLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -07001068 * @see #getMetaState()
1069 * @see #KEYCODE_SCROLL_LOCK
1070 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07001071 public static final int META_SCROLL_LOCK_ON = 0x400000;
Jeff Brown497a92c2010-09-12 17:55:08 -07001072
Jeff Brown64da12a2011-01-04 19:57:47 -08001073 /**
1074 * This mask is a combination of {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON}
1075 * and {@link #META_SHIFT_RIGHT_ON}.
1076 */
Jeff Brownc1df9072010-12-21 16:38:50 -08001077 public static final int META_SHIFT_MASK = META_SHIFT_ON
1078 | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON;
1079
Jeff Brown64da12a2011-01-04 19:57:47 -08001080 /**
1081 * This mask is a combination of {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON}
1082 * and {@link #META_ALT_RIGHT_ON}.
1083 */
Jeff Brownc1df9072010-12-21 16:38:50 -08001084 public static final int META_ALT_MASK = META_ALT_ON
1085 | META_ALT_LEFT_ON | META_ALT_RIGHT_ON;
1086
Jeff Brown64da12a2011-01-04 19:57:47 -08001087 /**
1088 * This mask is a combination of {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON}
1089 * and {@link #META_CTRL_RIGHT_ON}.
1090 */
Jeff Brownc1df9072010-12-21 16:38:50 -08001091 public static final int META_CTRL_MASK = META_CTRL_ON
1092 | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON;
1093
Jeff Brown64da12a2011-01-04 19:57:47 -08001094 /**
1095 * This mask is a combination of {@link #META_META_ON}, {@link #META_META_LEFT_ON}
1096 * and {@link #META_META_RIGHT_ON}.
1097 */
1098 public static final int META_META_MASK = META_META_ON
Jeff Brownc1df9072010-12-21 16:38:50 -08001099 | META_META_LEFT_ON | META_META_RIGHT_ON;
1100
Jeff Brown497a92c2010-09-12 17:55:08 -07001101 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 * This mask is set if the device woke because of this key event.
Jeff Brown037c33e2014-04-09 00:31:55 -07001103 *
1104 * @deprecated This flag will never be set by the system since the system
1105 * consumes all wake keys itself.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 */
Jeff Brown037c33e2014-04-09 00:31:55 -07001107 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 public static final int FLAG_WOKE_HERE = 0x1;
RoboErik01fe6612014-02-13 14:19:04 -08001109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 /**
1111 * This mask is set if the key event was generated by a software keyboard.
1112 */
1113 public static final int FLAG_SOFT_KEYBOARD = 0x2;
RoboErik01fe6612014-02-13 14:19:04 -08001114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 /**
1116 * This mask is set if we don't want the key event to cause us to leave
1117 * touch mode.
1118 */
1119 public static final int FLAG_KEEP_TOUCH_MODE = 0x4;
RoboErik01fe6612014-02-13 14:19:04 -08001120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001122 * This mask is set if an event was known to come from a trusted part
1123 * of the system. That is, the event is known to come from the user,
1124 * and could not have been spoofed by a third party component.
1125 */
1126 public static final int FLAG_FROM_SYSTEM = 0x8;
RoboErik01fe6612014-02-13 14:19:04 -08001127
The Android Open Source Project10592532009-03-18 17:39:46 -07001128 /**
1129 * This mask is used for compatibility, to identify enter keys that are
1130 * coming from an IME whose enter key has been auto-labelled "next" or
1131 * "done". This allows TextView to dispatch these as normal enter keys
1132 * for old applications, but still do the appropriate action when
1133 * receiving them.
1134 */
1135 public static final int FLAG_EDITOR_ACTION = 0x10;
RoboErik01fe6612014-02-13 14:19:04 -08001136
The Android Open Source Project10592532009-03-18 17:39:46 -07001137 /**
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07001138 * When associated with up key events, this indicates that the key press
1139 * has been canceled. Typically this is used with virtual touch screen
1140 * keys, where the user can slide from the virtual key area on to the
1141 * display: in that case, the application will receive a canceled up
1142 * event and should not perform the action normally associated with the
1143 * key. Note that for this to work, the application can not perform an
1144 * action for a key until it receives an up or the long press timeout has
RoboErik01fe6612014-02-13 14:19:04 -08001145 * expired.
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07001146 */
1147 public static final int FLAG_CANCELED = 0x20;
RoboErik01fe6612014-02-13 14:19:04 -08001148
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07001149 /**
1150 * This key event was generated by a virtual (on-screen) hard key area.
1151 * Typically this is an area of the touchscreen, outside of the regular
1152 * display, dedicated to "hardware" buttons.
1153 */
1154 public static final int FLAG_VIRTUAL_HARD_KEY = 0x40;
RoboErik01fe6612014-02-13 14:19:04 -08001155
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07001156 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001157 * This flag is set for the first key repeat that occurs after the
1158 * long press timeout.
1159 */
1160 public static final int FLAG_LONG_PRESS = 0x80;
RoboErik01fe6612014-02-13 14:19:04 -08001161
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001162 /**
1163 * Set when a key event has {@link #FLAG_CANCELED} set because a long
RoboErik01fe6612014-02-13 14:19:04 -08001164 * press action was executed while it was down.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001165 */
1166 public static final int FLAG_CANCELED_LONG_PRESS = 0x100;
RoboErik01fe6612014-02-13 14:19:04 -08001167
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001168 /**
1169 * Set for {@link #ACTION_UP} when this event's key code is still being
1170 * tracked from its initial down. That is, somebody requested that tracking
1171 * started on the key down and a long press has not caused
1172 * the tracking to be canceled.
1173 */
1174 public static final int FLAG_TRACKING = 0x200;
Jeff Brown49ed71d2010-12-06 17:13:33 -08001175
1176 /**
1177 * Set when a key event has been synthesized to implement default behavior
1178 * for an event that the application did not handle.
1179 * Fallback key events are generated by unhandled trackball motions
1180 * (to emulate a directional keypad) and by certain unhandled key presses
1181 * that are declared in the key map (such as special function numeric keypad
1182 * keys when numlock is off).
1183 */
1184 public static final int FLAG_FALLBACK = 0x400;
1185
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001186 /**
Michael Wrighta44dd262013-04-10 21:12:00 -07001187 * Signifies that the key is being predispatched.
1188 * @hide
1189 */
1190 public static final int FLAG_PREDISPATCH = 0x20000000;
1191
1192 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001193 * Private control to determine when an app is tracking a key sequence.
1194 * @hide
1195 */
1196 public static final int FLAG_START_TRACKING = 0x40000000;
Jeff Brown21bc5c92011-02-28 18:27:14 -08001197
1198 /**
1199 * Private flag that indicates when the system has detected that this key event
1200 * may be inconsistent with respect to the sequence of previously delivered key events,
1201 * such as when a key up event is sent but the key was not down.
1202 *
1203 * @hide
1204 * @see #isTainted
1205 * @see #setTainted
1206 */
1207 public static final int FLAG_TAINTED = 0x80000000;
1208
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001209 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 * Returns the maximum keycode.
1211 */
1212 public static int getMaxKeyCode() {
1213 return LAST_KEYCODE;
1214 }
1215
1216 /**
1217 * Get the character that is produced by putting accent on the character
1218 * c.
1219 * For example, getDeadChar('`', 'e') returns &egrave;.
1220 */
1221 public static int getDeadChar(int accent, int c) {
1222 return KeyCharacterMap.getDeadChar(accent, c);
1223 }
RoboErik01fe6612014-02-13 14:19:04 -08001224
Dianne Hackborn8d374262009-09-14 21:21:52 -07001225 static final boolean DEBUG = false;
1226 static final String TAG = "KeyEvent";
Jeff Brown1f245102010-11-18 20:53:46 -08001227
1228 private static final int MAX_RECYCLED = 10;
1229 private static final Object gRecyclerLock = new Object();
1230 private static int gRecyclerUsed;
1231 private static KeyEvent gRecyclerTop;
1232
1233 private KeyEvent mNext;
Jeff Brown1f245102010-11-18 20:53:46 -08001234
Jeff Brown91c69ab2011-02-14 17:03:18 -08001235 private int mDeviceId;
1236 private int mSource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 private int mMetaState;
1238 private int mAction;
1239 private int mKeyCode;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001240 private int mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 private int mRepeatCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 private int mFlags;
1243 private long mDownTime;
1244 private long mEventTime;
1245 private String mCharacters;
1246
1247 public interface Callback {
1248 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001249 * Called when a key down event has occurred. If you return true,
1250 * you can first call {@link KeyEvent#startTracking()
1251 * KeyEvent.startTracking()} to have the framework track the event
1252 * through its {@link #onKeyUp(int, KeyEvent)} and also call your
1253 * {@link #onKeyLongPress(int, KeyEvent)} if it occurs.
RoboErik01fe6612014-02-13 14:19:04 -08001254 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 * @param keyCode The value in event.getKeyCode().
1256 * @param event Description of the key event.
RoboErik01fe6612014-02-13 14:19:04 -08001257 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 * @return If you handled the event, return true. If you want to allow
1259 * the event to be handled by the next receiver, return false.
1260 */
1261 boolean onKeyDown(int keyCode, KeyEvent event);
1262
1263 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001264 * Called when a long press has occurred. If you return true,
1265 * the final key up will have {@link KeyEvent#FLAG_CANCELED} and
1266 * {@link KeyEvent#FLAG_CANCELED_LONG_PRESS} set. Note that in
1267 * order to receive this callback, someone in the event change
1268 * <em>must</em> return true from {@link #onKeyDown} <em>and</em>
1269 * call {@link KeyEvent#startTracking()} on the event.
RoboErik01fe6612014-02-13 14:19:04 -08001270 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001271 * @param keyCode The value in event.getKeyCode().
1272 * @param event Description of the key event.
RoboErik01fe6612014-02-13 14:19:04 -08001273 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001274 * @return If you handled the event, return true. If you want to allow
1275 * the event to be handled by the next receiver, return false.
1276 */
1277 boolean onKeyLongPress(int keyCode, KeyEvent event);
1278
1279 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 * Called when a key up event has occurred.
RoboErik01fe6612014-02-13 14:19:04 -08001281 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 * @param keyCode The value in event.getKeyCode().
1283 * @param event Description of the key event.
RoboErik01fe6612014-02-13 14:19:04 -08001284 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 * @return If you handled the event, return true. If you want to allow
1286 * the event to be handled by the next receiver, return false.
1287 */
1288 boolean onKeyUp(int keyCode, KeyEvent event);
1289
1290 /**
Kevin Hufnagleb248b1f2016-09-13 19:36:20 -07001291 * Called when a user's interaction with an analog control, such as
1292 * flinging a trackball, generates simulated down/up events for the same
1293 * key multiple times in quick succession.
RoboErik01fe6612014-02-13 14:19:04 -08001294 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 * @param keyCode The value in event.getKeyCode().
1296 * @param count Number of pairs as returned by event.getRepeatCount().
1297 * @param event Description of the key event.
RoboErik01fe6612014-02-13 14:19:04 -08001298 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 * @return If you handled the event, return true. If you want to allow
1300 * the event to be handled by the next receiver, return false.
1301 */
1302 boolean onKeyMultiple(int keyCode, int count, KeyEvent event);
1303 }
1304
Michael Wright337d9d22014-04-22 15:03:48 -07001305 private static native String nativeKeyCodeToString(int keyCode);
1306 private static native int nativeKeyCodeFromString(String keyCode);
Jeff Brown497a92c2010-09-12 17:55:08 -07001307
Jeff Brown1f245102010-11-18 20:53:46 -08001308 private KeyEvent() {
1309 }
1310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 /**
1312 * Create a new key event.
RoboErik01fe6612014-02-13 14:19:04 -08001313 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 * @param action Action code: either {@link #ACTION_DOWN},
1315 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1316 * @param code The key code.
1317 */
1318 public KeyEvent(int action, int code) {
1319 mAction = action;
1320 mKeyCode = code;
1321 mRepeatCount = 0;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001322 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 }
1324
1325 /**
1326 * Create a new key event.
RoboErik01fe6612014-02-13 14:19:04 -08001327 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1329 * at which this key code originally went down.
1330 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1331 * at which this event happened.
1332 * @param action Action code: either {@link #ACTION_DOWN},
1333 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1334 * @param code The key code.
1335 * @param repeat A repeat count for down events (> 0 if this is after the
1336 * initial down) or event count for multiple events.
1337 */
1338 public KeyEvent(long downTime, long eventTime, int action,
1339 int code, int repeat) {
1340 mDownTime = downTime;
1341 mEventTime = eventTime;
1342 mAction = action;
1343 mKeyCode = code;
1344 mRepeatCount = repeat;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001345 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 }
1347
1348 /**
1349 * Create a new key event.
RoboErik01fe6612014-02-13 14:19:04 -08001350 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1352 * at which this key code originally went down.
1353 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1354 * at which this event happened.
1355 * @param action Action code: either {@link #ACTION_DOWN},
1356 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1357 * @param code The key code.
1358 * @param repeat A repeat count for down events (> 0 if this is after the
1359 * initial down) or event count for multiple events.
1360 * @param metaState Flags indicating which meta keys are currently pressed.
1361 */
1362 public KeyEvent(long downTime, long eventTime, int action,
1363 int code, int repeat, int metaState) {
1364 mDownTime = downTime;
1365 mEventTime = eventTime;
1366 mAction = action;
1367 mKeyCode = code;
1368 mRepeatCount = repeat;
1369 mMetaState = metaState;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001370 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 }
1372
1373 /**
1374 * Create a new key event.
RoboErik01fe6612014-02-13 14:19:04 -08001375 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1377 * at which this key code originally went down.
1378 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1379 * at which this event happened.
1380 * @param action Action code: either {@link #ACTION_DOWN},
1381 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1382 * @param code The key code.
1383 * @param repeat A repeat count for down events (> 0 if this is after the
1384 * initial down) or event count for multiple events.
1385 * @param metaState Flags indicating which meta keys are currently pressed.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001386 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 * @param scancode Raw device scan code of the event.
1388 */
1389 public KeyEvent(long downTime, long eventTime, int action,
1390 int code, int repeat, int metaState,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001391 int deviceId, int scancode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 mDownTime = downTime;
1393 mEventTime = eventTime;
1394 mAction = action;
1395 mKeyCode = code;
1396 mRepeatCount = repeat;
1397 mMetaState = metaState;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001398 mDeviceId = deviceId;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001399 mScanCode = scancode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 }
1401
1402 /**
1403 * Create a new key event.
RoboErik01fe6612014-02-13 14:19:04 -08001404 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1406 * at which this key code originally went down.
1407 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1408 * at which this event happened.
1409 * @param action Action code: either {@link #ACTION_DOWN},
1410 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1411 * @param code The key code.
1412 * @param repeat A repeat count for down events (> 0 if this is after the
1413 * initial down) or event count for multiple events.
1414 * @param metaState Flags indicating which meta keys are currently pressed.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001415 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 * @param scancode Raw device scan code of the event.
1417 * @param flags The flags for this key event
1418 */
1419 public KeyEvent(long downTime, long eventTime, int action,
1420 int code, int repeat, int metaState,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001421 int deviceId, int scancode, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 mDownTime = downTime;
1423 mEventTime = eventTime;
1424 mAction = action;
1425 mKeyCode = code;
1426 mRepeatCount = repeat;
1427 mMetaState = metaState;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001428 mDeviceId = deviceId;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001429 mScanCode = scancode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 mFlags = flags;
1431 }
1432
1433 /**
Jeff Brownc5ed5912010-07-14 18:48:53 -07001434 * Create a new key event.
RoboErik01fe6612014-02-13 14:19:04 -08001435 *
Jeff Brownc5ed5912010-07-14 18:48:53 -07001436 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1437 * at which this key code originally went down.
1438 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1439 * at which this event happened.
1440 * @param action Action code: either {@link #ACTION_DOWN},
1441 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1442 * @param code The key code.
1443 * @param repeat A repeat count for down events (> 0 if this is after the
1444 * initial down) or event count for multiple events.
1445 * @param metaState Flags indicating which meta keys are currently pressed.
1446 * @param deviceId The device ID that generated the key event.
1447 * @param scancode Raw device scan code of the event.
1448 * @param flags The flags for this key event
1449 * @param source The input source such as {@link InputDevice#SOURCE_KEYBOARD}.
1450 */
1451 public KeyEvent(long downTime, long eventTime, int action,
1452 int code, int repeat, int metaState,
1453 int deviceId, int scancode, int flags, int source) {
1454 mDownTime = downTime;
1455 mEventTime = eventTime;
1456 mAction = action;
1457 mKeyCode = code;
1458 mRepeatCount = repeat;
1459 mMetaState = metaState;
1460 mDeviceId = deviceId;
1461 mScanCode = scancode;
1462 mFlags = flags;
1463 mSource = source;
1464 }
1465
1466 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 * Create a new key event for a string of characters. The key code,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001468 * action, repeat count and source will automatically be set to
1469 * {@link #KEYCODE_UNKNOWN}, {@link #ACTION_MULTIPLE}, 0, and
1470 * {@link InputDevice#SOURCE_KEYBOARD} for you.
RoboErik01fe6612014-02-13 14:19:04 -08001471 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 * @param time The time (in {@link android.os.SystemClock#uptimeMillis})
1473 * at which this event occured.
1474 * @param characters The string of characters.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001475 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 * @param flags The flags for this key event
1477 */
Jeff Brownc5ed5912010-07-14 18:48:53 -07001478 public KeyEvent(long time, String characters, int deviceId, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 mDownTime = time;
1480 mEventTime = time;
1481 mCharacters = characters;
1482 mAction = ACTION_MULTIPLE;
1483 mKeyCode = KEYCODE_UNKNOWN;
1484 mRepeatCount = 0;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001485 mDeviceId = deviceId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 mFlags = flags;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001487 mSource = InputDevice.SOURCE_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 }
1489
1490 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001491 * Make an exact copy of an existing key event.
1492 */
1493 public KeyEvent(KeyEvent origEvent) {
1494 mDownTime = origEvent.mDownTime;
1495 mEventTime = origEvent.mEventTime;
1496 mAction = origEvent.mAction;
1497 mKeyCode = origEvent.mKeyCode;
1498 mRepeatCount = origEvent.mRepeatCount;
1499 mMetaState = origEvent.mMetaState;
1500 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001501 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001502 mScanCode = origEvent.mScanCode;
The Android Open Source Project10592532009-03-18 17:39:46 -07001503 mFlags = origEvent.mFlags;
1504 mCharacters = origEvent.mCharacters;
1505 }
1506
1507 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 * Copy an existing key event, modifying its time and repeat count.
RoboErik01fe6612014-02-13 14:19:04 -08001509 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001510 * @deprecated Use {@link #changeTimeRepeat(KeyEvent, long, int)}
1511 * instead.
RoboErik01fe6612014-02-13 14:19:04 -08001512 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 * @param origEvent The existing event to be copied.
1514 * @param eventTime The new event time
1515 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1516 * @param newRepeat The new repeat count of the event.
1517 */
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001518 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 public KeyEvent(KeyEvent origEvent, long eventTime, int newRepeat) {
1520 mDownTime = origEvent.mDownTime;
1521 mEventTime = eventTime;
1522 mAction = origEvent.mAction;
1523 mKeyCode = origEvent.mKeyCode;
1524 mRepeatCount = newRepeat;
1525 mMetaState = origEvent.mMetaState;
1526 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001527 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001528 mScanCode = origEvent.mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 mFlags = origEvent.mFlags;
1530 mCharacters = origEvent.mCharacters;
1531 }
1532
Jeff Brown1f245102010-11-18 20:53:46 -08001533 private static KeyEvent obtain() {
1534 final KeyEvent ev;
1535 synchronized (gRecyclerLock) {
1536 ev = gRecyclerTop;
1537 if (ev == null) {
1538 return new KeyEvent();
1539 }
1540 gRecyclerTop = ev.mNext;
1541 gRecyclerUsed -= 1;
1542 }
Jeff Brown1f245102010-11-18 20:53:46 -08001543 ev.mNext = null;
Jeff Brown32cbc38552011-12-01 14:01:49 -08001544 ev.prepareForReuse();
Jeff Brown1f245102010-11-18 20:53:46 -08001545 return ev;
1546 }
1547
1548 /**
1549 * Obtains a (potentially recycled) key event.
1550 *
1551 * @hide
1552 */
1553 public static KeyEvent obtain(long downTime, long eventTime, int action,
1554 int code, int repeat, int metaState,
1555 int deviceId, int scancode, int flags, int source, String characters) {
1556 KeyEvent ev = obtain();
1557 ev.mDownTime = downTime;
1558 ev.mEventTime = eventTime;
1559 ev.mAction = action;
1560 ev.mKeyCode = code;
1561 ev.mRepeatCount = repeat;
1562 ev.mMetaState = metaState;
1563 ev.mDeviceId = deviceId;
1564 ev.mScanCode = scancode;
1565 ev.mFlags = flags;
1566 ev.mSource = source;
1567 ev.mCharacters = characters;
1568 return ev;
1569 }
1570
1571 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08001572 * Obtains a (potentially recycled) copy of another key event.
1573 *
1574 * @hide
1575 */
1576 public static KeyEvent obtain(KeyEvent other) {
1577 KeyEvent ev = obtain();
1578 ev.mDownTime = other.mDownTime;
1579 ev.mEventTime = other.mEventTime;
1580 ev.mAction = other.mAction;
1581 ev.mKeyCode = other.mKeyCode;
1582 ev.mRepeatCount = other.mRepeatCount;
1583 ev.mMetaState = other.mMetaState;
1584 ev.mDeviceId = other.mDeviceId;
1585 ev.mScanCode = other.mScanCode;
1586 ev.mFlags = other.mFlags;
1587 ev.mSource = other.mSource;
1588 ev.mCharacters = other.mCharacters;
1589 return ev;
1590 }
1591
1592 /** @hide */
1593 @Override
1594 public KeyEvent copy() {
1595 return obtain(this);
1596 }
1597
1598 /**
Jeff Brown1f245102010-11-18 20:53:46 -08001599 * Recycles a key event.
1600 * Key events should only be recycled if they are owned by the system since user
1601 * code expects them to be essentially immutable, "tracking" notwithstanding.
1602 *
1603 * @hide
1604 */
Jeff Brown92cc2d82011-12-02 01:19:47 -08001605 @Override
Jeff Brown1f245102010-11-18 20:53:46 -08001606 public final void recycle() {
Jeff Brown32cbc38552011-12-01 14:01:49 -08001607 super.recycle();
Jeff Brown1f245102010-11-18 20:53:46 -08001608 mCharacters = null;
1609
1610 synchronized (gRecyclerLock) {
1611 if (gRecyclerUsed < MAX_RECYCLED) {
1612 gRecyclerUsed++;
1613 mNext = gRecyclerTop;
1614 gRecyclerTop = this;
1615 }
1616 }
1617 }
1618
Jeff Brown92cc2d82011-12-02 01:19:47 -08001619 /** @hide */
1620 @Override
1621 public final void recycleIfNeededAfterDispatch() {
1622 // Do nothing.
1623 }
1624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001626 * Create a new key event that is the same as the given one, but whose
1627 * event time and repeat count are replaced with the given value.
RoboErik01fe6612014-02-13 14:19:04 -08001628 *
The Android Open Source Project10592532009-03-18 17:39:46 -07001629 * @param event The existing event to be copied. This is not modified.
1630 * @param eventTime The new event time
1631 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1632 * @param newRepeat The new repeat count of the event.
1633 */
1634 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime,
1635 int newRepeat) {
1636 return new KeyEvent(event, eventTime, newRepeat);
1637 }
RoboErik01fe6612014-02-13 14:19:04 -08001638
The Android Open Source Project10592532009-03-18 17:39:46 -07001639 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001640 * Create a new key event that is the same as the given one, but whose
1641 * event time and repeat count are replaced with the given value.
RoboErik01fe6612014-02-13 14:19:04 -08001642 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001643 * @param event The existing event to be copied. This is not modified.
1644 * @param eventTime The new event time
1645 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1646 * @param newRepeat The new repeat count of the event.
1647 * @param newFlags New flags for the event, replacing the entire value
1648 * in the original event.
1649 */
1650 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime,
1651 int newRepeat, int newFlags) {
1652 KeyEvent ret = new KeyEvent(event);
1653 ret.mEventTime = eventTime;
1654 ret.mRepeatCount = newRepeat;
1655 ret.mFlags = newFlags;
1656 return ret;
1657 }
RoboErik01fe6612014-02-13 14:19:04 -08001658
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001659 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 * Copy an existing key event, modifying its action.
RoboErik01fe6612014-02-13 14:19:04 -08001661 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 * @param origEvent The existing event to be copied.
1663 * @param action The new action code of the event.
1664 */
The Android Open Source Project10592532009-03-18 17:39:46 -07001665 private KeyEvent(KeyEvent origEvent, int action) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 mDownTime = origEvent.mDownTime;
1667 mEventTime = origEvent.mEventTime;
1668 mAction = action;
1669 mKeyCode = origEvent.mKeyCode;
1670 mRepeatCount = origEvent.mRepeatCount;
1671 mMetaState = origEvent.mMetaState;
1672 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001673 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001674 mScanCode = origEvent.mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 mFlags = origEvent.mFlags;
1676 // Don't copy mCharacters, since one way or the other we'll lose it
1677 // when changing the action.
1678 }
1679
1680 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001681 * Create a new key event that is the same as the given one, but whose
1682 * action is replaced with the given value.
RoboErik01fe6612014-02-13 14:19:04 -08001683 *
The Android Open Source Project10592532009-03-18 17:39:46 -07001684 * @param event The existing event to be copied. This is not modified.
1685 * @param action The new action code of the event.
1686 */
1687 public static KeyEvent changeAction(KeyEvent event, int action) {
1688 return new KeyEvent(event, action);
1689 }
RoboErik01fe6612014-02-13 14:19:04 -08001690
The Android Open Source Project10592532009-03-18 17:39:46 -07001691 /**
1692 * Create a new key event that is the same as the given one, but whose
1693 * flags are replaced with the given value.
RoboErik01fe6612014-02-13 14:19:04 -08001694 *
The Android Open Source Project10592532009-03-18 17:39:46 -07001695 * @param event The existing event to be copied. This is not modified.
1696 * @param flags The new flags constant.
1697 */
1698 public static KeyEvent changeFlags(KeyEvent event, int flags) {
1699 event = new KeyEvent(event);
1700 event.mFlags = flags;
1701 return event;
1702 }
Jeff Brown21bc5c92011-02-28 18:27:14 -08001703
1704 /** @hide */
1705 @Override
1706 public final boolean isTainted() {
1707 return (mFlags & FLAG_TAINTED) != 0;
1708 }
1709
1710 /** @hide */
1711 @Override
1712 public final void setTainted(boolean tainted) {
1713 mFlags = tainted ? mFlags | FLAG_TAINTED : mFlags & ~FLAG_TAINTED;
1714 }
1715
The Android Open Source Project10592532009-03-18 17:39:46 -07001716 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 * Don't use in new code, instead explicitly check
1718 * {@link #getAction()}.
RoboErik01fe6612014-02-13 14:19:04 -08001719 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 * @return If the action is ACTION_DOWN, returns true; else false.
1721 *
1722 * @deprecated
1723 * @hide
1724 */
1725 @Deprecated public final boolean isDown() {
1726 return mAction == ACTION_DOWN;
1727 }
1728
Michael Wright337d9d22014-04-22 15:03:48 -07001729 /** Is this a system key? System keys can not be used for menu shortcuts.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001730 */
1731 public final boolean isSystem() {
Michael Wright337d9d22014-04-22 15:03:48 -07001732 return isSystemKey(mKeyCode);
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -07001733 }
1734
1735 /** @hide */
Michael Wright337d9d22014-04-22 15:03:48 -07001736 public final boolean isWakeKey() {
1737 return isWakeKey(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 }
1739
Jeff Brown6f2fba42011-02-19 01:08:02 -08001740 /**
1741 * Returns true if the specified keycode is a gamepad button.
1742 * @return True if the keycode is a gamepad button, such as {@link #KEYCODE_BUTTON_A}.
1743 */
1744 public static final boolean isGamepadButton(int keyCode) {
1745 switch (keyCode) {
1746 case KeyEvent.KEYCODE_BUTTON_A:
1747 case KeyEvent.KEYCODE_BUTTON_B:
1748 case KeyEvent.KEYCODE_BUTTON_C:
1749 case KeyEvent.KEYCODE_BUTTON_X:
1750 case KeyEvent.KEYCODE_BUTTON_Y:
1751 case KeyEvent.KEYCODE_BUTTON_Z:
1752 case KeyEvent.KEYCODE_BUTTON_L1:
1753 case KeyEvent.KEYCODE_BUTTON_R1:
1754 case KeyEvent.KEYCODE_BUTTON_L2:
1755 case KeyEvent.KEYCODE_BUTTON_R2:
1756 case KeyEvent.KEYCODE_BUTTON_THUMBL:
1757 case KeyEvent.KEYCODE_BUTTON_THUMBR:
1758 case KeyEvent.KEYCODE_BUTTON_START:
1759 case KeyEvent.KEYCODE_BUTTON_SELECT:
1760 case KeyEvent.KEYCODE_BUTTON_MODE:
1761 case KeyEvent.KEYCODE_BUTTON_1:
1762 case KeyEvent.KEYCODE_BUTTON_2:
1763 case KeyEvent.KEYCODE_BUTTON_3:
1764 case KeyEvent.KEYCODE_BUTTON_4:
1765 case KeyEvent.KEYCODE_BUTTON_5:
1766 case KeyEvent.KEYCODE_BUTTON_6:
1767 case KeyEvent.KEYCODE_BUTTON_7:
1768 case KeyEvent.KEYCODE_BUTTON_8:
1769 case KeyEvent.KEYCODE_BUTTON_9:
1770 case KeyEvent.KEYCODE_BUTTON_10:
1771 case KeyEvent.KEYCODE_BUTTON_11:
1772 case KeyEvent.KEYCODE_BUTTON_12:
1773 case KeyEvent.KEYCODE_BUTTON_13:
1774 case KeyEvent.KEYCODE_BUTTON_14:
1775 case KeyEvent.KEYCODE_BUTTON_15:
1776 case KeyEvent.KEYCODE_BUTTON_16:
1777 return true;
1778 default:
1779 return false;
1780 }
1781 }
1782
Michael Wright25b0c302013-07-10 12:54:06 -07001783 /** Whether key will, by default, trigger a click on the focused view.
1784 * @hide
1785 */
1786 public static final boolean isConfirmKey(int keyCode) {
1787 switch (keyCode) {
1788 case KeyEvent.KEYCODE_DPAD_CENTER:
1789 case KeyEvent.KEYCODE_ENTER:
Michael Wrightaa1a94d2015-11-26 16:04:54 +00001790 case KeyEvent.KEYCODE_SPACE:
Selim Cinekce2bd0f2016-02-19 16:16:54 -08001791 case KeyEvent.KEYCODE_NUMPAD_ENTER:
Michael Wright25b0c302013-07-10 12:54:06 -07001792 return true;
1793 default:
1794 return false;
1795 }
1796 }
1797
RoboErik01fe6612014-02-13 14:19:04 -08001798 /**
1799 * Whether this key is a media key, which can be send to apps that are
1800 * interested in media key events.
1801 *
1802 * @hide
1803 */
1804 public static final boolean isMediaKey(int keyCode) {
1805 switch (keyCode) {
1806 case KeyEvent.KEYCODE_MEDIA_PLAY:
1807 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1808 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1809 case KeyEvent.KEYCODE_MUTE:
1810 case KeyEvent.KEYCODE_HEADSETHOOK:
1811 case KeyEvent.KEYCODE_MEDIA_STOP:
1812 case KeyEvent.KEYCODE_MEDIA_NEXT:
1813 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1814 case KeyEvent.KEYCODE_MEDIA_REWIND:
1815 case KeyEvent.KEYCODE_MEDIA_RECORD:
1816 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1817 return true;
1818 }
1819 return false;
1820 }
1821
Michael Wright337d9d22014-04-22 15:03:48 -07001822
1823 /** Is this a system key? System keys can not be used for menu shortcuts.
1824 * @hide
1825 */
1826 public static final boolean isSystemKey(int keyCode) {
1827 switch (keyCode) {
1828 case KeyEvent.KEYCODE_MENU:
1829 case KeyEvent.KEYCODE_SOFT_RIGHT:
1830 case KeyEvent.KEYCODE_HOME:
1831 case KeyEvent.KEYCODE_BACK:
1832 case KeyEvent.KEYCODE_CALL:
1833 case KeyEvent.KEYCODE_ENDCALL:
1834 case KeyEvent.KEYCODE_VOLUME_UP:
1835 case KeyEvent.KEYCODE_VOLUME_DOWN:
1836 case KeyEvent.KEYCODE_VOLUME_MUTE:
1837 case KeyEvent.KEYCODE_MUTE:
1838 case KeyEvent.KEYCODE_POWER:
1839 case KeyEvent.KEYCODE_HEADSETHOOK:
1840 case KeyEvent.KEYCODE_MEDIA_PLAY:
1841 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1842 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1843 case KeyEvent.KEYCODE_MEDIA_STOP:
1844 case KeyEvent.KEYCODE_MEDIA_NEXT:
1845 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1846 case KeyEvent.KEYCODE_MEDIA_REWIND:
1847 case KeyEvent.KEYCODE_MEDIA_RECORD:
1848 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1849 case KeyEvent.KEYCODE_CAMERA:
1850 case KeyEvent.KEYCODE_FOCUS:
1851 case KeyEvent.KEYCODE_SEARCH:
1852 case KeyEvent.KEYCODE_BRIGHTNESS_DOWN:
1853 case KeyEvent.KEYCODE_BRIGHTNESS_UP:
1854 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
Jim Miller07e03842016-06-22 15:18:13 -07001855 case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_UP:
1856 case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_DOWN:
1857 case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_LEFT:
1858 case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_RIGHT:
Michael Wright337d9d22014-04-22 15:03:48 -07001859 return true;
1860 }
1861
1862 return false;
1863 }
1864
1865 /** @hide */
1866 public static final boolean isWakeKey(int keyCode) {
1867 switch (keyCode) {
1868 case KeyEvent.KEYCODE_BACK:
Michael Wright337d9d22014-04-22 15:03:48 -07001869 case KeyEvent.KEYCODE_MENU:
Michael Wright337d9d22014-04-22 15:03:48 -07001870 case KeyEvent.KEYCODE_WAKEUP:
Michael Wright8ac485a2014-06-04 12:38:18 -07001871 case KeyEvent.KEYCODE_PAIRING:
Chenjie Luobad498f2016-01-13 11:01:59 -08001872 case KeyEvent.KEYCODE_STEM_1:
1873 case KeyEvent.KEYCODE_STEM_2:
1874 case KeyEvent.KEYCODE_STEM_3:
Michael Wright337d9d22014-04-22 15:03:48 -07001875 return true;
1876 }
1877 return false;
1878 }
1879
Michael Wrightce0c13a2014-07-30 10:49:21 -07001880 /** @hide */
1881 public static final boolean isMetaKey(int keyCode) {
1882 return keyCode == KeyEvent.KEYCODE_META_LEFT || keyCode == KeyEvent.KEYCODE_META_RIGHT;
1883 }
1884
Andrii Kulian112d0562016-03-08 10:44:22 -08001885 /** @hide */
1886 public static final boolean isAltKey(int keyCode) {
1887 return keyCode == KeyEvent.KEYCODE_ALT_LEFT || keyCode == KeyEvent.KEYCODE_ALT_RIGHT;
1888 }
1889
Jeff Brown91c69ab2011-02-14 17:03:18 -08001890 /** {@inheritDoc} */
1891 @Override
1892 public final int getDeviceId() {
1893 return mDeviceId;
1894 }
1895
1896 /** {@inheritDoc} */
1897 @Override
1898 public final int getSource() {
1899 return mSource;
1900 }
1901
1902 /** {@inheritDoc} */
1903 @Override
1904 public final void setSource(int source) {
1905 mSource = source;
1906 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907
1908 /**
1909 * <p>Returns the state of the meta keys.</p>
1910 *
1911 * @return an integer in which each bit set to 1 represents a pressed
1912 * meta key
1913 *
1914 * @see #isAltPressed()
1915 * @see #isShiftPressed()
1916 * @see #isSymPressed()
Jeff Brown497a92c2010-09-12 17:55:08 -07001917 * @see #isCtrlPressed()
1918 * @see #isMetaPressed()
1919 * @see #isFunctionPressed()
Jeff Brown51e7fe72010-10-29 22:19:53 -07001920 * @see #isCapsLockOn()
1921 * @see #isNumLockOn()
1922 * @see #isScrollLockOn()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 * @see #META_ALT_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001924 * @see #META_ALT_LEFT_ON
1925 * @see #META_ALT_RIGHT_ON
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 * @see #META_SHIFT_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001927 * @see #META_SHIFT_LEFT_ON
1928 * @see #META_SHIFT_RIGHT_ON
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929 * @see #META_SYM_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001930 * @see #META_FUNCTION_ON
1931 * @see #META_CTRL_ON
1932 * @see #META_CTRL_LEFT_ON
1933 * @see #META_CTRL_RIGHT_ON
1934 * @see #META_META_ON
1935 * @see #META_META_LEFT_ON
1936 * @see #META_META_RIGHT_ON
Jeff Brown51e7fe72010-10-29 22:19:53 -07001937 * @see #META_CAPS_LOCK_ON
1938 * @see #META_NUM_LOCK_ON
1939 * @see #META_SCROLL_LOCK_ON
Jeff Brown54875002011-04-06 15:33:01 -07001940 * @see #getModifiers
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 */
1942 public final int getMetaState() {
1943 return mMetaState;
1944 }
1945
1946 /**
Jeff Brown54875002011-04-06 15:33:01 -07001947 * Returns the state of the modifier keys.
1948 * <p>
1949 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1950 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1951 * not considered modifier keys. Consequently, this function specifically masks out
1952 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1953 * </p><p>
1954 * The value returned consists of the meta state (from {@link #getMetaState})
1955 * normalized using {@link #normalizeMetaState(int)} and then masked with
1956 * {@link #getModifierMetaStateMask} so that only valid modifier bits are retained.
1957 * </p>
1958 *
1959 * @return An integer in which each bit set to 1 represents a pressed modifier key.
1960 * @see #getMetaState
1961 */
1962 public final int getModifiers() {
1963 return normalizeMetaState(mMetaState) & META_MODIFIER_MASK;
1964 }
1965
1966 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 * Returns the flags for this key event.
1968 *
1969 * @see #FLAG_WOKE_HERE
1970 */
1971 public final int getFlags() {
1972 return mFlags;
1973 }
1974
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001975 // Mask of all modifier key meta states. Specifically excludes locked keys like caps lock.
1976 private static final int META_MODIFIER_MASK =
1977 META_SHIFT_ON | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON
1978 | META_ALT_ON | META_ALT_LEFT_ON | META_ALT_RIGHT_ON
1979 | META_CTRL_ON | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON
1980 | META_META_ON | META_META_LEFT_ON | META_META_RIGHT_ON
1981 | META_SYM_ON | META_FUNCTION_ON;
1982
1983 // Mask of all lock key meta states.
1984 private static final int META_LOCK_MASK =
1985 META_CAPS_LOCK_ON | META_NUM_LOCK_ON | META_SCROLL_LOCK_ON;
1986
1987 // Mask of all valid meta states.
1988 private static final int META_ALL_MASK = META_MODIFIER_MASK | META_LOCK_MASK;
1989
1990 // Mask of all synthetic meta states that are reserved for API compatibility with
1991 // historical uses in MetaKeyKeyListener.
1992 private static final int META_SYNTHETIC_MASK =
1993 META_CAP_LOCKED | META_ALT_LOCKED | META_SYM_LOCKED | META_SELECTING;
1994
1995 // Mask of all meta states that are not valid use in specifying a modifier key.
1996 // These bits are known to be used for purposes other than specifying modifiers.
1997 private static final int META_INVALID_MODIFIER_MASK =
1998 META_LOCK_MASK | META_SYNTHETIC_MASK;
1999
2000 /**
2001 * Gets a mask that includes all valid modifier key meta state bits.
2002 * <p>
2003 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2004 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2005 * not considered modifier keys. Consequently, the mask specifically excludes
2006 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2007 * </p>
2008 *
2009 * @return The modifier meta state mask which is a combination of
2010 * {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON}, {@link #META_SHIFT_RIGHT_ON},
2011 * {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON}, {@link #META_ALT_RIGHT_ON},
2012 * {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON}, {@link #META_CTRL_RIGHT_ON},
2013 * {@link #META_META_ON}, {@link #META_META_LEFT_ON}, {@link #META_META_RIGHT_ON},
2014 * {@link #META_SYM_ON}, {@link #META_FUNCTION_ON}.
2015 */
2016 public static int getModifierMetaStateMask() {
2017 return META_MODIFIER_MASK;
2018 }
2019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 /**
2021 * Returns true if this key code is a modifier key.
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002022 * <p>
2023 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2024 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2025 * not considered modifier keys. Consequently, this function return false
2026 * for those keys.
2027 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 *
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002029 * @return True if the key code is one of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 * {@link #KEYCODE_SHIFT_LEFT} {@link #KEYCODE_SHIFT_RIGHT},
Jeff Brown497a92c2010-09-12 17:55:08 -07002031 * {@link #KEYCODE_ALT_LEFT}, {@link #KEYCODE_ALT_RIGHT},
Jeff Brown497a92c2010-09-12 17:55:08 -07002032 * {@link #KEYCODE_CTRL_LEFT}, {@link #KEYCODE_CTRL_RIGHT},
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002033 * {@link #KEYCODE_META_LEFT}, or {@link #KEYCODE_META_RIGHT},
2034 * {@link #KEYCODE_SYM}, {@link #KEYCODE_NUM}, {@link #KEYCODE_FUNCTION}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 */
2036 public static boolean isModifierKey(int keyCode) {
Jeff Brown497a92c2010-09-12 17:55:08 -07002037 switch (keyCode) {
2038 case KEYCODE_SHIFT_LEFT:
2039 case KEYCODE_SHIFT_RIGHT:
2040 case KEYCODE_ALT_LEFT:
2041 case KEYCODE_ALT_RIGHT:
Jeff Brown497a92c2010-09-12 17:55:08 -07002042 case KEYCODE_CTRL_LEFT:
2043 case KEYCODE_CTRL_RIGHT:
2044 case KEYCODE_META_LEFT:
2045 case KEYCODE_META_RIGHT:
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002046 case KEYCODE_SYM:
2047 case KEYCODE_NUM:
2048 case KEYCODE_FUNCTION:
Jeff Brown497a92c2010-09-12 17:55:08 -07002049 return true;
2050 default:
2051 return false;
2052 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 }
2054
2055 /**
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002056 * Normalizes the specified meta state.
2057 * <p>
2058 * The meta state is normalized such that if either the left or right modifier meta state
2059 * bits are set then the result will also include the universal bit for that modifier.
2060 * </p><p>
2061 * If the specified meta state contains {@link #META_ALT_LEFT_ON} then
2062 * the result will also contain {@link #META_ALT_ON} in addition to {@link #META_ALT_LEFT_ON}
2063 * and the other bits that were specified in the input. The same is process is
2064 * performed for shift, control and meta.
2065 * </p><p>
2066 * If the specified meta state contains synthetic meta states defined by
2067 * {@link MetaKeyKeyListener}, then those states are translated here and the original
2068 * synthetic meta states are removed from the result.
2069 * {@link MetaKeyKeyListener#META_CAP_LOCKED} is translated to {@link #META_CAPS_LOCK_ON}.
2070 * {@link MetaKeyKeyListener#META_ALT_LOCKED} is translated to {@link #META_ALT_ON}.
2071 * {@link MetaKeyKeyListener#META_SYM_LOCKED} is translated to {@link #META_SYM_ON}.
2072 * </p><p>
2073 * Undefined meta state bits are removed.
2074 * </p>
2075 *
2076 * @param metaState The meta state.
2077 * @return The normalized meta state.
2078 */
2079 public static int normalizeMetaState(int metaState) {
2080 if ((metaState & (META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON)) != 0) {
2081 metaState |= META_SHIFT_ON;
2082 }
2083 if ((metaState & (META_ALT_LEFT_ON | META_ALT_RIGHT_ON)) != 0) {
2084 metaState |= META_ALT_ON;
2085 }
2086 if ((metaState & (META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON)) != 0) {
2087 metaState |= META_CTRL_ON;
2088 }
2089 if ((metaState & (META_META_LEFT_ON | META_META_RIGHT_ON)) != 0) {
2090 metaState |= META_META_ON;
2091 }
2092 if ((metaState & MetaKeyKeyListener.META_CAP_LOCKED) != 0) {
2093 metaState |= META_CAPS_LOCK_ON;
2094 }
2095 if ((metaState & MetaKeyKeyListener.META_ALT_LOCKED) != 0) {
2096 metaState |= META_ALT_ON;
2097 }
2098 if ((metaState & MetaKeyKeyListener.META_SYM_LOCKED) != 0) {
2099 metaState |= META_SYM_ON;
2100 }
2101 return metaState & META_ALL_MASK;
2102 }
2103
2104 /**
2105 * Returns true if no modifiers keys are pressed according to the specified meta state.
2106 * <p>
2107 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2108 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2109 * not considered modifier keys. Consequently, this function ignores
2110 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2111 * </p><p>
2112 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}.
2113 * </p>
2114 *
2115 * @param metaState The meta state to consider.
2116 * @return True if no modifier keys are pressed.
2117 * @see #hasNoModifiers()
2118 */
2119 public static boolean metaStateHasNoModifiers(int metaState) {
2120 return (normalizeMetaState(metaState) & META_MODIFIER_MASK) == 0;
2121 }
2122
2123 /**
2124 * Returns true if only the specified modifier keys are pressed according to
2125 * the specified meta state. Returns false if a different combination of modifier
2126 * keys are pressed.
2127 * <p>
2128 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2129 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2130 * not considered modifier keys. Consequently, this function ignores
2131 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2132 * </p><p>
2133 * If the specified modifier mask includes directional modifiers, such as
2134 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the
2135 * modifier is pressed on that side.
2136 * If the specified modifier mask includes non-directional modifiers, such as
2137 * {@link #META_SHIFT_ON}, then this method ensures that the modifier
2138 * is pressed on either side.
2139 * If the specified modifier mask includes both directional and non-directional modifiers
2140 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON},
2141 * then this method throws an illegal argument exception.
2142 * </p>
2143 *
2144 * @param metaState The meta state to consider.
2145 * @param modifiers The meta state of the modifier keys to check. May be a combination
2146 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to
2147 * ensure that no modifier keys are pressed.
2148 * @return True if only the specified modifier keys are pressed.
2149 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers
2150 * @see #hasModifiers
2151 */
2152 public static boolean metaStateHasModifiers(int metaState, int modifiers) {
2153 // Note: For forward compatibility, we allow the parameter to contain meta states
2154 // that we do not recognize but we explicitly disallow meta states that
2155 // are not valid modifiers.
2156 if ((modifiers & META_INVALID_MODIFIER_MASK) != 0) {
2157 throw new IllegalArgumentException("modifiers must not contain "
2158 + "META_CAPS_LOCK_ON, META_NUM_LOCK_ON, META_SCROLL_LOCK_ON, "
2159 + "META_CAP_LOCKED, META_ALT_LOCKED, META_SYM_LOCKED, "
2160 + "or META_SELECTING");
2161 }
2162
2163 metaState = normalizeMetaState(metaState) & META_MODIFIER_MASK;
2164 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2165 META_SHIFT_ON, META_SHIFT_LEFT_ON, META_SHIFT_RIGHT_ON);
2166 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2167 META_ALT_ON, META_ALT_LEFT_ON, META_ALT_RIGHT_ON);
2168 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2169 META_CTRL_ON, META_CTRL_LEFT_ON, META_CTRL_RIGHT_ON);
2170 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2171 META_META_ON, META_META_LEFT_ON, META_META_RIGHT_ON);
2172 return metaState == modifiers;
2173 }
2174
2175 private static int metaStateFilterDirectionalModifiers(int metaState,
2176 int modifiers, int basic, int left, int right) {
2177 final boolean wantBasic = (modifiers & basic) != 0;
2178 final int directional = left | right;
2179 final boolean wantLeftOrRight = (modifiers & directional) != 0;
2180
2181 if (wantBasic) {
2182 if (wantLeftOrRight) {
2183 throw new IllegalArgumentException("modifiers must not contain "
2184 + metaStateToString(basic) + " combined with "
2185 + metaStateToString(left) + " or " + metaStateToString(right));
2186 }
2187 return metaState & ~directional;
2188 } else if (wantLeftOrRight) {
2189 return metaState & ~basic;
2190 } else {
2191 return metaState;
2192 }
2193 }
2194
2195 /**
2196 * Returns true if no modifier keys are pressed.
2197 * <p>
2198 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2199 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2200 * not considered modifier keys. Consequently, this function ignores
2201 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2202 * </p><p>
2203 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}.
2204 * </p>
2205 *
2206 * @return True if no modifier keys are pressed.
2207 * @see #metaStateHasNoModifiers
2208 */
2209 public final boolean hasNoModifiers() {
2210 return metaStateHasNoModifiers(mMetaState);
2211 }
2212
2213 /**
2214 * Returns true if only the specified modifiers keys are pressed.
2215 * Returns false if a different combination of modifier keys are pressed.
2216 * <p>
2217 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2218 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2219 * not considered modifier keys. Consequently, this function ignores
2220 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2221 * </p><p>
2222 * If the specified modifier mask includes directional modifiers, such as
2223 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the
2224 * modifier is pressed on that side.
2225 * If the specified modifier mask includes non-directional modifiers, such as
2226 * {@link #META_SHIFT_ON}, then this method ensures that the modifier
2227 * is pressed on either side.
2228 * If the specified modifier mask includes both directional and non-directional modifiers
2229 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON},
2230 * then this method throws an illegal argument exception.
2231 * </p>
2232 *
2233 * @param modifiers The meta state of the modifier keys to check. May be a combination
2234 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to
2235 * ensure that no modifier keys are pressed.
2236 * @return True if only the specified modifier keys are pressed.
2237 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers
2238 * @see #metaStateHasModifiers
2239 */
2240 public final boolean hasModifiers(int modifiers) {
2241 return metaStateHasModifiers(mMetaState, modifiers);
2242 }
2243
2244 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002245 * <p>Returns the pressed state of the ALT meta key.</p>
2246 *
2247 * @return true if the ALT key is pressed, false otherwise
2248 *
2249 * @see #KEYCODE_ALT_LEFT
2250 * @see #KEYCODE_ALT_RIGHT
2251 * @see #META_ALT_ON
2252 */
2253 public final boolean isAltPressed() {
2254 return (mMetaState & META_ALT_ON) != 0;
2255 }
2256
2257 /**
2258 * <p>Returns the pressed state of the SHIFT meta key.</p>
2259 *
2260 * @return true if the SHIFT key is pressed, false otherwise
2261 *
2262 * @see #KEYCODE_SHIFT_LEFT
2263 * @see #KEYCODE_SHIFT_RIGHT
2264 * @see #META_SHIFT_ON
2265 */
2266 public final boolean isShiftPressed() {
2267 return (mMetaState & META_SHIFT_ON) != 0;
2268 }
2269
2270 /**
2271 * <p>Returns the pressed state of the SYM meta key.</p>
2272 *
2273 * @return true if the SYM key is pressed, false otherwise
2274 *
2275 * @see #KEYCODE_SYM
2276 * @see #META_SYM_ON
2277 */
2278 public final boolean isSymPressed() {
2279 return (mMetaState & META_SYM_ON) != 0;
2280 }
2281
2282 /**
Jeff Brown497a92c2010-09-12 17:55:08 -07002283 * <p>Returns the pressed state of the CTRL meta key.</p>
2284 *
2285 * @return true if the CTRL key is pressed, false otherwise
2286 *
2287 * @see #KEYCODE_CTRL_LEFT
2288 * @see #KEYCODE_CTRL_RIGHT
2289 * @see #META_CTRL_ON
2290 */
2291 public final boolean isCtrlPressed() {
2292 return (mMetaState & META_CTRL_ON) != 0;
2293 }
2294
2295 /**
2296 * <p>Returns the pressed state of the META meta key.</p>
2297 *
2298 * @return true if the META key is pressed, false otherwise
2299 *
2300 * @see #KEYCODE_META_LEFT
2301 * @see #KEYCODE_META_RIGHT
2302 * @see #META_META_ON
2303 */
2304 public final boolean isMetaPressed() {
2305 return (mMetaState & META_META_ON) != 0;
2306 }
2307
2308 /**
2309 * <p>Returns the pressed state of the FUNCTION meta key.</p>
2310 *
2311 * @return true if the FUNCTION key is pressed, false otherwise
2312 *
2313 * @see #KEYCODE_FUNCTION
2314 * @see #META_FUNCTION_ON
2315 */
2316 public final boolean isFunctionPressed() {
2317 return (mMetaState & META_FUNCTION_ON) != 0;
2318 }
2319
2320 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07002321 * <p>Returns the locked state of the CAPS LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002322 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002323 * @return true if the CAPS LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002324 *
2325 * @see #KEYCODE_CAPS_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002326 * @see #META_CAPS_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002327 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002328 public final boolean isCapsLockOn() {
2329 return (mMetaState & META_CAPS_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002330 }
2331
2332 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07002333 * <p>Returns the locked state of the NUM LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002334 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002335 * @return true if the NUM LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002336 *
2337 * @see #KEYCODE_NUM_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002338 * @see #META_NUM_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002339 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002340 public final boolean isNumLockOn() {
2341 return (mMetaState & META_NUM_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002342 }
2343
2344 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07002345 * <p>Returns the locked state of the SCROLL LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002346 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002347 * @return true if the SCROLL LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002348 *
2349 * @see #KEYCODE_SCROLL_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002350 * @see #META_SCROLL_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002351 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002352 public final boolean isScrollLockOn() {
2353 return (mMetaState & META_SCROLL_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002354 }
2355
2356 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002357 * Retrieve the action of this key event. May be either
2358 * {@link #ACTION_DOWN}, {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
RoboErik01fe6612014-02-13 14:19:04 -08002359 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002360 * @return The event action: ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.
2361 */
2362 public final int getAction() {
2363 return mAction;
2364 }
2365
2366 /**
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07002367 * For {@link #ACTION_UP} events, indicates that the event has been
2368 * canceled as per {@link #FLAG_CANCELED}.
2369 */
2370 public final boolean isCanceled() {
2371 return (mFlags&FLAG_CANCELED) != 0;
2372 }
RoboErik01fe6612014-02-13 14:19:04 -08002373
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07002374 /**
Wale Ogunwalec3672cd2014-11-05 15:17:35 -08002375 * Set {@link #FLAG_CANCELED} flag for the key event.
2376 *
2377 * @hide
2378 */
2379 @Override
2380 public final void cancel() {
2381 mFlags |= FLAG_CANCELED;
2382 }
2383
2384 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002385 * Call this during {@link Callback#onKeyDown} to have the system track
2386 * the key through its final up (possibly including a long press). Note
2387 * that only one key can be tracked at a time -- if another key down
2388 * event is received while a previous one is being tracked, tracking is
2389 * stopped on the previous event.
2390 */
2391 public final void startTracking() {
2392 mFlags |= FLAG_START_TRACKING;
2393 }
RoboErik01fe6612014-02-13 14:19:04 -08002394
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002395 /**
2396 * For {@link #ACTION_UP} events, indicates that the event is still being
2397 * tracked from its initial down event as per
2398 * {@link #FLAG_TRACKING}.
2399 */
2400 public final boolean isTracking() {
2401 return (mFlags&FLAG_TRACKING) != 0;
2402 }
RoboErik01fe6612014-02-13 14:19:04 -08002403
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002404 /**
2405 * For {@link #ACTION_DOWN} events, indicates that the event has been
2406 * canceled as per {@link #FLAG_LONG_PRESS}.
2407 */
2408 public final boolean isLongPress() {
2409 return (mFlags&FLAG_LONG_PRESS) != 0;
2410 }
RoboErik01fe6612014-02-13 14:19:04 -08002411
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002412 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 * Retrieve the key code of the key event. This is the physical key that
2414 * was pressed, <em>not</em> the Unicode character.
RoboErik01fe6612014-02-13 14:19:04 -08002415 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002416 * @return The key code of the event.
2417 */
2418 public final int getKeyCode() {
2419 return mKeyCode;
2420 }
2421
2422 /**
2423 * For the special case of a {@link #ACTION_MULTIPLE} event with key
2424 * code of {@link #KEYCODE_UNKNOWN}, this is a raw string of characters
2425 * associated with the event. In all other cases it is null.
RoboErik01fe6612014-02-13 14:19:04 -08002426 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427 * @return Returns a String of 1 or more characters associated with
2428 * the event.
2429 */
2430 public final String getCharacters() {
2431 return mCharacters;
2432 }
RoboErik01fe6612014-02-13 14:19:04 -08002433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002434 /**
2435 * Retrieve the hardware key id of this key event. These values are not
2436 * reliable and vary from device to device.
2437 *
2438 * {@more}
2439 * Mostly this is here for debugging purposes.
2440 */
2441 public final int getScanCode() {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002442 return mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002443 }
2444
2445 /**
2446 * Retrieve the repeat count of the event. For both key up and key down
2447 * events, this is the number of times the key has repeated with the first
2448 * down starting at 0 and counting up from there. For multiple key
2449 * events, this is the number of down/up pairs that have occurred.
RoboErik01fe6612014-02-13 14:19:04 -08002450 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 * @return The number of times the key has repeated.
2452 */
2453 public final int getRepeatCount() {
2454 return mRepeatCount;
2455 }
2456
2457 /**
2458 * Retrieve the time of the most recent key down event,
2459 * in the {@link android.os.SystemClock#uptimeMillis} time base. If this
2460 * is a down event, this will be the same as {@link #getEventTime()}.
2461 * Note that when chording keys, this value is the down time of the
2462 * most recently pressed key, which may <em>not</em> be the same physical
2463 * key of this event.
RoboErik01fe6612014-02-13 14:19:04 -08002464 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002465 * @return Returns the most recent key down time, in the
2466 * {@link android.os.SystemClock#uptimeMillis} time base
2467 */
2468 public final long getDownTime() {
2469 return mDownTime;
2470 }
2471
2472 /**
Jeff Brownb11499d2012-04-20 19:54:22 -07002473 * Retrieve the time this event occurred,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474 * in the {@link android.os.SystemClock#uptimeMillis} time base.
Jeff Brownb11499d2012-04-20 19:54:22 -07002475 *
RoboErik01fe6612014-02-13 14:19:04 -08002476 * @return Returns the time this event occurred,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002477 * in the {@link android.os.SystemClock#uptimeMillis} time base.
2478 */
Jeff Brownb11499d2012-04-20 19:54:22 -07002479 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002480 public final long getEventTime() {
2481 return mEventTime;
2482 }
2483
Jeff Brownb11499d2012-04-20 19:54:22 -07002484 /**
2485 * Retrieve the time this event occurred,
2486 * in the {@link android.os.SystemClock#uptimeMillis} time base but with
2487 * nanosecond (instead of millisecond) precision.
2488 * <p>
2489 * The value is in nanosecond precision but it may not have nanosecond accuracy.
2490 * </p>
2491 *
2492 * @return Returns the time this event occurred,
2493 * in the {@link android.os.SystemClock#uptimeMillis} time base but with
2494 * nanosecond (instead of millisecond) precision.
2495 *
2496 * @hide
2497 */
Jeff Brown4e91a182011-04-07 11:38:09 -07002498 @Override
2499 public final long getEventTimeNano() {
2500 return mEventTime * 1000000L;
2501 }
2502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002503 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 * Renamed to {@link #getDeviceId}.
RoboErik01fe6612014-02-13 14:19:04 -08002505 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002506 * @hide
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002507 * @deprecated use {@link #getDeviceId()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002508 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002509 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002510 public final int getKeyboardDevice() {
2511 return mDeviceId;
2512 }
2513
2514 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002515 * Gets the {@link KeyCharacterMap} associated with the keyboard device.
2516 *
2517 * @return The associated key character map.
Andrew Sapperstein8ab3dc72011-06-23 18:56:44 -07002518 * @throws {@link KeyCharacterMap.UnavailableException} if the key character map
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002519 * could not be loaded because it was malformed or the default key character map
2520 * is missing from the system.
2521 *
Andrew Sapperstein8ab3dc72011-06-23 18:56:44 -07002522 * @see KeyCharacterMap#load
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002523 */
2524 public final KeyCharacterMap getKeyCharacterMap() {
2525 return KeyCharacterMap.load(mDeviceId);
2526 }
2527
2528 /**
2529 * Gets the primary character for this key.
2530 * In other words, the label that is physically printed on it.
2531 *
2532 * @return The display label character, or 0 if none (eg. for non-printing keys).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002533 */
2534 public char getDisplayLabel() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002535 return getKeyCharacterMap().getDisplayLabel(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002536 }
RoboErik01fe6612014-02-13 14:19:04 -08002537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002539 * Gets the Unicode character generated by the specified key and meta
2540 * key state combination.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541 * <p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002542 * Returns the Unicode character that the specified key would produce
2543 * when the specified meta bits (see {@link MetaKeyKeyListener})
2544 * were active.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002545 * </p><p>
2546 * Returns 0 if the key is not one that is used to type Unicode
2547 * characters.
2548 * </p><p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002549 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the
2550 * key is a "dead key" that should be combined with another to
2551 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} --
2552 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002553 * </p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002554 *
2555 * @return The associated character or combining accent, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556 */
2557 public int getUnicodeChar() {
2558 return getUnicodeChar(mMetaState);
2559 }
RoboErik01fe6612014-02-13 14:19:04 -08002560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002561 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002562 * Gets the Unicode character generated by the specified key and meta
2563 * key state combination.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002564 * <p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002565 * Returns the Unicode character that the specified key would produce
2566 * when the specified meta bits (see {@link MetaKeyKeyListener})
2567 * were active.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002568 * </p><p>
2569 * Returns 0 if the key is not one that is used to type Unicode
2570 * characters.
2571 * </p><p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002572 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the
2573 * key is a "dead key" that should be combined with another to
2574 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} --
2575 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002576 * </p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002577 *
2578 * @param metaState The meta key modifier state.
2579 * @return The associated character or combining accent, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002580 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002581 public int getUnicodeChar(int metaState) {
2582 return getKeyCharacterMap().get(mKeyCode, metaState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 }
RoboErik01fe6612014-02-13 14:19:04 -08002584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002585 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002586 * Get the character conversion data for a given key code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 *
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002588 * @param results A {@link KeyCharacterMap.KeyData} instance that will be
2589 * filled with the results.
2590 * @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 -08002591 *
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002592 * @deprecated instead use {@link #getDisplayLabel()},
2593 * {@link #getNumber()} or {@link #getUnicodeChar(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002595 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 public boolean getKeyData(KeyData results) {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002597 return getKeyCharacterMap().getKeyData(mKeyCode, results);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598 }
RoboErik01fe6612014-02-13 14:19:04 -08002599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002600 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002601 * Gets the first character in the character array that can be generated
2602 * by the specified key code.
2603 * <p>
2604 * This is a convenience function that returns the same value as
2605 * {@link #getMatch(char[],int) getMatch(chars, 0)}.
2606 * </p>
2607 *
2608 * @param chars The array of matching characters to consider.
2609 * @return The matching associated character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002610 */
2611 public char getMatch(char[] chars) {
2612 return getMatch(chars, 0);
2613 }
RoboErik01fe6612014-02-13 14:19:04 -08002614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002615 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002616 * Gets the first character in the character array that can be generated
2617 * by the specified key code. If there are multiple choices, prefers
2618 * the one that would be generated with the specified meta key modifier state.
2619 *
2620 * @param chars The array of matching characters to consider.
2621 * @param metaState The preferred meta key modifier state.
2622 * @return The matching associated character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002624 public char getMatch(char[] chars, int metaState) {
2625 return getKeyCharacterMap().getMatch(mKeyCode, chars, metaState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626 }
RoboErik01fe6612014-02-13 14:19:04 -08002627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002629 * Gets the number or symbol associated with the key.
2630 * <p>
2631 * The character value is returned, not the numeric value.
2632 * If the key is not a number, but is a symbol, the symbol is retuned.
2633 * </p><p>
2634 * This method is intended to to support dial pads and other numeric or
2635 * symbolic entry on keyboards where certain keys serve dual function
2636 * as alphabetic and symbolic keys. This method returns the number
2637 * or symbol associated with the key independent of whether the user
2638 * has pressed the required modifier.
2639 * </p><p>
2640 * For example, on one particular keyboard the keys on the top QWERTY row generate
2641 * numbers when ALT is pressed such that ALT-Q maps to '1'. So for that keyboard
2642 * when {@link #getNumber} is called with {@link KeyEvent#KEYCODE_Q} it returns '1'
2643 * so that the user can type numbers without pressing ALT when it makes sense.
2644 * </p>
2645 *
2646 * @return The associated numeric or symbolic character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002647 */
2648 public char getNumber() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002649 return getKeyCharacterMap().getNumber(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650 }
RoboErik01fe6612014-02-13 14:19:04 -08002651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002653 * Returns true if this key produces a glyph.
2654 *
2655 * @return True if the key is a printing key.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002656 */
2657 public boolean isPrintingKey() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002658 return getKeyCharacterMap().isPrintingKey(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002659 }
RoboErik01fe6612014-02-13 14:19:04 -08002660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002661 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002662 * @deprecated Use {@link #dispatch(Callback, DispatcherState, Object)} instead.
2663 */
2664 @Deprecated
2665 public final boolean dispatch(Callback receiver) {
2666 return dispatch(receiver, null, null);
2667 }
RoboErik01fe6612014-02-13 14:19:04 -08002668
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002669 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002670 * Deliver this key event to a {@link Callback} interface. If this is
2671 * an ACTION_MULTIPLE event and it is not handled, then an attempt will
2672 * be made to deliver a single normal event.
RoboErik01fe6612014-02-13 14:19:04 -08002673 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 * @param receiver The Callback that will be given the event.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002675 * @param state State information retained across events.
2676 * @param target The target of the dispatch, for use in tracking.
RoboErik01fe6612014-02-13 14:19:04 -08002677 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002678 * @return The return value from the Callback method that was called.
2679 */
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002680 public final boolean dispatch(Callback receiver, DispatcherState state,
2681 Object target) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 switch (mAction) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002683 case ACTION_DOWN: {
2684 mFlags &= ~FLAG_START_TRACKING;
Dianne Hackborn8d374262009-09-14 21:21:52 -07002685 if (DEBUG) Log.v(TAG, "Key down to " + target + " in " + state
2686 + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002687 boolean res = receiver.onKeyDown(mKeyCode, this);
2688 if (state != null) {
2689 if (res && mRepeatCount == 0 && (mFlags&FLAG_START_TRACKING) != 0) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002690 if (DEBUG) Log.v(TAG, " Start tracking!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002691 state.startTracking(this, target);
2692 } else if (isLongPress() && state.isTracking(this)) {
2693 try {
2694 if (receiver.onKeyLongPress(mKeyCode, this)) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002695 if (DEBUG) Log.v(TAG, " Clear from long press!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002696 state.performedLongPress(this);
2697 res = true;
2698 }
2699 } catch (AbstractMethodError e) {
2700 }
2701 }
2702 }
2703 return res;
2704 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 case ACTION_UP:
Dianne Hackborn8d374262009-09-14 21:21:52 -07002706 if (DEBUG) Log.v(TAG, "Key up to " + target + " in " + state
2707 + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002708 if (state != null) {
2709 state.handleUpEvent(this);
2710 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 return receiver.onKeyUp(mKeyCode, this);
2712 case ACTION_MULTIPLE:
2713 final int count = mRepeatCount;
2714 final int code = mKeyCode;
2715 if (receiver.onKeyMultiple(code, count, this)) {
2716 return true;
2717 }
2718 if (code != KeyEvent.KEYCODE_UNKNOWN) {
2719 mAction = ACTION_DOWN;
2720 mRepeatCount = 0;
2721 boolean handled = receiver.onKeyDown(code, this);
2722 if (handled) {
2723 mAction = ACTION_UP;
2724 receiver.onKeyUp(code, this);
2725 }
2726 mAction = ACTION_MULTIPLE;
2727 mRepeatCount = count;
2728 return handled;
2729 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002730 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 }
2732 return false;
2733 }
2734
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002735 /**
2736 * Use with {@link KeyEvent#dispatch(Callback, DispatcherState, Object)}
2737 * for more advanced key dispatching, such as long presses.
2738 */
2739 public static class DispatcherState {
2740 int mDownKeyCode;
2741 Object mDownTarget;
2742 SparseIntArray mActiveLongPresses = new SparseIntArray();
RoboErik01fe6612014-02-13 14:19:04 -08002743
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002744 /**
2745 * Reset back to initial state.
2746 */
2747 public void reset() {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002748 if (DEBUG) Log.v(TAG, "Reset: " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002749 mDownKeyCode = 0;
2750 mDownTarget = null;
2751 mActiveLongPresses.clear();
2752 }
RoboErik01fe6612014-02-13 14:19:04 -08002753
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002754 /**
2755 * Stop any tracking associated with this target.
2756 */
2757 public void reset(Object target) {
2758 if (mDownTarget == target) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002759 if (DEBUG) Log.v(TAG, "Reset in " + target + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002760 mDownKeyCode = 0;
2761 mDownTarget = null;
2762 }
2763 }
RoboErik01fe6612014-02-13 14:19:04 -08002764
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002765 /**
2766 * Start tracking the key code associated with the given event. This
2767 * can only be called on a key down. It will allow you to see any
2768 * long press associated with the key, and will result in
2769 * {@link KeyEvent#isTracking} return true on the long press and up
2770 * events.
RoboErik01fe6612014-02-13 14:19:04 -08002771 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002772 * <p>This is only needed if you are directly dispatching events, rather
2773 * than handling them in {@link Callback#onKeyDown}.
2774 */
2775 public void startTracking(KeyEvent event, Object target) {
2776 if (event.getAction() != ACTION_DOWN) {
2777 throw new IllegalArgumentException(
2778 "Can only start tracking on a down event");
2779 }
Dianne Hackborn8d374262009-09-14 21:21:52 -07002780 if (DEBUG) Log.v(TAG, "Start trackingt in " + target + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002781 mDownKeyCode = event.getKeyCode();
2782 mDownTarget = target;
2783 }
RoboErik01fe6612014-02-13 14:19:04 -08002784
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002785 /**
2786 * Return true if the key event is for a key code that is currently
2787 * being tracked by the dispatcher.
2788 */
2789 public boolean isTracking(KeyEvent event) {
2790 return mDownKeyCode == event.getKeyCode();
2791 }
RoboErik01fe6612014-02-13 14:19:04 -08002792
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002793 /**
2794 * Keep track of the given event's key code as having performed an
2795 * action with a long press, so no action should occur on the up.
2796 * <p>This is only needed if you are directly dispatching events, rather
2797 * than handling them in {@link Callback#onKeyLongPress}.
2798 */
2799 public void performedLongPress(KeyEvent event) {
2800 mActiveLongPresses.put(event.getKeyCode(), 1);
2801 }
RoboErik01fe6612014-02-13 14:19:04 -08002802
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002803 /**
2804 * Handle key up event to stop tracking. This resets the dispatcher state,
2805 * and updates the key event state based on it.
2806 * <p>This is only needed if you are directly dispatching events, rather
2807 * than handling them in {@link Callback#onKeyUp}.
2808 */
2809 public void handleUpEvent(KeyEvent event) {
2810 final int keyCode = event.getKeyCode();
Dianne Hackborn8d374262009-09-14 21:21:52 -07002811 if (DEBUG) Log.v(TAG, "Handle key up " + event + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002812 int index = mActiveLongPresses.indexOfKey(keyCode);
2813 if (index >= 0) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002814 if (DEBUG) Log.v(TAG, " Index: " + index);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002815 event.mFlags |= FLAG_CANCELED | FLAG_CANCELED_LONG_PRESS;
2816 mActiveLongPresses.removeAt(index);
2817 }
2818 if (mDownKeyCode == keyCode) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002819 if (DEBUG) Log.v(TAG, " Tracking!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002820 event.mFlags |= FLAG_TRACKING;
2821 mDownKeyCode = 0;
2822 mDownTarget = null;
2823 }
2824 }
2825 }
Jeff Brown497a92c2010-09-12 17:55:08 -07002826
2827 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828 public String toString() {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002829 StringBuilder msg = new StringBuilder();
2830 msg.append("KeyEvent { action=").append(actionToString(mAction));
2831 msg.append(", keyCode=").append(keyCodeToString(mKeyCode));
2832 msg.append(", scanCode=").append(mScanCode);
2833 if (mCharacters != null) {
2834 msg.append(", characters=\"").append(mCharacters).append("\"");
2835 }
2836 msg.append(", metaState=").append(metaStateToString(mMetaState));
2837 msg.append(", flags=0x").append(Integer.toHexString(mFlags));
2838 msg.append(", repeatCount=").append(mRepeatCount);
2839 msg.append(", eventTime=").append(mEventTime);
2840 msg.append(", downTime=").append(mDownTime);
2841 msg.append(", deviceId=").append(mDeviceId);
2842 msg.append(", source=0x").append(Integer.toHexString(mSource));
2843 msg.append(" }");
2844 return msg.toString();
Jeff Brown497a92c2010-09-12 17:55:08 -07002845 }
2846
2847 /**
2848 * Returns a string that represents the symbolic name of the specified action
Jeff Brown6f2fba42011-02-19 01:08:02 -08002849 * such as "ACTION_DOWN", or an equivalent numeric constant such as "35" if unknown.
Jeff Brown497a92c2010-09-12 17:55:08 -07002850 *
2851 * @param action The action.
2852 * @return The symbolic name of the specified action.
2853 * @hide
2854 */
2855 public static String actionToString(int action) {
2856 switch (action) {
2857 case ACTION_DOWN:
2858 return "ACTION_DOWN";
2859 case ACTION_UP:
2860 return "ACTION_UP";
2861 case ACTION_MULTIPLE:
2862 return "ACTION_MULTIPLE";
2863 default:
2864 return Integer.toString(action);
2865 }
2866 }
2867
2868 /**
2869 * Returns a string that represents the symbolic name of the specified keycode
Jeff Brown6f2fba42011-02-19 01:08:02 -08002870 * such as "KEYCODE_A", "KEYCODE_DPAD_UP", or an equivalent numeric constant
2871 * such as "1001" if unknown.
Jeff Brown497a92c2010-09-12 17:55:08 -07002872 *
2873 * @param keyCode The key code.
2874 * @return The symbolic name of the specified keycode.
2875 *
2876 * @see KeyCharacterMap#getDisplayLabel
Jeff Brown497a92c2010-09-12 17:55:08 -07002877 */
2878 public static String keyCodeToString(int keyCode) {
Michael Wright337d9d22014-04-22 15:03:48 -07002879 String symbolicName = nativeKeyCodeToString(keyCode);
2880 return symbolicName != null ? LABEL_PREFIX + symbolicName : Integer.toString(keyCode);
Jeff Brown497a92c2010-09-12 17:55:08 -07002881 }
2882
2883 /**
Jeff Brown6f2fba42011-02-19 01:08:02 -08002884 * Gets a keycode by its symbolic name such as "KEYCODE_A" or an equivalent
2885 * numeric constant such as "1001".
Jeff Brown497a92c2010-09-12 17:55:08 -07002886 *
2887 * @param symbolicName The symbolic name of the keycode.
Jeff Brown6f2fba42011-02-19 01:08:02 -08002888 * @return The keycode or {@link #KEYCODE_UNKNOWN} if not found.
Michael Wright072137c2013-04-24 20:41:20 -07002889 * @see #keycodeToString(int)
Jeff Brown497a92c2010-09-12 17:55:08 -07002890 */
2891 public static int keyCodeFromString(String symbolicName) {
Michael Wright337d9d22014-04-22 15:03:48 -07002892 if (symbolicName.startsWith(LABEL_PREFIX)) {
2893 symbolicName = symbolicName.substring(LABEL_PREFIX.length());
Michael Wright973efa02014-05-13 15:38:56 -07002894 int keyCode = nativeKeyCodeFromString(symbolicName);
2895 if (keyCode > 0) {
2896 return keyCode;
2897 }
Jeff Brown497a92c2010-09-12 17:55:08 -07002898 }
Jeff Brown497a92c2010-09-12 17:55:08 -07002899 try {
Jeff Brown6f2fba42011-02-19 01:08:02 -08002900 return Integer.parseInt(symbolicName, 10);
Jeff Brown497a92c2010-09-12 17:55:08 -07002901 } catch (NumberFormatException ex) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08002902 return KEYCODE_UNKNOWN;
Jeff Brown497a92c2010-09-12 17:55:08 -07002903 }
2904 }
2905
2906 /**
2907 * Returns a string that represents the symbolic name of the specified combined meta
2908 * key modifier state flags such as "0", "META_SHIFT_ON",
Jeff Brown6f2fba42011-02-19 01:08:02 -08002909 * "META_ALT_ON|META_SHIFT_ON" or an equivalent numeric constant such as "0x10000000"
2910 * if unknown.
Jeff Brown497a92c2010-09-12 17:55:08 -07002911 *
2912 * @param metaState The meta state.
2913 * @return The symbolic name of the specified combined meta state flags.
2914 * @hide
2915 */
2916 public static String metaStateToString(int metaState) {
2917 if (metaState == 0) {
2918 return "0";
2919 }
2920 StringBuilder result = null;
2921 int i = 0;
2922 while (metaState != 0) {
2923 final boolean isSet = (metaState & 1) != 0;
2924 metaState >>>= 1; // unsigned shift!
2925 if (isSet) {
2926 final String name = META_SYMBOLIC_NAMES[i];
2927 if (result == null) {
2928 if (metaState == 0) {
2929 return name;
2930 }
2931 result = new StringBuilder(name);
2932 } else {
2933 result.append('|');
2934 result.append(name);
2935 }
2936 }
2937 i += 1;
2938 }
2939 return result.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002940 }
2941
2942 public static final Parcelable.Creator<KeyEvent> CREATOR
2943 = new Parcelable.Creator<KeyEvent>() {
Jim Miller07e03842016-06-22 15:18:13 -07002944 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 public KeyEvent createFromParcel(Parcel in) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07002946 in.readInt(); // skip token, we already know this is a KeyEvent
2947 return KeyEvent.createFromParcelBody(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002948 }
2949
Jim Miller07e03842016-06-22 15:18:13 -07002950 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 public KeyEvent[] newArray(int size) {
2952 return new KeyEvent[size];
2953 }
2954 };
RoboErik01fe6612014-02-13 14:19:04 -08002955
Jeff Brown6ec402b2010-07-28 15:48:59 -07002956 /** @hide */
2957 public static KeyEvent createFromParcelBody(Parcel in) {
2958 return new KeyEvent(in);
2959 }
RoboErik01fe6612014-02-13 14:19:04 -08002960
Jeff Brown6ec402b2010-07-28 15:48:59 -07002961 private KeyEvent(Parcel in) {
Jeff Brown91c69ab2011-02-14 17:03:18 -08002962 mDeviceId = in.readInt();
2963 mSource = in.readInt();
Jeff Brown6ec402b2010-07-28 15:48:59 -07002964 mAction = in.readInt();
2965 mKeyCode = in.readInt();
2966 mRepeatCount = in.readInt();
2967 mMetaState = in.readInt();
2968 mScanCode = in.readInt();
2969 mFlags = in.readInt();
2970 mDownTime = in.readLong();
2971 mEventTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002972 }
2973
Jim Miller07e03842016-06-22 15:18:13 -07002974 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002975 public void writeToParcel(Parcel out, int flags) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07002976 out.writeInt(PARCEL_TOKEN_KEY_EVENT);
Jeff Brown91c69ab2011-02-14 17:03:18 -08002977
2978 out.writeInt(mDeviceId);
2979 out.writeInt(mSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980 out.writeInt(mAction);
2981 out.writeInt(mKeyCode);
2982 out.writeInt(mRepeatCount);
2983 out.writeInt(mMetaState);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002984 out.writeInt(mScanCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002985 out.writeInt(mFlags);
2986 out.writeLong(mDownTime);
2987 out.writeLong(mEventTime);
2988 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002989}