blob: b33b4a07c1345220f616d83ce4c5d5751a49ff0f [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
Mathew Inwoode5ad5982018-08-17 15:07:52 +010019import android.annotation.UnsupportedAppUsage;
Mathew Inwood45d2c252018-09-14 12:35:36 +010020import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.os.Parcel;
22import android.os.Parcelable;
Jeff Brown6b53e8d2010-11-10 16:03:06 -080023import android.text.method.MetaKeyKeyListener;
Dianne Hackborn8d374262009-09-14 21:21:52 -070024import android.util.Log;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070025import android.util.SparseIntArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.view.KeyCharacterMap.KeyData;
27
28/**
Jeff Browndc1ab4b2010-09-14 18:03:38 -070029 * Object used to report key and button events.
30 * <p>
31 * Each key press is described by a sequence of key events. A key press
32 * starts with a key event with {@link #ACTION_DOWN}. If the key is held
33 * sufficiently long that it repeats, then the initial down is followed
34 * additional key events with {@link #ACTION_DOWN} and a non-zero value for
35 * {@link #getRepeatCount()}. The last key event is a {@link #ACTION_UP}
36 * for the key up. If the key press is canceled, the key up event will have the
37 * {@link #FLAG_CANCELED} flag set.
38 * </p><p>
39 * Key events are generally accompanied by a key code ({@link #getKeyCode()}),
40 * scan code ({@link #getScanCode()}) and meta state ({@link #getMetaState()}).
41 * Key code constants are defined in this class. Scan code constants are raw
42 * device-specific codes obtained from the OS and so are not generally meaningful
43 * to applications unless interpreted using the {@link KeyCharacterMap}.
44 * Meta states describe the pressed state of key modifiers
45 * such as {@link #META_SHIFT_ON} or {@link #META_ALT_ON}.
46 * </p><p>
Jeff Brown497a92c2010-09-12 17:55:08 -070047 * Key codes typically correspond one-to-one with individual keys on an input device.
48 * Many keys and key combinations serve quite different functions on different
49 * input devices so care must be taken when interpreting them. Always use the
50 * {@link KeyCharacterMap} associated with the input device when mapping keys
51 * to characters. Be aware that there may be multiple key input devices active
52 * at the same time and each will have its own key character map.
53 * </p><p>
Jean Chalard405bc512012-05-29 19:12:34 +090054 * As soft input methods can use multiple and inventive ways of inputting text,
55 * there is no guarantee that any key press on a soft keyboard will generate a key
56 * event: this is left to the IME's discretion, and in fact sending such events is
57 * discouraged. You should never rely on receiving KeyEvents for any key on a soft
58 * input method. In particular, the default software keyboard will never send any
59 * key event to any application targetting Jelly Bean or later, and will only send
60 * events for some presses of the delete and return keys to applications targetting
61 * Ice Cream Sandwich or earlier. Be aware that other software input methods may
62 * never send key events regardless of the version. Consider using editor actions
63 * like {@link android.view.inputmethod.EditorInfo#IME_ACTION_DONE} if you need
64 * specific interaction with the software keyboard, as it gives more visibility to
65 * the user as to how your application will react to key presses.
66 * </p><p>
Jeff Browndc1ab4b2010-09-14 18:03:38 -070067 * When interacting with an IME, the framework may deliver key events
68 * with the special action {@link #ACTION_MULTIPLE} that either specifies
69 * that single repeated key code or a sequence of characters to insert.
70 * </p><p>
Jeff Brownb6997262010-10-08 22:31:17 -070071 * In general, the framework cannot guarantee that the key events it delivers
72 * to a view always constitute complete key sequences since some events may be dropped
73 * or modified by containing views before they are delivered. The view implementation
74 * should be prepared to handle {@link #FLAG_CANCELED} and should tolerate anomalous
75 * situations such as receiving a new {@link #ACTION_DOWN} without first having
76 * received an {@link #ACTION_UP} for the prior key press.
Jeff Browndc1ab4b2010-09-14 18:03:38 -070077 * </p><p>
78 * Refer to {@link InputDevice} for more information about how different kinds of
79 * input devices and sources represent keys and buttons.
80 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 */
Jeff Brownc5ed5912010-07-14 18:48:53 -070082public class KeyEvent extends InputEvent implements Parcelable {
Jeff Browndc1ab4b2010-09-14 18:03:38 -070083 /** Key code constant: Unknown key code. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 public static final int KEYCODE_UNKNOWN = 0;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070085 /** Key code constant: Soft Left key.
86 * Usually situated below the display on phones and used as a multi-function
87 * feature key for selecting a software defined function shown on the bottom left
88 * of the display. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 public static final int KEYCODE_SOFT_LEFT = 1;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070090 /** Key code constant: Soft Right key.
91 * Usually situated below the display on phones and used as a multi-function
92 * feature key for selecting a software defined function shown on the bottom right
93 * of the display. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 public static final int KEYCODE_SOFT_RIGHT = 2;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070095 /** Key code constant: Home key.
96 * This key is handled by the framework and is never delivered to applications. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 public static final int KEYCODE_HOME = 3;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070098 /** Key code constant: Back key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 public static final int KEYCODE_BACK = 4;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700100 /** Key code constant: Call key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 public static final int KEYCODE_CALL = 5;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700102 /** Key code constant: End Call key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 public static final int KEYCODE_ENDCALL = 6;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700104 /** Key code constant: '0' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 public static final int KEYCODE_0 = 7;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700106 /** Key code constant: '1' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 public static final int KEYCODE_1 = 8;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700108 /** Key code constant: '2' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 public static final int KEYCODE_2 = 9;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700110 /** Key code constant: '3' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 public static final int KEYCODE_3 = 10;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700112 /** Key code constant: '4' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 public static final int KEYCODE_4 = 11;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700114 /** Key code constant: '5' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 public static final int KEYCODE_5 = 12;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700116 /** Key code constant: '6' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 public static final int KEYCODE_6 = 13;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700118 /** Key code constant: '7' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 public static final int KEYCODE_7 = 14;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700120 /** Key code constant: '8' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 public static final int KEYCODE_8 = 15;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700122 /** Key code constant: '9' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 public static final int KEYCODE_9 = 16;
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_STAR = 17;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700126 /** Key code constant: '#' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 public static final int KEYCODE_POUND = 18;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700128 /** Key code constant: Directional Pad Up key.
129 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 public static final int KEYCODE_DPAD_UP = 19;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700131 /** Key code constant: Directional Pad Down key.
132 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 public static final int KEYCODE_DPAD_DOWN = 20;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700134 /** Key code constant: Directional Pad Left key.
135 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 public static final int KEYCODE_DPAD_LEFT = 21;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700137 /** Key code constant: Directional Pad Right key.
138 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 public static final int KEYCODE_DPAD_RIGHT = 22;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700140 /** Key code constant: Directional Pad Center key.
141 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 public static final int KEYCODE_DPAD_CENTER = 23;
Jeff Brownb0418da2010-11-01 15:24:01 -0700143 /** Key code constant: Volume Up key.
144 * Adjusts the speaker volume up. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 public static final int KEYCODE_VOLUME_UP = 24;
Jeff Brownb0418da2010-11-01 15:24:01 -0700146 /** Key code constant: Volume Down key.
147 * Adjusts the speaker volume down. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 public static final int KEYCODE_VOLUME_DOWN = 25;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700149 /** Key code constant: Power key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 public static final int KEYCODE_POWER = 26;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700151 /** Key code constant: Camera key.
152 * Used to launch a camera application or take pictures. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 public static final int KEYCODE_CAMERA = 27;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700154 /** Key code constant: Clear key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 public static final int KEYCODE_CLEAR = 28;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700156 /** Key code constant: 'A' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 public static final int KEYCODE_A = 29;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700158 /** Key code constant: 'B' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 public static final int KEYCODE_B = 30;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700160 /** Key code constant: 'C' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 public static final int KEYCODE_C = 31;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700162 /** Key code constant: 'D' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 public static final int KEYCODE_D = 32;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700164 /** Key code constant: 'E' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 public static final int KEYCODE_E = 33;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700166 /** Key code constant: 'F' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 public static final int KEYCODE_F = 34;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700168 /** Key code constant: 'G' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 public static final int KEYCODE_G = 35;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700170 /** Key code constant: 'H' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 public static final int KEYCODE_H = 36;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700172 /** Key code constant: 'I' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 public static final int KEYCODE_I = 37;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700174 /** Key code constant: 'J' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 public static final int KEYCODE_J = 38;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700176 /** Key code constant: 'K' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 public static final int KEYCODE_K = 39;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700178 /** Key code constant: 'L' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 public static final int KEYCODE_L = 40;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700180 /** Key code constant: 'M' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 public static final int KEYCODE_M = 41;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700182 /** Key code constant: 'N' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 public static final int KEYCODE_N = 42;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700184 /** Key code constant: 'O' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 public static final int KEYCODE_O = 43;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700186 /** Key code constant: 'P' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 public static final int KEYCODE_P = 44;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700188 /** Key code constant: 'Q' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 public static final int KEYCODE_Q = 45;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700190 /** Key code constant: 'R' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 public static final int KEYCODE_R = 46;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700192 /** Key code constant: 'S' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 public static final int KEYCODE_S = 47;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700194 /** Key code constant: 'T' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 public static final int KEYCODE_T = 48;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700196 /** Key code constant: 'U' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 public static final int KEYCODE_U = 49;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700198 /** Key code constant: 'V' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 public static final int KEYCODE_V = 50;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700200 /** Key code constant: 'W' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 public static final int KEYCODE_W = 51;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700202 /** Key code constant: 'X' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 public static final int KEYCODE_X = 52;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700204 /** Key code constant: 'Y' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 public static final int KEYCODE_Y = 53;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700206 /** Key code constant: 'Z' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 public static final int KEYCODE_Z = 54;
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_COMMA = 55;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700210 /** Key code constant: '.' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 public static final int KEYCODE_PERIOD = 56;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700212 /** Key code constant: Left Alt modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 public static final int KEYCODE_ALT_LEFT = 57;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700214 /** Key code constant: Right Alt modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 public static final int KEYCODE_ALT_RIGHT = 58;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700216 /** Key code constant: Left Shift modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 public static final int KEYCODE_SHIFT_LEFT = 59;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700218 /** Key code constant: Right Shift modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 public static final int KEYCODE_SHIFT_RIGHT = 60;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700220 /** Key code constant: Tab key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 public static final int KEYCODE_TAB = 61;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700222 /** Key code constant: Space key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 public static final int KEYCODE_SPACE = 62;
Jeff Brown224d4a12010-10-07 20:28:53 -0700224 /** Key code constant: Symbol modifier key.
225 * Used to enter alternate symbols. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 public static final int KEYCODE_SYM = 63;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700227 /** Key code constant: Explorer special function key.
228 * Used to launch a browser application. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 public static final int KEYCODE_EXPLORER = 64;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700230 /** Key code constant: Envelope special function key.
231 * Used to launch a mail application. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 public static final int KEYCODE_ENVELOPE = 65;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700233 /** Key code constant: Enter key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 public static final int KEYCODE_ENTER = 66;
Jeff Brown224d4a12010-10-07 20:28:53 -0700235 /** Key code constant: Backspace key.
Jeff Brown497a92c2010-09-12 17:55:08 -0700236 * Deletes characters before the insertion point, unlike {@link #KEYCODE_FORWARD_DEL}. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 public static final int KEYCODE_DEL = 67;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700238 /** Key code constant: '`' (backtick) key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 public static final int KEYCODE_GRAVE = 68;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700240 /** Key code constant: '-'. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 public static final int KEYCODE_MINUS = 69;
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_EQUALS = 70;
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_LEFT_BRACKET = 71;
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_RIGHT_BRACKET = 72;
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_BACKSLASH = 73;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700250 /** Key code constant: ';' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 public static final int KEYCODE_SEMICOLON = 74;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700252 /** Key code constant: ''' (apostrophe) key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 public static final int KEYCODE_APOSTROPHE = 75;
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_SLASH = 76;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700256 /** Key code constant: '@' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 public static final int KEYCODE_AT = 77;
Jeff Brown224d4a12010-10-07 20:28:53 -0700258 /** Key code constant: Number modifier key.
259 * Used to enter numeric symbols.
260 * This key is not Num Lock; it is more like {@link #KEYCODE_ALT_LEFT} and is
261 * interpreted as an ALT key by {@link android.text.method.MetaKeyKeyListener}. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 public static final int KEYCODE_NUM = 78;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700263 /** Key code constant: Headset Hook key.
264 * Used to hang up calls and stop media. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 public static final int KEYCODE_HEADSETHOOK = 79;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700266 /** Key code constant: Camera Focus key.
267 * Used to focus the camera. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 public static final int KEYCODE_FOCUS = 80; // *Camera* focus
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700269 /** Key code constant: '+' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 public static final int KEYCODE_PLUS = 81;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700271 /** Key code constant: Menu key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 public static final int KEYCODE_MENU = 82;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700273 /** Key code constant: Notification key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 public static final int KEYCODE_NOTIFICATION = 83;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700275 /** Key code constant: Search key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 public static final int KEYCODE_SEARCH = 84;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700277 /** Key code constant: Play/Pause media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700278 public static final int KEYCODE_MEDIA_PLAY_PAUSE= 85;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700279 /** Key code constant: Stop media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700280 public static final int KEYCODE_MEDIA_STOP = 86;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700281 /** Key code constant: Play Next media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700282 public static final int KEYCODE_MEDIA_NEXT = 87;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700283 /** Key code constant: Play Previous media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700284 public static final int KEYCODE_MEDIA_PREVIOUS = 88;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700285 /** Key code constant: Rewind media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700286 public static final int KEYCODE_MEDIA_REWIND = 89;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700287 /** Key code constant: Fast Forward media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700288 public static final int KEYCODE_MEDIA_FAST_FORWARD = 90;
Jeff Brownb0418da2010-11-01 15:24:01 -0700289 /** Key code constant: Mute key.
290 * Mutes the microphone, unlike {@link #KEYCODE_VOLUME_MUTE}. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 public static final int KEYCODE_MUTE = 91;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700292 /** Key code constant: Page Up key. */
Chih-Wei Huang4fedd802009-05-27 15:52:50 +0800293 public static final int KEYCODE_PAGE_UP = 92;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700294 /** Key code constant: Page Down key. */
Chih-Wei Huang4fedd802009-05-27 15:52:50 +0800295 public static final int KEYCODE_PAGE_DOWN = 93;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700296 /** Key code constant: Picture Symbols modifier key.
297 * Used to switch symbol sets (Emoji, Kao-moji). */
mogimob032bc02009-10-03 03:13:56 +0900298 public static final int KEYCODE_PICTSYMBOLS = 94; // switch symbol-sets (Emoji,Kao-moji)
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700299 /** Key code constant: Switch Charset modifier key.
300 * Used to switch character sets (Kanji, Katakana). */
mogimob032bc02009-10-03 03:13:56 +0900301 public static final int KEYCODE_SWITCH_CHARSET = 95; // switch char-sets (Kanji,Katakana)
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700302 /** Key code constant: A Button key.
303 * On a game controller, the A button should be either the button labeled A
Michael Wright6b57bde2013-01-28 20:35:58 -0800304 * or the first button on the bottom row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700305 public static final int KEYCODE_BUTTON_A = 96;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700306 /** Key code constant: B Button key.
307 * On a game controller, the B button should be either the button labeled B
Michael Wright6b57bde2013-01-28 20:35:58 -0800308 * or the second button on the bottom row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700309 public static final int KEYCODE_BUTTON_B = 97;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700310 /** Key code constant: C Button key.
311 * On a game controller, the C button should be either the button labeled C
Michael Wright6b57bde2013-01-28 20:35:58 -0800312 * or the third button on the bottom row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700313 public static final int KEYCODE_BUTTON_C = 98;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700314 /** Key code constant: X Button key.
315 * On a game controller, the X button should be either the button labeled X
Michael Wright6b57bde2013-01-28 20:35:58 -0800316 * or the first button on the upper row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700317 public static final int KEYCODE_BUTTON_X = 99;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700318 /** Key code constant: Y Button key.
319 * On a game controller, the Y button should be either the button labeled Y
Michael Wright6b57bde2013-01-28 20:35:58 -0800320 * or the second button on the upper row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700321 public static final int KEYCODE_BUTTON_Y = 100;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700322 /** Key code constant: Z Button key.
323 * On a game controller, the Z button should be either the button labeled Z
Michael Wright6b57bde2013-01-28 20:35:58 -0800324 * or the third button on the upper row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700325 public static final int KEYCODE_BUTTON_Z = 101;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700326 /** Key code constant: L1 Button key.
327 * On a game controller, the L1 button should be either the button labeled L1 (or L)
328 * or the top left trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700329 public static final int KEYCODE_BUTTON_L1 = 102;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700330 /** Key code constant: R1 Button key.
331 * On a game controller, the R1 button should be either the button labeled R1 (or R)
332 * or the top right trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700333 public static final int KEYCODE_BUTTON_R1 = 103;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700334 /** Key code constant: L2 Button key.
335 * On a game controller, the L2 button should be either the button labeled L2
336 * or the bottom left trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700337 public static final int KEYCODE_BUTTON_L2 = 104;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700338 /** Key code constant: R2 Button key.
339 * On a game controller, the R2 button should be either the button labeled R2
340 * or the bottom right trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700341 public static final int KEYCODE_BUTTON_R2 = 105;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700342 /** Key code constant: Left Thumb Button key.
343 * On a game controller, the left thumb button indicates that the left (or only)
344 * joystick is pressed. */
Jeff Brownfd035822010-06-30 16:10:35 -0700345 public static final int KEYCODE_BUTTON_THUMBL = 106;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700346 /** Key code constant: Right Thumb Button key.
347 * On a game controller, the right thumb button indicates that the right
348 * joystick is pressed. */
Jeff Brownfd035822010-06-30 16:10:35 -0700349 public static final int KEYCODE_BUTTON_THUMBR = 107;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700350 /** Key code constant: Start Button key.
351 * On a game controller, the button labeled Start. */
Jeff Brownfd035822010-06-30 16:10:35 -0700352 public static final int KEYCODE_BUTTON_START = 108;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700353 /** Key code constant: Select Button key.
354 * On a game controller, the button labeled Select. */
Jeff Brownfd035822010-06-30 16:10:35 -0700355 public static final int KEYCODE_BUTTON_SELECT = 109;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700356 /** Key code constant: Mode Button key.
357 * On a game controller, the button labeled Mode. */
Jeff Brownfd035822010-06-30 16:10:35 -0700358 public static final int KEYCODE_BUTTON_MODE = 110;
Jeff Brown497a92c2010-09-12 17:55:08 -0700359 /** Key code constant: Escape key. */
360 public static final int KEYCODE_ESCAPE = 111;
361 /** Key code constant: Forward Delete key.
362 * Deletes characters ahead of the insertion point, unlike {@link #KEYCODE_DEL}. */
363 public static final int KEYCODE_FORWARD_DEL = 112;
364 /** Key code constant: Left Control modifier key. */
365 public static final int KEYCODE_CTRL_LEFT = 113;
366 /** Key code constant: Right Control modifier key. */
367 public static final int KEYCODE_CTRL_RIGHT = 114;
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800368 /** Key code constant: Caps Lock key. */
Jeff Brown497a92c2010-09-12 17:55:08 -0700369 public static final int KEYCODE_CAPS_LOCK = 115;
370 /** Key code constant: Scroll Lock key. */
371 public static final int KEYCODE_SCROLL_LOCK = 116;
372 /** Key code constant: Left Meta modifier key. */
373 public static final int KEYCODE_META_LEFT = 117;
374 /** Key code constant: Right Meta modifier key. */
375 public static final int KEYCODE_META_RIGHT = 118;
376 /** Key code constant: Function modifier key. */
377 public static final int KEYCODE_FUNCTION = 119;
378 /** Key code constant: System Request / Print Screen key. */
379 public static final int KEYCODE_SYSRQ = 120;
380 /** Key code constant: Break / Pause key. */
381 public static final int KEYCODE_BREAK = 121;
382 /** Key code constant: Home Movement key.
383 * Used for scrolling or moving the cursor around to the start of a line
384 * or to the top of a list. */
385 public static final int KEYCODE_MOVE_HOME = 122;
386 /** Key code constant: End Movement key.
387 * Used for scrolling or moving the cursor around to the end of a line
388 * or to the bottom of a list. */
389 public static final int KEYCODE_MOVE_END = 123;
390 /** Key code constant: Insert key.
391 * Toggles insert / overwrite edit mode. */
392 public static final int KEYCODE_INSERT = 124;
393 /** Key code constant: Forward key.
394 * Navigates forward in the history stack. Complement of {@link #KEYCODE_BACK}. */
395 public static final int KEYCODE_FORWARD = 125;
396 /** Key code constant: Play media key. */
397 public static final int KEYCODE_MEDIA_PLAY = 126;
398 /** Key code constant: Pause media key. */
399 public static final int KEYCODE_MEDIA_PAUSE = 127;
400 /** Key code constant: Close media key.
401 * May be used to close a CD tray, for example. */
402 public static final int KEYCODE_MEDIA_CLOSE = 128;
403 /** Key code constant: Eject media key.
404 * May be used to eject a CD tray, for example. */
405 public static final int KEYCODE_MEDIA_EJECT = 129;
406 /** Key code constant: Record media key. */
407 public static final int KEYCODE_MEDIA_RECORD = 130;
408 /** Key code constant: F1 key. */
409 public static final int KEYCODE_F1 = 131;
410 /** Key code constant: F2 key. */
411 public static final int KEYCODE_F2 = 132;
412 /** Key code constant: F3 key. */
413 public static final int KEYCODE_F3 = 133;
414 /** Key code constant: F4 key. */
415 public static final int KEYCODE_F4 = 134;
416 /** Key code constant: F5 key. */
417 public static final int KEYCODE_F5 = 135;
418 /** Key code constant: F6 key. */
419 public static final int KEYCODE_F6 = 136;
420 /** Key code constant: F7 key. */
421 public static final int KEYCODE_F7 = 137;
422 /** Key code constant: F8 key. */
423 public static final int KEYCODE_F8 = 138;
424 /** Key code constant: F9 key. */
425 public static final int KEYCODE_F9 = 139;
426 /** Key code constant: F10 key. */
427 public static final int KEYCODE_F10 = 140;
428 /** Key code constant: F11 key. */
429 public static final int KEYCODE_F11 = 141;
430 /** Key code constant: F12 key. */
431 public static final int KEYCODE_F12 = 142;
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800432 /** Key code constant: Num Lock key.
Jeff Brown497a92c2010-09-12 17:55:08 -0700433 * This is the Num Lock key; it is different from {@link #KEYCODE_NUM}.
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800434 * This key alters the behavior of other keys on the numeric keypad. */
Jeff Brown497a92c2010-09-12 17:55:08 -0700435 public static final int KEYCODE_NUM_LOCK = 143;
436 /** Key code constant: Numeric keypad '0' key. */
437 public static final int KEYCODE_NUMPAD_0 = 144;
438 /** Key code constant: Numeric keypad '1' key. */
439 public static final int KEYCODE_NUMPAD_1 = 145;
440 /** Key code constant: Numeric keypad '2' key. */
441 public static final int KEYCODE_NUMPAD_2 = 146;
442 /** Key code constant: Numeric keypad '3' key. */
443 public static final int KEYCODE_NUMPAD_3 = 147;
444 /** Key code constant: Numeric keypad '4' key. */
445 public static final int KEYCODE_NUMPAD_4 = 148;
446 /** Key code constant: Numeric keypad '5' key. */
447 public static final int KEYCODE_NUMPAD_5 = 149;
448 /** Key code constant: Numeric keypad '6' key. */
449 public static final int KEYCODE_NUMPAD_6 = 150;
450 /** Key code constant: Numeric keypad '7' key. */
451 public static final int KEYCODE_NUMPAD_7 = 151;
452 /** Key code constant: Numeric keypad '8' key. */
453 public static final int KEYCODE_NUMPAD_8 = 152;
454 /** Key code constant: Numeric keypad '9' key. */
455 public static final int KEYCODE_NUMPAD_9 = 153;
456 /** Key code constant: Numeric keypad '/' key (for division). */
457 public static final int KEYCODE_NUMPAD_DIVIDE = 154;
458 /** Key code constant: Numeric keypad '*' key (for multiplication). */
459 public static final int KEYCODE_NUMPAD_MULTIPLY = 155;
460 /** Key code constant: Numeric keypad '-' key (for subtraction). */
461 public static final int KEYCODE_NUMPAD_SUBTRACT = 156;
462 /** Key code constant: Numeric keypad '+' key (for addition). */
463 public static final int KEYCODE_NUMPAD_ADD = 157;
464 /** Key code constant: Numeric keypad '.' key (for decimals or digit grouping). */
465 public static final int KEYCODE_NUMPAD_DOT = 158;
466 /** Key code constant: Numeric keypad ',' key (for decimals or digit grouping). */
467 public static final int KEYCODE_NUMPAD_COMMA = 159;
468 /** Key code constant: Numeric keypad Enter key. */
469 public static final int KEYCODE_NUMPAD_ENTER = 160;
470 /** Key code constant: Numeric keypad '=' key. */
471 public static final int KEYCODE_NUMPAD_EQUALS = 161;
472 /** Key code constant: Numeric keypad '(' key. */
473 public static final int KEYCODE_NUMPAD_LEFT_PAREN = 162;
474 /** Key code constant: Numeric keypad ')' key. */
475 public static final int KEYCODE_NUMPAD_RIGHT_PAREN = 163;
Jeff Brownb0418da2010-11-01 15:24:01 -0700476 /** Key code constant: Volume Mute key.
477 * Mutes the speaker, unlike {@link #KEYCODE_MUTE}.
478 * This key should normally be implemented as a toggle such that the first press
479 * mutes the speaker and the second press restores the original volume. */
480 public static final int KEYCODE_VOLUME_MUTE = 164;
Jason Bayer3adf4902010-11-09 14:54:55 -0800481 /** Key code constant: Info key.
482 * Common on TV remotes to show additional information related to what is
483 * currently being viewed. */
484 public static final int KEYCODE_INFO = 165;
485 /** Key code constant: Channel up key.
486 * On TV remotes, increments the television channel. */
487 public static final int KEYCODE_CHANNEL_UP = 166;
488 /** Key code constant: Channel down key.
489 * On TV remotes, decrements the television channel. */
490 public static final int KEYCODE_CHANNEL_DOWN = 167;
491 /** Key code constant: Zoom in key. */
492 public static final int KEYCODE_ZOOM_IN = 168;
493 /** Key code constant: Zoom out key. */
494 public static final int KEYCODE_ZOOM_OUT = 169;
495 /** Key code constant: TV key.
496 * On TV remotes, switches to viewing live TV. */
497 public static final int KEYCODE_TV = 170;
498 /** Key code constant: Window key.
Julius D'souza03d4a652017-04-03 10:16:51 -0700499 * On TV remotes, toggles picture-in-picture mode or other windowing functions.
500 * On Android Wear devices, triggers a display offset. */
Jason Bayer3adf4902010-11-09 14:54:55 -0800501 public static final int KEYCODE_WINDOW = 171;
502 /** Key code constant: Guide key.
503 * On TV remotes, shows a programming guide. */
504 public static final int KEYCODE_GUIDE = 172;
505 /** Key code constant: DVR key.
506 * On some TV remotes, switches to a DVR mode for recorded shows. */
507 public static final int KEYCODE_DVR = 173;
508 /** Key code constant: Bookmark key.
509 * On some TV remotes, bookmarks content or web pages. */
510 public static final int KEYCODE_BOOKMARK = 174;
511 /** Key code constant: Toggle captions key.
512 * Switches the mode for closed-captioning text, for example during television shows. */
513 public static final int KEYCODE_CAPTIONS = 175;
514 /** Key code constant: Settings key.
515 * Starts the system settings activity. */
516 public static final int KEYCODE_SETTINGS = 176;
517 /** Key code constant: TV power key.
518 * On TV remotes, toggles the power on a television screen. */
519 public static final int KEYCODE_TV_POWER = 177;
520 /** Key code constant: TV input key.
521 * On TV remotes, switches the input on a television screen. */
522 public static final int KEYCODE_TV_INPUT = 178;
523 /** Key code constant: Set-top-box power key.
524 * On TV remotes, toggles the power on an external Set-top-box. */
525 public static final int KEYCODE_STB_POWER = 179;
526 /** Key code constant: Set-top-box input key.
527 * On TV remotes, switches the input mode on an external Set-top-box. */
528 public static final int KEYCODE_STB_INPUT = 180;
529 /** Key code constant: A/V Receiver power key.
530 * On TV remotes, toggles the power on an external A/V Receiver. */
531 public static final int KEYCODE_AVR_POWER = 181;
532 /** Key code constant: A/V Receiver input key.
533 * On TV remotes, switches the input mode on an external A/V Receiver. */
534 public static final int KEYCODE_AVR_INPUT = 182;
535 /** Key code constant: Red "programmable" key.
536 * On TV remotes, acts as a contextual/programmable key. */
537 public static final int KEYCODE_PROG_RED = 183;
538 /** Key code constant: Green "programmable" key.
539 * On TV remotes, actsas a contextual/programmable key. */
540 public static final int KEYCODE_PROG_GREEN = 184;
541 /** Key code constant: Yellow "programmable" key.
542 * On TV remotes, acts as a contextual/programmable key. */
543 public static final int KEYCODE_PROG_YELLOW = 185;
544 /** Key code constant: Blue "programmable" key.
545 * On TV remotes, acts as a contextual/programmable key. */
546 public static final int KEYCODE_PROG_BLUE = 186;
Jeff Brown49ed71d2010-12-06 17:13:33 -0800547 /** Key code constant: App switch key.
548 * Should bring up the application switcher dialog. */
549 public static final int KEYCODE_APP_SWITCH = 187;
Jeff Browncb1404e2011-01-15 18:14:15 -0800550 /** Key code constant: Generic Game Pad Button #1.*/
551 public static final int KEYCODE_BUTTON_1 = 188;
552 /** Key code constant: Generic Game Pad Button #2.*/
553 public static final int KEYCODE_BUTTON_2 = 189;
554 /** Key code constant: Generic Game Pad Button #3.*/
555 public static final int KEYCODE_BUTTON_3 = 190;
556 /** Key code constant: Generic Game Pad Button #4.*/
557 public static final int KEYCODE_BUTTON_4 = 191;
558 /** Key code constant: Generic Game Pad Button #5.*/
559 public static final int KEYCODE_BUTTON_5 = 192;
560 /** Key code constant: Generic Game Pad Button #6.*/
561 public static final int KEYCODE_BUTTON_6 = 193;
562 /** Key code constant: Generic Game Pad Button #7.*/
563 public static final int KEYCODE_BUTTON_7 = 194;
564 /** Key code constant: Generic Game Pad Button #8.*/
565 public static final int KEYCODE_BUTTON_8 = 195;
566 /** Key code constant: Generic Game Pad Button #9.*/
567 public static final int KEYCODE_BUTTON_9 = 196;
568 /** Key code constant: Generic Game Pad Button #10.*/
569 public static final int KEYCODE_BUTTON_10 = 197;
570 /** Key code constant: Generic Game Pad Button #11.*/
571 public static final int KEYCODE_BUTTON_11 = 198;
572 /** Key code constant: Generic Game Pad Button #12.*/
573 public static final int KEYCODE_BUTTON_12 = 199;
574 /** Key code constant: Generic Game Pad Button #13.*/
575 public static final int KEYCODE_BUTTON_13 = 200;
576 /** Key code constant: Generic Game Pad Button #14.*/
577 public static final int KEYCODE_BUTTON_14 = 201;
578 /** Key code constant: Generic Game Pad Button #15.*/
579 public static final int KEYCODE_BUTTON_15 = 202;
580 /** Key code constant: Generic Game Pad Button #16.*/
581 public static final int KEYCODE_BUTTON_16 = 203;
Jeff Brown9812aed2011-03-07 17:09:51 -0800582 /** Key code constant: Language Switch key.
583 * Toggles the current input language such as switching between English and Japanese on
584 * a QWERTY keyboard. On some devices, the same function may be performed by
585 * pressing Shift+Spacebar. */
586 public static final int KEYCODE_LANGUAGE_SWITCH = 204;
587 /** Key code constant: Manner Mode key.
588 * Toggles silent or vibrate mode on and off to make the device behave more politely
589 * in certain settings such as on a crowded train. On some devices, the key may only
590 * operate when long-pressed. */
591 public static final int KEYCODE_MANNER_MODE = 205;
592 /** Key code constant: 3D Mode key.
593 * Toggles the display between 2D and 3D mode. */
594 public static final int KEYCODE_3D_MODE = 206;
Jeff Brown6651a632011-11-28 12:59:11 -0800595 /** Key code constant: Contacts special function key.
596 * Used to launch an address book application. */
597 public static final int KEYCODE_CONTACTS = 207;
598 /** Key code constant: Calendar special function key.
599 * Used to launch a calendar application. */
600 public static final int KEYCODE_CALENDAR = 208;
601 /** Key code constant: Music special function key.
602 * Used to launch a music player application. */
603 public static final int KEYCODE_MUSIC = 209;
604 /** Key code constant: Calculator special function key.
605 * Used to launch a calculator application. */
606 public static final int KEYCODE_CALCULATOR = 210;
Yang Chuang7511f9c2012-02-10 15:18:26 +0800607 /** Key code constant: Japanese full-width / half-width key. */
608 public static final int KEYCODE_ZENKAKU_HANKAKU = 211;
609 /** Key code constant: Japanese alphanumeric key. */
610 public static final int KEYCODE_EISU = 212;
611 /** Key code constant: Japanese non-conversion key. */
612 public static final int KEYCODE_MUHENKAN = 213;
613 /** Key code constant: Japanese conversion key. */
614 public static final int KEYCODE_HENKAN = 214;
615 /** Key code constant: Japanese katakana / hiragana key. */
616 public static final int KEYCODE_KATAKANA_HIRAGANA = 215;
617 /** Key code constant: Japanese Yen key. */
618 public static final int KEYCODE_YEN = 216;
619 /** Key code constant: Japanese Ro key. */
620 public static final int KEYCODE_RO = 217;
621 /** Key code constant: Japanese kana key. */
622 public static final int KEYCODE_KANA = 218;
Jeff Brownde7a8ea2012-06-13 18:28:57 -0700623 /** Key code constant: Assist key.
624 * Launches the global assist activity. Not delivered to applications. */
625 public static final int KEYCODE_ASSIST = 219;
Michael Wright1df477a2013-01-31 16:19:18 -0800626 /** Key code constant: Brightness Down key.
627 * Adjusts the screen brightness down. */
628 public static final int KEYCODE_BRIGHTNESS_DOWN = 220;
629 /** Key code constant: Brightness Up key.
630 * Adjusts the screen brightness up. */
631 public static final int KEYCODE_BRIGHTNESS_UP = 221;
Jeff Brown6212a492014-03-07 13:58:47 -0800632 /** Key code constant: Audio Track key.
Jaekyun Seokbfdad8e2013-07-08 13:53:21 +0900633 * Switches the audio tracks. */
634 public static final int KEYCODE_MEDIA_AUDIO_TRACK = 222;
Jeff Brown6212a492014-03-07 13:58:47 -0800635 /** Key code constant: Sleep key.
636 * Puts the device to sleep. Behaves somewhat like {@link #KEYCODE_POWER} but it
637 * has no effect if the device is already asleep. */
638 public static final int KEYCODE_SLEEP = 223;
639 /** Key code constant: Wakeup key.
640 * Wakes up the device. Behaves somewhat like {@link #KEYCODE_POWER} but it
641 * has no effect if the device is already awake. */
642 public static final int KEYCODE_WAKEUP = 224;
Tim Kilbourn87cd0dc2014-04-14 15:37:51 -0700643 /** Key code constant: Pairing key.
644 * Initiates peripheral pairing mode. Useful for pairing remote control
645 * devices or game controllers, especially if no other input mode is
646 * available. */
647 public static final int KEYCODE_PAIRING = 225;
Jinsuk Kim96658f72014-05-14 15:33:43 +0900648 /** Key code constant: Media Top Menu key.
649 * Goes to the top of media menu. */
650 public static final int KEYCODE_MEDIA_TOP_MENU = 226;
651 /** Key code constant: '11' key. */
652 public static final int KEYCODE_11 = 227;
653 /** Key code constant: '12' key. */
654 public static final int KEYCODE_12 = 228;
655 /** Key code constant: Last Channel key.
656 * Goes to the last viewed channel. */
657 public static final int KEYCODE_LAST_CHANNEL = 229;
658 /** Key code constant: TV data service key.
659 * Displays data services like weather, sports. */
660 public static final int KEYCODE_TV_DATA_SERVICE = 230;
Michael Wrightdc63f7b2014-08-21 19:05:21 -0700661 /** Key code constant: Voice Assist key.
662 * Launches the global voice assist activity. Not delivered to applications. */
663 public static final int KEYCODE_VOICE_ASSIST = 231;
ASAZU, Hidekidbd6aba2014-08-27 18:03:30 +0900664 /** Key code constant: Radio key.
665 * Toggles TV service / Radio service. */
666 public static final int KEYCODE_TV_RADIO_SERVICE = 232;
667 /** Key code constant: Teletext key.
668 * Displays Teletext service. */
669 public static final int KEYCODE_TV_TELETEXT = 233;
670 /** Key code constant: Number entry key.
671 * Initiates to enter multi-digit channel nubmber when each digit key is assigned
672 * for selecting separate channel. Corresponds to Number Entry Mode (0x1D) of CEC
673 * User Control Code. */
674 public static final int KEYCODE_TV_NUMBER_ENTRY = 234;
675 /** Key code constant: Analog Terrestrial key.
676 * Switches to analog terrestrial broadcast service. */
677 public static final int KEYCODE_TV_TERRESTRIAL_ANALOG = 235;
678 /** Key code constant: Digital Terrestrial key.
679 * Switches to digital terrestrial broadcast service. */
680 public static final int KEYCODE_TV_TERRESTRIAL_DIGITAL = 236;
681 /** Key code constant: Satellite key.
682 * Switches to digital satellite broadcast service. */
683 public static final int KEYCODE_TV_SATELLITE = 237;
684 /** Key code constant: BS key.
685 * Switches to BS digital satellite broadcasting service available in Japan. */
686 public static final int KEYCODE_TV_SATELLITE_BS = 238;
687 /** Key code constant: CS key.
688 * Switches to CS digital satellite broadcasting service available in Japan. */
689 public static final int KEYCODE_TV_SATELLITE_CS = 239;
690 /** Key code constant: BS/CS key.
691 * Toggles between BS and CS digital satellite services. */
692 public static final int KEYCODE_TV_SATELLITE_SERVICE = 240;
693 /** Key code constant: Toggle Network key.
694 * Toggles selecting broacast services. */
695 public static final int KEYCODE_TV_NETWORK = 241;
696 /** Key code constant: Antenna/Cable key.
697 * Toggles broadcast input source between antenna and cable. */
698 public static final int KEYCODE_TV_ANTENNA_CABLE = 242;
699 /** Key code constant: HDMI #1 key.
700 * Switches to HDMI input #1. */
701 public static final int KEYCODE_TV_INPUT_HDMI_1 = 243;
702 /** Key code constant: HDMI #2 key.
703 * Switches to HDMI input #2. */
704 public static final int KEYCODE_TV_INPUT_HDMI_2 = 244;
705 /** Key code constant: HDMI #3 key.
706 * Switches to HDMI input #3. */
707 public static final int KEYCODE_TV_INPUT_HDMI_3 = 245;
708 /** Key code constant: HDMI #4 key.
709 * Switches to HDMI input #4. */
710 public static final int KEYCODE_TV_INPUT_HDMI_4 = 246;
711 /** Key code constant: Composite #1 key.
712 * Switches to composite video input #1. */
713 public static final int KEYCODE_TV_INPUT_COMPOSITE_1 = 247;
714 /** Key code constant: Composite #2 key.
715 * Switches to composite video input #2. */
716 public static final int KEYCODE_TV_INPUT_COMPOSITE_2 = 248;
717 /** Key code constant: Component #1 key.
718 * Switches to component video input #1. */
719 public static final int KEYCODE_TV_INPUT_COMPONENT_1 = 249;
720 /** Key code constant: Component #2 key.
721 * Switches to component video input #2. */
722 public static final int KEYCODE_TV_INPUT_COMPONENT_2 = 250;
723 /** Key code constant: VGA #1 key.
724 * Switches to VGA (analog RGB) input #1. */
725 public static final int KEYCODE_TV_INPUT_VGA_1 = 251;
726 /** Key code constant: Audio description key.
727 * Toggles audio description off / on. */
728 public static final int KEYCODE_TV_AUDIO_DESCRIPTION = 252;
729 /** Key code constant: Audio description mixing volume up key.
730 * Louden audio description volume as compared with normal audio volume. */
731 public static final int KEYCODE_TV_AUDIO_DESCRIPTION_MIX_UP = 253;
732 /** Key code constant: Audio description mixing volume down key.
733 * Lessen audio description volume as compared with normal audio volume. */
734 public static final int KEYCODE_TV_AUDIO_DESCRIPTION_MIX_DOWN = 254;
735 /** Key code constant: Zoom mode key.
736 * Changes Zoom mode (Normal, Full, Zoom, Wide-zoom, etc.) */
737 public static final int KEYCODE_TV_ZOOM_MODE = 255;
738 /** Key code constant: Contents menu key.
739 * Goes to the title list. Corresponds to Contents Menu (0x0B) of CEC User Control
740 * Code */
741 public static final int KEYCODE_TV_CONTENTS_MENU = 256;
742 /** Key code constant: Media context menu key.
743 * Goes to the context menu of media contents. Corresponds to Media Context-sensitive
744 * Menu (0x11) of CEC User Control Code. */
745 public static final int KEYCODE_TV_MEDIA_CONTEXT_MENU = 257;
746 /** Key code constant: Timer programming key.
747 * Goes to the timer recording menu. Corresponds to Timer Programming (0x54) of
748 * CEC User Control Code. */
749 public static final int KEYCODE_TV_TIMER_PROGRAMMING = 258;
750 /** Key code constant: Help key. */
751 public static final int KEYCODE_HELP = 259;
Michael Wright962c9532015-08-06 15:16:22 +0100752 /** Key code constant: Navigate to previous key.
Joseph Cooper55b9ed42015-04-15 16:21:58 -0700753 * Goes backward by one item in an ordered collection of items. */
754 public static final int KEYCODE_NAVIGATE_PREVIOUS = 260;
Michael Wright962c9532015-08-06 15:16:22 +0100755 /** Key code constant: Navigate to next key.
Joseph Cooper55b9ed42015-04-15 16:21:58 -0700756 * Advances to the next item in an ordered collection of items. */
757 public static final int KEYCODE_NAVIGATE_NEXT = 261;
758 /** Key code constant: Navigate in key.
Michael Wright962c9532015-08-06 15:16:22 +0100759 * Activates the item that currently has focus or expands to the next level of a navigation
Joseph Cooper55b9ed42015-04-15 16:21:58 -0700760 * hierarchy. */
761 public static final int KEYCODE_NAVIGATE_IN = 262;
762 /** Key code constant: Navigate out key.
Michael Wright962c9532015-08-06 15:16:22 +0100763 * Backs out one level of a navigation hierarchy or collapses the item that currently has
Joseph Cooper55b9ed42015-04-15 16:21:58 -0700764 * focus. */
765 public static final int KEYCODE_NAVIGATE_OUT = 263;
Anthony Hugh9d826682015-06-23 10:44:17 -0700766 /** Key code constant: Primary stem key for Wear
767 * Main power/reset button on watch. */
768 public static final int KEYCODE_STEM_PRIMARY = 264;
769 /** Key code constant: Generic stem key 1 for Wear */
770 public static final int KEYCODE_STEM_1 = 265;
771 /** Key code constant: Generic stem key 2 for Wear */
772 public static final int KEYCODE_STEM_2 = 266;
773 /** Key code constant: Generic stem key 3 for Wear */
774 public static final int KEYCODE_STEM_3 = 267;
David Stevensa487f0c2015-07-31 11:00:50 -0700775 /** Key code constant: Directional Pad Up-Left */
776 public static final int KEYCODE_DPAD_UP_LEFT = 268;
777 /** Key code constant: Directional Pad Down-Left */
778 public static final int KEYCODE_DPAD_DOWN_LEFT = 269;
779 /** Key code constant: Directional Pad Up-Right */
780 public static final int KEYCODE_DPAD_UP_RIGHT = 270;
781 /** Key code constant: Directional Pad Down-Right */
782 public static final int KEYCODE_DPAD_DOWN_RIGHT = 271;
Michael Wright962c9532015-08-06 15:16:22 +0100783 /** Key code constant: Skip forward media key. */
784 public static final int KEYCODE_MEDIA_SKIP_FORWARD = 272;
785 /** Key code constant: Skip backward media key. */
786 public static final int KEYCODE_MEDIA_SKIP_BACKWARD = 273;
787 /** Key code constant: Step forward media key.
788 * Steps media forward, one frame at a time. */
789 public static final int KEYCODE_MEDIA_STEP_FORWARD = 274;
790 /** Key code constant: Step backward media key.
791 * Steps media backward, one frame at a time. */
792 public static final int KEYCODE_MEDIA_STEP_BACKWARD = 275;
Nick Armstrong-Crews3a5a8c72015-09-09 09:25:03 -0700793 /** Key code constant: put device to sleep unless a wakelock is held. */
Nick Armstrong-Crews56ecfcc2015-09-07 21:46:50 -0700794 public static final int KEYCODE_SOFT_SLEEP = 276;
Michael Wrightea84cff2015-10-21 18:08:30 +0100795 /** Key code constant: Cut key. */
796 public static final int KEYCODE_CUT = 277;
797 /** Key code constant: Copy key. */
798 public static final int KEYCODE_COPY = 278;
799 /** Key code constant: Paste key. */
800 public static final int KEYCODE_PASTE = 279;
Jim Miller07e03842016-06-22 15:18:13 -0700801 /** Key code constant: Consumed by the system for navigation up */
802 public static final int KEYCODE_SYSTEM_NAVIGATION_UP = 280;
803 /** Key code constant: Consumed by the system for navigation down */
804 public static final int KEYCODE_SYSTEM_NAVIGATION_DOWN = 281;
805 /** Key code constant: Consumed by the system for navigation left*/
806 public static final int KEYCODE_SYSTEM_NAVIGATION_LEFT = 282;
807 /** Key code constant: Consumed by the system for navigation right */
808 public static final int KEYCODE_SYSTEM_NAVIGATION_RIGHT = 283;
Sujith Ramakrishnand2c9bc82017-11-02 09:54:16 -0700809 /** Key code constant: Show all apps */
Sujith Ramakrishnanf8942c02017-07-18 18:35:14 -0700810 public static final int KEYCODE_ALL_APPS = 284;
Yuichiro Hanadac1415f32018-01-15 22:36:00 +0900811 /** Key code constant: Refresh key. */
812 public static final int KEYCODE_REFRESH = 285;
Jeff Brown497a92c2010-09-12 17:55:08 -0700813
Yuichiro Hanadac1415f32018-01-15 22:36:00 +0900814 private static final int LAST_KEYCODE = KEYCODE_REFRESH;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815
816 // NOTE: If you add a new keycode here you must also add it to:
817 // isSystem()
Michael Wrightdc63f7b2014-08-21 19:05:21 -0700818 // isWakeKey()
Chirayu Desai61c37ae2013-04-15 20:11:37 +0530819 // frameworks/native/include/android/keycodes.h
Jinsuk Kim96658f72014-05-14 15:33:43 +0900820 // frameworks/native/include/input/InputEventLabels.h
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 // frameworks/base/core/res/res/values/attrs.xml
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 // emulator?
Jeff Brown6651a632011-11-28 12:59:11 -0800823 // LAST_KEYCODE
Dianne Hackborn935ae462009-04-13 16:11:55 -0700824 //
825 // Also Android currently does not reserve code ranges for vendor-
826 // specific key codes. If you have new key codes to have, you
827 // MUST contribute a patch to the open source project to define
828 // those new codes. This is intended to maintain a consistent
829 // set of key code definitions across all Android devices.
Jeff Brown497a92c2010-09-12 17:55:08 -0700830
Jeff Brown497a92c2010-09-12 17:55:08 -0700831 // Symbolic names of all metakeys in bit order from least significant to most significant.
832 // Accordingly there are exactly 32 values in this table.
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100833 @UnsupportedAppUsage
Jeff Brown497a92c2010-09-12 17:55:08 -0700834 private static final String[] META_SYMBOLIC_NAMES = new String[] {
835 "META_SHIFT_ON",
836 "META_ALT_ON",
837 "META_SYM_ON",
838 "META_FUNCTION_ON",
839 "META_ALT_LEFT_ON",
840 "META_ALT_RIGHT_ON",
841 "META_SHIFT_LEFT_ON",
842 "META_SHIFT_RIGHT_ON",
843 "META_CAP_LOCKED",
844 "META_ALT_LOCKED",
845 "META_SYM_LOCKED",
846 "0x00000800",
847 "META_CTRL_ON",
848 "META_CTRL_LEFT_ON",
849 "META_CTRL_RIGHT_ON",
850 "0x00008000",
851 "META_META_ON",
852 "META_META_LEFT_ON",
853 "META_META_RIGHT_ON",
854 "0x00080000",
Jeff Brown51e7fe72010-10-29 22:19:53 -0700855 "META_CAPS_LOCK_ON",
856 "META_NUM_LOCK_ON",
857 "META_SCROLL_LOCK_ON",
Jeff Brown497a92c2010-09-12 17:55:08 -0700858 "0x00800000",
859 "0x01000000",
860 "0x02000000",
861 "0x04000000",
862 "0x08000000",
863 "0x10000000",
864 "0x20000000",
865 "0x40000000",
866 "0x80000000",
867 };
868
Michael Wright337d9d22014-04-22 15:03:48 -0700869 private static final String LABEL_PREFIX = "KEYCODE_";
870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 /**
872 * @deprecated There are now more than MAX_KEYCODE keycodes.
873 * Use {@link #getMaxKeyCode()} instead.
874 */
875 @Deprecated
876 public static final int MAX_KEYCODE = 84;
877
878 /**
879 * {@link #getAction} value: the key has been pressed down.
880 */
881 public static final int ACTION_DOWN = 0;
882 /**
883 * {@link #getAction} value: the key has been released.
884 */
885 public static final int ACTION_UP = 1;
886 /**
887 * {@link #getAction} value: multiple duplicate key events have
888 * occurred in a row, or a complex string is being delivered. If the
Andrew Solovaya44f2c072018-10-02 14:14:42 -0700889 * key code is not {@link #KEYCODE_UNKNOWN} then the
890 * {@link #getRepeatCount()} method returns the number of times
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 * the given key code should be executed.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700892 * Otherwise, if the key code is {@link #KEYCODE_UNKNOWN}, then
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 * this is a sequence of characters as returned by {@link #getCharacters}.
894 */
895 public static final int ACTION_MULTIPLE = 2;
896
897 /**
Jeff Brown497a92c2010-09-12 17:55:08 -0700898 * SHIFT key locked in CAPS mode.
899 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
900 * @hide
901 */
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100902 @UnsupportedAppUsage
Jeff Brown497a92c2010-09-12 17:55:08 -0700903 public static final int META_CAP_LOCKED = 0x100;
904
905 /**
906 * ALT key locked.
907 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
908 * @hide
909 */
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100910 @UnsupportedAppUsage
Jeff Brown497a92c2010-09-12 17:55:08 -0700911 public static final int META_ALT_LOCKED = 0x200;
912
913 /**
914 * SYM key locked.
915 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
916 * @hide
917 */
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100918 @UnsupportedAppUsage
Jeff Brown497a92c2010-09-12 17:55:08 -0700919 public static final int META_SYM_LOCKED = 0x400;
920
921 /**
922 * Text is in selection mode.
923 * Reserved for use by {@link MetaKeyKeyListener} for a private unpublished constant
924 * in its API that is currently being retained for legacy reasons.
925 * @hide
926 */
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100927 @UnsupportedAppUsage
Jeff Brown497a92c2010-09-12 17:55:08 -0700928 public static final int META_SELECTING = 0x800;
929
930 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 * <p>This mask is used to check whether one of the ALT meta keys is pressed.</p>
932 *
933 * @see #isAltPressed()
934 * @see #getMetaState()
935 * @see #KEYCODE_ALT_LEFT
936 * @see #KEYCODE_ALT_RIGHT
937 */
938 public static final int META_ALT_ON = 0x02;
939
940 /**
941 * <p>This mask is used to check whether the left ALT meta key is pressed.</p>
942 *
943 * @see #isAltPressed()
944 * @see #getMetaState()
945 * @see #KEYCODE_ALT_LEFT
946 */
947 public static final int META_ALT_LEFT_ON = 0x10;
948
949 /**
950 * <p>This mask is used to check whether the right the ALT meta key is pressed.</p>
951 *
952 * @see #isAltPressed()
953 * @see #getMetaState()
954 * @see #KEYCODE_ALT_RIGHT
955 */
956 public static final int META_ALT_RIGHT_ON = 0x20;
957
958 /**
959 * <p>This mask is used to check whether one of the SHIFT meta keys is pressed.</p>
960 *
961 * @see #isShiftPressed()
962 * @see #getMetaState()
963 * @see #KEYCODE_SHIFT_LEFT
964 * @see #KEYCODE_SHIFT_RIGHT
965 */
966 public static final int META_SHIFT_ON = 0x1;
967
968 /**
969 * <p>This mask is used to check whether the left SHIFT meta key is pressed.</p>
970 *
971 * @see #isShiftPressed()
972 * @see #getMetaState()
973 * @see #KEYCODE_SHIFT_LEFT
974 */
975 public static final int META_SHIFT_LEFT_ON = 0x40;
976
977 /**
978 * <p>This mask is used to check whether the right SHIFT meta key is pressed.</p>
979 *
980 * @see #isShiftPressed()
981 * @see #getMetaState()
982 * @see #KEYCODE_SHIFT_RIGHT
983 */
984 public static final int META_SHIFT_RIGHT_ON = 0x80;
985
986 /**
987 * <p>This mask is used to check whether the SYM meta key is pressed.</p>
988 *
989 * @see #isSymPressed()
990 * @see #getMetaState()
991 */
992 public static final int META_SYM_ON = 0x4;
993
994 /**
Jeff Brown497a92c2010-09-12 17:55:08 -0700995 * <p>This mask is used to check whether the FUNCTION meta key is pressed.</p>
996 *
997 * @see #isFunctionPressed()
998 * @see #getMetaState()
999 */
1000 public static final int META_FUNCTION_ON = 0x8;
1001
1002 /**
1003 * <p>This mask is used to check whether one of the CTRL meta keys is pressed.</p>
1004 *
1005 * @see #isCtrlPressed()
1006 * @see #getMetaState()
1007 * @see #KEYCODE_CTRL_LEFT
1008 * @see #KEYCODE_CTRL_RIGHT
1009 */
1010 public static final int META_CTRL_ON = 0x1000;
1011
1012 /**
1013 * <p>This mask is used to check whether the left CTRL meta key is pressed.</p>
1014 *
1015 * @see #isCtrlPressed()
1016 * @see #getMetaState()
1017 * @see #KEYCODE_CTRL_LEFT
1018 */
1019 public static final int META_CTRL_LEFT_ON = 0x2000;
1020
1021 /**
1022 * <p>This mask is used to check whether the right CTRL meta key is pressed.</p>
1023 *
1024 * @see #isCtrlPressed()
1025 * @see #getMetaState()
1026 * @see #KEYCODE_CTRL_RIGHT
1027 */
1028 public static final int META_CTRL_RIGHT_ON = 0x4000;
1029
1030 /**
1031 * <p>This mask is used to check whether one of the META meta keys is pressed.</p>
1032 *
1033 * @see #isMetaPressed()
1034 * @see #getMetaState()
1035 * @see #KEYCODE_META_LEFT
1036 * @see #KEYCODE_META_RIGHT
1037 */
1038 public static final int META_META_ON = 0x10000;
1039
1040 /**
1041 * <p>This mask is used to check whether the left META meta key is pressed.</p>
1042 *
1043 * @see #isMetaPressed()
1044 * @see #getMetaState()
1045 * @see #KEYCODE_META_LEFT
1046 */
1047 public static final int META_META_LEFT_ON = 0x20000;
1048
1049 /**
1050 * <p>This mask is used to check whether the right META meta key is pressed.</p>
1051 *
1052 * @see #isMetaPressed()
1053 * @see #getMetaState()
1054 * @see #KEYCODE_META_RIGHT
1055 */
1056 public static final int META_META_RIGHT_ON = 0x40000;
1057
1058 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07001059 * <p>This mask is used to check whether the CAPS LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07001060 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07001061 * @see #isCapsLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -07001062 * @see #getMetaState()
1063 * @see #KEYCODE_CAPS_LOCK
1064 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07001065 public static final int META_CAPS_LOCK_ON = 0x100000;
Jeff Brown497a92c2010-09-12 17:55:08 -07001066
1067 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07001068 * <p>This mask is used to check whether the NUM LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07001069 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07001070 * @see #isNumLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -07001071 * @see #getMetaState()
1072 * @see #KEYCODE_NUM_LOCK
1073 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07001074 public static final int META_NUM_LOCK_ON = 0x200000;
Jeff Brown497a92c2010-09-12 17:55:08 -07001075
1076 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07001077 * <p>This mask is used to check whether the SCROLL LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07001078 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07001079 * @see #isScrollLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -07001080 * @see #getMetaState()
1081 * @see #KEYCODE_SCROLL_LOCK
1082 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07001083 public static final int META_SCROLL_LOCK_ON = 0x400000;
Jeff Brown497a92c2010-09-12 17:55:08 -07001084
Jeff Brown64da12a2011-01-04 19:57:47 -08001085 /**
1086 * This mask is a combination of {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON}
1087 * and {@link #META_SHIFT_RIGHT_ON}.
1088 */
Jeff Brownc1df9072010-12-21 16:38:50 -08001089 public static final int META_SHIFT_MASK = META_SHIFT_ON
1090 | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON;
1091
Jeff Brown64da12a2011-01-04 19:57:47 -08001092 /**
1093 * This mask is a combination of {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON}
1094 * and {@link #META_ALT_RIGHT_ON}.
1095 */
Jeff Brownc1df9072010-12-21 16:38:50 -08001096 public static final int META_ALT_MASK = META_ALT_ON
1097 | META_ALT_LEFT_ON | META_ALT_RIGHT_ON;
1098
Jeff Brown64da12a2011-01-04 19:57:47 -08001099 /**
1100 * This mask is a combination of {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON}
1101 * and {@link #META_CTRL_RIGHT_ON}.
1102 */
Jeff Brownc1df9072010-12-21 16:38:50 -08001103 public static final int META_CTRL_MASK = META_CTRL_ON
1104 | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON;
1105
Jeff Brown64da12a2011-01-04 19:57:47 -08001106 /**
1107 * This mask is a combination of {@link #META_META_ON}, {@link #META_META_LEFT_ON}
1108 * and {@link #META_META_RIGHT_ON}.
1109 */
1110 public static final int META_META_MASK = META_META_ON
Jeff Brownc1df9072010-12-21 16:38:50 -08001111 | META_META_LEFT_ON | META_META_RIGHT_ON;
1112
Jeff Brown497a92c2010-09-12 17:55:08 -07001113 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 * This mask is set if the device woke because of this key event.
Jeff Brown037c33e2014-04-09 00:31:55 -07001115 *
1116 * @deprecated This flag will never be set by the system since the system
1117 * consumes all wake keys itself.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118 */
Jeff Brown037c33e2014-04-09 00:31:55 -07001119 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 public static final int FLAG_WOKE_HERE = 0x1;
RoboErik01fe6612014-02-13 14:19:04 -08001121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 /**
1123 * This mask is set if the key event was generated by a software keyboard.
1124 */
1125 public static final int FLAG_SOFT_KEYBOARD = 0x2;
RoboErik01fe6612014-02-13 14:19:04 -08001126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 /**
1128 * This mask is set if we don't want the key event to cause us to leave
1129 * touch mode.
1130 */
1131 public static final int FLAG_KEEP_TOUCH_MODE = 0x4;
RoboErik01fe6612014-02-13 14:19:04 -08001132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001134 * This mask is set if an event was known to come from a trusted part
1135 * of the system. That is, the event is known to come from the user,
1136 * and could not have been spoofed by a third party component.
1137 */
1138 public static final int FLAG_FROM_SYSTEM = 0x8;
RoboErik01fe6612014-02-13 14:19:04 -08001139
The Android Open Source Project10592532009-03-18 17:39:46 -07001140 /**
1141 * This mask is used for compatibility, to identify enter keys that are
1142 * coming from an IME whose enter key has been auto-labelled "next" or
1143 * "done". This allows TextView to dispatch these as normal enter keys
1144 * for old applications, but still do the appropriate action when
1145 * receiving them.
1146 */
1147 public static final int FLAG_EDITOR_ACTION = 0x10;
RoboErik01fe6612014-02-13 14:19:04 -08001148
The Android Open Source Project10592532009-03-18 17:39:46 -07001149 /**
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07001150 * When associated with up key events, this indicates that the key press
1151 * has been canceled. Typically this is used with virtual touch screen
1152 * keys, where the user can slide from the virtual key area on to the
1153 * display: in that case, the application will receive a canceled up
1154 * event and should not perform the action normally associated with the
1155 * key. Note that for this to work, the application can not perform an
1156 * action for a key until it receives an up or the long press timeout has
RoboErik01fe6612014-02-13 14:19:04 -08001157 * expired.
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07001158 */
1159 public static final int FLAG_CANCELED = 0x20;
RoboErik01fe6612014-02-13 14:19:04 -08001160
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07001161 /**
1162 * This key event was generated by a virtual (on-screen) hard key area.
1163 * Typically this is an area of the touchscreen, outside of the regular
1164 * display, dedicated to "hardware" buttons.
1165 */
1166 public static final int FLAG_VIRTUAL_HARD_KEY = 0x40;
RoboErik01fe6612014-02-13 14:19:04 -08001167
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07001168 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001169 * This flag is set for the first key repeat that occurs after the
1170 * long press timeout.
1171 */
1172 public static final int FLAG_LONG_PRESS = 0x80;
RoboErik01fe6612014-02-13 14:19:04 -08001173
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001174 /**
1175 * Set when a key event has {@link #FLAG_CANCELED} set because a long
RoboErik01fe6612014-02-13 14:19:04 -08001176 * press action was executed while it was down.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001177 */
1178 public static final int FLAG_CANCELED_LONG_PRESS = 0x100;
RoboErik01fe6612014-02-13 14:19:04 -08001179
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001180 /**
1181 * Set for {@link #ACTION_UP} when this event's key code is still being
1182 * tracked from its initial down. That is, somebody requested that tracking
1183 * started on the key down and a long press has not caused
1184 * the tracking to be canceled.
1185 */
1186 public static final int FLAG_TRACKING = 0x200;
Jeff Brown49ed71d2010-12-06 17:13:33 -08001187
1188 /**
1189 * Set when a key event has been synthesized to implement default behavior
1190 * for an event that the application did not handle.
1191 * Fallback key events are generated by unhandled trackball motions
1192 * (to emulate a directional keypad) and by certain unhandled key presses
1193 * that are declared in the key map (such as special function numeric keypad
1194 * keys when numlock is off).
1195 */
1196 public static final int FLAG_FALLBACK = 0x400;
1197
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001198 /**
Michael Wrighta44dd262013-04-10 21:12:00 -07001199 * Signifies that the key is being predispatched.
1200 * @hide
1201 */
1202 public static final int FLAG_PREDISPATCH = 0x20000000;
1203
1204 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001205 * Private control to determine when an app is tracking a key sequence.
1206 * @hide
1207 */
1208 public static final int FLAG_START_TRACKING = 0x40000000;
Jeff Brown21bc5c92011-02-28 18:27:14 -08001209
1210 /**
1211 * Private flag that indicates when the system has detected that this key event
1212 * may be inconsistent with respect to the sequence of previously delivered key events,
1213 * such as when a key up event is sent but the key was not down.
1214 *
1215 * @hide
1216 * @see #isTainted
1217 * @see #setTainted
1218 */
1219 public static final int FLAG_TAINTED = 0x80000000;
1220
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001221 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 * Returns the maximum keycode.
1223 */
1224 public static int getMaxKeyCode() {
1225 return LAST_KEYCODE;
1226 }
1227
1228 /**
1229 * Get the character that is produced by putting accent on the character
1230 * c.
1231 * For example, getDeadChar('`', 'e') returns &egrave;.
1232 */
1233 public static int getDeadChar(int accent, int c) {
1234 return KeyCharacterMap.getDeadChar(accent, c);
1235 }
RoboErik01fe6612014-02-13 14:19:04 -08001236
Dianne Hackborn8d374262009-09-14 21:21:52 -07001237 static final boolean DEBUG = false;
1238 static final String TAG = "KeyEvent";
Jeff Brown1f245102010-11-18 20:53:46 -08001239
1240 private static final int MAX_RECYCLED = 10;
1241 private static final Object gRecyclerLock = new Object();
1242 private static int gRecyclerUsed;
1243 private static KeyEvent gRecyclerTop;
1244
1245 private KeyEvent mNext;
Jeff Brown1f245102010-11-18 20:53:46 -08001246
Mathew Inwoode5ad5982018-08-17 15:07:52 +01001247 @UnsupportedAppUsage
Jeff Brown91c69ab2011-02-14 17:03:18 -08001248 private int mDeviceId;
Mathew Inwood45d2c252018-09-14 12:35:36 +01001249 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Jeff Brown91c69ab2011-02-14 17:03:18 -08001250 private int mSource;
Mathew Inwoode5ad5982018-08-17 15:07:52 +01001251 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 private int mMetaState;
Mathew Inwoode5ad5982018-08-17 15:07:52 +01001253 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 private int mAction;
Mathew Inwoode5ad5982018-08-17 15:07:52 +01001255 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 private int mKeyCode;
Mathew Inwoode5ad5982018-08-17 15:07:52 +01001257 @UnsupportedAppUsage
Jeff Brown46b9ac02010-04-22 18:58:52 -07001258 private int mScanCode;
Mathew Inwoode5ad5982018-08-17 15:07:52 +01001259 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 private int mRepeatCount;
Mathew Inwoode5ad5982018-08-17 15:07:52 +01001261 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 private int mFlags;
Mathew Inwoode5ad5982018-08-17 15:07:52 +01001263 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 private long mDownTime;
Mathew Inwoode5ad5982018-08-17 15:07:52 +01001265 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 private long mEventTime;
Mathew Inwoode5ad5982018-08-17 15:07:52 +01001267 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 private String mCharacters;
1269
1270 public interface Callback {
1271 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001272 * Called when a key down event has occurred. If you return true,
1273 * you can first call {@link KeyEvent#startTracking()
1274 * KeyEvent.startTracking()} to have the framework track the event
1275 * through its {@link #onKeyUp(int, KeyEvent)} and also call your
1276 * {@link #onKeyLongPress(int, KeyEvent)} if it occurs.
RoboErik01fe6612014-02-13 14:19:04 -08001277 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 * @param keyCode The value in event.getKeyCode().
1279 * @param event Description of the key event.
RoboErik01fe6612014-02-13 14:19:04 -08001280 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 * @return If you handled the event, return true. If you want to allow
1282 * the event to be handled by the next receiver, return false.
1283 */
1284 boolean onKeyDown(int keyCode, KeyEvent event);
1285
1286 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001287 * Called when a long press has occurred. If you return true,
1288 * the final key up will have {@link KeyEvent#FLAG_CANCELED} and
1289 * {@link KeyEvent#FLAG_CANCELED_LONG_PRESS} set. Note that in
1290 * order to receive this callback, someone in the event change
1291 * <em>must</em> return true from {@link #onKeyDown} <em>and</em>
1292 * call {@link KeyEvent#startTracking()} on the event.
RoboErik01fe6612014-02-13 14:19:04 -08001293 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001294 * @param keyCode The value in event.getKeyCode().
1295 * @param event Description of the key event.
RoboErik01fe6612014-02-13 14:19:04 -08001296 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001297 * @return If you handled the event, return true. If you want to allow
1298 * the event to be handled by the next receiver, return false.
1299 */
1300 boolean onKeyLongPress(int keyCode, KeyEvent event);
1301
1302 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 * Called when a key up event has occurred.
RoboErik01fe6612014-02-13 14:19:04 -08001304 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 * @param keyCode The value in event.getKeyCode().
1306 * @param event Description of the key event.
RoboErik01fe6612014-02-13 14:19:04 -08001307 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 * @return If you handled the event, return true. If you want to allow
1309 * the event to be handled by the next receiver, return false.
1310 */
1311 boolean onKeyUp(int keyCode, KeyEvent event);
1312
1313 /**
Kevin Hufnagleb248b1f2016-09-13 19:36:20 -07001314 * Called when a user's interaction with an analog control, such as
1315 * flinging a trackball, generates simulated down/up events for the same
1316 * key multiple times in quick succession.
RoboErik01fe6612014-02-13 14:19:04 -08001317 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 * @param keyCode The value in event.getKeyCode().
1319 * @param count Number of pairs as returned by event.getRepeatCount().
1320 * @param event Description of the key event.
RoboErik01fe6612014-02-13 14:19:04 -08001321 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 * @return If you handled the event, return true. If you want to allow
1323 * the event to be handled by the next receiver, return false.
1324 */
1325 boolean onKeyMultiple(int keyCode, int count, KeyEvent event);
1326 }
1327
Michael Wright337d9d22014-04-22 15:03:48 -07001328 private static native String nativeKeyCodeToString(int keyCode);
1329 private static native int nativeKeyCodeFromString(String keyCode);
Jeff Brown497a92c2010-09-12 17:55:08 -07001330
Jeff Brown1f245102010-11-18 20:53:46 -08001331 private KeyEvent() {
1332 }
1333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 /**
1335 * Create a new key event.
RoboErik01fe6612014-02-13 14:19:04 -08001336 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 * @param action Action code: either {@link #ACTION_DOWN},
1338 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1339 * @param code The key code.
1340 */
1341 public KeyEvent(int action, int code) {
1342 mAction = action;
1343 mKeyCode = code;
1344 mRepeatCount = 0;
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 */
1361 public KeyEvent(long downTime, long eventTime, int action,
1362 int code, int repeat) {
1363 mDownTime = downTime;
1364 mEventTime = eventTime;
1365 mAction = action;
1366 mKeyCode = code;
1367 mRepeatCount = repeat;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001368 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 }
1370
1371 /**
1372 * Create a new key event.
RoboErik01fe6612014-02-13 14:19:04 -08001373 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1375 * at which this key code originally went down.
1376 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1377 * at which this event happened.
1378 * @param action Action code: either {@link #ACTION_DOWN},
1379 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1380 * @param code The key code.
1381 * @param repeat A repeat count for down events (> 0 if this is after the
1382 * initial down) or event count for multiple events.
1383 * @param metaState Flags indicating which meta keys are currently pressed.
1384 */
1385 public KeyEvent(long downTime, long eventTime, int action,
1386 int code, int repeat, int metaState) {
1387 mDownTime = downTime;
1388 mEventTime = eventTime;
1389 mAction = action;
1390 mKeyCode = code;
1391 mRepeatCount = repeat;
1392 mMetaState = metaState;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001393 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 }
1395
1396 /**
1397 * Create a new key event.
RoboErik01fe6612014-02-13 14:19:04 -08001398 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1400 * at which this key code originally went down.
1401 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1402 * at which this event happened.
1403 * @param action Action code: either {@link #ACTION_DOWN},
1404 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1405 * @param code The key code.
1406 * @param repeat A repeat count for down events (> 0 if this is after the
1407 * initial down) or event count for multiple events.
1408 * @param metaState Flags indicating which meta keys are currently pressed.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001409 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 * @param scancode Raw device scan code of the event.
1411 */
1412 public KeyEvent(long downTime, long eventTime, int action,
1413 int code, int repeat, int metaState,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001414 int deviceId, int scancode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 mDownTime = downTime;
1416 mEventTime = eventTime;
1417 mAction = action;
1418 mKeyCode = code;
1419 mRepeatCount = repeat;
1420 mMetaState = metaState;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001421 mDeviceId = deviceId;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001422 mScanCode = scancode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 }
1424
1425 /**
1426 * Create a new key event.
RoboErik01fe6612014-02-13 14:19:04 -08001427 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1429 * at which this key code originally went down.
1430 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1431 * at which this event happened.
1432 * @param action Action code: either {@link #ACTION_DOWN},
1433 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1434 * @param code The key code.
1435 * @param repeat A repeat count for down events (> 0 if this is after the
1436 * initial down) or event count for multiple events.
1437 * @param metaState Flags indicating which meta keys are currently pressed.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001438 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 * @param scancode Raw device scan code of the event.
1440 * @param flags The flags for this key event
1441 */
1442 public KeyEvent(long downTime, long eventTime, int action,
1443 int code, int repeat, int metaState,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001444 int deviceId, int scancode, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 mDownTime = downTime;
1446 mEventTime = eventTime;
1447 mAction = action;
1448 mKeyCode = code;
1449 mRepeatCount = repeat;
1450 mMetaState = metaState;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001451 mDeviceId = deviceId;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001452 mScanCode = scancode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 mFlags = flags;
1454 }
1455
1456 /**
Jeff Brownc5ed5912010-07-14 18:48:53 -07001457 * Create a new key event.
RoboErik01fe6612014-02-13 14:19:04 -08001458 *
Jeff Brownc5ed5912010-07-14 18:48:53 -07001459 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1460 * at which this key code originally went down.
1461 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1462 * at which this event happened.
1463 * @param action Action code: either {@link #ACTION_DOWN},
1464 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1465 * @param code The key code.
1466 * @param repeat A repeat count for down events (> 0 if this is after the
1467 * initial down) or event count for multiple events.
1468 * @param metaState Flags indicating which meta keys are currently pressed.
1469 * @param deviceId The device ID that generated the key event.
1470 * @param scancode Raw device scan code of the event.
1471 * @param flags The flags for this key event
1472 * @param source The input source such as {@link InputDevice#SOURCE_KEYBOARD}.
1473 */
1474 public KeyEvent(long downTime, long eventTime, int action,
1475 int code, int repeat, int metaState,
1476 int deviceId, int scancode, int flags, int source) {
1477 mDownTime = downTime;
1478 mEventTime = eventTime;
1479 mAction = action;
1480 mKeyCode = code;
1481 mRepeatCount = repeat;
1482 mMetaState = metaState;
1483 mDeviceId = deviceId;
1484 mScanCode = scancode;
1485 mFlags = flags;
1486 mSource = source;
1487 }
1488
1489 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 * Create a new key event for a string of characters. The key code,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001491 * action, repeat count and source will automatically be set to
1492 * {@link #KEYCODE_UNKNOWN}, {@link #ACTION_MULTIPLE}, 0, and
1493 * {@link InputDevice#SOURCE_KEYBOARD} for you.
RoboErik01fe6612014-02-13 14:19:04 -08001494 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 * @param time The time (in {@link android.os.SystemClock#uptimeMillis})
1496 * at which this event occured.
1497 * @param characters The string of characters.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001498 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 * @param flags The flags for this key event
1500 */
Jeff Brownc5ed5912010-07-14 18:48:53 -07001501 public KeyEvent(long time, String characters, int deviceId, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 mDownTime = time;
1503 mEventTime = time;
1504 mCharacters = characters;
1505 mAction = ACTION_MULTIPLE;
1506 mKeyCode = KEYCODE_UNKNOWN;
1507 mRepeatCount = 0;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001508 mDeviceId = deviceId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 mFlags = flags;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001510 mSource = InputDevice.SOURCE_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 }
1512
1513 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001514 * Make an exact copy of an existing key event.
1515 */
1516 public KeyEvent(KeyEvent origEvent) {
1517 mDownTime = origEvent.mDownTime;
1518 mEventTime = origEvent.mEventTime;
1519 mAction = origEvent.mAction;
1520 mKeyCode = origEvent.mKeyCode;
1521 mRepeatCount = origEvent.mRepeatCount;
1522 mMetaState = origEvent.mMetaState;
1523 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001524 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001525 mScanCode = origEvent.mScanCode;
The Android Open Source Project10592532009-03-18 17:39:46 -07001526 mFlags = origEvent.mFlags;
1527 mCharacters = origEvent.mCharacters;
1528 }
1529
1530 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 * Copy an existing key event, modifying its time and repeat count.
RoboErik01fe6612014-02-13 14:19:04 -08001532 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001533 * @deprecated Use {@link #changeTimeRepeat(KeyEvent, long, int)}
1534 * instead.
RoboErik01fe6612014-02-13 14:19:04 -08001535 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 * @param origEvent The existing event to be copied.
1537 * @param eventTime The new event time
1538 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1539 * @param newRepeat The new repeat count of the event.
1540 */
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001541 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 public KeyEvent(KeyEvent origEvent, long eventTime, int newRepeat) {
1543 mDownTime = origEvent.mDownTime;
1544 mEventTime = eventTime;
1545 mAction = origEvent.mAction;
1546 mKeyCode = origEvent.mKeyCode;
1547 mRepeatCount = newRepeat;
1548 mMetaState = origEvent.mMetaState;
1549 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001550 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001551 mScanCode = origEvent.mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 mFlags = origEvent.mFlags;
1553 mCharacters = origEvent.mCharacters;
1554 }
1555
Jeff Brown1f245102010-11-18 20:53:46 -08001556 private static KeyEvent obtain() {
1557 final KeyEvent ev;
1558 synchronized (gRecyclerLock) {
1559 ev = gRecyclerTop;
1560 if (ev == null) {
1561 return new KeyEvent();
1562 }
1563 gRecyclerTop = ev.mNext;
1564 gRecyclerUsed -= 1;
1565 }
Jeff Brown1f245102010-11-18 20:53:46 -08001566 ev.mNext = null;
Jeff Brown32cbc38552011-12-01 14:01:49 -08001567 ev.prepareForReuse();
Jeff Brown1f245102010-11-18 20:53:46 -08001568 return ev;
1569 }
1570
1571 /**
1572 * Obtains a (potentially recycled) key event.
1573 *
1574 * @hide
1575 */
Mathew Inwoode5ad5982018-08-17 15:07:52 +01001576 @UnsupportedAppUsage
Jeff Brown1f245102010-11-18 20:53:46 -08001577 public static KeyEvent obtain(long downTime, long eventTime, int action,
1578 int code, int repeat, int metaState,
1579 int deviceId, int scancode, int flags, int source, String characters) {
1580 KeyEvent ev = obtain();
1581 ev.mDownTime = downTime;
1582 ev.mEventTime = eventTime;
1583 ev.mAction = action;
1584 ev.mKeyCode = code;
1585 ev.mRepeatCount = repeat;
1586 ev.mMetaState = metaState;
1587 ev.mDeviceId = deviceId;
1588 ev.mScanCode = scancode;
1589 ev.mFlags = flags;
1590 ev.mSource = source;
1591 ev.mCharacters = characters;
1592 return ev;
1593 }
1594
1595 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08001596 * Obtains a (potentially recycled) copy of another key event.
1597 *
1598 * @hide
1599 */
1600 public static KeyEvent obtain(KeyEvent other) {
1601 KeyEvent ev = obtain();
1602 ev.mDownTime = other.mDownTime;
1603 ev.mEventTime = other.mEventTime;
1604 ev.mAction = other.mAction;
1605 ev.mKeyCode = other.mKeyCode;
1606 ev.mRepeatCount = other.mRepeatCount;
1607 ev.mMetaState = other.mMetaState;
1608 ev.mDeviceId = other.mDeviceId;
1609 ev.mScanCode = other.mScanCode;
1610 ev.mFlags = other.mFlags;
1611 ev.mSource = other.mSource;
1612 ev.mCharacters = other.mCharacters;
1613 return ev;
1614 }
1615
1616 /** @hide */
1617 @Override
1618 public KeyEvent copy() {
1619 return obtain(this);
1620 }
1621
1622 /**
Jeff Brown1f245102010-11-18 20:53:46 -08001623 * Recycles a key event.
1624 * Key events should only be recycled if they are owned by the system since user
1625 * code expects them to be essentially immutable, "tracking" notwithstanding.
1626 *
1627 * @hide
1628 */
Jeff Brown92cc2d82011-12-02 01:19:47 -08001629 @Override
Mathew Inwood45d2c252018-09-14 12:35:36 +01001630 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Jeff Brown1f245102010-11-18 20:53:46 -08001631 public final void recycle() {
Jeff Brown32cbc38552011-12-01 14:01:49 -08001632 super.recycle();
Jeff Brown1f245102010-11-18 20:53:46 -08001633 mCharacters = null;
1634
1635 synchronized (gRecyclerLock) {
1636 if (gRecyclerUsed < MAX_RECYCLED) {
1637 gRecyclerUsed++;
1638 mNext = gRecyclerTop;
1639 gRecyclerTop = this;
1640 }
1641 }
1642 }
1643
Jeff Brown92cc2d82011-12-02 01:19:47 -08001644 /** @hide */
1645 @Override
1646 public final void recycleIfNeededAfterDispatch() {
1647 // Do nothing.
1648 }
1649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001651 * Create a new key event that is the same as the given one, but whose
1652 * event time and repeat count are replaced with the given value.
RoboErik01fe6612014-02-13 14:19:04 -08001653 *
The Android Open Source Project10592532009-03-18 17:39:46 -07001654 * @param event The existing event to be copied. This is not modified.
1655 * @param eventTime The new event time
1656 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1657 * @param newRepeat The new repeat count of the event.
1658 */
1659 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime,
1660 int newRepeat) {
1661 return new KeyEvent(event, eventTime, newRepeat);
1662 }
RoboErik01fe6612014-02-13 14:19:04 -08001663
The Android Open Source Project10592532009-03-18 17:39:46 -07001664 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001665 * Create a new key event that is the same as the given one, but whose
1666 * event time and repeat count are replaced with the given value.
RoboErik01fe6612014-02-13 14:19:04 -08001667 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001668 * @param event The existing event to be copied. This is not modified.
1669 * @param eventTime The new event time
1670 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1671 * @param newRepeat The new repeat count of the event.
1672 * @param newFlags New flags for the event, replacing the entire value
1673 * in the original event.
1674 */
1675 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime,
1676 int newRepeat, int newFlags) {
1677 KeyEvent ret = new KeyEvent(event);
1678 ret.mEventTime = eventTime;
1679 ret.mRepeatCount = newRepeat;
1680 ret.mFlags = newFlags;
1681 return ret;
1682 }
RoboErik01fe6612014-02-13 14:19:04 -08001683
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001684 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 * Copy an existing key event, modifying its action.
RoboErik01fe6612014-02-13 14:19:04 -08001686 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 * @param origEvent The existing event to be copied.
1688 * @param action The new action code of the event.
1689 */
The Android Open Source Project10592532009-03-18 17:39:46 -07001690 private KeyEvent(KeyEvent origEvent, int action) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 mDownTime = origEvent.mDownTime;
1692 mEventTime = origEvent.mEventTime;
1693 mAction = action;
1694 mKeyCode = origEvent.mKeyCode;
1695 mRepeatCount = origEvent.mRepeatCount;
1696 mMetaState = origEvent.mMetaState;
1697 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001698 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001699 mScanCode = origEvent.mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 mFlags = origEvent.mFlags;
1701 // Don't copy mCharacters, since one way or the other we'll lose it
1702 // when changing the action.
1703 }
1704
1705 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001706 * Create a new key event that is the same as the given one, but whose
1707 * action is replaced with the given value.
RoboErik01fe6612014-02-13 14:19:04 -08001708 *
The Android Open Source Project10592532009-03-18 17:39:46 -07001709 * @param event The existing event to be copied. This is not modified.
1710 * @param action The new action code of the event.
1711 */
1712 public static KeyEvent changeAction(KeyEvent event, int action) {
1713 return new KeyEvent(event, action);
1714 }
RoboErik01fe6612014-02-13 14:19:04 -08001715
The Android Open Source Project10592532009-03-18 17:39:46 -07001716 /**
1717 * Create a new key event that is the same as the given one, but whose
1718 * flags are replaced with the given value.
RoboErik01fe6612014-02-13 14:19:04 -08001719 *
The Android Open Source Project10592532009-03-18 17:39:46 -07001720 * @param event The existing event to be copied. This is not modified.
1721 * @param flags The new flags constant.
1722 */
1723 public static KeyEvent changeFlags(KeyEvent event, int flags) {
1724 event = new KeyEvent(event);
1725 event.mFlags = flags;
1726 return event;
1727 }
Jeff Brown21bc5c92011-02-28 18:27:14 -08001728
1729 /** @hide */
1730 @Override
1731 public final boolean isTainted() {
1732 return (mFlags & FLAG_TAINTED) != 0;
1733 }
1734
1735 /** @hide */
1736 @Override
1737 public final void setTainted(boolean tainted) {
1738 mFlags = tainted ? mFlags | FLAG_TAINTED : mFlags & ~FLAG_TAINTED;
1739 }
1740
The Android Open Source Project10592532009-03-18 17:39:46 -07001741 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742 * Don't use in new code, instead explicitly check
1743 * {@link #getAction()}.
RoboErik01fe6612014-02-13 14:19:04 -08001744 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 * @return If the action is ACTION_DOWN, returns true; else false.
1746 *
1747 * @deprecated
1748 * @hide
1749 */
Mathew Inwoode5ad5982018-08-17 15:07:52 +01001750 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 @Deprecated public final boolean isDown() {
1752 return mAction == ACTION_DOWN;
1753 }
1754
Michael Wright337d9d22014-04-22 15:03:48 -07001755 /** Is this a system key? System keys can not be used for menu shortcuts.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 */
1757 public final boolean isSystem() {
Michael Wright337d9d22014-04-22 15:03:48 -07001758 return isSystemKey(mKeyCode);
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -07001759 }
1760
1761 /** @hide */
Michael Wright337d9d22014-04-22 15:03:48 -07001762 public final boolean isWakeKey() {
1763 return isWakeKey(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 }
1765
Jeff Brown6f2fba42011-02-19 01:08:02 -08001766 /**
1767 * Returns true if the specified keycode is a gamepad button.
1768 * @return True if the keycode is a gamepad button, such as {@link #KEYCODE_BUTTON_A}.
1769 */
1770 public static final boolean isGamepadButton(int keyCode) {
1771 switch (keyCode) {
1772 case KeyEvent.KEYCODE_BUTTON_A:
1773 case KeyEvent.KEYCODE_BUTTON_B:
1774 case KeyEvent.KEYCODE_BUTTON_C:
1775 case KeyEvent.KEYCODE_BUTTON_X:
1776 case KeyEvent.KEYCODE_BUTTON_Y:
1777 case KeyEvent.KEYCODE_BUTTON_Z:
1778 case KeyEvent.KEYCODE_BUTTON_L1:
1779 case KeyEvent.KEYCODE_BUTTON_R1:
1780 case KeyEvent.KEYCODE_BUTTON_L2:
1781 case KeyEvent.KEYCODE_BUTTON_R2:
1782 case KeyEvent.KEYCODE_BUTTON_THUMBL:
1783 case KeyEvent.KEYCODE_BUTTON_THUMBR:
1784 case KeyEvent.KEYCODE_BUTTON_START:
1785 case KeyEvent.KEYCODE_BUTTON_SELECT:
1786 case KeyEvent.KEYCODE_BUTTON_MODE:
1787 case KeyEvent.KEYCODE_BUTTON_1:
1788 case KeyEvent.KEYCODE_BUTTON_2:
1789 case KeyEvent.KEYCODE_BUTTON_3:
1790 case KeyEvent.KEYCODE_BUTTON_4:
1791 case KeyEvent.KEYCODE_BUTTON_5:
1792 case KeyEvent.KEYCODE_BUTTON_6:
1793 case KeyEvent.KEYCODE_BUTTON_7:
1794 case KeyEvent.KEYCODE_BUTTON_8:
1795 case KeyEvent.KEYCODE_BUTTON_9:
1796 case KeyEvent.KEYCODE_BUTTON_10:
1797 case KeyEvent.KEYCODE_BUTTON_11:
1798 case KeyEvent.KEYCODE_BUTTON_12:
1799 case KeyEvent.KEYCODE_BUTTON_13:
1800 case KeyEvent.KEYCODE_BUTTON_14:
1801 case KeyEvent.KEYCODE_BUTTON_15:
1802 case KeyEvent.KEYCODE_BUTTON_16:
1803 return true;
1804 default:
1805 return false;
1806 }
1807 }
1808
Michael Wright25b0c302013-07-10 12:54:06 -07001809 /** Whether key will, by default, trigger a click on the focused view.
1810 * @hide
1811 */
Mathew Inwoode5ad5982018-08-17 15:07:52 +01001812 @UnsupportedAppUsage
Michael Wright25b0c302013-07-10 12:54:06 -07001813 public static final boolean isConfirmKey(int keyCode) {
1814 switch (keyCode) {
1815 case KeyEvent.KEYCODE_DPAD_CENTER:
1816 case KeyEvent.KEYCODE_ENTER:
Michael Wrightaa1a94d2015-11-26 16:04:54 +00001817 case KeyEvent.KEYCODE_SPACE:
Selim Cinekce2bd0f2016-02-19 16:16:54 -08001818 case KeyEvent.KEYCODE_NUMPAD_ENTER:
Michael Wright25b0c302013-07-10 12:54:06 -07001819 return true;
1820 default:
1821 return false;
1822 }
1823 }
1824
RoboErik01fe6612014-02-13 14:19:04 -08001825 /**
1826 * Whether this key is a media key, which can be send to apps that are
1827 * interested in media key events.
1828 *
1829 * @hide
1830 */
1831 public static final boolean isMediaKey(int keyCode) {
1832 switch (keyCode) {
1833 case KeyEvent.KEYCODE_MEDIA_PLAY:
1834 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1835 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1836 case KeyEvent.KEYCODE_MUTE:
1837 case KeyEvent.KEYCODE_HEADSETHOOK:
1838 case KeyEvent.KEYCODE_MEDIA_STOP:
1839 case KeyEvent.KEYCODE_MEDIA_NEXT:
1840 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1841 case KeyEvent.KEYCODE_MEDIA_REWIND:
1842 case KeyEvent.KEYCODE_MEDIA_RECORD:
1843 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1844 return true;
1845 }
1846 return false;
1847 }
1848
Michael Wright337d9d22014-04-22 15:03:48 -07001849
1850 /** Is this a system key? System keys can not be used for menu shortcuts.
1851 * @hide
1852 */
1853 public static final boolean isSystemKey(int keyCode) {
1854 switch (keyCode) {
1855 case KeyEvent.KEYCODE_MENU:
1856 case KeyEvent.KEYCODE_SOFT_RIGHT:
1857 case KeyEvent.KEYCODE_HOME:
1858 case KeyEvent.KEYCODE_BACK:
1859 case KeyEvent.KEYCODE_CALL:
1860 case KeyEvent.KEYCODE_ENDCALL:
1861 case KeyEvent.KEYCODE_VOLUME_UP:
1862 case KeyEvent.KEYCODE_VOLUME_DOWN:
1863 case KeyEvent.KEYCODE_VOLUME_MUTE:
1864 case KeyEvent.KEYCODE_MUTE:
1865 case KeyEvent.KEYCODE_POWER:
1866 case KeyEvent.KEYCODE_HEADSETHOOK:
1867 case KeyEvent.KEYCODE_MEDIA_PLAY:
1868 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1869 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1870 case KeyEvent.KEYCODE_MEDIA_STOP:
1871 case KeyEvent.KEYCODE_MEDIA_NEXT:
1872 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1873 case KeyEvent.KEYCODE_MEDIA_REWIND:
1874 case KeyEvent.KEYCODE_MEDIA_RECORD:
1875 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1876 case KeyEvent.KEYCODE_CAMERA:
1877 case KeyEvent.KEYCODE_FOCUS:
1878 case KeyEvent.KEYCODE_SEARCH:
1879 case KeyEvent.KEYCODE_BRIGHTNESS_DOWN:
1880 case KeyEvent.KEYCODE_BRIGHTNESS_UP:
1881 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
Jim Miller07e03842016-06-22 15:18:13 -07001882 case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_UP:
1883 case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_DOWN:
1884 case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_LEFT:
1885 case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_RIGHT:
Michael Wright337d9d22014-04-22 15:03:48 -07001886 return true;
1887 }
1888
1889 return false;
1890 }
1891
1892 /** @hide */
1893 public static final boolean isWakeKey(int keyCode) {
1894 switch (keyCode) {
1895 case KeyEvent.KEYCODE_BACK:
Felix2deb9b52019-06-25 06:34:39 +02001896 case KeyEvent.KEYCODE_CAMERA:
Michael Wright337d9d22014-04-22 15:03:48 -07001897 case KeyEvent.KEYCODE_MENU:
Michael Wright8ac485a2014-06-04 12:38:18 -07001898 case KeyEvent.KEYCODE_PAIRING:
Chenjie Luobad498f2016-01-13 11:01:59 -08001899 case KeyEvent.KEYCODE_STEM_1:
1900 case KeyEvent.KEYCODE_STEM_2:
1901 case KeyEvent.KEYCODE_STEM_3:
Felix2deb9b52019-06-25 06:34:39 +02001902 case KeyEvent.KEYCODE_WAKEUP:
Michael Wright337d9d22014-04-22 15:03:48 -07001903 return true;
1904 }
1905 return false;
1906 }
1907
Michael Wrightce0c13a2014-07-30 10:49:21 -07001908 /** @hide */
1909 public static final boolean isMetaKey(int keyCode) {
1910 return keyCode == KeyEvent.KEYCODE_META_LEFT || keyCode == KeyEvent.KEYCODE_META_RIGHT;
1911 }
1912
Andrii Kulian112d0562016-03-08 10:44:22 -08001913 /** @hide */
1914 public static final boolean isAltKey(int keyCode) {
1915 return keyCode == KeyEvent.KEYCODE_ALT_LEFT || keyCode == KeyEvent.KEYCODE_ALT_RIGHT;
1916 }
1917
Jeff Brown91c69ab2011-02-14 17:03:18 -08001918 /** {@inheritDoc} */
1919 @Override
1920 public final int getDeviceId() {
1921 return mDeviceId;
1922 }
1923
1924 /** {@inheritDoc} */
1925 @Override
1926 public final int getSource() {
1927 return mSource;
1928 }
1929
1930 /** {@inheritDoc} */
1931 @Override
1932 public final void setSource(int source) {
1933 mSource = source;
1934 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935
1936 /**
1937 * <p>Returns the state of the meta keys.</p>
1938 *
1939 * @return an integer in which each bit set to 1 represents a pressed
1940 * meta key
1941 *
1942 * @see #isAltPressed()
1943 * @see #isShiftPressed()
1944 * @see #isSymPressed()
Jeff Brown497a92c2010-09-12 17:55:08 -07001945 * @see #isCtrlPressed()
1946 * @see #isMetaPressed()
1947 * @see #isFunctionPressed()
Jeff Brown51e7fe72010-10-29 22:19:53 -07001948 * @see #isCapsLockOn()
1949 * @see #isNumLockOn()
1950 * @see #isScrollLockOn()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 * @see #META_ALT_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001952 * @see #META_ALT_LEFT_ON
1953 * @see #META_ALT_RIGHT_ON
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 * @see #META_SHIFT_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001955 * @see #META_SHIFT_LEFT_ON
1956 * @see #META_SHIFT_RIGHT_ON
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 * @see #META_SYM_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001958 * @see #META_FUNCTION_ON
1959 * @see #META_CTRL_ON
1960 * @see #META_CTRL_LEFT_ON
1961 * @see #META_CTRL_RIGHT_ON
1962 * @see #META_META_ON
1963 * @see #META_META_LEFT_ON
1964 * @see #META_META_RIGHT_ON
Jeff Brown51e7fe72010-10-29 22:19:53 -07001965 * @see #META_CAPS_LOCK_ON
1966 * @see #META_NUM_LOCK_ON
1967 * @see #META_SCROLL_LOCK_ON
Jeff Brown54875002011-04-06 15:33:01 -07001968 * @see #getModifiers
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001969 */
1970 public final int getMetaState() {
1971 return mMetaState;
1972 }
1973
1974 /**
Jeff Brown54875002011-04-06 15:33:01 -07001975 * Returns the state of the modifier keys.
1976 * <p>
1977 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1978 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1979 * not considered modifier keys. Consequently, this function specifically masks out
1980 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1981 * </p><p>
1982 * The value returned consists of the meta state (from {@link #getMetaState})
1983 * normalized using {@link #normalizeMetaState(int)} and then masked with
1984 * {@link #getModifierMetaStateMask} so that only valid modifier bits are retained.
1985 * </p>
1986 *
1987 * @return An integer in which each bit set to 1 represents a pressed modifier key.
1988 * @see #getMetaState
1989 */
1990 public final int getModifiers() {
1991 return normalizeMetaState(mMetaState) & META_MODIFIER_MASK;
1992 }
1993
1994 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 * Returns the flags for this key event.
1996 *
1997 * @see #FLAG_WOKE_HERE
1998 */
1999 public final int getFlags() {
2000 return mFlags;
2001 }
2002
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002003 // Mask of all modifier key meta states. Specifically excludes locked keys like caps lock.
Mathew Inwoode5ad5982018-08-17 15:07:52 +01002004 @UnsupportedAppUsage
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002005 private static final int META_MODIFIER_MASK =
2006 META_SHIFT_ON | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON
2007 | META_ALT_ON | META_ALT_LEFT_ON | META_ALT_RIGHT_ON
2008 | META_CTRL_ON | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON
2009 | META_META_ON | META_META_LEFT_ON | META_META_RIGHT_ON
2010 | META_SYM_ON | META_FUNCTION_ON;
2011
2012 // Mask of all lock key meta states.
Mathew Inwoode5ad5982018-08-17 15:07:52 +01002013 @UnsupportedAppUsage
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002014 private static final int META_LOCK_MASK =
2015 META_CAPS_LOCK_ON | META_NUM_LOCK_ON | META_SCROLL_LOCK_ON;
2016
2017 // Mask of all valid meta states.
Mathew Inwoode5ad5982018-08-17 15:07:52 +01002018 @UnsupportedAppUsage
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002019 private static final int META_ALL_MASK = META_MODIFIER_MASK | META_LOCK_MASK;
2020
2021 // Mask of all synthetic meta states that are reserved for API compatibility with
2022 // historical uses in MetaKeyKeyListener.
Mathew Inwoode5ad5982018-08-17 15:07:52 +01002023 @UnsupportedAppUsage
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002024 private static final int META_SYNTHETIC_MASK =
2025 META_CAP_LOCKED | META_ALT_LOCKED | META_SYM_LOCKED | META_SELECTING;
2026
2027 // Mask of all meta states that are not valid use in specifying a modifier key.
2028 // These bits are known to be used for purposes other than specifying modifiers.
Mathew Inwoode5ad5982018-08-17 15:07:52 +01002029 @UnsupportedAppUsage
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002030 private static final int META_INVALID_MODIFIER_MASK =
2031 META_LOCK_MASK | META_SYNTHETIC_MASK;
2032
2033 /**
2034 * Gets a mask that includes all valid modifier key meta state bits.
2035 * <p>
2036 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2037 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2038 * not considered modifier keys. Consequently, the mask specifically excludes
2039 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2040 * </p>
2041 *
2042 * @return The modifier meta state mask which is a combination of
2043 * {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON}, {@link #META_SHIFT_RIGHT_ON},
2044 * {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON}, {@link #META_ALT_RIGHT_ON},
2045 * {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON}, {@link #META_CTRL_RIGHT_ON},
2046 * {@link #META_META_ON}, {@link #META_META_LEFT_ON}, {@link #META_META_RIGHT_ON},
2047 * {@link #META_SYM_ON}, {@link #META_FUNCTION_ON}.
2048 */
2049 public static int getModifierMetaStateMask() {
2050 return META_MODIFIER_MASK;
2051 }
2052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 /**
2054 * Returns true if this key code is a modifier key.
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002055 * <p>
2056 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2057 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2058 * not considered modifier keys. Consequently, this function return false
2059 * for those keys.
2060 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 *
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002062 * @return True if the key code is one of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 * {@link #KEYCODE_SHIFT_LEFT} {@link #KEYCODE_SHIFT_RIGHT},
Jeff Brown497a92c2010-09-12 17:55:08 -07002064 * {@link #KEYCODE_ALT_LEFT}, {@link #KEYCODE_ALT_RIGHT},
Jeff Brown497a92c2010-09-12 17:55:08 -07002065 * {@link #KEYCODE_CTRL_LEFT}, {@link #KEYCODE_CTRL_RIGHT},
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002066 * {@link #KEYCODE_META_LEFT}, or {@link #KEYCODE_META_RIGHT},
2067 * {@link #KEYCODE_SYM}, {@link #KEYCODE_NUM}, {@link #KEYCODE_FUNCTION}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 */
2069 public static boolean isModifierKey(int keyCode) {
Jeff Brown497a92c2010-09-12 17:55:08 -07002070 switch (keyCode) {
2071 case KEYCODE_SHIFT_LEFT:
2072 case KEYCODE_SHIFT_RIGHT:
2073 case KEYCODE_ALT_LEFT:
2074 case KEYCODE_ALT_RIGHT:
Jeff Brown497a92c2010-09-12 17:55:08 -07002075 case KEYCODE_CTRL_LEFT:
2076 case KEYCODE_CTRL_RIGHT:
2077 case KEYCODE_META_LEFT:
2078 case KEYCODE_META_RIGHT:
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002079 case KEYCODE_SYM:
2080 case KEYCODE_NUM:
2081 case KEYCODE_FUNCTION:
Jeff Brown497a92c2010-09-12 17:55:08 -07002082 return true;
2083 default:
2084 return false;
2085 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 }
2087
2088 /**
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002089 * Normalizes the specified meta state.
2090 * <p>
2091 * The meta state is normalized such that if either the left or right modifier meta state
2092 * bits are set then the result will also include the universal bit for that modifier.
2093 * </p><p>
2094 * If the specified meta state contains {@link #META_ALT_LEFT_ON} then
2095 * the result will also contain {@link #META_ALT_ON} in addition to {@link #META_ALT_LEFT_ON}
2096 * and the other bits that were specified in the input. The same is process is
2097 * performed for shift, control and meta.
2098 * </p><p>
2099 * If the specified meta state contains synthetic meta states defined by
2100 * {@link MetaKeyKeyListener}, then those states are translated here and the original
2101 * synthetic meta states are removed from the result.
2102 * {@link MetaKeyKeyListener#META_CAP_LOCKED} is translated to {@link #META_CAPS_LOCK_ON}.
2103 * {@link MetaKeyKeyListener#META_ALT_LOCKED} is translated to {@link #META_ALT_ON}.
2104 * {@link MetaKeyKeyListener#META_SYM_LOCKED} is translated to {@link #META_SYM_ON}.
2105 * </p><p>
2106 * Undefined meta state bits are removed.
2107 * </p>
2108 *
2109 * @param metaState The meta state.
2110 * @return The normalized meta state.
2111 */
2112 public static int normalizeMetaState(int metaState) {
2113 if ((metaState & (META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON)) != 0) {
2114 metaState |= META_SHIFT_ON;
2115 }
2116 if ((metaState & (META_ALT_LEFT_ON | META_ALT_RIGHT_ON)) != 0) {
2117 metaState |= META_ALT_ON;
2118 }
2119 if ((metaState & (META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON)) != 0) {
2120 metaState |= META_CTRL_ON;
2121 }
2122 if ((metaState & (META_META_LEFT_ON | META_META_RIGHT_ON)) != 0) {
2123 metaState |= META_META_ON;
2124 }
2125 if ((metaState & MetaKeyKeyListener.META_CAP_LOCKED) != 0) {
2126 metaState |= META_CAPS_LOCK_ON;
2127 }
2128 if ((metaState & MetaKeyKeyListener.META_ALT_LOCKED) != 0) {
2129 metaState |= META_ALT_ON;
2130 }
2131 if ((metaState & MetaKeyKeyListener.META_SYM_LOCKED) != 0) {
2132 metaState |= META_SYM_ON;
2133 }
2134 return metaState & META_ALL_MASK;
2135 }
2136
2137 /**
2138 * Returns true if no modifiers keys are pressed according to the specified meta state.
2139 * <p>
2140 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2141 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2142 * not considered modifier keys. Consequently, this function ignores
2143 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2144 * </p><p>
2145 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}.
2146 * </p>
2147 *
2148 * @param metaState The meta state to consider.
2149 * @return True if no modifier keys are pressed.
2150 * @see #hasNoModifiers()
2151 */
2152 public static boolean metaStateHasNoModifiers(int metaState) {
2153 return (normalizeMetaState(metaState) & META_MODIFIER_MASK) == 0;
2154 }
2155
2156 /**
2157 * Returns true if only the specified modifier keys are pressed according to
2158 * the specified meta state. Returns false if a different combination of modifier
2159 * keys are pressed.
2160 * <p>
2161 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2162 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2163 * not considered modifier keys. Consequently, this function ignores
2164 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2165 * </p><p>
2166 * If the specified modifier mask includes directional modifiers, such as
2167 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the
2168 * modifier is pressed on that side.
2169 * If the specified modifier mask includes non-directional modifiers, such as
2170 * {@link #META_SHIFT_ON}, then this method ensures that the modifier
2171 * is pressed on either side.
2172 * If the specified modifier mask includes both directional and non-directional modifiers
2173 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON},
2174 * then this method throws an illegal argument exception.
2175 * </p>
2176 *
2177 * @param metaState The meta state to consider.
2178 * @param modifiers The meta state of the modifier keys to check. May be a combination
2179 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to
2180 * ensure that no modifier keys are pressed.
2181 * @return True if only the specified modifier keys are pressed.
2182 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers
2183 * @see #hasModifiers
2184 */
2185 public static boolean metaStateHasModifiers(int metaState, int modifiers) {
2186 // Note: For forward compatibility, we allow the parameter to contain meta states
2187 // that we do not recognize but we explicitly disallow meta states that
2188 // are not valid modifiers.
2189 if ((modifiers & META_INVALID_MODIFIER_MASK) != 0) {
2190 throw new IllegalArgumentException("modifiers must not contain "
2191 + "META_CAPS_LOCK_ON, META_NUM_LOCK_ON, META_SCROLL_LOCK_ON, "
2192 + "META_CAP_LOCKED, META_ALT_LOCKED, META_SYM_LOCKED, "
2193 + "or META_SELECTING");
2194 }
2195
2196 metaState = normalizeMetaState(metaState) & META_MODIFIER_MASK;
2197 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2198 META_SHIFT_ON, META_SHIFT_LEFT_ON, META_SHIFT_RIGHT_ON);
2199 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2200 META_ALT_ON, META_ALT_LEFT_ON, META_ALT_RIGHT_ON);
2201 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2202 META_CTRL_ON, META_CTRL_LEFT_ON, META_CTRL_RIGHT_ON);
2203 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2204 META_META_ON, META_META_LEFT_ON, META_META_RIGHT_ON);
2205 return metaState == modifiers;
2206 }
2207
2208 private static int metaStateFilterDirectionalModifiers(int metaState,
2209 int modifiers, int basic, int left, int right) {
2210 final boolean wantBasic = (modifiers & basic) != 0;
2211 final int directional = left | right;
2212 final boolean wantLeftOrRight = (modifiers & directional) != 0;
2213
2214 if (wantBasic) {
2215 if (wantLeftOrRight) {
2216 throw new IllegalArgumentException("modifiers must not contain "
2217 + metaStateToString(basic) + " combined with "
2218 + metaStateToString(left) + " or " + metaStateToString(right));
2219 }
2220 return metaState & ~directional;
2221 } else if (wantLeftOrRight) {
2222 return metaState & ~basic;
2223 } else {
2224 return metaState;
2225 }
2226 }
2227
2228 /**
2229 * Returns true if no modifier keys are pressed.
2230 * <p>
2231 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2232 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2233 * not considered modifier keys. Consequently, this function ignores
2234 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2235 * </p><p>
2236 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}.
2237 * </p>
2238 *
2239 * @return True if no modifier keys are pressed.
2240 * @see #metaStateHasNoModifiers
2241 */
2242 public final boolean hasNoModifiers() {
2243 return metaStateHasNoModifiers(mMetaState);
2244 }
2245
2246 /**
2247 * Returns true if only the specified modifiers keys are pressed.
2248 * Returns false if a different combination of modifier keys are pressed.
2249 * <p>
2250 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2251 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2252 * not considered modifier keys. Consequently, this function ignores
2253 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2254 * </p><p>
2255 * If the specified modifier mask includes directional modifiers, such as
2256 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the
2257 * modifier is pressed on that side.
2258 * If the specified modifier mask includes non-directional modifiers, such as
2259 * {@link #META_SHIFT_ON}, then this method ensures that the modifier
2260 * is pressed on either side.
2261 * If the specified modifier mask includes both directional and non-directional modifiers
2262 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON},
2263 * then this method throws an illegal argument exception.
2264 * </p>
2265 *
2266 * @param modifiers The meta state of the modifier keys to check. May be a combination
2267 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to
2268 * ensure that no modifier keys are pressed.
2269 * @return True if only the specified modifier keys are pressed.
2270 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers
2271 * @see #metaStateHasModifiers
2272 */
2273 public final boolean hasModifiers(int modifiers) {
2274 return metaStateHasModifiers(mMetaState, modifiers);
2275 }
2276
2277 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002278 * <p>Returns the pressed state of the ALT meta key.</p>
2279 *
2280 * @return true if the ALT key is pressed, false otherwise
2281 *
2282 * @see #KEYCODE_ALT_LEFT
2283 * @see #KEYCODE_ALT_RIGHT
2284 * @see #META_ALT_ON
2285 */
2286 public final boolean isAltPressed() {
2287 return (mMetaState & META_ALT_ON) != 0;
2288 }
2289
2290 /**
2291 * <p>Returns the pressed state of the SHIFT meta key.</p>
2292 *
2293 * @return true if the SHIFT key is pressed, false otherwise
2294 *
2295 * @see #KEYCODE_SHIFT_LEFT
2296 * @see #KEYCODE_SHIFT_RIGHT
2297 * @see #META_SHIFT_ON
2298 */
2299 public final boolean isShiftPressed() {
2300 return (mMetaState & META_SHIFT_ON) != 0;
2301 }
2302
2303 /**
2304 * <p>Returns the pressed state of the SYM meta key.</p>
2305 *
2306 * @return true if the SYM key is pressed, false otherwise
2307 *
2308 * @see #KEYCODE_SYM
2309 * @see #META_SYM_ON
2310 */
2311 public final boolean isSymPressed() {
2312 return (mMetaState & META_SYM_ON) != 0;
2313 }
2314
2315 /**
Jeff Brown497a92c2010-09-12 17:55:08 -07002316 * <p>Returns the pressed state of the CTRL meta key.</p>
2317 *
2318 * @return true if the CTRL key is pressed, false otherwise
2319 *
2320 * @see #KEYCODE_CTRL_LEFT
2321 * @see #KEYCODE_CTRL_RIGHT
2322 * @see #META_CTRL_ON
2323 */
2324 public final boolean isCtrlPressed() {
2325 return (mMetaState & META_CTRL_ON) != 0;
2326 }
2327
2328 /**
2329 * <p>Returns the pressed state of the META meta key.</p>
2330 *
2331 * @return true if the META key is pressed, false otherwise
2332 *
2333 * @see #KEYCODE_META_LEFT
2334 * @see #KEYCODE_META_RIGHT
2335 * @see #META_META_ON
2336 */
2337 public final boolean isMetaPressed() {
2338 return (mMetaState & META_META_ON) != 0;
2339 }
2340
2341 /**
2342 * <p>Returns the pressed state of the FUNCTION meta key.</p>
2343 *
2344 * @return true if the FUNCTION key is pressed, false otherwise
2345 *
2346 * @see #KEYCODE_FUNCTION
2347 * @see #META_FUNCTION_ON
2348 */
2349 public final boolean isFunctionPressed() {
2350 return (mMetaState & META_FUNCTION_ON) != 0;
2351 }
2352
2353 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07002354 * <p>Returns the locked state of the CAPS LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002355 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002356 * @return true if the CAPS LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002357 *
2358 * @see #KEYCODE_CAPS_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002359 * @see #META_CAPS_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002360 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002361 public final boolean isCapsLockOn() {
2362 return (mMetaState & META_CAPS_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002363 }
2364
2365 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07002366 * <p>Returns the locked state of the NUM LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002367 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002368 * @return true if the NUM LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002369 *
2370 * @see #KEYCODE_NUM_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002371 * @see #META_NUM_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002372 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002373 public final boolean isNumLockOn() {
2374 return (mMetaState & META_NUM_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002375 }
2376
2377 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07002378 * <p>Returns the locked state of the SCROLL LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002379 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002380 * @return true if the SCROLL LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002381 *
2382 * @see #KEYCODE_SCROLL_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002383 * @see #META_SCROLL_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002384 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002385 public final boolean isScrollLockOn() {
2386 return (mMetaState & META_SCROLL_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002387 }
2388
2389 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390 * Retrieve the action of this key event. May be either
2391 * {@link #ACTION_DOWN}, {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
RoboErik01fe6612014-02-13 14:19:04 -08002392 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393 * @return The event action: ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.
2394 */
2395 public final int getAction() {
2396 return mAction;
2397 }
2398
2399 /**
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07002400 * For {@link #ACTION_UP} events, indicates that the event has been
2401 * canceled as per {@link #FLAG_CANCELED}.
2402 */
2403 public final boolean isCanceled() {
2404 return (mFlags&FLAG_CANCELED) != 0;
2405 }
RoboErik01fe6612014-02-13 14:19:04 -08002406
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07002407 /**
Wale Ogunwalec3672cd2014-11-05 15:17:35 -08002408 * Set {@link #FLAG_CANCELED} flag for the key event.
2409 *
2410 * @hide
2411 */
2412 @Override
2413 public final void cancel() {
2414 mFlags |= FLAG_CANCELED;
2415 }
2416
2417 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002418 * Call this during {@link Callback#onKeyDown} to have the system track
2419 * the key through its final up (possibly including a long press). Note
2420 * that only one key can be tracked at a time -- if another key down
2421 * event is received while a previous one is being tracked, tracking is
2422 * stopped on the previous event.
2423 */
2424 public final void startTracking() {
2425 mFlags |= FLAG_START_TRACKING;
2426 }
RoboErik01fe6612014-02-13 14:19:04 -08002427
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002428 /**
2429 * For {@link #ACTION_UP} events, indicates that the event is still being
2430 * tracked from its initial down event as per
2431 * {@link #FLAG_TRACKING}.
2432 */
2433 public final boolean isTracking() {
2434 return (mFlags&FLAG_TRACKING) != 0;
2435 }
RoboErik01fe6612014-02-13 14:19:04 -08002436
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002437 /**
2438 * For {@link #ACTION_DOWN} events, indicates that the event has been
2439 * canceled as per {@link #FLAG_LONG_PRESS}.
2440 */
2441 public final boolean isLongPress() {
2442 return (mFlags&FLAG_LONG_PRESS) != 0;
2443 }
RoboErik01fe6612014-02-13 14:19:04 -08002444
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002445 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002446 * Retrieve the key code of the key event. This is the physical key that
2447 * was pressed, <em>not</em> the Unicode character.
RoboErik01fe6612014-02-13 14:19:04 -08002448 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002449 * @return The key code of the event.
2450 */
2451 public final int getKeyCode() {
2452 return mKeyCode;
2453 }
2454
2455 /**
2456 * For the special case of a {@link #ACTION_MULTIPLE} event with key
2457 * code of {@link #KEYCODE_UNKNOWN}, this is a raw string of characters
2458 * associated with the event. In all other cases it is null.
RoboErik01fe6612014-02-13 14:19:04 -08002459 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002460 * @return Returns a String of 1 or more characters associated with
2461 * the event.
2462 */
2463 public final String getCharacters() {
2464 return mCharacters;
2465 }
RoboErik01fe6612014-02-13 14:19:04 -08002466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 /**
2468 * Retrieve the hardware key id of this key event. These values are not
2469 * reliable and vary from device to device.
2470 *
2471 * {@more}
2472 * Mostly this is here for debugging purposes.
2473 */
2474 public final int getScanCode() {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002475 return mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002476 }
2477
2478 /**
2479 * Retrieve the repeat count of the event. For both key up and key down
2480 * events, this is the number of times the key has repeated with the first
2481 * down starting at 0 and counting up from there. For multiple key
2482 * events, this is the number of down/up pairs that have occurred.
RoboErik01fe6612014-02-13 14:19:04 -08002483 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002484 * @return The number of times the key has repeated.
2485 */
2486 public final int getRepeatCount() {
2487 return mRepeatCount;
2488 }
2489
2490 /**
2491 * Retrieve the time of the most recent key down event,
2492 * in the {@link android.os.SystemClock#uptimeMillis} time base. If this
2493 * is a down event, this will be the same as {@link #getEventTime()}.
2494 * Note that when chording keys, this value is the down time of the
2495 * most recently pressed key, which may <em>not</em> be the same physical
2496 * key of this event.
RoboErik01fe6612014-02-13 14:19:04 -08002497 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498 * @return Returns the most recent key down time, in the
2499 * {@link android.os.SystemClock#uptimeMillis} time base
2500 */
2501 public final long getDownTime() {
2502 return mDownTime;
2503 }
2504
2505 /**
Jeff Brownb11499d2012-04-20 19:54:22 -07002506 * Retrieve the time this event occurred,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507 * in the {@link android.os.SystemClock#uptimeMillis} time base.
Jeff Brownb11499d2012-04-20 19:54:22 -07002508 *
RoboErik01fe6612014-02-13 14:19:04 -08002509 * @return Returns the time this event occurred,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002510 * in the {@link android.os.SystemClock#uptimeMillis} time base.
2511 */
Jeff Brownb11499d2012-04-20 19:54:22 -07002512 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002513 public final long getEventTime() {
2514 return mEventTime;
2515 }
2516
Jeff Brownb11499d2012-04-20 19:54:22 -07002517 /**
2518 * Retrieve the time this event occurred,
2519 * in the {@link android.os.SystemClock#uptimeMillis} time base but with
2520 * nanosecond (instead of millisecond) precision.
2521 * <p>
2522 * The value is in nanosecond precision but it may not have nanosecond accuracy.
2523 * </p>
2524 *
2525 * @return Returns the time this event occurred,
2526 * in the {@link android.os.SystemClock#uptimeMillis} time base but with
2527 * nanosecond (instead of millisecond) precision.
2528 *
2529 * @hide
2530 */
Jeff Brown4e91a182011-04-07 11:38:09 -07002531 @Override
2532 public final long getEventTimeNano() {
2533 return mEventTime * 1000000L;
2534 }
2535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002536 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002537 * Renamed to {@link #getDeviceId}.
RoboErik01fe6612014-02-13 14:19:04 -08002538 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002539 * @hide
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002540 * @deprecated use {@link #getDeviceId()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002542 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002543 public final int getKeyboardDevice() {
2544 return mDeviceId;
2545 }
2546
2547 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002548 * Gets the {@link KeyCharacterMap} associated with the keyboard device.
2549 *
2550 * @return The associated key character map.
Andrew Sapperstein8ab3dc72011-06-23 18:56:44 -07002551 * @throws {@link KeyCharacterMap.UnavailableException} if the key character map
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002552 * could not be loaded because it was malformed or the default key character map
2553 * is missing from the system.
2554 *
Andrew Sapperstein8ab3dc72011-06-23 18:56:44 -07002555 * @see KeyCharacterMap#load
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002556 */
2557 public final KeyCharacterMap getKeyCharacterMap() {
2558 return KeyCharacterMap.load(mDeviceId);
2559 }
2560
2561 /**
2562 * Gets the primary character for this key.
2563 * In other words, the label that is physically printed on it.
2564 *
2565 * @return The display label character, or 0 if none (eg. for non-printing keys).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002566 */
2567 public char getDisplayLabel() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002568 return getKeyCharacterMap().getDisplayLabel(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 }
RoboErik01fe6612014-02-13 14:19:04 -08002570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002572 * Gets the Unicode character generated by the specified key and meta
2573 * key state combination.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002574 * <p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002575 * Returns the Unicode character that the specified key would produce
2576 * when the specified meta bits (see {@link MetaKeyKeyListener})
2577 * were active.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 * </p><p>
2579 * Returns 0 if the key is not one that is used to type Unicode
2580 * characters.
2581 * </p><p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002582 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the
2583 * key is a "dead key" that should be combined with another to
2584 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} --
2585 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002586 * </p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002587 *
2588 * @return The associated character or combining accent, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002589 */
2590 public int getUnicodeChar() {
2591 return getUnicodeChar(mMetaState);
2592 }
RoboErik01fe6612014-02-13 14:19:04 -08002593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002595 * Gets the Unicode character generated by the specified key and meta
2596 * key state combination.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002597 * <p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002598 * Returns the Unicode character that the specified key would produce
2599 * when the specified meta bits (see {@link MetaKeyKeyListener})
2600 * were active.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002601 * </p><p>
2602 * Returns 0 if the key is not one that is used to type Unicode
2603 * characters.
2604 * </p><p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002605 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the
2606 * key is a "dead key" that should be combined with another to
2607 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} --
2608 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 * </p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002610 *
2611 * @param metaState The meta key modifier state.
2612 * @return The associated character or combining accent, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002613 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002614 public int getUnicodeChar(int metaState) {
2615 return getKeyCharacterMap().get(mKeyCode, metaState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 }
RoboErik01fe6612014-02-13 14:19:04 -08002617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002618 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002619 * Get the character conversion data for a given key code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002620 *
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002621 * @param results A {@link KeyCharacterMap.KeyData} instance that will be
2622 * filled with the results.
2623 * @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 -08002624 *
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002625 * @deprecated instead use {@link #getDisplayLabel()},
2626 * {@link #getNumber()} or {@link #getUnicodeChar(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002628 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002629 public boolean getKeyData(KeyData results) {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002630 return getKeyCharacterMap().getKeyData(mKeyCode, results);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 }
RoboErik01fe6612014-02-13 14:19:04 -08002632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002633 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002634 * Gets the first character in the character array that can be generated
2635 * by the specified key code.
2636 * <p>
2637 * This is a convenience function that returns the same value as
2638 * {@link #getMatch(char[],int) getMatch(chars, 0)}.
2639 * </p>
2640 *
2641 * @param chars The array of matching characters to consider.
2642 * @return The matching associated character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002643 */
2644 public char getMatch(char[] chars) {
2645 return getMatch(chars, 0);
2646 }
RoboErik01fe6612014-02-13 14:19:04 -08002647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002648 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002649 * Gets the first character in the character array that can be generated
2650 * by the specified key code. If there are multiple choices, prefers
2651 * the one that would be generated with the specified meta key modifier state.
2652 *
2653 * @param chars The array of matching characters to consider.
2654 * @param metaState The preferred meta key modifier state.
2655 * @return The matching associated character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002656 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002657 public char getMatch(char[] chars, int metaState) {
2658 return getKeyCharacterMap().getMatch(mKeyCode, chars, metaState);
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 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002662 * Gets the number or symbol associated with the key.
2663 * <p>
2664 * The character value is returned, not the numeric value.
2665 * If the key is not a number, but is a symbol, the symbol is retuned.
2666 * </p><p>
2667 * This method is intended to to support dial pads and other numeric or
2668 * symbolic entry on keyboards where certain keys serve dual function
2669 * as alphabetic and symbolic keys. This method returns the number
2670 * or symbol associated with the key independent of whether the user
2671 * has pressed the required modifier.
2672 * </p><p>
2673 * For example, on one particular keyboard the keys on the top QWERTY row generate
2674 * numbers when ALT is pressed such that ALT-Q maps to '1'. So for that keyboard
2675 * when {@link #getNumber} is called with {@link KeyEvent#KEYCODE_Q} it returns '1'
2676 * so that the user can type numbers without pressing ALT when it makes sense.
2677 * </p>
2678 *
2679 * @return The associated numeric or symbolic character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002680 */
2681 public char getNumber() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002682 return getKeyCharacterMap().getNumber(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683 }
RoboErik01fe6612014-02-13 14:19:04 -08002684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002686 * Returns true if this key produces a glyph.
2687 *
2688 * @return True if the key is a printing key.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002689 */
2690 public boolean isPrintingKey() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002691 return getKeyCharacterMap().isPrintingKey(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002692 }
RoboErik01fe6612014-02-13 14:19:04 -08002693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002695 * @deprecated Use {@link #dispatch(Callback, DispatcherState, Object)} instead.
2696 */
2697 @Deprecated
2698 public final boolean dispatch(Callback receiver) {
2699 return dispatch(receiver, null, null);
2700 }
RoboErik01fe6612014-02-13 14:19:04 -08002701
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002702 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 * Deliver this key event to a {@link Callback} interface. If this is
2704 * an ACTION_MULTIPLE event and it is not handled, then an attempt will
2705 * be made to deliver a single normal event.
RoboErik01fe6612014-02-13 14:19:04 -08002706 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002707 * @param receiver The Callback that will be given the event.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002708 * @param state State information retained across events.
2709 * @param target The target of the dispatch, for use in tracking.
RoboErik01fe6612014-02-13 14:19:04 -08002710 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 * @return The return value from the Callback method that was called.
2712 */
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002713 public final boolean dispatch(Callback receiver, DispatcherState state,
2714 Object target) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715 switch (mAction) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002716 case ACTION_DOWN: {
2717 mFlags &= ~FLAG_START_TRACKING;
Dianne Hackborn8d374262009-09-14 21:21:52 -07002718 if (DEBUG) Log.v(TAG, "Key down to " + target + " in " + state
2719 + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002720 boolean res = receiver.onKeyDown(mKeyCode, this);
2721 if (state != null) {
2722 if (res && mRepeatCount == 0 && (mFlags&FLAG_START_TRACKING) != 0) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002723 if (DEBUG) Log.v(TAG, " Start tracking!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002724 state.startTracking(this, target);
2725 } else if (isLongPress() && state.isTracking(this)) {
2726 try {
2727 if (receiver.onKeyLongPress(mKeyCode, this)) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002728 if (DEBUG) Log.v(TAG, " Clear from long press!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002729 state.performedLongPress(this);
2730 res = true;
2731 }
2732 } catch (AbstractMethodError e) {
2733 }
2734 }
2735 }
2736 return res;
2737 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002738 case ACTION_UP:
Dianne Hackborn8d374262009-09-14 21:21:52 -07002739 if (DEBUG) Log.v(TAG, "Key up to " + target + " in " + state
2740 + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002741 if (state != null) {
2742 state.handleUpEvent(this);
2743 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 return receiver.onKeyUp(mKeyCode, this);
2745 case ACTION_MULTIPLE:
2746 final int count = mRepeatCount;
2747 final int code = mKeyCode;
2748 if (receiver.onKeyMultiple(code, count, this)) {
2749 return true;
2750 }
2751 if (code != KeyEvent.KEYCODE_UNKNOWN) {
2752 mAction = ACTION_DOWN;
2753 mRepeatCount = 0;
2754 boolean handled = receiver.onKeyDown(code, this);
2755 if (handled) {
2756 mAction = ACTION_UP;
2757 receiver.onKeyUp(code, this);
2758 }
2759 mAction = ACTION_MULTIPLE;
2760 mRepeatCount = count;
2761 return handled;
2762 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002763 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 }
2765 return false;
2766 }
2767
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002768 /**
2769 * Use with {@link KeyEvent#dispatch(Callback, DispatcherState, Object)}
2770 * for more advanced key dispatching, such as long presses.
2771 */
2772 public static class DispatcherState {
2773 int mDownKeyCode;
2774 Object mDownTarget;
2775 SparseIntArray mActiveLongPresses = new SparseIntArray();
RoboErik01fe6612014-02-13 14:19:04 -08002776
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002777 /**
2778 * Reset back to initial state.
2779 */
2780 public void reset() {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002781 if (DEBUG) Log.v(TAG, "Reset: " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002782 mDownKeyCode = 0;
2783 mDownTarget = null;
2784 mActiveLongPresses.clear();
2785 }
RoboErik01fe6612014-02-13 14:19:04 -08002786
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002787 /**
2788 * Stop any tracking associated with this target.
2789 */
2790 public void reset(Object target) {
2791 if (mDownTarget == target) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002792 if (DEBUG) Log.v(TAG, "Reset in " + target + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002793 mDownKeyCode = 0;
2794 mDownTarget = null;
2795 }
2796 }
RoboErik01fe6612014-02-13 14:19:04 -08002797
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002798 /**
2799 * Start tracking the key code associated with the given event. This
2800 * can only be called on a key down. It will allow you to see any
2801 * long press associated with the key, and will result in
2802 * {@link KeyEvent#isTracking} return true on the long press and up
2803 * events.
RoboErik01fe6612014-02-13 14:19:04 -08002804 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002805 * <p>This is only needed if you are directly dispatching events, rather
2806 * than handling them in {@link Callback#onKeyDown}.
2807 */
2808 public void startTracking(KeyEvent event, Object target) {
2809 if (event.getAction() != ACTION_DOWN) {
2810 throw new IllegalArgumentException(
2811 "Can only start tracking on a down event");
2812 }
Dianne Hackborn8d374262009-09-14 21:21:52 -07002813 if (DEBUG) Log.v(TAG, "Start trackingt in " + target + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002814 mDownKeyCode = event.getKeyCode();
2815 mDownTarget = target;
2816 }
RoboErik01fe6612014-02-13 14:19:04 -08002817
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002818 /**
2819 * Return true if the key event is for a key code that is currently
2820 * being tracked by the dispatcher.
2821 */
2822 public boolean isTracking(KeyEvent event) {
2823 return mDownKeyCode == event.getKeyCode();
2824 }
RoboErik01fe6612014-02-13 14:19:04 -08002825
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002826 /**
2827 * Keep track of the given event's key code as having performed an
2828 * action with a long press, so no action should occur on the up.
2829 * <p>This is only needed if you are directly dispatching events, rather
2830 * than handling them in {@link Callback#onKeyLongPress}.
2831 */
2832 public void performedLongPress(KeyEvent event) {
2833 mActiveLongPresses.put(event.getKeyCode(), 1);
2834 }
RoboErik01fe6612014-02-13 14:19:04 -08002835
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002836 /**
2837 * Handle key up event to stop tracking. This resets the dispatcher state,
2838 * and updates the key event state based on it.
2839 * <p>This is only needed if you are directly dispatching events, rather
2840 * than handling them in {@link Callback#onKeyUp}.
2841 */
2842 public void handleUpEvent(KeyEvent event) {
2843 final int keyCode = event.getKeyCode();
Dianne Hackborn8d374262009-09-14 21:21:52 -07002844 if (DEBUG) Log.v(TAG, "Handle key up " + event + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002845 int index = mActiveLongPresses.indexOfKey(keyCode);
2846 if (index >= 0) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002847 if (DEBUG) Log.v(TAG, " Index: " + index);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002848 event.mFlags |= FLAG_CANCELED | FLAG_CANCELED_LONG_PRESS;
2849 mActiveLongPresses.removeAt(index);
2850 }
2851 if (mDownKeyCode == keyCode) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002852 if (DEBUG) Log.v(TAG, " Tracking!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002853 event.mFlags |= FLAG_TRACKING;
2854 mDownKeyCode = 0;
2855 mDownTarget = null;
2856 }
2857 }
2858 }
Jeff Brown497a92c2010-09-12 17:55:08 -07002859
2860 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 public String toString() {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002862 StringBuilder msg = new StringBuilder();
2863 msg.append("KeyEvent { action=").append(actionToString(mAction));
2864 msg.append(", keyCode=").append(keyCodeToString(mKeyCode));
2865 msg.append(", scanCode=").append(mScanCode);
2866 if (mCharacters != null) {
2867 msg.append(", characters=\"").append(mCharacters).append("\"");
2868 }
2869 msg.append(", metaState=").append(metaStateToString(mMetaState));
2870 msg.append(", flags=0x").append(Integer.toHexString(mFlags));
2871 msg.append(", repeatCount=").append(mRepeatCount);
2872 msg.append(", eventTime=").append(mEventTime);
2873 msg.append(", downTime=").append(mDownTime);
2874 msg.append(", deviceId=").append(mDeviceId);
2875 msg.append(", source=0x").append(Integer.toHexString(mSource));
2876 msg.append(" }");
2877 return msg.toString();
Jeff Brown497a92c2010-09-12 17:55:08 -07002878 }
2879
2880 /**
2881 * Returns a string that represents the symbolic name of the specified action
Jeff Brown6f2fba42011-02-19 01:08:02 -08002882 * such as "ACTION_DOWN", or an equivalent numeric constant such as "35" if unknown.
Jeff Brown497a92c2010-09-12 17:55:08 -07002883 *
2884 * @param action The action.
2885 * @return The symbolic name of the specified action.
2886 * @hide
2887 */
Mathew Inwoode5ad5982018-08-17 15:07:52 +01002888 @UnsupportedAppUsage
Jeff Brown497a92c2010-09-12 17:55:08 -07002889 public static String actionToString(int action) {
2890 switch (action) {
2891 case ACTION_DOWN:
2892 return "ACTION_DOWN";
2893 case ACTION_UP:
2894 return "ACTION_UP";
2895 case ACTION_MULTIPLE:
2896 return "ACTION_MULTIPLE";
2897 default:
2898 return Integer.toString(action);
2899 }
2900 }
2901
2902 /**
2903 * Returns a string that represents the symbolic name of the specified keycode
Jeff Brown6f2fba42011-02-19 01:08:02 -08002904 * such as "KEYCODE_A", "KEYCODE_DPAD_UP", or an equivalent numeric constant
2905 * such as "1001" if unknown.
Jeff Brown497a92c2010-09-12 17:55:08 -07002906 *
2907 * @param keyCode The key code.
2908 * @return The symbolic name of the specified keycode.
2909 *
2910 * @see KeyCharacterMap#getDisplayLabel
Jeff Brown497a92c2010-09-12 17:55:08 -07002911 */
2912 public static String keyCodeToString(int keyCode) {
Michael Wright337d9d22014-04-22 15:03:48 -07002913 String symbolicName = nativeKeyCodeToString(keyCode);
2914 return symbolicName != null ? LABEL_PREFIX + symbolicName : Integer.toString(keyCode);
Jeff Brown497a92c2010-09-12 17:55:08 -07002915 }
2916
2917 /**
Jeff Brown6f2fba42011-02-19 01:08:02 -08002918 * Gets a keycode by its symbolic name such as "KEYCODE_A" or an equivalent
2919 * numeric constant such as "1001".
Jeff Brown497a92c2010-09-12 17:55:08 -07002920 *
2921 * @param symbolicName The symbolic name of the keycode.
Jeff Brown6f2fba42011-02-19 01:08:02 -08002922 * @return The keycode or {@link #KEYCODE_UNKNOWN} if not found.
Michael Wright072137c2013-04-24 20:41:20 -07002923 * @see #keycodeToString(int)
Jeff Brown497a92c2010-09-12 17:55:08 -07002924 */
2925 public static int keyCodeFromString(String symbolicName) {
Michael Wright337d9d22014-04-22 15:03:48 -07002926 if (symbolicName.startsWith(LABEL_PREFIX)) {
2927 symbolicName = symbolicName.substring(LABEL_PREFIX.length());
Michael Wright973efa02014-05-13 15:38:56 -07002928 int keyCode = nativeKeyCodeFromString(symbolicName);
2929 if (keyCode > 0) {
2930 return keyCode;
2931 }
Jeff Brown497a92c2010-09-12 17:55:08 -07002932 }
Jeff Brown497a92c2010-09-12 17:55:08 -07002933 try {
Jeff Brown6f2fba42011-02-19 01:08:02 -08002934 return Integer.parseInt(symbolicName, 10);
Jeff Brown497a92c2010-09-12 17:55:08 -07002935 } catch (NumberFormatException ex) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08002936 return KEYCODE_UNKNOWN;
Jeff Brown497a92c2010-09-12 17:55:08 -07002937 }
2938 }
2939
2940 /**
2941 * Returns a string that represents the symbolic name of the specified combined meta
2942 * key modifier state flags such as "0", "META_SHIFT_ON",
Jeff Brown6f2fba42011-02-19 01:08:02 -08002943 * "META_ALT_ON|META_SHIFT_ON" or an equivalent numeric constant such as "0x10000000"
2944 * if unknown.
Jeff Brown497a92c2010-09-12 17:55:08 -07002945 *
2946 * @param metaState The meta state.
2947 * @return The symbolic name of the specified combined meta state flags.
2948 * @hide
2949 */
2950 public static String metaStateToString(int metaState) {
2951 if (metaState == 0) {
2952 return "0";
2953 }
2954 StringBuilder result = null;
2955 int i = 0;
2956 while (metaState != 0) {
2957 final boolean isSet = (metaState & 1) != 0;
2958 metaState >>>= 1; // unsigned shift!
2959 if (isSet) {
2960 final String name = META_SYMBOLIC_NAMES[i];
2961 if (result == null) {
2962 if (metaState == 0) {
2963 return name;
2964 }
2965 result = new StringBuilder(name);
2966 } else {
2967 result.append('|');
2968 result.append(name);
2969 }
2970 }
2971 i += 1;
2972 }
2973 return result.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002974 }
2975
2976 public static final Parcelable.Creator<KeyEvent> CREATOR
2977 = new Parcelable.Creator<KeyEvent>() {
Jim Miller07e03842016-06-22 15:18:13 -07002978 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002979 public KeyEvent createFromParcel(Parcel in) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07002980 in.readInt(); // skip token, we already know this is a KeyEvent
2981 return KeyEvent.createFromParcelBody(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002982 }
2983
Jim Miller07e03842016-06-22 15:18:13 -07002984 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002985 public KeyEvent[] newArray(int size) {
2986 return new KeyEvent[size];
2987 }
2988 };
RoboErik01fe6612014-02-13 14:19:04 -08002989
Jeff Brown6ec402b2010-07-28 15:48:59 -07002990 /** @hide */
2991 public static KeyEvent createFromParcelBody(Parcel in) {
2992 return new KeyEvent(in);
2993 }
RoboErik01fe6612014-02-13 14:19:04 -08002994
Jeff Brown6ec402b2010-07-28 15:48:59 -07002995 private KeyEvent(Parcel in) {
Jeff Brown91c69ab2011-02-14 17:03:18 -08002996 mDeviceId = in.readInt();
2997 mSource = in.readInt();
Jeff Brown6ec402b2010-07-28 15:48:59 -07002998 mAction = in.readInt();
2999 mKeyCode = in.readInt();
3000 mRepeatCount = in.readInt();
3001 mMetaState = in.readInt();
3002 mScanCode = in.readInt();
3003 mFlags = in.readInt();
3004 mDownTime = in.readLong();
3005 mEventTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003006 }
3007
Jim Miller07e03842016-06-22 15:18:13 -07003008 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003009 public void writeToParcel(Parcel out, int flags) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07003010 out.writeInt(PARCEL_TOKEN_KEY_EVENT);
Jeff Brown91c69ab2011-02-14 17:03:18 -08003011
3012 out.writeInt(mDeviceId);
3013 out.writeInt(mSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003014 out.writeInt(mAction);
3015 out.writeInt(mKeyCode);
3016 out.writeInt(mRepeatCount);
3017 out.writeInt(mMetaState);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003018 out.writeInt(mScanCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003019 out.writeInt(mFlags);
3020 out.writeLong(mDownTime);
3021 out.writeLong(mEventTime);
3022 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023}