blob: 9cced4e0052f8bcd0bdb85048c34aeb13d87a262 [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
Siarhei Vishniakou91fa08f2018-06-08 22:49:30 +010019import static android.view.Display.INVALID_DISPLAY;
20
Siarhei Vishniakoude1f9042018-05-09 09:54:43 -070021import android.annotation.NonNull;
Siarhei Vishniakou4c96a5e2018-04-24 17:57:44 -070022import android.annotation.TestApi;
Mathew Inwooda570dee2018-08-17 14:56:00 +010023import android.annotation.UnsupportedAppUsage;
Mathew Inwood8c854f82018-09-14 12:35:36 +010024import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.os.Parcel;
26import android.os.Parcelable;
Jeff Brown6b53e8d2010-11-10 16:03:06 -080027import android.text.method.MetaKeyKeyListener;
Dianne Hackborn8d374262009-09-14 21:21:52 -070028import android.util.Log;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070029import android.util.SparseIntArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.view.KeyCharacterMap.KeyData;
31
32/**
Jeff Browndc1ab4b2010-09-14 18:03:38 -070033 * Object used to report key and button events.
34 * <p>
35 * Each key press is described by a sequence of key events. A key press
36 * starts with a key event with {@link #ACTION_DOWN}. If the key is held
37 * sufficiently long that it repeats, then the initial down is followed
38 * additional key events with {@link #ACTION_DOWN} and a non-zero value for
39 * {@link #getRepeatCount()}. The last key event is a {@link #ACTION_UP}
40 * for the key up. If the key press is canceled, the key up event will have the
41 * {@link #FLAG_CANCELED} flag set.
42 * </p><p>
43 * Key events are generally accompanied by a key code ({@link #getKeyCode()}),
44 * scan code ({@link #getScanCode()}) and meta state ({@link #getMetaState()}).
45 * Key code constants are defined in this class. Scan code constants are raw
46 * device-specific codes obtained from the OS and so are not generally meaningful
47 * to applications unless interpreted using the {@link KeyCharacterMap}.
48 * Meta states describe the pressed state of key modifiers
49 * such as {@link #META_SHIFT_ON} or {@link #META_ALT_ON}.
50 * </p><p>
Jeff Brown497a92c2010-09-12 17:55:08 -070051 * Key codes typically correspond one-to-one with individual keys on an input device.
52 * Many keys and key combinations serve quite different functions on different
53 * input devices so care must be taken when interpreting them. Always use the
54 * {@link KeyCharacterMap} associated with the input device when mapping keys
55 * to characters. Be aware that there may be multiple key input devices active
56 * at the same time and each will have its own key character map.
57 * </p><p>
Jean Chalard405bc512012-05-29 19:12:34 +090058 * As soft input methods can use multiple and inventive ways of inputting text,
59 * there is no guarantee that any key press on a soft keyboard will generate a key
60 * event: this is left to the IME's discretion, and in fact sending such events is
61 * discouraged. You should never rely on receiving KeyEvents for any key on a soft
62 * input method. In particular, the default software keyboard will never send any
63 * key event to any application targetting Jelly Bean or later, and will only send
64 * events for some presses of the delete and return keys to applications targetting
65 * Ice Cream Sandwich or earlier. Be aware that other software input methods may
66 * never send key events regardless of the version. Consider using editor actions
67 * like {@link android.view.inputmethod.EditorInfo#IME_ACTION_DONE} if you need
68 * specific interaction with the software keyboard, as it gives more visibility to
69 * the user as to how your application will react to key presses.
70 * </p><p>
Jeff Browndc1ab4b2010-09-14 18:03:38 -070071 * When interacting with an IME, the framework may deliver key events
72 * with the special action {@link #ACTION_MULTIPLE} that either specifies
73 * that single repeated key code or a sequence of characters to insert.
74 * </p><p>
Jeff Brownb6997262010-10-08 22:31:17 -070075 * In general, the framework cannot guarantee that the key events it delivers
76 * to a view always constitute complete key sequences since some events may be dropped
77 * or modified by containing views before they are delivered. The view implementation
78 * should be prepared to handle {@link #FLAG_CANCELED} and should tolerate anomalous
79 * situations such as receiving a new {@link #ACTION_DOWN} without first having
80 * received an {@link #ACTION_UP} for the prior key press.
Jeff Browndc1ab4b2010-09-14 18:03:38 -070081 * </p><p>
82 * Refer to {@link InputDevice} for more information about how different kinds of
83 * input devices and sources represent keys and buttons.
84 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 */
Jeff Brownc5ed5912010-07-14 18:48:53 -070086public class KeyEvent extends InputEvent implements Parcelable {
Jeff Browndc1ab4b2010-09-14 18:03:38 -070087 /** Key code constant: Unknown key code. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 public static final int KEYCODE_UNKNOWN = 0;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070089 /** Key code constant: Soft Left key.
90 * Usually situated below the display on phones and used as a multi-function
91 * feature key for selecting a software defined function shown on the bottom left
92 * of the display. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 public static final int KEYCODE_SOFT_LEFT = 1;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070094 /** Key code constant: Soft Right key.
95 * Usually situated below the display on phones and used as a multi-function
96 * feature key for selecting a software defined function shown on the bottom right
97 * of the display. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 public static final int KEYCODE_SOFT_RIGHT = 2;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070099 /** Key code constant: Home key.
100 * This key is handled by the framework and is never delivered to applications. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 public static final int KEYCODE_HOME = 3;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700102 /** Key code constant: Back key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 public static final int KEYCODE_BACK = 4;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700104 /** Key code constant: Call key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 public static final int KEYCODE_CALL = 5;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700106 /** Key code constant: End Call key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 public static final int KEYCODE_ENDCALL = 6;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700108 /** Key code constant: '0' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 public static final int KEYCODE_0 = 7;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700110 /** Key code constant: '1' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 public static final int KEYCODE_1 = 8;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700112 /** Key code constant: '2' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 public static final int KEYCODE_2 = 9;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700114 /** Key code constant: '3' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 public static final int KEYCODE_3 = 10;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700116 /** Key code constant: '4' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 public static final int KEYCODE_4 = 11;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700118 /** Key code constant: '5' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 public static final int KEYCODE_5 = 12;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700120 /** Key code constant: '6' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 public static final int KEYCODE_6 = 13;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700122 /** Key code constant: '7' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 public static final int KEYCODE_7 = 14;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700124 /** Key code constant: '8' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 public static final int KEYCODE_8 = 15;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700126 /** Key code constant: '9' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 public static final int KEYCODE_9 = 16;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700128 /** Key code constant: '*' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 public static final int KEYCODE_STAR = 17;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700130 /** Key code constant: '#' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 public static final int KEYCODE_POUND = 18;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700132 /** Key code constant: Directional Pad Up key.
133 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 public static final int KEYCODE_DPAD_UP = 19;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700135 /** Key code constant: Directional Pad Down key.
136 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 public static final int KEYCODE_DPAD_DOWN = 20;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700138 /** Key code constant: Directional Pad Left key.
139 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 public static final int KEYCODE_DPAD_LEFT = 21;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700141 /** Key code constant: Directional Pad Right key.
142 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 public static final int KEYCODE_DPAD_RIGHT = 22;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700144 /** Key code constant: Directional Pad Center key.
145 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 public static final int KEYCODE_DPAD_CENTER = 23;
Jeff Brownb0418da2010-11-01 15:24:01 -0700147 /** Key code constant: Volume Up key.
148 * Adjusts the speaker volume up. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 public static final int KEYCODE_VOLUME_UP = 24;
Jeff Brownb0418da2010-11-01 15:24:01 -0700150 /** Key code constant: Volume Down key.
151 * Adjusts the speaker volume down. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 public static final int KEYCODE_VOLUME_DOWN = 25;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700153 /** Key code constant: Power key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 public static final int KEYCODE_POWER = 26;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700155 /** Key code constant: Camera key.
156 * Used to launch a camera application or take pictures. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 public static final int KEYCODE_CAMERA = 27;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700158 /** Key code constant: Clear key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 public static final int KEYCODE_CLEAR = 28;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700160 /** Key code constant: 'A' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 public static final int KEYCODE_A = 29;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700162 /** Key code constant: 'B' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 public static final int KEYCODE_B = 30;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700164 /** Key code constant: 'C' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 public static final int KEYCODE_C = 31;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700166 /** Key code constant: 'D' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 public static final int KEYCODE_D = 32;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700168 /** Key code constant: 'E' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 public static final int KEYCODE_E = 33;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700170 /** Key code constant: 'F' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 public static final int KEYCODE_F = 34;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700172 /** Key code constant: 'G' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 public static final int KEYCODE_G = 35;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700174 /** Key code constant: 'H' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 public static final int KEYCODE_H = 36;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700176 /** Key code constant: 'I' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 public static final int KEYCODE_I = 37;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700178 /** Key code constant: 'J' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 public static final int KEYCODE_J = 38;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700180 /** Key code constant: 'K' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 public static final int KEYCODE_K = 39;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700182 /** Key code constant: 'L' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 public static final int KEYCODE_L = 40;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700184 /** Key code constant: 'M' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 public static final int KEYCODE_M = 41;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700186 /** Key code constant: 'N' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 public static final int KEYCODE_N = 42;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700188 /** Key code constant: 'O' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 public static final int KEYCODE_O = 43;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700190 /** Key code constant: 'P' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 public static final int KEYCODE_P = 44;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700192 /** Key code constant: 'Q' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 public static final int KEYCODE_Q = 45;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700194 /** Key code constant: 'R' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 public static final int KEYCODE_R = 46;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700196 /** Key code constant: 'S' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 public static final int KEYCODE_S = 47;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700198 /** Key code constant: 'T' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 public static final int KEYCODE_T = 48;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700200 /** Key code constant: 'U' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 public static final int KEYCODE_U = 49;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700202 /** Key code constant: 'V' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 public static final int KEYCODE_V = 50;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700204 /** Key code constant: 'W' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 public static final int KEYCODE_W = 51;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700206 /** Key code constant: 'X' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 public static final int KEYCODE_X = 52;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700208 /** Key code constant: 'Y' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 public static final int KEYCODE_Y = 53;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700210 /** Key code constant: 'Z' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 public static final int KEYCODE_Z = 54;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700212 /** Key code constant: ',' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 public static final int KEYCODE_COMMA = 55;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700214 /** Key code constant: '.' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 public static final int KEYCODE_PERIOD = 56;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700216 /** Key code constant: Left Alt modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 public static final int KEYCODE_ALT_LEFT = 57;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700218 /** Key code constant: Right Alt modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 public static final int KEYCODE_ALT_RIGHT = 58;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700220 /** Key code constant: Left Shift modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 public static final int KEYCODE_SHIFT_LEFT = 59;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700222 /** Key code constant: Right Shift modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 public static final int KEYCODE_SHIFT_RIGHT = 60;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700224 /** Key code constant: Tab key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 public static final int KEYCODE_TAB = 61;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700226 /** Key code constant: Space key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 public static final int KEYCODE_SPACE = 62;
Jeff Brown224d4a12010-10-07 20:28:53 -0700228 /** Key code constant: Symbol modifier key.
229 * Used to enter alternate symbols. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 public static final int KEYCODE_SYM = 63;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700231 /** Key code constant: Explorer special function key.
232 * Used to launch a browser application. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 public static final int KEYCODE_EXPLORER = 64;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700234 /** Key code constant: Envelope special function key.
235 * Used to launch a mail application. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 public static final int KEYCODE_ENVELOPE = 65;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700237 /** Key code constant: Enter key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 public static final int KEYCODE_ENTER = 66;
Jeff Brown224d4a12010-10-07 20:28:53 -0700239 /** Key code constant: Backspace key.
Jeff Brown497a92c2010-09-12 17:55:08 -0700240 * Deletes characters before the insertion point, unlike {@link #KEYCODE_FORWARD_DEL}. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 public static final int KEYCODE_DEL = 67;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700242 /** Key code constant: '`' (backtick) key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 public static final int KEYCODE_GRAVE = 68;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700244 /** Key code constant: '-'. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 public static final int KEYCODE_MINUS = 69;
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_EQUALS = 70;
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_LEFT_BRACKET = 71;
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_RIGHT_BRACKET = 72;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700252 /** Key code constant: '\' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 public static final int KEYCODE_BACKSLASH = 73;
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_SEMICOLON = 74;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700256 /** Key code constant: ''' (apostrophe) key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 public static final int KEYCODE_APOSTROPHE = 75;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700258 /** Key code constant: '/' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 public static final int KEYCODE_SLASH = 76;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700260 /** Key code constant: '@' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 public static final int KEYCODE_AT = 77;
Jeff Brown224d4a12010-10-07 20:28:53 -0700262 /** Key code constant: Number modifier key.
263 * Used to enter numeric symbols.
264 * This key is not Num Lock; it is more like {@link #KEYCODE_ALT_LEFT} and is
265 * interpreted as an ALT key by {@link android.text.method.MetaKeyKeyListener}. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 public static final int KEYCODE_NUM = 78;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700267 /** Key code constant: Headset Hook key.
268 * Used to hang up calls and stop media. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 public static final int KEYCODE_HEADSETHOOK = 79;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700270 /** Key code constant: Camera Focus key.
271 * Used to focus the camera. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 public static final int KEYCODE_FOCUS = 80; // *Camera* focus
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700273 /** Key code constant: '+' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 public static final int KEYCODE_PLUS = 81;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700275 /** Key code constant: Menu key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 public static final int KEYCODE_MENU = 82;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700277 /** Key code constant: Notification key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 public static final int KEYCODE_NOTIFICATION = 83;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700279 /** Key code constant: Search key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 public static final int KEYCODE_SEARCH = 84;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700281 /** Key code constant: Play/Pause media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700282 public static final int KEYCODE_MEDIA_PLAY_PAUSE= 85;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700283 /** Key code constant: Stop media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700284 public static final int KEYCODE_MEDIA_STOP = 86;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700285 /** Key code constant: Play Next media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700286 public static final int KEYCODE_MEDIA_NEXT = 87;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700287 /** Key code constant: Play Previous media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700288 public static final int KEYCODE_MEDIA_PREVIOUS = 88;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700289 /** Key code constant: Rewind media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700290 public static final int KEYCODE_MEDIA_REWIND = 89;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700291 /** Key code constant: Fast Forward media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700292 public static final int KEYCODE_MEDIA_FAST_FORWARD = 90;
Jeff Brownb0418da2010-11-01 15:24:01 -0700293 /** Key code constant: Mute key.
294 * Mutes the microphone, unlike {@link #KEYCODE_VOLUME_MUTE}. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 public static final int KEYCODE_MUTE = 91;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700296 /** Key code constant: Page Up key. */
Chih-Wei Huang4fedd802009-05-27 15:52:50 +0800297 public static final int KEYCODE_PAGE_UP = 92;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700298 /** Key code constant: Page Down key. */
Chih-Wei Huang4fedd802009-05-27 15:52:50 +0800299 public static final int KEYCODE_PAGE_DOWN = 93;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700300 /** Key code constant: Picture Symbols modifier key.
301 * Used to switch symbol sets (Emoji, Kao-moji). */
mogimob032bc02009-10-03 03:13:56 +0900302 public static final int KEYCODE_PICTSYMBOLS = 94; // switch symbol-sets (Emoji,Kao-moji)
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700303 /** Key code constant: Switch Charset modifier key.
304 * Used to switch character sets (Kanji, Katakana). */
mogimob032bc02009-10-03 03:13:56 +0900305 public static final int KEYCODE_SWITCH_CHARSET = 95; // switch char-sets (Kanji,Katakana)
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700306 /** Key code constant: A Button key.
307 * On a game controller, the A button should be either the button labeled A
Michael Wright6b57bde2013-01-28 20:35:58 -0800308 * or the first button on the bottom row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700309 public static final int KEYCODE_BUTTON_A = 96;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700310 /** Key code constant: B Button key.
311 * On a game controller, the B button should be either the button labeled B
Michael Wright6b57bde2013-01-28 20:35:58 -0800312 * or the second button on the bottom row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700313 public static final int KEYCODE_BUTTON_B = 97;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700314 /** Key code constant: C Button key.
315 * On a game controller, the C button should be either the button labeled C
Michael Wright6b57bde2013-01-28 20:35:58 -0800316 * or the third button on the bottom row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700317 public static final int KEYCODE_BUTTON_C = 98;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700318 /** Key code constant: X Button key.
319 * On a game controller, the X button should be either the button labeled X
Michael Wright6b57bde2013-01-28 20:35:58 -0800320 * or the first button on the upper row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700321 public static final int KEYCODE_BUTTON_X = 99;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700322 /** Key code constant: Y Button key.
323 * On a game controller, the Y button should be either the button labeled Y
Michael Wright6b57bde2013-01-28 20:35:58 -0800324 * or the second button on the upper row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700325 public static final int KEYCODE_BUTTON_Y = 100;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700326 /** Key code constant: Z Button key.
327 * On a game controller, the Z button should be either the button labeled Z
Michael Wright6b57bde2013-01-28 20:35:58 -0800328 * or the third button on the upper row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700329 public static final int KEYCODE_BUTTON_Z = 101;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700330 /** Key code constant: L1 Button key.
331 * On a game controller, the L1 button should be either the button labeled L1 (or L)
332 * or the top left trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700333 public static final int KEYCODE_BUTTON_L1 = 102;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700334 /** Key code constant: R1 Button key.
335 * On a game controller, the R1 button should be either the button labeled R1 (or R)
336 * or the top right trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700337 public static final int KEYCODE_BUTTON_R1 = 103;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700338 /** Key code constant: L2 Button key.
339 * On a game controller, the L2 button should be either the button labeled L2
340 * or the bottom left trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700341 public static final int KEYCODE_BUTTON_L2 = 104;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700342 /** Key code constant: R2 Button key.
343 * On a game controller, the R2 button should be either the button labeled R2
344 * or the bottom right trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700345 public static final int KEYCODE_BUTTON_R2 = 105;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700346 /** Key code constant: Left Thumb Button key.
347 * On a game controller, the left thumb button indicates that the left (or only)
348 * joystick is pressed. */
Jeff Brownfd035822010-06-30 16:10:35 -0700349 public static final int KEYCODE_BUTTON_THUMBL = 106;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700350 /** Key code constant: Right Thumb Button key.
351 * On a game controller, the right thumb button indicates that the right
352 * joystick is pressed. */
Jeff Brownfd035822010-06-30 16:10:35 -0700353 public static final int KEYCODE_BUTTON_THUMBR = 107;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700354 /** Key code constant: Start Button key.
355 * On a game controller, the button labeled Start. */
Jeff Brownfd035822010-06-30 16:10:35 -0700356 public static final int KEYCODE_BUTTON_START = 108;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700357 /** Key code constant: Select Button key.
358 * On a game controller, the button labeled Select. */
Jeff Brownfd035822010-06-30 16:10:35 -0700359 public static final int KEYCODE_BUTTON_SELECT = 109;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700360 /** Key code constant: Mode Button key.
361 * On a game controller, the button labeled Mode. */
Jeff Brownfd035822010-06-30 16:10:35 -0700362 public static final int KEYCODE_BUTTON_MODE = 110;
Jeff Brown497a92c2010-09-12 17:55:08 -0700363 /** Key code constant: Escape key. */
364 public static final int KEYCODE_ESCAPE = 111;
365 /** Key code constant: Forward Delete key.
366 * Deletes characters ahead of the insertion point, unlike {@link #KEYCODE_DEL}. */
367 public static final int KEYCODE_FORWARD_DEL = 112;
368 /** Key code constant: Left Control modifier key. */
369 public static final int KEYCODE_CTRL_LEFT = 113;
370 /** Key code constant: Right Control modifier key. */
371 public static final int KEYCODE_CTRL_RIGHT = 114;
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800372 /** Key code constant: Caps Lock key. */
Jeff Brown497a92c2010-09-12 17:55:08 -0700373 public static final int KEYCODE_CAPS_LOCK = 115;
374 /** Key code constant: Scroll Lock key. */
375 public static final int KEYCODE_SCROLL_LOCK = 116;
376 /** Key code constant: Left Meta modifier key. */
377 public static final int KEYCODE_META_LEFT = 117;
378 /** Key code constant: Right Meta modifier key. */
379 public static final int KEYCODE_META_RIGHT = 118;
380 /** Key code constant: Function modifier key. */
381 public static final int KEYCODE_FUNCTION = 119;
382 /** Key code constant: System Request / Print Screen key. */
383 public static final int KEYCODE_SYSRQ = 120;
384 /** Key code constant: Break / Pause key. */
385 public static final int KEYCODE_BREAK = 121;
386 /** Key code constant: Home Movement key.
387 * Used for scrolling or moving the cursor around to the start of a line
388 * or to the top of a list. */
389 public static final int KEYCODE_MOVE_HOME = 122;
390 /** Key code constant: End Movement key.
391 * Used for scrolling or moving the cursor around to the end of a line
392 * or to the bottom of a list. */
393 public static final int KEYCODE_MOVE_END = 123;
394 /** Key code constant: Insert key.
395 * Toggles insert / overwrite edit mode. */
396 public static final int KEYCODE_INSERT = 124;
397 /** Key code constant: Forward key.
398 * Navigates forward in the history stack. Complement of {@link #KEYCODE_BACK}. */
399 public static final int KEYCODE_FORWARD = 125;
400 /** Key code constant: Play media key. */
401 public static final int KEYCODE_MEDIA_PLAY = 126;
402 /** Key code constant: Pause media key. */
403 public static final int KEYCODE_MEDIA_PAUSE = 127;
404 /** Key code constant: Close media key.
405 * May be used to close a CD tray, for example. */
406 public static final int KEYCODE_MEDIA_CLOSE = 128;
407 /** Key code constant: Eject media key.
408 * May be used to eject a CD tray, for example. */
409 public static final int KEYCODE_MEDIA_EJECT = 129;
410 /** Key code constant: Record media key. */
411 public static final int KEYCODE_MEDIA_RECORD = 130;
412 /** Key code constant: F1 key. */
413 public static final int KEYCODE_F1 = 131;
414 /** Key code constant: F2 key. */
415 public static final int KEYCODE_F2 = 132;
416 /** Key code constant: F3 key. */
417 public static final int KEYCODE_F3 = 133;
418 /** Key code constant: F4 key. */
419 public static final int KEYCODE_F4 = 134;
420 /** Key code constant: F5 key. */
421 public static final int KEYCODE_F5 = 135;
422 /** Key code constant: F6 key. */
423 public static final int KEYCODE_F6 = 136;
424 /** Key code constant: F7 key. */
425 public static final int KEYCODE_F7 = 137;
426 /** Key code constant: F8 key. */
427 public static final int KEYCODE_F8 = 138;
428 /** Key code constant: F9 key. */
429 public static final int KEYCODE_F9 = 139;
430 /** Key code constant: F10 key. */
431 public static final int KEYCODE_F10 = 140;
432 /** Key code constant: F11 key. */
433 public static final int KEYCODE_F11 = 141;
434 /** Key code constant: F12 key. */
435 public static final int KEYCODE_F12 = 142;
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800436 /** Key code constant: Num Lock key.
Jeff Brown497a92c2010-09-12 17:55:08 -0700437 * This is the Num Lock key; it is different from {@link #KEYCODE_NUM}.
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800438 * This key alters the behavior of other keys on the numeric keypad. */
Jeff Brown497a92c2010-09-12 17:55:08 -0700439 public static final int KEYCODE_NUM_LOCK = 143;
440 /** Key code constant: Numeric keypad '0' key. */
441 public static final int KEYCODE_NUMPAD_0 = 144;
442 /** Key code constant: Numeric keypad '1' key. */
443 public static final int KEYCODE_NUMPAD_1 = 145;
444 /** Key code constant: Numeric keypad '2' key. */
445 public static final int KEYCODE_NUMPAD_2 = 146;
446 /** Key code constant: Numeric keypad '3' key. */
447 public static final int KEYCODE_NUMPAD_3 = 147;
448 /** Key code constant: Numeric keypad '4' key. */
449 public static final int KEYCODE_NUMPAD_4 = 148;
450 /** Key code constant: Numeric keypad '5' key. */
451 public static final int KEYCODE_NUMPAD_5 = 149;
452 /** Key code constant: Numeric keypad '6' key. */
453 public static final int KEYCODE_NUMPAD_6 = 150;
454 /** Key code constant: Numeric keypad '7' key. */
455 public static final int KEYCODE_NUMPAD_7 = 151;
456 /** Key code constant: Numeric keypad '8' key. */
457 public static final int KEYCODE_NUMPAD_8 = 152;
458 /** Key code constant: Numeric keypad '9' key. */
459 public static final int KEYCODE_NUMPAD_9 = 153;
460 /** Key code constant: Numeric keypad '/' key (for division). */
461 public static final int KEYCODE_NUMPAD_DIVIDE = 154;
462 /** Key code constant: Numeric keypad '*' key (for multiplication). */
463 public static final int KEYCODE_NUMPAD_MULTIPLY = 155;
464 /** Key code constant: Numeric keypad '-' key (for subtraction). */
465 public static final int KEYCODE_NUMPAD_SUBTRACT = 156;
466 /** Key code constant: Numeric keypad '+' key (for addition). */
467 public static final int KEYCODE_NUMPAD_ADD = 157;
468 /** Key code constant: Numeric keypad '.' key (for decimals or digit grouping). */
469 public static final int KEYCODE_NUMPAD_DOT = 158;
470 /** Key code constant: Numeric keypad ',' key (for decimals or digit grouping). */
471 public static final int KEYCODE_NUMPAD_COMMA = 159;
472 /** Key code constant: Numeric keypad Enter key. */
473 public static final int KEYCODE_NUMPAD_ENTER = 160;
474 /** Key code constant: Numeric keypad '=' key. */
475 public static final int KEYCODE_NUMPAD_EQUALS = 161;
476 /** Key code constant: Numeric keypad '(' key. */
477 public static final int KEYCODE_NUMPAD_LEFT_PAREN = 162;
478 /** Key code constant: Numeric keypad ')' key. */
479 public static final int KEYCODE_NUMPAD_RIGHT_PAREN = 163;
Jeff Brownb0418da2010-11-01 15:24:01 -0700480 /** Key code constant: Volume Mute key.
481 * Mutes the speaker, unlike {@link #KEYCODE_MUTE}.
482 * This key should normally be implemented as a toggle such that the first press
483 * mutes the speaker and the second press restores the original volume. */
484 public static final int KEYCODE_VOLUME_MUTE = 164;
Jason Bayer3adf4902010-11-09 14:54:55 -0800485 /** Key code constant: Info key.
486 * Common on TV remotes to show additional information related to what is
487 * currently being viewed. */
488 public static final int KEYCODE_INFO = 165;
489 /** Key code constant: Channel up key.
490 * On TV remotes, increments the television channel. */
491 public static final int KEYCODE_CHANNEL_UP = 166;
492 /** Key code constant: Channel down key.
493 * On TV remotes, decrements the television channel. */
494 public static final int KEYCODE_CHANNEL_DOWN = 167;
495 /** Key code constant: Zoom in key. */
496 public static final int KEYCODE_ZOOM_IN = 168;
497 /** Key code constant: Zoom out key. */
498 public static final int KEYCODE_ZOOM_OUT = 169;
499 /** Key code constant: TV key.
500 * On TV remotes, switches to viewing live TV. */
501 public static final int KEYCODE_TV = 170;
502 /** Key code constant: Window key.
Julius D'souza03d4a652017-04-03 10:16:51 -0700503 * On TV remotes, toggles picture-in-picture mode or other windowing functions.
504 * On Android Wear devices, triggers a display offset. */
Jason Bayer3adf4902010-11-09 14:54:55 -0800505 public static final int KEYCODE_WINDOW = 171;
506 /** Key code constant: Guide key.
507 * On TV remotes, shows a programming guide. */
508 public static final int KEYCODE_GUIDE = 172;
509 /** Key code constant: DVR key.
510 * On some TV remotes, switches to a DVR mode for recorded shows. */
511 public static final int KEYCODE_DVR = 173;
512 /** Key code constant: Bookmark key.
513 * On some TV remotes, bookmarks content or web pages. */
514 public static final int KEYCODE_BOOKMARK = 174;
515 /** Key code constant: Toggle captions key.
516 * Switches the mode for closed-captioning text, for example during television shows. */
517 public static final int KEYCODE_CAPTIONS = 175;
518 /** Key code constant: Settings key.
519 * Starts the system settings activity. */
520 public static final int KEYCODE_SETTINGS = 176;
521 /** Key code constant: TV power key.
522 * On TV remotes, toggles the power on a television screen. */
523 public static final int KEYCODE_TV_POWER = 177;
524 /** Key code constant: TV input key.
525 * On TV remotes, switches the input on a television screen. */
526 public static final int KEYCODE_TV_INPUT = 178;
527 /** Key code constant: Set-top-box power key.
528 * On TV remotes, toggles the power on an external Set-top-box. */
529 public static final int KEYCODE_STB_POWER = 179;
530 /** Key code constant: Set-top-box input key.
531 * On TV remotes, switches the input mode on an external Set-top-box. */
532 public static final int KEYCODE_STB_INPUT = 180;
533 /** Key code constant: A/V Receiver power key.
534 * On TV remotes, toggles the power on an external A/V Receiver. */
535 public static final int KEYCODE_AVR_POWER = 181;
536 /** Key code constant: A/V Receiver input key.
537 * On TV remotes, switches the input mode on an external A/V Receiver. */
538 public static final int KEYCODE_AVR_INPUT = 182;
539 /** Key code constant: Red "programmable" key.
540 * On TV remotes, acts as a contextual/programmable key. */
541 public static final int KEYCODE_PROG_RED = 183;
542 /** Key code constant: Green "programmable" key.
543 * On TV remotes, actsas a contextual/programmable key. */
544 public static final int KEYCODE_PROG_GREEN = 184;
545 /** Key code constant: Yellow "programmable" key.
546 * On TV remotes, acts as a contextual/programmable key. */
547 public static final int KEYCODE_PROG_YELLOW = 185;
548 /** Key code constant: Blue "programmable" key.
549 * On TV remotes, acts as a contextual/programmable key. */
550 public static final int KEYCODE_PROG_BLUE = 186;
Jeff Brown49ed71d2010-12-06 17:13:33 -0800551 /** Key code constant: App switch key.
552 * Should bring up the application switcher dialog. */
553 public static final int KEYCODE_APP_SWITCH = 187;
Jeff Browncb1404e2011-01-15 18:14:15 -0800554 /** Key code constant: Generic Game Pad Button #1.*/
555 public static final int KEYCODE_BUTTON_1 = 188;
556 /** Key code constant: Generic Game Pad Button #2.*/
557 public static final int KEYCODE_BUTTON_2 = 189;
558 /** Key code constant: Generic Game Pad Button #3.*/
559 public static final int KEYCODE_BUTTON_3 = 190;
560 /** Key code constant: Generic Game Pad Button #4.*/
561 public static final int KEYCODE_BUTTON_4 = 191;
562 /** Key code constant: Generic Game Pad Button #5.*/
563 public static final int KEYCODE_BUTTON_5 = 192;
564 /** Key code constant: Generic Game Pad Button #6.*/
565 public static final int KEYCODE_BUTTON_6 = 193;
566 /** Key code constant: Generic Game Pad Button #7.*/
567 public static final int KEYCODE_BUTTON_7 = 194;
568 /** Key code constant: Generic Game Pad Button #8.*/
569 public static final int KEYCODE_BUTTON_8 = 195;
570 /** Key code constant: Generic Game Pad Button #9.*/
571 public static final int KEYCODE_BUTTON_9 = 196;
572 /** Key code constant: Generic Game Pad Button #10.*/
573 public static final int KEYCODE_BUTTON_10 = 197;
574 /** Key code constant: Generic Game Pad Button #11.*/
575 public static final int KEYCODE_BUTTON_11 = 198;
576 /** Key code constant: Generic Game Pad Button #12.*/
577 public static final int KEYCODE_BUTTON_12 = 199;
578 /** Key code constant: Generic Game Pad Button #13.*/
579 public static final int KEYCODE_BUTTON_13 = 200;
580 /** Key code constant: Generic Game Pad Button #14.*/
581 public static final int KEYCODE_BUTTON_14 = 201;
582 /** Key code constant: Generic Game Pad Button #15.*/
583 public static final int KEYCODE_BUTTON_15 = 202;
584 /** Key code constant: Generic Game Pad Button #16.*/
585 public static final int KEYCODE_BUTTON_16 = 203;
Jeff Brown9812aed2011-03-07 17:09:51 -0800586 /** Key code constant: Language Switch key.
587 * Toggles the current input language such as switching between English and Japanese on
588 * a QWERTY keyboard. On some devices, the same function may be performed by
589 * pressing Shift+Spacebar. */
590 public static final int KEYCODE_LANGUAGE_SWITCH = 204;
591 /** Key code constant: Manner Mode key.
592 * Toggles silent or vibrate mode on and off to make the device behave more politely
593 * in certain settings such as on a crowded train. On some devices, the key may only
594 * operate when long-pressed. */
595 public static final int KEYCODE_MANNER_MODE = 205;
596 /** Key code constant: 3D Mode key.
597 * Toggles the display between 2D and 3D mode. */
598 public static final int KEYCODE_3D_MODE = 206;
Jeff Brown6651a632011-11-28 12:59:11 -0800599 /** Key code constant: Contacts special function key.
600 * Used to launch an address book application. */
601 public static final int KEYCODE_CONTACTS = 207;
602 /** Key code constant: Calendar special function key.
603 * Used to launch a calendar application. */
604 public static final int KEYCODE_CALENDAR = 208;
605 /** Key code constant: Music special function key.
606 * Used to launch a music player application. */
607 public static final int KEYCODE_MUSIC = 209;
608 /** Key code constant: Calculator special function key.
609 * Used to launch a calculator application. */
610 public static final int KEYCODE_CALCULATOR = 210;
Yang Chuang7511f9c2012-02-10 15:18:26 +0800611 /** Key code constant: Japanese full-width / half-width key. */
612 public static final int KEYCODE_ZENKAKU_HANKAKU = 211;
613 /** Key code constant: Japanese alphanumeric key. */
614 public static final int KEYCODE_EISU = 212;
615 /** Key code constant: Japanese non-conversion key. */
616 public static final int KEYCODE_MUHENKAN = 213;
617 /** Key code constant: Japanese conversion key. */
618 public static final int KEYCODE_HENKAN = 214;
619 /** Key code constant: Japanese katakana / hiragana key. */
620 public static final int KEYCODE_KATAKANA_HIRAGANA = 215;
621 /** Key code constant: Japanese Yen key. */
622 public static final int KEYCODE_YEN = 216;
623 /** Key code constant: Japanese Ro key. */
624 public static final int KEYCODE_RO = 217;
625 /** Key code constant: Japanese kana key. */
626 public static final int KEYCODE_KANA = 218;
Jeff Brownde7a8ea2012-06-13 18:28:57 -0700627 /** Key code constant: Assist key.
628 * Launches the global assist activity. Not delivered to applications. */
629 public static final int KEYCODE_ASSIST = 219;
Michael Wright1df477a2013-01-31 16:19:18 -0800630 /** Key code constant: Brightness Down key.
631 * Adjusts the screen brightness down. */
632 public static final int KEYCODE_BRIGHTNESS_DOWN = 220;
633 /** Key code constant: Brightness Up key.
634 * Adjusts the screen brightness up. */
635 public static final int KEYCODE_BRIGHTNESS_UP = 221;
Jeff Brown6212a492014-03-07 13:58:47 -0800636 /** Key code constant: Audio Track key.
Jaekyun Seokbfdad8e2013-07-08 13:53:21 +0900637 * Switches the audio tracks. */
638 public static final int KEYCODE_MEDIA_AUDIO_TRACK = 222;
Jeff Brown6212a492014-03-07 13:58:47 -0800639 /** Key code constant: Sleep key.
640 * Puts the device to sleep. Behaves somewhat like {@link #KEYCODE_POWER} but it
641 * has no effect if the device is already asleep. */
642 public static final int KEYCODE_SLEEP = 223;
643 /** Key code constant: Wakeup key.
644 * Wakes up the device. Behaves somewhat like {@link #KEYCODE_POWER} but it
645 * has no effect if the device is already awake. */
646 public static final int KEYCODE_WAKEUP = 224;
Tim Kilbourn87cd0dc2014-04-14 15:37:51 -0700647 /** Key code constant: Pairing key.
648 * Initiates peripheral pairing mode. Useful for pairing remote control
649 * devices or game controllers, especially if no other input mode is
650 * available. */
651 public static final int KEYCODE_PAIRING = 225;
Jinsuk Kim96658f72014-05-14 15:33:43 +0900652 /** Key code constant: Media Top Menu key.
653 * Goes to the top of media menu. */
654 public static final int KEYCODE_MEDIA_TOP_MENU = 226;
655 /** Key code constant: '11' key. */
656 public static final int KEYCODE_11 = 227;
657 /** Key code constant: '12' key. */
658 public static final int KEYCODE_12 = 228;
659 /** Key code constant: Last Channel key.
660 * Goes to the last viewed channel. */
661 public static final int KEYCODE_LAST_CHANNEL = 229;
662 /** Key code constant: TV data service key.
663 * Displays data services like weather, sports. */
664 public static final int KEYCODE_TV_DATA_SERVICE = 230;
Michael Wrightdc63f7b2014-08-21 19:05:21 -0700665 /** Key code constant: Voice Assist key.
666 * Launches the global voice assist activity. Not delivered to applications. */
667 public static final int KEYCODE_VOICE_ASSIST = 231;
ASAZU, Hidekidbd6aba2014-08-27 18:03:30 +0900668 /** Key code constant: Radio key.
669 * Toggles TV service / Radio service. */
670 public static final int KEYCODE_TV_RADIO_SERVICE = 232;
671 /** Key code constant: Teletext key.
672 * Displays Teletext service. */
673 public static final int KEYCODE_TV_TELETEXT = 233;
674 /** Key code constant: Number entry key.
675 * Initiates to enter multi-digit channel nubmber when each digit key is assigned
676 * for selecting separate channel. Corresponds to Number Entry Mode (0x1D) of CEC
677 * User Control Code. */
678 public static final int KEYCODE_TV_NUMBER_ENTRY = 234;
679 /** Key code constant: Analog Terrestrial key.
680 * Switches to analog terrestrial broadcast service. */
681 public static final int KEYCODE_TV_TERRESTRIAL_ANALOG = 235;
682 /** Key code constant: Digital Terrestrial key.
683 * Switches to digital terrestrial broadcast service. */
684 public static final int KEYCODE_TV_TERRESTRIAL_DIGITAL = 236;
685 /** Key code constant: Satellite key.
686 * Switches to digital satellite broadcast service. */
687 public static final int KEYCODE_TV_SATELLITE = 237;
688 /** Key code constant: BS key.
689 * Switches to BS digital satellite broadcasting service available in Japan. */
690 public static final int KEYCODE_TV_SATELLITE_BS = 238;
691 /** Key code constant: CS key.
692 * Switches to CS digital satellite broadcasting service available in Japan. */
693 public static final int KEYCODE_TV_SATELLITE_CS = 239;
694 /** Key code constant: BS/CS key.
695 * Toggles between BS and CS digital satellite services. */
696 public static final int KEYCODE_TV_SATELLITE_SERVICE = 240;
697 /** Key code constant: Toggle Network key.
698 * Toggles selecting broacast services. */
699 public static final int KEYCODE_TV_NETWORK = 241;
700 /** Key code constant: Antenna/Cable key.
701 * Toggles broadcast input source between antenna and cable. */
702 public static final int KEYCODE_TV_ANTENNA_CABLE = 242;
703 /** Key code constant: HDMI #1 key.
704 * Switches to HDMI input #1. */
705 public static final int KEYCODE_TV_INPUT_HDMI_1 = 243;
706 /** Key code constant: HDMI #2 key.
707 * Switches to HDMI input #2. */
708 public static final int KEYCODE_TV_INPUT_HDMI_2 = 244;
709 /** Key code constant: HDMI #3 key.
710 * Switches to HDMI input #3. */
711 public static final int KEYCODE_TV_INPUT_HDMI_3 = 245;
712 /** Key code constant: HDMI #4 key.
713 * Switches to HDMI input #4. */
714 public static final int KEYCODE_TV_INPUT_HDMI_4 = 246;
715 /** Key code constant: Composite #1 key.
716 * Switches to composite video input #1. */
717 public static final int KEYCODE_TV_INPUT_COMPOSITE_1 = 247;
718 /** Key code constant: Composite #2 key.
719 * Switches to composite video input #2. */
720 public static final int KEYCODE_TV_INPUT_COMPOSITE_2 = 248;
721 /** Key code constant: Component #1 key.
722 * Switches to component video input #1. */
723 public static final int KEYCODE_TV_INPUT_COMPONENT_1 = 249;
724 /** Key code constant: Component #2 key.
725 * Switches to component video input #2. */
726 public static final int KEYCODE_TV_INPUT_COMPONENT_2 = 250;
727 /** Key code constant: VGA #1 key.
728 * Switches to VGA (analog RGB) input #1. */
729 public static final int KEYCODE_TV_INPUT_VGA_1 = 251;
730 /** Key code constant: Audio description key.
731 * Toggles audio description off / on. */
732 public static final int KEYCODE_TV_AUDIO_DESCRIPTION = 252;
733 /** Key code constant: Audio description mixing volume up key.
734 * Louden audio description volume as compared with normal audio volume. */
735 public static final int KEYCODE_TV_AUDIO_DESCRIPTION_MIX_UP = 253;
736 /** Key code constant: Audio description mixing volume down key.
737 * Lessen audio description volume as compared with normal audio volume. */
738 public static final int KEYCODE_TV_AUDIO_DESCRIPTION_MIX_DOWN = 254;
739 /** Key code constant: Zoom mode key.
740 * Changes Zoom mode (Normal, Full, Zoom, Wide-zoom, etc.) */
741 public static final int KEYCODE_TV_ZOOM_MODE = 255;
742 /** Key code constant: Contents menu key.
743 * Goes to the title list. Corresponds to Contents Menu (0x0B) of CEC User Control
744 * Code */
745 public static final int KEYCODE_TV_CONTENTS_MENU = 256;
746 /** Key code constant: Media context menu key.
747 * Goes to the context menu of media contents. Corresponds to Media Context-sensitive
748 * Menu (0x11) of CEC User Control Code. */
749 public static final int KEYCODE_TV_MEDIA_CONTEXT_MENU = 257;
750 /** Key code constant: Timer programming key.
751 * Goes to the timer recording menu. Corresponds to Timer Programming (0x54) of
752 * CEC User Control Code. */
753 public static final int KEYCODE_TV_TIMER_PROGRAMMING = 258;
754 /** Key code constant: Help key. */
755 public static final int KEYCODE_HELP = 259;
Michael Wright962c9532015-08-06 15:16:22 +0100756 /** Key code constant: Navigate to previous key.
Joseph Cooper55b9ed42015-04-15 16:21:58 -0700757 * Goes backward by one item in an ordered collection of items. */
758 public static final int KEYCODE_NAVIGATE_PREVIOUS = 260;
Michael Wright962c9532015-08-06 15:16:22 +0100759 /** Key code constant: Navigate to next key.
Joseph Cooper55b9ed42015-04-15 16:21:58 -0700760 * Advances to the next item in an ordered collection of items. */
761 public static final int KEYCODE_NAVIGATE_NEXT = 261;
762 /** Key code constant: Navigate in key.
Michael Wright962c9532015-08-06 15:16:22 +0100763 * Activates the item that currently has focus or expands to the next level of a navigation
Joseph Cooper55b9ed42015-04-15 16:21:58 -0700764 * hierarchy. */
765 public static final int KEYCODE_NAVIGATE_IN = 262;
766 /** Key code constant: Navigate out key.
Michael Wright962c9532015-08-06 15:16:22 +0100767 * Backs out one level of a navigation hierarchy or collapses the item that currently has
Joseph Cooper55b9ed42015-04-15 16:21:58 -0700768 * focus. */
769 public static final int KEYCODE_NAVIGATE_OUT = 263;
Anthony Hugh9d826682015-06-23 10:44:17 -0700770 /** Key code constant: Primary stem key for Wear
771 * Main power/reset button on watch. */
772 public static final int KEYCODE_STEM_PRIMARY = 264;
773 /** Key code constant: Generic stem key 1 for Wear */
774 public static final int KEYCODE_STEM_1 = 265;
775 /** Key code constant: Generic stem key 2 for Wear */
776 public static final int KEYCODE_STEM_2 = 266;
777 /** Key code constant: Generic stem key 3 for Wear */
778 public static final int KEYCODE_STEM_3 = 267;
David Stevensa487f0c2015-07-31 11:00:50 -0700779 /** Key code constant: Directional Pad Up-Left */
780 public static final int KEYCODE_DPAD_UP_LEFT = 268;
781 /** Key code constant: Directional Pad Down-Left */
782 public static final int KEYCODE_DPAD_DOWN_LEFT = 269;
783 /** Key code constant: Directional Pad Up-Right */
784 public static final int KEYCODE_DPAD_UP_RIGHT = 270;
785 /** Key code constant: Directional Pad Down-Right */
786 public static final int KEYCODE_DPAD_DOWN_RIGHT = 271;
Michael Wright962c9532015-08-06 15:16:22 +0100787 /** Key code constant: Skip forward media key. */
788 public static final int KEYCODE_MEDIA_SKIP_FORWARD = 272;
789 /** Key code constant: Skip backward media key. */
790 public static final int KEYCODE_MEDIA_SKIP_BACKWARD = 273;
791 /** Key code constant: Step forward media key.
792 * Steps media forward, one frame at a time. */
793 public static final int KEYCODE_MEDIA_STEP_FORWARD = 274;
794 /** Key code constant: Step backward media key.
795 * Steps media backward, one frame at a time. */
796 public static final int KEYCODE_MEDIA_STEP_BACKWARD = 275;
Nick Armstrong-Crews3a5a8c72015-09-09 09:25:03 -0700797 /** Key code constant: put device to sleep unless a wakelock is held. */
Nick Armstrong-Crews56ecfcc2015-09-07 21:46:50 -0700798 public static final int KEYCODE_SOFT_SLEEP = 276;
Michael Wrightea84cff2015-10-21 18:08:30 +0100799 /** Key code constant: Cut key. */
800 public static final int KEYCODE_CUT = 277;
801 /** Key code constant: Copy key. */
802 public static final int KEYCODE_COPY = 278;
803 /** Key code constant: Paste key. */
804 public static final int KEYCODE_PASTE = 279;
Jim Miller07e03842016-06-22 15:18:13 -0700805 /** Key code constant: Consumed by the system for navigation up */
806 public static final int KEYCODE_SYSTEM_NAVIGATION_UP = 280;
807 /** Key code constant: Consumed by the system for navigation down */
808 public static final int KEYCODE_SYSTEM_NAVIGATION_DOWN = 281;
809 /** Key code constant: Consumed by the system for navigation left*/
810 public static final int KEYCODE_SYSTEM_NAVIGATION_LEFT = 282;
811 /** Key code constant: Consumed by the system for navigation right */
812 public static final int KEYCODE_SYSTEM_NAVIGATION_RIGHT = 283;
Sujith Ramakrishnand2c9bc82017-11-02 09:54:16 -0700813 /** Key code constant: Show all apps */
Sujith Ramakrishnanf8942c02017-07-18 18:35:14 -0700814 public static final int KEYCODE_ALL_APPS = 284;
Yuichiro Hanadac1415f32018-01-15 22:36:00 +0900815 /** Key code constant: Refresh key. */
816 public static final int KEYCODE_REFRESH = 285;
Oleg Kibirevcdd17b52018-11-07 12:02:21 -0800817 /** Key code constant: Thumbs up key. Apps can use this to let user upvote content. */
818 public static final int KEYCODE_THUMBS_UP = 286;
819 /** Key code constant: Thumbs down key. Apps can use this to let user downvote content. */
820 public static final int KEYCODE_THUMBS_DOWN = 287;
821 /** Key code constant: Consumed by system to switch current viewer profile. */
822 public static final int KEYCODE_PROFILE_SWITCH = 288;
Jeff Brown497a92c2010-09-12 17:55:08 -0700823
Siarhei Vishniakoude1f9042018-05-09 09:54:43 -0700824 /**
825 * Integer value of the last KEYCODE. Increases as new keycodes are added to KeyEvent.
826 * @hide
827 */
828 @TestApi
Oleg Kibirevcdd17b52018-11-07 12:02:21 -0800829 public static final int LAST_KEYCODE = KEYCODE_PROFILE_SWITCH;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830
831 // NOTE: If you add a new keycode here you must also add it to:
832 // isSystem()
Michael Wrightdc63f7b2014-08-21 19:05:21 -0700833 // isWakeKey()
Chirayu Desai61c37ae2013-04-15 20:11:37 +0530834 // frameworks/native/include/android/keycodes.h
Jinsuk Kim96658f72014-05-14 15:33:43 +0900835 // frameworks/native/include/input/InputEventLabels.h
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 // frameworks/base/core/res/res/values/attrs.xml
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 // emulator?
Jeff Brown6651a632011-11-28 12:59:11 -0800838 // LAST_KEYCODE
Dianne Hackborn935ae462009-04-13 16:11:55 -0700839 //
840 // Also Android currently does not reserve code ranges for vendor-
841 // specific key codes. If you have new key codes to have, you
842 // MUST contribute a patch to the open source project to define
843 // those new codes. This is intended to maintain a consistent
844 // set of key code definitions across all Android devices.
Jeff Brown497a92c2010-09-12 17:55:08 -0700845
Jeff Brown497a92c2010-09-12 17:55:08 -0700846 // Symbolic names of all metakeys in bit order from least significant to most significant.
847 // Accordingly there are exactly 32 values in this table.
Mathew Inwooda570dee2018-08-17 14:56:00 +0100848 @UnsupportedAppUsage
Jeff Brown497a92c2010-09-12 17:55:08 -0700849 private static final String[] META_SYMBOLIC_NAMES = new String[] {
850 "META_SHIFT_ON",
851 "META_ALT_ON",
852 "META_SYM_ON",
853 "META_FUNCTION_ON",
854 "META_ALT_LEFT_ON",
855 "META_ALT_RIGHT_ON",
856 "META_SHIFT_LEFT_ON",
857 "META_SHIFT_RIGHT_ON",
858 "META_CAP_LOCKED",
859 "META_ALT_LOCKED",
860 "META_SYM_LOCKED",
861 "0x00000800",
862 "META_CTRL_ON",
863 "META_CTRL_LEFT_ON",
864 "META_CTRL_RIGHT_ON",
865 "0x00008000",
866 "META_META_ON",
867 "META_META_LEFT_ON",
868 "META_META_RIGHT_ON",
869 "0x00080000",
Jeff Brown51e7fe72010-10-29 22:19:53 -0700870 "META_CAPS_LOCK_ON",
871 "META_NUM_LOCK_ON",
872 "META_SCROLL_LOCK_ON",
Jeff Brown497a92c2010-09-12 17:55:08 -0700873 "0x00800000",
874 "0x01000000",
875 "0x02000000",
876 "0x04000000",
877 "0x08000000",
878 "0x10000000",
879 "0x20000000",
880 "0x40000000",
881 "0x80000000",
882 };
883
Michael Wright337d9d22014-04-22 15:03:48 -0700884 private static final String LABEL_PREFIX = "KEYCODE_";
885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 /**
887 * @deprecated There are now more than MAX_KEYCODE keycodes.
888 * Use {@link #getMaxKeyCode()} instead.
889 */
890 @Deprecated
891 public static final int MAX_KEYCODE = 84;
892
893 /**
894 * {@link #getAction} value: the key has been pressed down.
895 */
896 public static final int ACTION_DOWN = 0;
897 /**
898 * {@link #getAction} value: the key has been released.
899 */
900 public static final int ACTION_UP = 1;
901 /**
Siarhei Vishniakoue481d332018-07-18 14:46:00 +0100902 * @deprecated No longer used by the input system.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 * {@link #getAction} value: multiple duplicate key events have
904 * occurred in a row, or a complex string is being delivered. If the
Andrew Solovay5c05ded2018-10-02 14:14:42 -0700905 * key code is not {@link #KEYCODE_UNKNOWN} then the
906 * {@link #getRepeatCount()} method returns the number of times
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 * the given key code should be executed.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700908 * Otherwise, if the key code is {@link #KEYCODE_UNKNOWN}, then
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 * this is a sequence of characters as returned by {@link #getCharacters}.
910 */
Siarhei Vishniakoue481d332018-07-18 14:46:00 +0100911 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 public static final int ACTION_MULTIPLE = 2;
913
914 /**
Jeff Brown497a92c2010-09-12 17:55:08 -0700915 * SHIFT key locked in CAPS mode.
916 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
917 * @hide
918 */
Mathew Inwooda570dee2018-08-17 14:56:00 +0100919 @UnsupportedAppUsage
Jeff Brown497a92c2010-09-12 17:55:08 -0700920 public static final int META_CAP_LOCKED = 0x100;
921
922 /**
923 * ALT key locked.
924 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
925 * @hide
926 */
Mathew Inwooda570dee2018-08-17 14:56:00 +0100927 @UnsupportedAppUsage
Jeff Brown497a92c2010-09-12 17:55:08 -0700928 public static final int META_ALT_LOCKED = 0x200;
929
930 /**
931 * SYM key locked.
932 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
933 * @hide
934 */
Mathew Inwooda570dee2018-08-17 14:56:00 +0100935 @UnsupportedAppUsage
Jeff Brown497a92c2010-09-12 17:55:08 -0700936 public static final int META_SYM_LOCKED = 0x400;
937
938 /**
939 * Text is in selection mode.
940 * Reserved for use by {@link MetaKeyKeyListener} for a private unpublished constant
941 * in its API that is currently being retained for legacy reasons.
942 * @hide
943 */
Mathew Inwooda570dee2018-08-17 14:56:00 +0100944 @UnsupportedAppUsage
Jeff Brown497a92c2010-09-12 17:55:08 -0700945 public static final int META_SELECTING = 0x800;
946
947 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 * <p>This mask is used to check whether one of the ALT meta keys is pressed.</p>
949 *
950 * @see #isAltPressed()
951 * @see #getMetaState()
952 * @see #KEYCODE_ALT_LEFT
953 * @see #KEYCODE_ALT_RIGHT
954 */
955 public static final int META_ALT_ON = 0x02;
956
957 /**
958 * <p>This mask is used to check whether the left ALT meta key is pressed.</p>
959 *
960 * @see #isAltPressed()
961 * @see #getMetaState()
962 * @see #KEYCODE_ALT_LEFT
963 */
964 public static final int META_ALT_LEFT_ON = 0x10;
965
966 /**
967 * <p>This mask is used to check whether the right the ALT meta key is pressed.</p>
968 *
969 * @see #isAltPressed()
970 * @see #getMetaState()
971 * @see #KEYCODE_ALT_RIGHT
972 */
973 public static final int META_ALT_RIGHT_ON = 0x20;
974
975 /**
976 * <p>This mask is used to check whether one of the SHIFT meta keys is pressed.</p>
977 *
978 * @see #isShiftPressed()
979 * @see #getMetaState()
980 * @see #KEYCODE_SHIFT_LEFT
981 * @see #KEYCODE_SHIFT_RIGHT
982 */
983 public static final int META_SHIFT_ON = 0x1;
984
985 /**
986 * <p>This mask is used to check whether the left SHIFT meta key is pressed.</p>
987 *
988 * @see #isShiftPressed()
989 * @see #getMetaState()
990 * @see #KEYCODE_SHIFT_LEFT
991 */
992 public static final int META_SHIFT_LEFT_ON = 0x40;
993
994 /**
995 * <p>This mask is used to check whether the right SHIFT meta key is pressed.</p>
996 *
997 * @see #isShiftPressed()
998 * @see #getMetaState()
999 * @see #KEYCODE_SHIFT_RIGHT
1000 */
1001 public static final int META_SHIFT_RIGHT_ON = 0x80;
1002
1003 /**
1004 * <p>This mask is used to check whether the SYM meta key is pressed.</p>
1005 *
1006 * @see #isSymPressed()
1007 * @see #getMetaState()
1008 */
1009 public static final int META_SYM_ON = 0x4;
1010
1011 /**
Jeff Brown497a92c2010-09-12 17:55:08 -07001012 * <p>This mask is used to check whether the FUNCTION meta key is pressed.</p>
1013 *
1014 * @see #isFunctionPressed()
1015 * @see #getMetaState()
1016 */
1017 public static final int META_FUNCTION_ON = 0x8;
1018
1019 /**
1020 * <p>This mask is used to check whether one of the CTRL meta keys is pressed.</p>
1021 *
1022 * @see #isCtrlPressed()
1023 * @see #getMetaState()
1024 * @see #KEYCODE_CTRL_LEFT
1025 * @see #KEYCODE_CTRL_RIGHT
1026 */
1027 public static final int META_CTRL_ON = 0x1000;
1028
1029 /**
1030 * <p>This mask is used to check whether the left CTRL meta key is pressed.</p>
1031 *
1032 * @see #isCtrlPressed()
1033 * @see #getMetaState()
1034 * @see #KEYCODE_CTRL_LEFT
1035 */
1036 public static final int META_CTRL_LEFT_ON = 0x2000;
1037
1038 /**
1039 * <p>This mask is used to check whether the right CTRL meta key is pressed.</p>
1040 *
1041 * @see #isCtrlPressed()
1042 * @see #getMetaState()
1043 * @see #KEYCODE_CTRL_RIGHT
1044 */
1045 public static final int META_CTRL_RIGHT_ON = 0x4000;
1046
1047 /**
1048 * <p>This mask is used to check whether one of the META meta keys is pressed.</p>
1049 *
1050 * @see #isMetaPressed()
1051 * @see #getMetaState()
1052 * @see #KEYCODE_META_LEFT
1053 * @see #KEYCODE_META_RIGHT
1054 */
1055 public static final int META_META_ON = 0x10000;
1056
1057 /**
1058 * <p>This mask is used to check whether the left META meta key is pressed.</p>
1059 *
1060 * @see #isMetaPressed()
1061 * @see #getMetaState()
1062 * @see #KEYCODE_META_LEFT
1063 */
1064 public static final int META_META_LEFT_ON = 0x20000;
1065
1066 /**
1067 * <p>This mask is used to check whether the right META meta key is pressed.</p>
1068 *
1069 * @see #isMetaPressed()
1070 * @see #getMetaState()
1071 * @see #KEYCODE_META_RIGHT
1072 */
1073 public static final int META_META_RIGHT_ON = 0x40000;
1074
1075 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07001076 * <p>This mask is used to check whether the CAPS LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07001077 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07001078 * @see #isCapsLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -07001079 * @see #getMetaState()
1080 * @see #KEYCODE_CAPS_LOCK
1081 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07001082 public static final int META_CAPS_LOCK_ON = 0x100000;
Jeff Brown497a92c2010-09-12 17:55:08 -07001083
1084 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07001085 * <p>This mask is used to check whether the NUM LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07001086 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07001087 * @see #isNumLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -07001088 * @see #getMetaState()
1089 * @see #KEYCODE_NUM_LOCK
1090 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07001091 public static final int META_NUM_LOCK_ON = 0x200000;
Jeff Brown497a92c2010-09-12 17:55:08 -07001092
1093 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07001094 * <p>This mask is used to check whether the SCROLL LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07001095 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07001096 * @see #isScrollLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -07001097 * @see #getMetaState()
1098 * @see #KEYCODE_SCROLL_LOCK
1099 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07001100 public static final int META_SCROLL_LOCK_ON = 0x400000;
Jeff Brown497a92c2010-09-12 17:55:08 -07001101
Jeff Brown64da12a2011-01-04 19:57:47 -08001102 /**
1103 * This mask is a combination of {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON}
1104 * and {@link #META_SHIFT_RIGHT_ON}.
1105 */
Jeff Brownc1df9072010-12-21 16:38:50 -08001106 public static final int META_SHIFT_MASK = META_SHIFT_ON
1107 | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON;
1108
Jeff Brown64da12a2011-01-04 19:57:47 -08001109 /**
1110 * This mask is a combination of {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON}
1111 * and {@link #META_ALT_RIGHT_ON}.
1112 */
Jeff Brownc1df9072010-12-21 16:38:50 -08001113 public static final int META_ALT_MASK = META_ALT_ON
1114 | META_ALT_LEFT_ON | META_ALT_RIGHT_ON;
1115
Jeff Brown64da12a2011-01-04 19:57:47 -08001116 /**
1117 * This mask is a combination of {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON}
1118 * and {@link #META_CTRL_RIGHT_ON}.
1119 */
Jeff Brownc1df9072010-12-21 16:38:50 -08001120 public static final int META_CTRL_MASK = META_CTRL_ON
1121 | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON;
1122
Jeff Brown64da12a2011-01-04 19:57:47 -08001123 /**
1124 * This mask is a combination of {@link #META_META_ON}, {@link #META_META_LEFT_ON}
1125 * and {@link #META_META_RIGHT_ON}.
1126 */
1127 public static final int META_META_MASK = META_META_ON
Jeff Brownc1df9072010-12-21 16:38:50 -08001128 | META_META_LEFT_ON | META_META_RIGHT_ON;
1129
Jeff Brown497a92c2010-09-12 17:55:08 -07001130 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 * This mask is set if the device woke because of this key event.
Jeff Brown037c33e2014-04-09 00:31:55 -07001132 *
1133 * @deprecated This flag will never be set by the system since the system
1134 * consumes all wake keys itself.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 */
Jeff Brown037c33e2014-04-09 00:31:55 -07001136 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 public static final int FLAG_WOKE_HERE = 0x1;
RoboErik01fe6612014-02-13 14:19:04 -08001138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 /**
1140 * This mask is set if the key event was generated by a software keyboard.
1141 */
1142 public static final int FLAG_SOFT_KEYBOARD = 0x2;
RoboErik01fe6612014-02-13 14:19:04 -08001143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 /**
1145 * This mask is set if we don't want the key event to cause us to leave
1146 * touch mode.
1147 */
1148 public static final int FLAG_KEEP_TOUCH_MODE = 0x4;
RoboErik01fe6612014-02-13 14:19:04 -08001149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001151 * This mask is set if an event was known to come from a trusted part
1152 * of the system. That is, the event is known to come from the user,
1153 * and could not have been spoofed by a third party component.
1154 */
1155 public static final int FLAG_FROM_SYSTEM = 0x8;
RoboErik01fe6612014-02-13 14:19:04 -08001156
The Android Open Source Project10592532009-03-18 17:39:46 -07001157 /**
1158 * This mask is used for compatibility, to identify enter keys that are
1159 * coming from an IME whose enter key has been auto-labelled "next" or
1160 * "done". This allows TextView to dispatch these as normal enter keys
1161 * for old applications, but still do the appropriate action when
1162 * receiving them.
1163 */
1164 public static final int FLAG_EDITOR_ACTION = 0x10;
RoboErik01fe6612014-02-13 14:19:04 -08001165
The Android Open Source Project10592532009-03-18 17:39:46 -07001166 /**
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07001167 * When associated with up key events, this indicates that the key press
1168 * has been canceled. Typically this is used with virtual touch screen
1169 * keys, where the user can slide from the virtual key area on to the
1170 * display: in that case, the application will receive a canceled up
1171 * event and should not perform the action normally associated with the
1172 * key. Note that for this to work, the application can not perform an
1173 * action for a key until it receives an up or the long press timeout has
RoboErik01fe6612014-02-13 14:19:04 -08001174 * expired.
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07001175 */
1176 public static final int FLAG_CANCELED = 0x20;
RoboErik01fe6612014-02-13 14:19:04 -08001177
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07001178 /**
1179 * This key event was generated by a virtual (on-screen) hard key area.
1180 * Typically this is an area of the touchscreen, outside of the regular
1181 * display, dedicated to "hardware" buttons.
1182 */
1183 public static final int FLAG_VIRTUAL_HARD_KEY = 0x40;
RoboErik01fe6612014-02-13 14:19:04 -08001184
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07001185 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001186 * This flag is set for the first key repeat that occurs after the
1187 * long press timeout.
1188 */
1189 public static final int FLAG_LONG_PRESS = 0x80;
RoboErik01fe6612014-02-13 14:19:04 -08001190
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001191 /**
1192 * Set when a key event has {@link #FLAG_CANCELED} set because a long
RoboErik01fe6612014-02-13 14:19:04 -08001193 * press action was executed while it was down.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001194 */
1195 public static final int FLAG_CANCELED_LONG_PRESS = 0x100;
RoboErik01fe6612014-02-13 14:19:04 -08001196
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001197 /**
1198 * Set for {@link #ACTION_UP} when this event's key code is still being
1199 * tracked from its initial down. That is, somebody requested that tracking
1200 * started on the key down and a long press has not caused
1201 * the tracking to be canceled.
1202 */
1203 public static final int FLAG_TRACKING = 0x200;
Jeff Brown49ed71d2010-12-06 17:13:33 -08001204
1205 /**
1206 * Set when a key event has been synthesized to implement default behavior
1207 * for an event that the application did not handle.
1208 * Fallback key events are generated by unhandled trackball motions
1209 * (to emulate a directional keypad) and by certain unhandled key presses
1210 * that are declared in the key map (such as special function numeric keypad
1211 * keys when numlock is off).
1212 */
1213 public static final int FLAG_FALLBACK = 0x400;
1214
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001215 /**
Michael Wrighta44dd262013-04-10 21:12:00 -07001216 * Signifies that the key is being predispatched.
1217 * @hide
1218 */
1219 public static final int FLAG_PREDISPATCH = 0x20000000;
1220
1221 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001222 * Private control to determine when an app is tracking a key sequence.
1223 * @hide
1224 */
1225 public static final int FLAG_START_TRACKING = 0x40000000;
Jeff Brown21bc5c92011-02-28 18:27:14 -08001226
1227 /**
1228 * Private flag that indicates when the system has detected that this key event
1229 * may be inconsistent with respect to the sequence of previously delivered key events,
1230 * such as when a key up event is sent but the key was not down.
1231 *
1232 * @hide
1233 * @see #isTainted
1234 * @see #setTainted
1235 */
1236 public static final int FLAG_TAINTED = 0x80000000;
1237
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001238 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 * Returns the maximum keycode.
1240 */
1241 public static int getMaxKeyCode() {
1242 return LAST_KEYCODE;
1243 }
1244
1245 /**
1246 * Get the character that is produced by putting accent on the character
1247 * c.
1248 * For example, getDeadChar('`', 'e') returns &egrave;.
1249 */
1250 public static int getDeadChar(int accent, int c) {
1251 return KeyCharacterMap.getDeadChar(accent, c);
1252 }
RoboErik01fe6612014-02-13 14:19:04 -08001253
Dianne Hackborn8d374262009-09-14 21:21:52 -07001254 static final boolean DEBUG = false;
1255 static final String TAG = "KeyEvent";
Jeff Brown1f245102010-11-18 20:53:46 -08001256
1257 private static final int MAX_RECYCLED = 10;
1258 private static final Object gRecyclerLock = new Object();
1259 private static int gRecyclerUsed;
1260 private static KeyEvent gRecyclerTop;
1261
1262 private KeyEvent mNext;
Jeff Brown1f245102010-11-18 20:53:46 -08001263
Mathew Inwooda570dee2018-08-17 14:56:00 +01001264 @UnsupportedAppUsage
Jeff Brown91c69ab2011-02-14 17:03:18 -08001265 private int mDeviceId;
Mathew Inwood8c854f82018-09-14 12:35:36 +01001266 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Jeff Brown91c69ab2011-02-14 17:03:18 -08001267 private int mSource;
Siarhei Vishniakou91fa08f2018-06-08 22:49:30 +01001268 private int mDisplayId;
Mathew Inwooda570dee2018-08-17 14:56:00 +01001269 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 private int mMetaState;
Mathew Inwooda570dee2018-08-17 14:56:00 +01001271 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 private int mAction;
Mathew Inwooda570dee2018-08-17 14:56:00 +01001273 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 private int mKeyCode;
Mathew Inwooda570dee2018-08-17 14:56:00 +01001275 @UnsupportedAppUsage
Jeff Brown46b9ac02010-04-22 18:58:52 -07001276 private int mScanCode;
Mathew Inwooda570dee2018-08-17 14:56:00 +01001277 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 private int mRepeatCount;
Mathew Inwooda570dee2018-08-17 14:56:00 +01001279 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 private int mFlags;
Mathew Inwooda570dee2018-08-17 14:56:00 +01001281 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 private long mDownTime;
Mathew Inwooda570dee2018-08-17 14:56:00 +01001283 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 private long mEventTime;
Mathew Inwooda570dee2018-08-17 14:56:00 +01001285 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 private String mCharacters;
1287
1288 public interface Callback {
1289 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001290 * Called when a key down event has occurred. If you return true,
1291 * you can first call {@link KeyEvent#startTracking()
1292 * KeyEvent.startTracking()} to have the framework track the event
1293 * through its {@link #onKeyUp(int, KeyEvent)} and also call your
1294 * {@link #onKeyLongPress(int, KeyEvent)} if it occurs.
RoboErik01fe6612014-02-13 14:19:04 -08001295 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 * @param keyCode The value in event.getKeyCode().
1297 * @param event Description of the key event.
RoboErik01fe6612014-02-13 14:19:04 -08001298 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 * @return If you handled the event, return true. If you want to allow
1300 * the event to be handled by the next receiver, return false.
1301 */
1302 boolean onKeyDown(int keyCode, KeyEvent event);
1303
1304 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001305 * Called when a long press has occurred. If you return true,
1306 * the final key up will have {@link KeyEvent#FLAG_CANCELED} and
1307 * {@link KeyEvent#FLAG_CANCELED_LONG_PRESS} set. Note that in
1308 * order to receive this callback, someone in the event change
1309 * <em>must</em> return true from {@link #onKeyDown} <em>and</em>
1310 * call {@link KeyEvent#startTracking()} on the event.
RoboErik01fe6612014-02-13 14:19:04 -08001311 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001312 * @param keyCode The value in event.getKeyCode().
1313 * @param event Description of the key event.
RoboErik01fe6612014-02-13 14:19:04 -08001314 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001315 * @return If you handled the event, return true. If you want to allow
1316 * the event to be handled by the next receiver, return false.
1317 */
1318 boolean onKeyLongPress(int keyCode, KeyEvent event);
1319
1320 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 * Called when a key up event has occurred.
RoboErik01fe6612014-02-13 14:19:04 -08001322 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 * @param keyCode The value in event.getKeyCode().
1324 * @param event Description of the key event.
RoboErik01fe6612014-02-13 14:19:04 -08001325 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 * @return If you handled the event, return true. If you want to allow
1327 * the event to be handled by the next receiver, return false.
1328 */
1329 boolean onKeyUp(int keyCode, KeyEvent event);
1330
1331 /**
Kevin Hufnagleb248b1f2016-09-13 19:36:20 -07001332 * Called when a user's interaction with an analog control, such as
1333 * flinging a trackball, generates simulated down/up events for the same
1334 * key multiple times in quick succession.
RoboErik01fe6612014-02-13 14:19:04 -08001335 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 * @param keyCode The value in event.getKeyCode().
1337 * @param count Number of pairs as returned by event.getRepeatCount().
1338 * @param event Description of the key event.
RoboErik01fe6612014-02-13 14:19:04 -08001339 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 * @return If you handled the event, return true. If you want to allow
1341 * the event to be handled by the next receiver, return false.
1342 */
1343 boolean onKeyMultiple(int keyCode, int count, KeyEvent event);
1344 }
1345
Michael Wright337d9d22014-04-22 15:03:48 -07001346 private static native String nativeKeyCodeToString(int keyCode);
1347 private static native int nativeKeyCodeFromString(String keyCode);
Jeff Brown497a92c2010-09-12 17:55:08 -07001348
Jeff Brown1f245102010-11-18 20:53:46 -08001349 private KeyEvent() {
1350 }
1351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 /**
1353 * Create a new key event.
RoboErik01fe6612014-02-13 14:19:04 -08001354 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 * @param action Action code: either {@link #ACTION_DOWN},
1356 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1357 * @param code The key code.
1358 */
1359 public KeyEvent(int action, int code) {
1360 mAction = action;
1361 mKeyCode = code;
1362 mRepeatCount = 0;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001363 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 }
1365
1366 /**
1367 * Create a new key event.
RoboErik01fe6612014-02-13 14:19:04 -08001368 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1370 * at which this key code originally went down.
1371 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1372 * at which this event happened.
1373 * @param action Action code: either {@link #ACTION_DOWN},
1374 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1375 * @param code The key code.
1376 * @param repeat A repeat count for down events (> 0 if this is after the
1377 * initial down) or event count for multiple events.
1378 */
1379 public KeyEvent(long downTime, long eventTime, int action,
1380 int code, int repeat) {
1381 mDownTime = downTime;
1382 mEventTime = eventTime;
1383 mAction = action;
1384 mKeyCode = code;
1385 mRepeatCount = repeat;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001386 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 }
1388
1389 /**
1390 * Create a new key event.
RoboErik01fe6612014-02-13 14:19:04 -08001391 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1393 * at which this key code originally went down.
1394 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1395 * at which this event happened.
1396 * @param action Action code: either {@link #ACTION_DOWN},
1397 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1398 * @param code The key code.
1399 * @param repeat A repeat count for down events (> 0 if this is after the
1400 * initial down) or event count for multiple events.
1401 * @param metaState Flags indicating which meta keys are currently pressed.
1402 */
1403 public KeyEvent(long downTime, long eventTime, int action,
1404 int code, int repeat, int metaState) {
1405 mDownTime = downTime;
1406 mEventTime = eventTime;
1407 mAction = action;
1408 mKeyCode = code;
1409 mRepeatCount = repeat;
1410 mMetaState = metaState;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001411 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 }
1413
1414 /**
1415 * Create a new key event.
RoboErik01fe6612014-02-13 14:19:04 -08001416 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1418 * at which this key code originally went down.
1419 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1420 * at which this event happened.
1421 * @param action Action code: either {@link #ACTION_DOWN},
1422 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1423 * @param code The key code.
1424 * @param repeat A repeat count for down events (> 0 if this is after the
1425 * initial down) or event count for multiple events.
1426 * @param metaState Flags indicating which meta keys are currently pressed.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001427 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 * @param scancode Raw device scan code of the event.
1429 */
1430 public KeyEvent(long downTime, long eventTime, int action,
1431 int code, int repeat, int metaState,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001432 int deviceId, int scancode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 mDownTime = downTime;
1434 mEventTime = eventTime;
1435 mAction = action;
1436 mKeyCode = code;
1437 mRepeatCount = repeat;
1438 mMetaState = metaState;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001439 mDeviceId = deviceId;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001440 mScanCode = scancode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 }
1442
1443 /**
1444 * Create a new key event.
RoboErik01fe6612014-02-13 14:19:04 -08001445 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1447 * at which this key code originally went down.
1448 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1449 * at which this event happened.
1450 * @param action Action code: either {@link #ACTION_DOWN},
1451 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1452 * @param code The key code.
1453 * @param repeat A repeat count for down events (> 0 if this is after the
1454 * initial down) or event count for multiple events.
1455 * @param metaState Flags indicating which meta keys are currently pressed.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001456 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 * @param scancode Raw device scan code of the event.
1458 * @param flags The flags for this key event
1459 */
1460 public KeyEvent(long downTime, long eventTime, int action,
1461 int code, int repeat, int metaState,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001462 int deviceId, int scancode, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 mDownTime = downTime;
1464 mEventTime = eventTime;
1465 mAction = action;
1466 mKeyCode = code;
1467 mRepeatCount = repeat;
1468 mMetaState = metaState;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001469 mDeviceId = deviceId;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001470 mScanCode = scancode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 mFlags = flags;
1472 }
1473
1474 /**
Jeff Brownc5ed5912010-07-14 18:48:53 -07001475 * Create a new key event.
RoboErik01fe6612014-02-13 14:19:04 -08001476 *
Jeff Brownc5ed5912010-07-14 18:48:53 -07001477 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1478 * at which this key code originally went down.
1479 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1480 * at which this event happened.
1481 * @param action Action code: either {@link #ACTION_DOWN},
1482 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1483 * @param code The key code.
1484 * @param repeat A repeat count for down events (> 0 if this is after the
1485 * initial down) or event count for multiple events.
1486 * @param metaState Flags indicating which meta keys are currently pressed.
1487 * @param deviceId The device ID that generated the key event.
1488 * @param scancode Raw device scan code of the event.
1489 * @param flags The flags for this key event
1490 * @param source The input source such as {@link InputDevice#SOURCE_KEYBOARD}.
1491 */
1492 public KeyEvent(long downTime, long eventTime, int action,
1493 int code, int repeat, int metaState,
1494 int deviceId, int scancode, int flags, int source) {
1495 mDownTime = downTime;
1496 mEventTime = eventTime;
1497 mAction = action;
1498 mKeyCode = code;
1499 mRepeatCount = repeat;
1500 mMetaState = metaState;
1501 mDeviceId = deviceId;
1502 mScanCode = scancode;
1503 mFlags = flags;
1504 mSource = source;
Siarhei Vishniakou91fa08f2018-06-08 22:49:30 +01001505 mDisplayId = INVALID_DISPLAY;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001506 }
1507
1508 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 * Create a new key event for a string of characters. The key code,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001510 * action, repeat count and source will automatically be set to
1511 * {@link #KEYCODE_UNKNOWN}, {@link #ACTION_MULTIPLE}, 0, and
1512 * {@link InputDevice#SOURCE_KEYBOARD} for you.
RoboErik01fe6612014-02-13 14:19:04 -08001513 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 * @param time The time (in {@link android.os.SystemClock#uptimeMillis})
1515 * at which this event occured.
1516 * @param characters The string of characters.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001517 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 * @param flags The flags for this key event
1519 */
Jeff Brownc5ed5912010-07-14 18:48:53 -07001520 public KeyEvent(long time, String characters, int deviceId, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 mDownTime = time;
1522 mEventTime = time;
1523 mCharacters = characters;
1524 mAction = ACTION_MULTIPLE;
1525 mKeyCode = KEYCODE_UNKNOWN;
1526 mRepeatCount = 0;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001527 mDeviceId = deviceId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 mFlags = flags;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001529 mSource = InputDevice.SOURCE_KEYBOARD;
Siarhei Vishniakou91fa08f2018-06-08 22:49:30 +01001530 mDisplayId = INVALID_DISPLAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 }
1532
1533 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001534 * Make an exact copy of an existing key event.
1535 */
1536 public KeyEvent(KeyEvent origEvent) {
1537 mDownTime = origEvent.mDownTime;
1538 mEventTime = origEvent.mEventTime;
1539 mAction = origEvent.mAction;
1540 mKeyCode = origEvent.mKeyCode;
1541 mRepeatCount = origEvent.mRepeatCount;
1542 mMetaState = origEvent.mMetaState;
1543 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001544 mSource = origEvent.mSource;
Siarhei Vishniakou91fa08f2018-06-08 22:49:30 +01001545 mDisplayId = origEvent.mDisplayId;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001546 mScanCode = origEvent.mScanCode;
The Android Open Source Project10592532009-03-18 17:39:46 -07001547 mFlags = origEvent.mFlags;
1548 mCharacters = origEvent.mCharacters;
1549 }
1550
1551 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 * Copy an existing key event, modifying its time and repeat count.
RoboErik01fe6612014-02-13 14:19:04 -08001553 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001554 * @deprecated Use {@link #changeTimeRepeat(KeyEvent, long, int)}
1555 * instead.
RoboErik01fe6612014-02-13 14:19:04 -08001556 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557 * @param origEvent The existing event to be copied.
1558 * @param eventTime The new event time
1559 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1560 * @param newRepeat The new repeat count of the event.
1561 */
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001562 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 public KeyEvent(KeyEvent origEvent, long eventTime, int newRepeat) {
1564 mDownTime = origEvent.mDownTime;
1565 mEventTime = eventTime;
1566 mAction = origEvent.mAction;
1567 mKeyCode = origEvent.mKeyCode;
1568 mRepeatCount = newRepeat;
1569 mMetaState = origEvent.mMetaState;
1570 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001571 mSource = origEvent.mSource;
Siarhei Vishniakou91fa08f2018-06-08 22:49:30 +01001572 mDisplayId = origEvent.mDisplayId;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001573 mScanCode = origEvent.mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 mFlags = origEvent.mFlags;
1575 mCharacters = origEvent.mCharacters;
1576 }
1577
Jeff Brown1f245102010-11-18 20:53:46 -08001578 private static KeyEvent obtain() {
1579 final KeyEvent ev;
1580 synchronized (gRecyclerLock) {
1581 ev = gRecyclerTop;
1582 if (ev == null) {
1583 return new KeyEvent();
1584 }
1585 gRecyclerTop = ev.mNext;
1586 gRecyclerUsed -= 1;
1587 }
Jeff Brown1f245102010-11-18 20:53:46 -08001588 ev.mNext = null;
Jeff Brown32cbc38552011-12-01 14:01:49 -08001589 ev.prepareForReuse();
Jeff Brown1f245102010-11-18 20:53:46 -08001590 return ev;
1591 }
1592
1593 /**
1594 * Obtains a (potentially recycled) key event.
1595 *
1596 * @hide
1597 */
1598 public static KeyEvent obtain(long downTime, long eventTime, int action,
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001599 int code, int repeat, int metaState,
Siarhei Vishniakou91fa08f2018-06-08 22:49:30 +01001600 int deviceId, int scancode, int flags, int source, int displayId, String characters) {
Jeff Brown1f245102010-11-18 20:53:46 -08001601 KeyEvent ev = obtain();
1602 ev.mDownTime = downTime;
1603 ev.mEventTime = eventTime;
1604 ev.mAction = action;
1605 ev.mKeyCode = code;
1606 ev.mRepeatCount = repeat;
1607 ev.mMetaState = metaState;
1608 ev.mDeviceId = deviceId;
1609 ev.mScanCode = scancode;
1610 ev.mFlags = flags;
1611 ev.mSource = source;
Siarhei Vishniakou91fa08f2018-06-08 22:49:30 +01001612 ev.mDisplayId = displayId;
Jeff Brown1f245102010-11-18 20:53:46 -08001613 ev.mCharacters = characters;
1614 return ev;
1615 }
1616
1617 /**
Siarhei Vishniakou91fa08f2018-06-08 22:49:30 +01001618 * Obtains a (potentially recycled) key event.
1619 *
1620 * @hide
1621 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001622 @UnsupportedAppUsage
Siarhei Vishniakou91fa08f2018-06-08 22:49:30 +01001623 public static KeyEvent obtain(long downTime, long eventTime, int action,
1624 int code, int repeat, int metaState,
1625 int deviceId, int scancode, int flags, int source, String characters) {
1626 return obtain(downTime, eventTime, action, code, repeat, metaState, deviceId, scancode,
1627 flags, source, INVALID_DISPLAY, characters);
1628 }
1629
1630 /**
1631
1632 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08001633 * Obtains a (potentially recycled) copy of another key event.
1634 *
1635 * @hide
1636 */
1637 public static KeyEvent obtain(KeyEvent other) {
1638 KeyEvent ev = obtain();
1639 ev.mDownTime = other.mDownTime;
1640 ev.mEventTime = other.mEventTime;
1641 ev.mAction = other.mAction;
1642 ev.mKeyCode = other.mKeyCode;
1643 ev.mRepeatCount = other.mRepeatCount;
1644 ev.mMetaState = other.mMetaState;
1645 ev.mDeviceId = other.mDeviceId;
1646 ev.mScanCode = other.mScanCode;
1647 ev.mFlags = other.mFlags;
1648 ev.mSource = other.mSource;
Siarhei Vishniakou91fa08f2018-06-08 22:49:30 +01001649 ev.mDisplayId = other.mDisplayId;
Jeff Brown21bc5c92011-02-28 18:27:14 -08001650 ev.mCharacters = other.mCharacters;
1651 return ev;
1652 }
1653
1654 /** @hide */
1655 @Override
1656 public KeyEvent copy() {
1657 return obtain(this);
1658 }
1659
1660 /**
Jeff Brown1f245102010-11-18 20:53:46 -08001661 * Recycles a key event.
1662 * Key events should only be recycled if they are owned by the system since user
1663 * code expects them to be essentially immutable, "tracking" notwithstanding.
1664 *
1665 * @hide
1666 */
Jeff Brown92cc2d82011-12-02 01:19:47 -08001667 @Override
Mathew Inwood8c854f82018-09-14 12:35:36 +01001668 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Jeff Brown1f245102010-11-18 20:53:46 -08001669 public final void recycle() {
Jeff Brown32cbc38552011-12-01 14:01:49 -08001670 super.recycle();
Jeff Brown1f245102010-11-18 20:53:46 -08001671 mCharacters = null;
1672
1673 synchronized (gRecyclerLock) {
1674 if (gRecyclerUsed < MAX_RECYCLED) {
1675 gRecyclerUsed++;
1676 mNext = gRecyclerTop;
1677 gRecyclerTop = this;
1678 }
1679 }
1680 }
1681
Jeff Brown92cc2d82011-12-02 01:19:47 -08001682 /** @hide */
1683 @Override
1684 public final void recycleIfNeededAfterDispatch() {
1685 // Do nothing.
1686 }
1687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001689 * Create a new key event that is the same as the given one, but whose
1690 * event time and repeat count are replaced with the given value.
RoboErik01fe6612014-02-13 14:19:04 -08001691 *
The Android Open Source Project10592532009-03-18 17:39:46 -07001692 * @param event The existing event to be copied. This is not modified.
1693 * @param eventTime The new event time
1694 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1695 * @param newRepeat The new repeat count of the event.
1696 */
1697 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime,
1698 int newRepeat) {
1699 return new KeyEvent(event, eventTime, newRepeat);
1700 }
RoboErik01fe6612014-02-13 14:19:04 -08001701
The Android Open Source Project10592532009-03-18 17:39:46 -07001702 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001703 * Create a new key event that is the same as the given one, but whose
1704 * event time and repeat count are replaced with the given value.
RoboErik01fe6612014-02-13 14:19:04 -08001705 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001706 * @param event The existing event to be copied. This is not modified.
1707 * @param eventTime The new event time
1708 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1709 * @param newRepeat The new repeat count of the event.
1710 * @param newFlags New flags for the event, replacing the entire value
1711 * in the original event.
1712 */
1713 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime,
1714 int newRepeat, int newFlags) {
1715 KeyEvent ret = new KeyEvent(event);
1716 ret.mEventTime = eventTime;
1717 ret.mRepeatCount = newRepeat;
1718 ret.mFlags = newFlags;
1719 return ret;
1720 }
RoboErik01fe6612014-02-13 14:19:04 -08001721
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001722 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 * Copy an existing key event, modifying its action.
RoboErik01fe6612014-02-13 14:19:04 -08001724 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725 * @param origEvent The existing event to be copied.
1726 * @param action The new action code of the event.
1727 */
The Android Open Source Project10592532009-03-18 17:39:46 -07001728 private KeyEvent(KeyEvent origEvent, int action) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 mDownTime = origEvent.mDownTime;
1730 mEventTime = origEvent.mEventTime;
1731 mAction = action;
1732 mKeyCode = origEvent.mKeyCode;
1733 mRepeatCount = origEvent.mRepeatCount;
1734 mMetaState = origEvent.mMetaState;
1735 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001736 mSource = origEvent.mSource;
Siarhei Vishniakou91fa08f2018-06-08 22:49:30 +01001737 mDisplayId = origEvent.mDisplayId;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001738 mScanCode = origEvent.mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739 mFlags = origEvent.mFlags;
1740 // Don't copy mCharacters, since one way or the other we'll lose it
1741 // when changing the action.
1742 }
1743
1744 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001745 * Create a new key event that is the same as the given one, but whose
1746 * action is replaced with the given value.
RoboErik01fe6612014-02-13 14:19:04 -08001747 *
The Android Open Source Project10592532009-03-18 17:39:46 -07001748 * @param event The existing event to be copied. This is not modified.
1749 * @param action The new action code of the event.
1750 */
1751 public static KeyEvent changeAction(KeyEvent event, int action) {
1752 return new KeyEvent(event, action);
1753 }
RoboErik01fe6612014-02-13 14:19:04 -08001754
The Android Open Source Project10592532009-03-18 17:39:46 -07001755 /**
1756 * Create a new key event that is the same as the given one, but whose
1757 * flags are replaced with the given value.
RoboErik01fe6612014-02-13 14:19:04 -08001758 *
The Android Open Source Project10592532009-03-18 17:39:46 -07001759 * @param event The existing event to be copied. This is not modified.
1760 * @param flags The new flags constant.
1761 */
1762 public static KeyEvent changeFlags(KeyEvent event, int flags) {
1763 event = new KeyEvent(event);
1764 event.mFlags = flags;
1765 return event;
1766 }
Jeff Brown21bc5c92011-02-28 18:27:14 -08001767
1768 /** @hide */
1769 @Override
1770 public final boolean isTainted() {
1771 return (mFlags & FLAG_TAINTED) != 0;
1772 }
1773
1774 /** @hide */
1775 @Override
1776 public final void setTainted(boolean tainted) {
1777 mFlags = tainted ? mFlags | FLAG_TAINTED : mFlags & ~FLAG_TAINTED;
1778 }
1779
The Android Open Source Project10592532009-03-18 17:39:46 -07001780 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 * Don't use in new code, instead explicitly check
1782 * {@link #getAction()}.
RoboErik01fe6612014-02-13 14:19:04 -08001783 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 * @return If the action is ACTION_DOWN, returns true; else false.
1785 *
1786 * @deprecated
1787 * @hide
1788 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001789 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 @Deprecated public final boolean isDown() {
1791 return mAction == ACTION_DOWN;
1792 }
1793
Michael Wright337d9d22014-04-22 15:03:48 -07001794 /** Is this a system key? System keys can not be used for menu shortcuts.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 */
1796 public final boolean isSystem() {
Michael Wright337d9d22014-04-22 15:03:48 -07001797 return isSystemKey(mKeyCode);
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -07001798 }
1799
1800 /** @hide */
Michael Wright337d9d22014-04-22 15:03:48 -07001801 public final boolean isWakeKey() {
1802 return isWakeKey(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 }
1804
Jeff Brown6f2fba42011-02-19 01:08:02 -08001805 /**
Hyundo Moon3d1353a2018-11-30 19:41:48 +09001806 * Returns whether this key will be sent to the
1807 * {@link android.media.session.MediaSession.Callback} if not handled.
1808 */
1809 public static final boolean isMediaSessionKey(int keyCode) {
1810 switch (keyCode) {
1811 case KeyEvent.KEYCODE_MEDIA_PLAY:
1812 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1813 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1814 case KeyEvent.KEYCODE_MUTE:
1815 case KeyEvent.KEYCODE_HEADSETHOOK:
1816 case KeyEvent.KEYCODE_MEDIA_STOP:
1817 case KeyEvent.KEYCODE_MEDIA_NEXT:
1818 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1819 case KeyEvent.KEYCODE_MEDIA_REWIND:
1820 case KeyEvent.KEYCODE_MEDIA_RECORD:
1821 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1822 return true;
1823 }
1824 return false;
1825 }
1826
1827 /**
Jeff Brown6f2fba42011-02-19 01:08:02 -08001828 * Returns true if the specified keycode is a gamepad button.
1829 * @return True if the keycode is a gamepad button, such as {@link #KEYCODE_BUTTON_A}.
1830 */
1831 public static final boolean isGamepadButton(int keyCode) {
1832 switch (keyCode) {
1833 case KeyEvent.KEYCODE_BUTTON_A:
1834 case KeyEvent.KEYCODE_BUTTON_B:
1835 case KeyEvent.KEYCODE_BUTTON_C:
1836 case KeyEvent.KEYCODE_BUTTON_X:
1837 case KeyEvent.KEYCODE_BUTTON_Y:
1838 case KeyEvent.KEYCODE_BUTTON_Z:
1839 case KeyEvent.KEYCODE_BUTTON_L1:
1840 case KeyEvent.KEYCODE_BUTTON_R1:
1841 case KeyEvent.KEYCODE_BUTTON_L2:
1842 case KeyEvent.KEYCODE_BUTTON_R2:
1843 case KeyEvent.KEYCODE_BUTTON_THUMBL:
1844 case KeyEvent.KEYCODE_BUTTON_THUMBR:
1845 case KeyEvent.KEYCODE_BUTTON_START:
1846 case KeyEvent.KEYCODE_BUTTON_SELECT:
1847 case KeyEvent.KEYCODE_BUTTON_MODE:
1848 case KeyEvent.KEYCODE_BUTTON_1:
1849 case KeyEvent.KEYCODE_BUTTON_2:
1850 case KeyEvent.KEYCODE_BUTTON_3:
1851 case KeyEvent.KEYCODE_BUTTON_4:
1852 case KeyEvent.KEYCODE_BUTTON_5:
1853 case KeyEvent.KEYCODE_BUTTON_6:
1854 case KeyEvent.KEYCODE_BUTTON_7:
1855 case KeyEvent.KEYCODE_BUTTON_8:
1856 case KeyEvent.KEYCODE_BUTTON_9:
1857 case KeyEvent.KEYCODE_BUTTON_10:
1858 case KeyEvent.KEYCODE_BUTTON_11:
1859 case KeyEvent.KEYCODE_BUTTON_12:
1860 case KeyEvent.KEYCODE_BUTTON_13:
1861 case KeyEvent.KEYCODE_BUTTON_14:
1862 case KeyEvent.KEYCODE_BUTTON_15:
1863 case KeyEvent.KEYCODE_BUTTON_16:
1864 return true;
1865 default:
1866 return false;
1867 }
1868 }
1869
Michael Wright25b0c302013-07-10 12:54:06 -07001870 /** Whether key will, by default, trigger a click on the focused view.
1871 * @hide
1872 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001873 @UnsupportedAppUsage
Michael Wright25b0c302013-07-10 12:54:06 -07001874 public static final boolean isConfirmKey(int keyCode) {
1875 switch (keyCode) {
1876 case KeyEvent.KEYCODE_DPAD_CENTER:
1877 case KeyEvent.KEYCODE_ENTER:
Michael Wrightaa1a94d2015-11-26 16:04:54 +00001878 case KeyEvent.KEYCODE_SPACE:
Selim Cinekce2bd0f2016-02-19 16:16:54 -08001879 case KeyEvent.KEYCODE_NUMPAD_ENTER:
Michael Wright25b0c302013-07-10 12:54:06 -07001880 return true;
1881 default:
1882 return false;
1883 }
1884 }
1885
Michael Wright337d9d22014-04-22 15:03:48 -07001886 /** Is this a system key? System keys can not be used for menu shortcuts.
1887 * @hide
1888 */
1889 public static final boolean isSystemKey(int keyCode) {
1890 switch (keyCode) {
1891 case KeyEvent.KEYCODE_MENU:
1892 case KeyEvent.KEYCODE_SOFT_RIGHT:
1893 case KeyEvent.KEYCODE_HOME:
1894 case KeyEvent.KEYCODE_BACK:
1895 case KeyEvent.KEYCODE_CALL:
1896 case KeyEvent.KEYCODE_ENDCALL:
1897 case KeyEvent.KEYCODE_VOLUME_UP:
1898 case KeyEvent.KEYCODE_VOLUME_DOWN:
1899 case KeyEvent.KEYCODE_VOLUME_MUTE:
1900 case KeyEvent.KEYCODE_MUTE:
1901 case KeyEvent.KEYCODE_POWER:
1902 case KeyEvent.KEYCODE_HEADSETHOOK:
1903 case KeyEvent.KEYCODE_MEDIA_PLAY:
1904 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1905 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1906 case KeyEvent.KEYCODE_MEDIA_STOP:
1907 case KeyEvent.KEYCODE_MEDIA_NEXT:
1908 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1909 case KeyEvent.KEYCODE_MEDIA_REWIND:
1910 case KeyEvent.KEYCODE_MEDIA_RECORD:
1911 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1912 case KeyEvent.KEYCODE_CAMERA:
1913 case KeyEvent.KEYCODE_FOCUS:
1914 case KeyEvent.KEYCODE_SEARCH:
1915 case KeyEvent.KEYCODE_BRIGHTNESS_DOWN:
1916 case KeyEvent.KEYCODE_BRIGHTNESS_UP:
1917 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
Jim Miller07e03842016-06-22 15:18:13 -07001918 case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_UP:
1919 case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_DOWN:
1920 case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_LEFT:
1921 case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_RIGHT:
Michael Wright337d9d22014-04-22 15:03:48 -07001922 return true;
1923 }
1924
1925 return false;
1926 }
1927
1928 /** @hide */
1929 public static final boolean isWakeKey(int keyCode) {
1930 switch (keyCode) {
1931 case KeyEvent.KEYCODE_BACK:
Michael Wright337d9d22014-04-22 15:03:48 -07001932 case KeyEvent.KEYCODE_MENU:
Michael Wright337d9d22014-04-22 15:03:48 -07001933 case KeyEvent.KEYCODE_WAKEUP:
Michael Wright8ac485a2014-06-04 12:38:18 -07001934 case KeyEvent.KEYCODE_PAIRING:
Chenjie Luobad498f2016-01-13 11:01:59 -08001935 case KeyEvent.KEYCODE_STEM_1:
1936 case KeyEvent.KEYCODE_STEM_2:
1937 case KeyEvent.KEYCODE_STEM_3:
Michael Wright337d9d22014-04-22 15:03:48 -07001938 return true;
1939 }
1940 return false;
1941 }
1942
Michael Wrightce0c13a2014-07-30 10:49:21 -07001943 /** @hide */
1944 public static final boolean isMetaKey(int keyCode) {
1945 return keyCode == KeyEvent.KEYCODE_META_LEFT || keyCode == KeyEvent.KEYCODE_META_RIGHT;
1946 }
1947
Andrii Kulian112d0562016-03-08 10:44:22 -08001948 /** @hide */
1949 public static final boolean isAltKey(int keyCode) {
1950 return keyCode == KeyEvent.KEYCODE_ALT_LEFT || keyCode == KeyEvent.KEYCODE_ALT_RIGHT;
1951 }
1952
Jeff Brown91c69ab2011-02-14 17:03:18 -08001953 /** {@inheritDoc} */
1954 @Override
1955 public final int getDeviceId() {
1956 return mDeviceId;
1957 }
1958
1959 /** {@inheritDoc} */
1960 @Override
1961 public final int getSource() {
1962 return mSource;
1963 }
1964
1965 /** {@inheritDoc} */
1966 @Override
1967 public final void setSource(int source) {
1968 mSource = source;
1969 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970
Siarhei Vishniakou91fa08f2018-06-08 22:49:30 +01001971 /** @hide */
1972 @Override
1973 public final int getDisplayId() {
1974 return mDisplayId;
1975 }
1976
1977 /** @hide */
Tiger Huangfc218452018-09-27 00:38:50 +08001978 @TestApi
Siarhei Vishniakou91fa08f2018-06-08 22:49:30 +01001979 @Override
1980 public final void setDisplayId(int displayId) {
1981 mDisplayId = displayId;
1982 }
1983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 /**
1985 * <p>Returns the state of the meta keys.</p>
1986 *
1987 * @return an integer in which each bit set to 1 represents a pressed
1988 * meta key
1989 *
1990 * @see #isAltPressed()
1991 * @see #isShiftPressed()
1992 * @see #isSymPressed()
Jeff Brown497a92c2010-09-12 17:55:08 -07001993 * @see #isCtrlPressed()
1994 * @see #isMetaPressed()
1995 * @see #isFunctionPressed()
Jeff Brown51e7fe72010-10-29 22:19:53 -07001996 * @see #isCapsLockOn()
1997 * @see #isNumLockOn()
1998 * @see #isScrollLockOn()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 * @see #META_ALT_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002000 * @see #META_ALT_LEFT_ON
2001 * @see #META_ALT_RIGHT_ON
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 * @see #META_SHIFT_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002003 * @see #META_SHIFT_LEFT_ON
2004 * @see #META_SHIFT_RIGHT_ON
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 * @see #META_SYM_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002006 * @see #META_FUNCTION_ON
2007 * @see #META_CTRL_ON
2008 * @see #META_CTRL_LEFT_ON
2009 * @see #META_CTRL_RIGHT_ON
2010 * @see #META_META_ON
2011 * @see #META_META_LEFT_ON
2012 * @see #META_META_RIGHT_ON
Jeff Brown51e7fe72010-10-29 22:19:53 -07002013 * @see #META_CAPS_LOCK_ON
2014 * @see #META_NUM_LOCK_ON
2015 * @see #META_SCROLL_LOCK_ON
Jeff Brown54875002011-04-06 15:33:01 -07002016 * @see #getModifiers
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 */
2018 public final int getMetaState() {
2019 return mMetaState;
2020 }
2021
2022 /**
Jeff Brown54875002011-04-06 15:33:01 -07002023 * Returns the state of the modifier keys.
2024 * <p>
2025 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2026 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2027 * not considered modifier keys. Consequently, this function specifically masks out
2028 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2029 * </p><p>
2030 * The value returned consists of the meta state (from {@link #getMetaState})
2031 * normalized using {@link #normalizeMetaState(int)} and then masked with
2032 * {@link #getModifierMetaStateMask} so that only valid modifier bits are retained.
2033 * </p>
2034 *
2035 * @return An integer in which each bit set to 1 represents a pressed modifier key.
2036 * @see #getMetaState
2037 */
2038 public final int getModifiers() {
2039 return normalizeMetaState(mMetaState) & META_MODIFIER_MASK;
2040 }
2041
2042 /**
Tiger Huangfc218452018-09-27 00:38:50 +08002043 * Modifies the flags of the event.
2044 *
2045 * @param newFlags New flags for the event, replacing the entire value.
2046 * @hide
2047 */
2048 public final void setFlags(int newFlags) {
2049 mFlags = newFlags;
2050 }
2051
2052 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 * Returns the flags for this key event.
2054 *
2055 * @see #FLAG_WOKE_HERE
2056 */
2057 public final int getFlags() {
2058 return mFlags;
2059 }
2060
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002061 // Mask of all modifier key meta states. Specifically excludes locked keys like caps lock.
Mathew Inwooda570dee2018-08-17 14:56:00 +01002062 @UnsupportedAppUsage
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002063 private static final int META_MODIFIER_MASK =
2064 META_SHIFT_ON | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON
2065 | META_ALT_ON | META_ALT_LEFT_ON | META_ALT_RIGHT_ON
2066 | META_CTRL_ON | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON
2067 | META_META_ON | META_META_LEFT_ON | META_META_RIGHT_ON
2068 | META_SYM_ON | META_FUNCTION_ON;
2069
2070 // Mask of all lock key meta states.
Mathew Inwooda570dee2018-08-17 14:56:00 +01002071 @UnsupportedAppUsage
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002072 private static final int META_LOCK_MASK =
2073 META_CAPS_LOCK_ON | META_NUM_LOCK_ON | META_SCROLL_LOCK_ON;
2074
2075 // Mask of all valid meta states.
Mathew Inwooda570dee2018-08-17 14:56:00 +01002076 @UnsupportedAppUsage
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002077 private static final int META_ALL_MASK = META_MODIFIER_MASK | META_LOCK_MASK;
2078
2079 // Mask of all synthetic meta states that are reserved for API compatibility with
2080 // historical uses in MetaKeyKeyListener.
Mathew Inwooda570dee2018-08-17 14:56:00 +01002081 @UnsupportedAppUsage
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002082 private static final int META_SYNTHETIC_MASK =
2083 META_CAP_LOCKED | META_ALT_LOCKED | META_SYM_LOCKED | META_SELECTING;
2084
2085 // Mask of all meta states that are not valid use in specifying a modifier key.
2086 // These bits are known to be used for purposes other than specifying modifiers.
Mathew Inwooda570dee2018-08-17 14:56:00 +01002087 @UnsupportedAppUsage
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002088 private static final int META_INVALID_MODIFIER_MASK =
2089 META_LOCK_MASK | META_SYNTHETIC_MASK;
2090
2091 /**
2092 * Gets a mask that includes all valid modifier key meta state bits.
2093 * <p>
2094 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2095 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2096 * not considered modifier keys. Consequently, the mask specifically excludes
2097 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2098 * </p>
2099 *
2100 * @return The modifier meta state mask which is a combination of
2101 * {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON}, {@link #META_SHIFT_RIGHT_ON},
2102 * {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON}, {@link #META_ALT_RIGHT_ON},
2103 * {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON}, {@link #META_CTRL_RIGHT_ON},
2104 * {@link #META_META_ON}, {@link #META_META_LEFT_ON}, {@link #META_META_RIGHT_ON},
2105 * {@link #META_SYM_ON}, {@link #META_FUNCTION_ON}.
2106 */
2107 public static int getModifierMetaStateMask() {
2108 return META_MODIFIER_MASK;
2109 }
2110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002111 /**
2112 * Returns true if this key code is a modifier key.
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002113 * <p>
2114 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2115 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2116 * not considered modifier keys. Consequently, this function return false
2117 * for those keys.
2118 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 *
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002120 * @return True if the key code is one of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002121 * {@link #KEYCODE_SHIFT_LEFT} {@link #KEYCODE_SHIFT_RIGHT},
Jeff Brown497a92c2010-09-12 17:55:08 -07002122 * {@link #KEYCODE_ALT_LEFT}, {@link #KEYCODE_ALT_RIGHT},
Jeff Brown497a92c2010-09-12 17:55:08 -07002123 * {@link #KEYCODE_CTRL_LEFT}, {@link #KEYCODE_CTRL_RIGHT},
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002124 * {@link #KEYCODE_META_LEFT}, or {@link #KEYCODE_META_RIGHT},
2125 * {@link #KEYCODE_SYM}, {@link #KEYCODE_NUM}, {@link #KEYCODE_FUNCTION}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 */
2127 public static boolean isModifierKey(int keyCode) {
Jeff Brown497a92c2010-09-12 17:55:08 -07002128 switch (keyCode) {
2129 case KEYCODE_SHIFT_LEFT:
2130 case KEYCODE_SHIFT_RIGHT:
2131 case KEYCODE_ALT_LEFT:
2132 case KEYCODE_ALT_RIGHT:
Jeff Brown497a92c2010-09-12 17:55:08 -07002133 case KEYCODE_CTRL_LEFT:
2134 case KEYCODE_CTRL_RIGHT:
2135 case KEYCODE_META_LEFT:
2136 case KEYCODE_META_RIGHT:
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002137 case KEYCODE_SYM:
2138 case KEYCODE_NUM:
2139 case KEYCODE_FUNCTION:
Jeff Brown497a92c2010-09-12 17:55:08 -07002140 return true;
2141 default:
2142 return false;
2143 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 }
2145
2146 /**
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002147 * Normalizes the specified meta state.
2148 * <p>
2149 * The meta state is normalized such that if either the left or right modifier meta state
2150 * bits are set then the result will also include the universal bit for that modifier.
2151 * </p><p>
2152 * If the specified meta state contains {@link #META_ALT_LEFT_ON} then
2153 * the result will also contain {@link #META_ALT_ON} in addition to {@link #META_ALT_LEFT_ON}
2154 * and the other bits that were specified in the input. The same is process is
2155 * performed for shift, control and meta.
2156 * </p><p>
2157 * If the specified meta state contains synthetic meta states defined by
2158 * {@link MetaKeyKeyListener}, then those states are translated here and the original
2159 * synthetic meta states are removed from the result.
2160 * {@link MetaKeyKeyListener#META_CAP_LOCKED} is translated to {@link #META_CAPS_LOCK_ON}.
2161 * {@link MetaKeyKeyListener#META_ALT_LOCKED} is translated to {@link #META_ALT_ON}.
2162 * {@link MetaKeyKeyListener#META_SYM_LOCKED} is translated to {@link #META_SYM_ON}.
2163 * </p><p>
2164 * Undefined meta state bits are removed.
2165 * </p>
2166 *
2167 * @param metaState The meta state.
2168 * @return The normalized meta state.
2169 */
2170 public static int normalizeMetaState(int metaState) {
2171 if ((metaState & (META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON)) != 0) {
2172 metaState |= META_SHIFT_ON;
2173 }
2174 if ((metaState & (META_ALT_LEFT_ON | META_ALT_RIGHT_ON)) != 0) {
2175 metaState |= META_ALT_ON;
2176 }
2177 if ((metaState & (META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON)) != 0) {
2178 metaState |= META_CTRL_ON;
2179 }
2180 if ((metaState & (META_META_LEFT_ON | META_META_RIGHT_ON)) != 0) {
2181 metaState |= META_META_ON;
2182 }
2183 if ((metaState & MetaKeyKeyListener.META_CAP_LOCKED) != 0) {
2184 metaState |= META_CAPS_LOCK_ON;
2185 }
2186 if ((metaState & MetaKeyKeyListener.META_ALT_LOCKED) != 0) {
2187 metaState |= META_ALT_ON;
2188 }
2189 if ((metaState & MetaKeyKeyListener.META_SYM_LOCKED) != 0) {
2190 metaState |= META_SYM_ON;
2191 }
2192 return metaState & META_ALL_MASK;
2193 }
2194
2195 /**
2196 * Returns true if no modifiers keys are pressed according to the specified meta state.
2197 * <p>
2198 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2199 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2200 * not considered modifier keys. Consequently, this function ignores
2201 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2202 * </p><p>
2203 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}.
2204 * </p>
2205 *
2206 * @param metaState The meta state to consider.
2207 * @return True if no modifier keys are pressed.
2208 * @see #hasNoModifiers()
2209 */
2210 public static boolean metaStateHasNoModifiers(int metaState) {
2211 return (normalizeMetaState(metaState) & META_MODIFIER_MASK) == 0;
2212 }
2213
2214 /**
2215 * Returns true if only the specified modifier keys are pressed according to
2216 * the specified meta state. Returns false if a different combination of modifier
2217 * keys are pressed.
2218 * <p>
2219 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2220 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2221 * not considered modifier keys. Consequently, this function ignores
2222 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2223 * </p><p>
2224 * If the specified modifier mask includes directional modifiers, such as
2225 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the
2226 * modifier is pressed on that side.
2227 * If the specified modifier mask includes non-directional modifiers, such as
2228 * {@link #META_SHIFT_ON}, then this method ensures that the modifier
2229 * is pressed on either side.
2230 * If the specified modifier mask includes both directional and non-directional modifiers
2231 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON},
2232 * then this method throws an illegal argument exception.
2233 * </p>
2234 *
2235 * @param metaState The meta state to consider.
2236 * @param modifiers The meta state of the modifier keys to check. May be a combination
2237 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to
2238 * ensure that no modifier keys are pressed.
2239 * @return True if only the specified modifier keys are pressed.
2240 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers
2241 * @see #hasModifiers
2242 */
2243 public static boolean metaStateHasModifiers(int metaState, int modifiers) {
2244 // Note: For forward compatibility, we allow the parameter to contain meta states
2245 // that we do not recognize but we explicitly disallow meta states that
2246 // are not valid modifiers.
2247 if ((modifiers & META_INVALID_MODIFIER_MASK) != 0) {
2248 throw new IllegalArgumentException("modifiers must not contain "
2249 + "META_CAPS_LOCK_ON, META_NUM_LOCK_ON, META_SCROLL_LOCK_ON, "
2250 + "META_CAP_LOCKED, META_ALT_LOCKED, META_SYM_LOCKED, "
2251 + "or META_SELECTING");
2252 }
2253
2254 metaState = normalizeMetaState(metaState) & META_MODIFIER_MASK;
2255 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2256 META_SHIFT_ON, META_SHIFT_LEFT_ON, META_SHIFT_RIGHT_ON);
2257 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2258 META_ALT_ON, META_ALT_LEFT_ON, META_ALT_RIGHT_ON);
2259 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2260 META_CTRL_ON, META_CTRL_LEFT_ON, META_CTRL_RIGHT_ON);
2261 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2262 META_META_ON, META_META_LEFT_ON, META_META_RIGHT_ON);
2263 return metaState == modifiers;
2264 }
2265
2266 private static int metaStateFilterDirectionalModifiers(int metaState,
2267 int modifiers, int basic, int left, int right) {
2268 final boolean wantBasic = (modifiers & basic) != 0;
2269 final int directional = left | right;
2270 final boolean wantLeftOrRight = (modifiers & directional) != 0;
2271
2272 if (wantBasic) {
2273 if (wantLeftOrRight) {
2274 throw new IllegalArgumentException("modifiers must not contain "
2275 + metaStateToString(basic) + " combined with "
2276 + metaStateToString(left) + " or " + metaStateToString(right));
2277 }
2278 return metaState & ~directional;
2279 } else if (wantLeftOrRight) {
2280 return metaState & ~basic;
2281 } else {
2282 return metaState;
2283 }
2284 }
2285
2286 /**
2287 * Returns true if no modifier keys are pressed.
2288 * <p>
2289 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2290 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2291 * not considered modifier keys. Consequently, this function ignores
2292 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2293 * </p><p>
2294 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}.
2295 * </p>
2296 *
2297 * @return True if no modifier keys are pressed.
2298 * @see #metaStateHasNoModifiers
2299 */
2300 public final boolean hasNoModifiers() {
2301 return metaStateHasNoModifiers(mMetaState);
2302 }
2303
2304 /**
2305 * Returns true if only the specified modifiers keys are pressed.
2306 * Returns false if a different combination of modifier keys are pressed.
2307 * <p>
2308 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2309 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2310 * not considered modifier keys. Consequently, this function ignores
2311 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2312 * </p><p>
2313 * If the specified modifier mask includes directional modifiers, such as
2314 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the
2315 * modifier is pressed on that side.
2316 * If the specified modifier mask includes non-directional modifiers, such as
2317 * {@link #META_SHIFT_ON}, then this method ensures that the modifier
2318 * is pressed on either side.
2319 * If the specified modifier mask includes both directional and non-directional modifiers
2320 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON},
2321 * then this method throws an illegal argument exception.
2322 * </p>
2323 *
2324 * @param modifiers The meta state of the modifier keys to check. May be a combination
2325 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to
2326 * ensure that no modifier keys are pressed.
2327 * @return True if only the specified modifier keys are pressed.
2328 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers
2329 * @see #metaStateHasModifiers
2330 */
2331 public final boolean hasModifiers(int modifiers) {
2332 return metaStateHasModifiers(mMetaState, modifiers);
2333 }
2334
2335 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 * <p>Returns the pressed state of the ALT meta key.</p>
2337 *
2338 * @return true if the ALT key is pressed, false otherwise
2339 *
2340 * @see #KEYCODE_ALT_LEFT
2341 * @see #KEYCODE_ALT_RIGHT
2342 * @see #META_ALT_ON
2343 */
2344 public final boolean isAltPressed() {
2345 return (mMetaState & META_ALT_ON) != 0;
2346 }
2347
2348 /**
2349 * <p>Returns the pressed state of the SHIFT meta key.</p>
2350 *
2351 * @return true if the SHIFT key is pressed, false otherwise
2352 *
2353 * @see #KEYCODE_SHIFT_LEFT
2354 * @see #KEYCODE_SHIFT_RIGHT
2355 * @see #META_SHIFT_ON
2356 */
2357 public final boolean isShiftPressed() {
2358 return (mMetaState & META_SHIFT_ON) != 0;
2359 }
2360
2361 /**
2362 * <p>Returns the pressed state of the SYM meta key.</p>
2363 *
2364 * @return true if the SYM key is pressed, false otherwise
2365 *
2366 * @see #KEYCODE_SYM
2367 * @see #META_SYM_ON
2368 */
2369 public final boolean isSymPressed() {
2370 return (mMetaState & META_SYM_ON) != 0;
2371 }
2372
2373 /**
Jeff Brown497a92c2010-09-12 17:55:08 -07002374 * <p>Returns the pressed state of the CTRL meta key.</p>
2375 *
2376 * @return true if the CTRL key is pressed, false otherwise
2377 *
2378 * @see #KEYCODE_CTRL_LEFT
2379 * @see #KEYCODE_CTRL_RIGHT
2380 * @see #META_CTRL_ON
2381 */
2382 public final boolean isCtrlPressed() {
2383 return (mMetaState & META_CTRL_ON) != 0;
2384 }
2385
2386 /**
2387 * <p>Returns the pressed state of the META meta key.</p>
2388 *
2389 * @return true if the META key is pressed, false otherwise
2390 *
2391 * @see #KEYCODE_META_LEFT
2392 * @see #KEYCODE_META_RIGHT
2393 * @see #META_META_ON
2394 */
2395 public final boolean isMetaPressed() {
2396 return (mMetaState & META_META_ON) != 0;
2397 }
2398
2399 /**
2400 * <p>Returns the pressed state of the FUNCTION meta key.</p>
2401 *
2402 * @return true if the FUNCTION key is pressed, false otherwise
2403 *
2404 * @see #KEYCODE_FUNCTION
2405 * @see #META_FUNCTION_ON
2406 */
2407 public final boolean isFunctionPressed() {
2408 return (mMetaState & META_FUNCTION_ON) != 0;
2409 }
2410
2411 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07002412 * <p>Returns the locked state of the CAPS LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002413 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002414 * @return true if the CAPS LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002415 *
2416 * @see #KEYCODE_CAPS_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002417 * @see #META_CAPS_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002418 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002419 public final boolean isCapsLockOn() {
2420 return (mMetaState & META_CAPS_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002421 }
2422
2423 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07002424 * <p>Returns the locked state of the NUM LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002425 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002426 * @return true if the NUM LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002427 *
2428 * @see #KEYCODE_NUM_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002429 * @see #META_NUM_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002430 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002431 public final boolean isNumLockOn() {
2432 return (mMetaState & META_NUM_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002433 }
2434
2435 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07002436 * <p>Returns the locked state of the SCROLL LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002437 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002438 * @return true if the SCROLL LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002439 *
2440 * @see #KEYCODE_SCROLL_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002441 * @see #META_SCROLL_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002442 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002443 public final boolean isScrollLockOn() {
2444 return (mMetaState & META_SCROLL_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002445 }
2446
2447 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002448 * Retrieve the action of this key event. May be either
2449 * {@link #ACTION_DOWN}, {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
RoboErik01fe6612014-02-13 14:19:04 -08002450 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 * @return The event action: ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.
2452 */
2453 public final int getAction() {
2454 return mAction;
2455 }
2456
2457 /**
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07002458 * For {@link #ACTION_UP} events, indicates that the event has been
2459 * canceled as per {@link #FLAG_CANCELED}.
2460 */
2461 public final boolean isCanceled() {
2462 return (mFlags&FLAG_CANCELED) != 0;
2463 }
RoboErik01fe6612014-02-13 14:19:04 -08002464
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07002465 /**
Wale Ogunwalec3672cd2014-11-05 15:17:35 -08002466 * Set {@link #FLAG_CANCELED} flag for the key event.
2467 *
2468 * @hide
2469 */
2470 @Override
2471 public final void cancel() {
2472 mFlags |= FLAG_CANCELED;
2473 }
2474
2475 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002476 * Call this during {@link Callback#onKeyDown} to have the system track
2477 * the key through its final up (possibly including a long press). Note
2478 * that only one key can be tracked at a time -- if another key down
2479 * event is received while a previous one is being tracked, tracking is
2480 * stopped on the previous event.
2481 */
2482 public final void startTracking() {
2483 mFlags |= FLAG_START_TRACKING;
2484 }
RoboErik01fe6612014-02-13 14:19:04 -08002485
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002486 /**
2487 * For {@link #ACTION_UP} events, indicates that the event is still being
2488 * tracked from its initial down event as per
2489 * {@link #FLAG_TRACKING}.
2490 */
2491 public final boolean isTracking() {
2492 return (mFlags&FLAG_TRACKING) != 0;
2493 }
RoboErik01fe6612014-02-13 14:19:04 -08002494
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002495 /**
2496 * For {@link #ACTION_DOWN} events, indicates that the event has been
2497 * canceled as per {@link #FLAG_LONG_PRESS}.
2498 */
2499 public final boolean isLongPress() {
2500 return (mFlags&FLAG_LONG_PRESS) != 0;
2501 }
RoboErik01fe6612014-02-13 14:19:04 -08002502
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002503 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 * Retrieve the key code of the key event. This is the physical key that
2505 * was pressed, <em>not</em> the Unicode character.
RoboErik01fe6612014-02-13 14:19:04 -08002506 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507 * @return The key code of the event.
2508 */
2509 public final int getKeyCode() {
2510 return mKeyCode;
2511 }
2512
2513 /**
2514 * For the special case of a {@link #ACTION_MULTIPLE} event with key
2515 * code of {@link #KEYCODE_UNKNOWN}, this is a raw string of characters
2516 * associated with the event. In all other cases it is null.
RoboErik01fe6612014-02-13 14:19:04 -08002517 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518 * @return Returns a String of 1 or more characters associated with
2519 * the event.
Siarhei Vishniakoue481d332018-07-18 14:46:00 +01002520 *
2521 * @deprecated no longer used by the input system.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002522 */
Siarhei Vishniakoue481d332018-07-18 14:46:00 +01002523 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002524 public final String getCharacters() {
2525 return mCharacters;
2526 }
RoboErik01fe6612014-02-13 14:19:04 -08002527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 /**
2529 * Retrieve the hardware key id of this key event. These values are not
2530 * reliable and vary from device to device.
2531 *
2532 * {@more}
2533 * Mostly this is here for debugging purposes.
2534 */
2535 public final int getScanCode() {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002536 return mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002537 }
2538
2539 /**
Siarhei Vishniakou4fddffc2018-11-07 17:02:29 -08002540 * Retrieve the repeat count of the event. For key down events,
2541 * this is the number of times the key has repeated with the first
2542 * down starting at 0 and counting up from there. For key up events,
2543 * this is always equal to zero. For multiple key events,
2544 * this is the number of down/up pairs that have occurred.
RoboErik01fe6612014-02-13 14:19:04 -08002545 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002546 * @return The number of times the key has repeated.
2547 */
2548 public final int getRepeatCount() {
2549 return mRepeatCount;
2550 }
2551
2552 /**
Tiger Huangfc218452018-09-27 00:38:50 +08002553 * Modifies the down time and the event time of the event.
2554 *
2555 * @param downTime The new down time (in {@link android.os.SystemClock#uptimeMillis}) of the
2556 * event.
2557 * @param eventTime The new event time (in {@link android.os.SystemClock#uptimeMillis}) of the
2558 * event.
2559 * @hide
2560 */
2561 public final void setTime(long downTime, long eventTime) {
2562 mDownTime = downTime;
2563 mEventTime = eventTime;
2564 }
2565
2566 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567 * Retrieve the time of the most recent key down event,
2568 * in the {@link android.os.SystemClock#uptimeMillis} time base. If this
2569 * is a down event, this will be the same as {@link #getEventTime()}.
2570 * Note that when chording keys, this value is the down time of the
2571 * most recently pressed key, which may <em>not</em> be the same physical
2572 * key of this event.
RoboErik01fe6612014-02-13 14:19:04 -08002573 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002574 * @return Returns the most recent key down time, in the
2575 * {@link android.os.SystemClock#uptimeMillis} time base
2576 */
2577 public final long getDownTime() {
2578 return mDownTime;
2579 }
2580
2581 /**
Jeff Brownb11499d2012-04-20 19:54:22 -07002582 * Retrieve the time this event occurred,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 * in the {@link android.os.SystemClock#uptimeMillis} time base.
Jeff Brownb11499d2012-04-20 19:54:22 -07002584 *
RoboErik01fe6612014-02-13 14:19:04 -08002585 * @return Returns the time this event occurred,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002586 * in the {@link android.os.SystemClock#uptimeMillis} time base.
2587 */
Jeff Brownb11499d2012-04-20 19:54:22 -07002588 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002589 public final long getEventTime() {
2590 return mEventTime;
2591 }
2592
Jeff Brownb11499d2012-04-20 19:54:22 -07002593 /**
2594 * Retrieve the time this event occurred,
2595 * in the {@link android.os.SystemClock#uptimeMillis} time base but with
2596 * nanosecond (instead of millisecond) precision.
2597 * <p>
2598 * The value is in nanosecond precision but it may not have nanosecond accuracy.
2599 * </p>
2600 *
2601 * @return Returns the time this event occurred,
2602 * in the {@link android.os.SystemClock#uptimeMillis} time base but with
2603 * nanosecond (instead of millisecond) precision.
2604 *
2605 * @hide
2606 */
Jeff Brown4e91a182011-04-07 11:38:09 -07002607 @Override
2608 public final long getEventTimeNano() {
2609 return mEventTime * 1000000L;
2610 }
2611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002612 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002613 * Renamed to {@link #getDeviceId}.
RoboErik01fe6612014-02-13 14:19:04 -08002614 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002615 * @hide
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002616 * @deprecated use {@link #getDeviceId()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002618 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002619 public final int getKeyboardDevice() {
2620 return mDeviceId;
2621 }
2622
2623 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002624 * Gets the {@link KeyCharacterMap} associated with the keyboard device.
2625 *
2626 * @return The associated key character map.
Andrew Sapperstein8ab3dc72011-06-23 18:56:44 -07002627 * @throws {@link KeyCharacterMap.UnavailableException} if the key character map
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002628 * could not be loaded because it was malformed or the default key character map
2629 * is missing from the system.
2630 *
Andrew Sapperstein8ab3dc72011-06-23 18:56:44 -07002631 * @see KeyCharacterMap#load
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002632 */
2633 public final KeyCharacterMap getKeyCharacterMap() {
2634 return KeyCharacterMap.load(mDeviceId);
2635 }
2636
2637 /**
2638 * Gets the primary character for this key.
2639 * In other words, the label that is physically printed on it.
2640 *
2641 * @return The display label character, or 0 if none (eg. for non-printing keys).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642 */
2643 public char getDisplayLabel() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002644 return getKeyCharacterMap().getDisplayLabel(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 }
RoboErik01fe6612014-02-13 14:19:04 -08002646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002647 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002648 * Gets the Unicode character generated by the specified key and meta
2649 * key state combination.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650 * <p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002651 * Returns the Unicode character that the specified key would produce
2652 * when the specified meta bits (see {@link MetaKeyKeyListener})
2653 * were active.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 * </p><p>
2655 * Returns 0 if the key is not one that is used to type Unicode
2656 * characters.
2657 * </p><p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002658 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the
2659 * key is a "dead key" that should be combined with another to
2660 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} --
2661 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002662 * </p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002663 *
2664 * @return The associated character or combining accent, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 */
2666 public int getUnicodeChar() {
2667 return getUnicodeChar(mMetaState);
2668 }
RoboErik01fe6612014-02-13 14:19:04 -08002669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002670 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002671 * Gets the Unicode character generated by the specified key and meta
2672 * key state combination.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002673 * <p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002674 * Returns the Unicode character that the specified key would produce
2675 * when the specified meta bits (see {@link MetaKeyKeyListener})
2676 * were active.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002677 * </p><p>
2678 * Returns 0 if the key is not one that is used to type Unicode
2679 * characters.
2680 * </p><p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002681 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the
2682 * key is a "dead key" that should be combined with another to
2683 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} --
2684 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 * </p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002686 *
2687 * @param metaState The meta key modifier state.
2688 * @return The associated character or combining accent, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002689 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002690 public int getUnicodeChar(int metaState) {
2691 return getKeyCharacterMap().get(mKeyCode, metaState);
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 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002695 * Get the character conversion data for a given key code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002696 *
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002697 * @param results A {@link KeyCharacterMap.KeyData} instance that will be
2698 * filled with the results.
2699 * @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 -08002700 *
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002701 * @deprecated instead use {@link #getDisplayLabel()},
2702 * {@link #getNumber()} or {@link #getUnicodeChar(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002704 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 public boolean getKeyData(KeyData results) {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002706 return getKeyCharacterMap().getKeyData(mKeyCode, results);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002707 }
RoboErik01fe6612014-02-13 14:19:04 -08002708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002709 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002710 * Gets the first character in the character array that can be generated
2711 * by the specified key code.
2712 * <p>
2713 * This is a convenience function that returns the same value as
2714 * {@link #getMatch(char[],int) getMatch(chars, 0)}.
2715 * </p>
2716 *
2717 * @param chars The array of matching characters to consider.
2718 * @return The matching associated character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 */
2720 public char getMatch(char[] chars) {
2721 return getMatch(chars, 0);
2722 }
RoboErik01fe6612014-02-13 14:19:04 -08002723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002725 * Gets the first character in the character array that can be generated
2726 * by the specified key code. If there are multiple choices, prefers
2727 * the one that would be generated with the specified meta key modifier state.
2728 *
2729 * @param chars The array of matching characters to consider.
2730 * @param metaState The preferred meta key modifier state.
2731 * @return The matching associated character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002733 public char getMatch(char[] chars, int metaState) {
2734 return getKeyCharacterMap().getMatch(mKeyCode, chars, metaState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735 }
RoboErik01fe6612014-02-13 14:19:04 -08002736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002737 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002738 * Gets the number or symbol associated with the key.
2739 * <p>
2740 * The character value is returned, not the numeric value.
2741 * If the key is not a number, but is a symbol, the symbol is retuned.
2742 * </p><p>
2743 * This method is intended to to support dial pads and other numeric or
2744 * symbolic entry on keyboards where certain keys serve dual function
2745 * as alphabetic and symbolic keys. This method returns the number
2746 * or symbol associated with the key independent of whether the user
2747 * has pressed the required modifier.
2748 * </p><p>
2749 * For example, on one particular keyboard the keys on the top QWERTY row generate
2750 * numbers when ALT is pressed such that ALT-Q maps to '1'. So for that keyboard
2751 * when {@link #getNumber} is called with {@link KeyEvent#KEYCODE_Q} it returns '1'
2752 * so that the user can type numbers without pressing ALT when it makes sense.
2753 * </p>
2754 *
2755 * @return The associated numeric or symbolic character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 */
2757 public char getNumber() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002758 return getKeyCharacterMap().getNumber(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759 }
RoboErik01fe6612014-02-13 14:19:04 -08002760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002761 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002762 * Returns true if this key produces a glyph.
2763 *
2764 * @return True if the key is a printing key.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002765 */
2766 public boolean isPrintingKey() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002767 return getKeyCharacterMap().isPrintingKey(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002768 }
RoboErik01fe6612014-02-13 14:19:04 -08002769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002771 * @deprecated Use {@link #dispatch(Callback, DispatcherState, Object)} instead.
2772 */
2773 @Deprecated
2774 public final boolean dispatch(Callback receiver) {
2775 return dispatch(receiver, null, null);
2776 }
RoboErik01fe6612014-02-13 14:19:04 -08002777
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002778 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 * Deliver this key event to a {@link Callback} interface. If this is
2780 * an ACTION_MULTIPLE event and it is not handled, then an attempt will
2781 * be made to deliver a single normal event.
RoboErik01fe6612014-02-13 14:19:04 -08002782 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002783 * @param receiver The Callback that will be given the event.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002784 * @param state State information retained across events.
2785 * @param target The target of the dispatch, for use in tracking.
RoboErik01fe6612014-02-13 14:19:04 -08002786 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 * @return The return value from the Callback method that was called.
2788 */
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002789 public final boolean dispatch(Callback receiver, DispatcherState state,
2790 Object target) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 switch (mAction) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002792 case ACTION_DOWN: {
2793 mFlags &= ~FLAG_START_TRACKING;
Dianne Hackborn8d374262009-09-14 21:21:52 -07002794 if (DEBUG) Log.v(TAG, "Key down to " + target + " in " + state
2795 + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002796 boolean res = receiver.onKeyDown(mKeyCode, this);
2797 if (state != null) {
2798 if (res && mRepeatCount == 0 && (mFlags&FLAG_START_TRACKING) != 0) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002799 if (DEBUG) Log.v(TAG, " Start tracking!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002800 state.startTracking(this, target);
2801 } else if (isLongPress() && state.isTracking(this)) {
2802 try {
2803 if (receiver.onKeyLongPress(mKeyCode, this)) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002804 if (DEBUG) Log.v(TAG, " Clear from long press!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002805 state.performedLongPress(this);
2806 res = true;
2807 }
2808 } catch (AbstractMethodError e) {
2809 }
2810 }
2811 }
2812 return res;
2813 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002814 case ACTION_UP:
Dianne Hackborn8d374262009-09-14 21:21:52 -07002815 if (DEBUG) Log.v(TAG, "Key up to " + target + " in " + state
2816 + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002817 if (state != null) {
2818 state.handleUpEvent(this);
2819 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 return receiver.onKeyUp(mKeyCode, this);
2821 case ACTION_MULTIPLE:
2822 final int count = mRepeatCount;
2823 final int code = mKeyCode;
2824 if (receiver.onKeyMultiple(code, count, this)) {
2825 return true;
2826 }
2827 if (code != KeyEvent.KEYCODE_UNKNOWN) {
2828 mAction = ACTION_DOWN;
2829 mRepeatCount = 0;
2830 boolean handled = receiver.onKeyDown(code, this);
2831 if (handled) {
2832 mAction = ACTION_UP;
2833 receiver.onKeyUp(code, this);
2834 }
2835 mAction = ACTION_MULTIPLE;
2836 mRepeatCount = count;
2837 return handled;
2838 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002839 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840 }
2841 return false;
2842 }
2843
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002844 /**
2845 * Use with {@link KeyEvent#dispatch(Callback, DispatcherState, Object)}
2846 * for more advanced key dispatching, such as long presses.
2847 */
2848 public static class DispatcherState {
2849 int mDownKeyCode;
2850 Object mDownTarget;
2851 SparseIntArray mActiveLongPresses = new SparseIntArray();
RoboErik01fe6612014-02-13 14:19:04 -08002852
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002853 /**
2854 * Reset back to initial state.
2855 */
2856 public void reset() {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002857 if (DEBUG) Log.v(TAG, "Reset: " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002858 mDownKeyCode = 0;
2859 mDownTarget = null;
2860 mActiveLongPresses.clear();
2861 }
RoboErik01fe6612014-02-13 14:19:04 -08002862
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002863 /**
2864 * Stop any tracking associated with this target.
2865 */
2866 public void reset(Object target) {
2867 if (mDownTarget == target) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002868 if (DEBUG) Log.v(TAG, "Reset in " + target + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002869 mDownKeyCode = 0;
2870 mDownTarget = null;
2871 }
2872 }
RoboErik01fe6612014-02-13 14:19:04 -08002873
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002874 /**
2875 * Start tracking the key code associated with the given event. This
2876 * can only be called on a key down. It will allow you to see any
2877 * long press associated with the key, and will result in
2878 * {@link KeyEvent#isTracking} return true on the long press and up
2879 * events.
RoboErik01fe6612014-02-13 14:19:04 -08002880 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002881 * <p>This is only needed if you are directly dispatching events, rather
2882 * than handling them in {@link Callback#onKeyDown}.
2883 */
2884 public void startTracking(KeyEvent event, Object target) {
2885 if (event.getAction() != ACTION_DOWN) {
2886 throw new IllegalArgumentException(
2887 "Can only start tracking on a down event");
2888 }
Dianne Hackborn8d374262009-09-14 21:21:52 -07002889 if (DEBUG) Log.v(TAG, "Start trackingt in " + target + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002890 mDownKeyCode = event.getKeyCode();
2891 mDownTarget = target;
2892 }
RoboErik01fe6612014-02-13 14:19:04 -08002893
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002894 /**
2895 * Return true if the key event is for a key code that is currently
2896 * being tracked by the dispatcher.
2897 */
2898 public boolean isTracking(KeyEvent event) {
2899 return mDownKeyCode == event.getKeyCode();
2900 }
RoboErik01fe6612014-02-13 14:19:04 -08002901
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002902 /**
2903 * Keep track of the given event's key code as having performed an
2904 * action with a long press, so no action should occur on the up.
2905 * <p>This is only needed if you are directly dispatching events, rather
2906 * than handling them in {@link Callback#onKeyLongPress}.
2907 */
2908 public void performedLongPress(KeyEvent event) {
2909 mActiveLongPresses.put(event.getKeyCode(), 1);
2910 }
RoboErik01fe6612014-02-13 14:19:04 -08002911
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002912 /**
2913 * Handle key up event to stop tracking. This resets the dispatcher state,
2914 * and updates the key event state based on it.
2915 * <p>This is only needed if you are directly dispatching events, rather
2916 * than handling them in {@link Callback#onKeyUp}.
2917 */
2918 public void handleUpEvent(KeyEvent event) {
2919 final int keyCode = event.getKeyCode();
Dianne Hackborn8d374262009-09-14 21:21:52 -07002920 if (DEBUG) Log.v(TAG, "Handle key up " + event + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002921 int index = mActiveLongPresses.indexOfKey(keyCode);
2922 if (index >= 0) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002923 if (DEBUG) Log.v(TAG, " Index: " + index);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002924 event.mFlags |= FLAG_CANCELED | FLAG_CANCELED_LONG_PRESS;
2925 mActiveLongPresses.removeAt(index);
2926 }
2927 if (mDownKeyCode == keyCode) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002928 if (DEBUG) Log.v(TAG, " Tracking!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002929 event.mFlags |= FLAG_TRACKING;
2930 mDownKeyCode = 0;
2931 mDownTarget = null;
2932 }
2933 }
2934 }
Jeff Brown497a92c2010-09-12 17:55:08 -07002935
2936 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 public String toString() {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002938 StringBuilder msg = new StringBuilder();
2939 msg.append("KeyEvent { action=").append(actionToString(mAction));
2940 msg.append(", keyCode=").append(keyCodeToString(mKeyCode));
2941 msg.append(", scanCode=").append(mScanCode);
2942 if (mCharacters != null) {
2943 msg.append(", characters=\"").append(mCharacters).append("\"");
2944 }
2945 msg.append(", metaState=").append(metaStateToString(mMetaState));
2946 msg.append(", flags=0x").append(Integer.toHexString(mFlags));
2947 msg.append(", repeatCount=").append(mRepeatCount);
2948 msg.append(", eventTime=").append(mEventTime);
2949 msg.append(", downTime=").append(mDownTime);
2950 msg.append(", deviceId=").append(mDeviceId);
2951 msg.append(", source=0x").append(Integer.toHexString(mSource));
Siarhei Vishniakou91fa08f2018-06-08 22:49:30 +01002952 msg.append(", displayId=").append(mDisplayId);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002953 msg.append(" }");
2954 return msg.toString();
Jeff Brown497a92c2010-09-12 17:55:08 -07002955 }
2956
2957 /**
2958 * Returns a string that represents the symbolic name of the specified action
Jeff Brown6f2fba42011-02-19 01:08:02 -08002959 * such as "ACTION_DOWN", or an equivalent numeric constant such as "35" if unknown.
Jeff Brown497a92c2010-09-12 17:55:08 -07002960 *
2961 * @param action The action.
2962 * @return The symbolic name of the specified action.
2963 * @hide
2964 */
Siarhei Vishniakou4c96a5e2018-04-24 17:57:44 -07002965 @TestApi
Jeff Brown497a92c2010-09-12 17:55:08 -07002966 public static String actionToString(int action) {
2967 switch (action) {
2968 case ACTION_DOWN:
2969 return "ACTION_DOWN";
2970 case ACTION_UP:
2971 return "ACTION_UP";
2972 case ACTION_MULTIPLE:
2973 return "ACTION_MULTIPLE";
2974 default:
2975 return Integer.toString(action);
2976 }
2977 }
2978
2979 /**
2980 * Returns a string that represents the symbolic name of the specified keycode
Jeff Brown6f2fba42011-02-19 01:08:02 -08002981 * such as "KEYCODE_A", "KEYCODE_DPAD_UP", or an equivalent numeric constant
2982 * such as "1001" if unknown.
Jeff Brown497a92c2010-09-12 17:55:08 -07002983 *
Siarhei Vishniakou6ace3f22018-07-17 17:10:00 +01002984 * This function is intended to be used mostly for debugging, logging, and testing. It is not
2985 * locale-specific and is not intended to be used in a user-facing manner.
2986 *
Jeff Brown497a92c2010-09-12 17:55:08 -07002987 * @param keyCode The key code.
2988 * @return The symbolic name of the specified keycode.
2989 *
2990 * @see KeyCharacterMap#getDisplayLabel
Jeff Brown497a92c2010-09-12 17:55:08 -07002991 */
2992 public static String keyCodeToString(int keyCode) {
Michael Wright337d9d22014-04-22 15:03:48 -07002993 String symbolicName = nativeKeyCodeToString(keyCode);
2994 return symbolicName != null ? LABEL_PREFIX + symbolicName : Integer.toString(keyCode);
Jeff Brown497a92c2010-09-12 17:55:08 -07002995 }
2996
2997 /**
Jeff Brown6f2fba42011-02-19 01:08:02 -08002998 * Gets a keycode by its symbolic name such as "KEYCODE_A" or an equivalent
Siarhei Vishniakoude1f9042018-05-09 09:54:43 -07002999 * numeric constant such as "29". For symbolic names,
3000 * starting in {@link android.os.Build.VERSION_CODES#Q} the prefix "KEYCODE_" is optional.
Jeff Brown497a92c2010-09-12 17:55:08 -07003001 *
3002 * @param symbolicName The symbolic name of the keycode.
Jeff Brown6f2fba42011-02-19 01:08:02 -08003003 * @return The keycode or {@link #KEYCODE_UNKNOWN} if not found.
Aurimas Liutikase701dc12018-06-01 16:04:37 -07003004 * @see #keyCodeToString(int)
Jeff Brown497a92c2010-09-12 17:55:08 -07003005 */
Siarhei Vishniakoude1f9042018-05-09 09:54:43 -07003006 public static int keyCodeFromString(@NonNull String symbolicName) {
3007 try {
3008 int keyCode = Integer.parseInt(symbolicName);
3009 if (keyCodeIsValid(keyCode)) {
Michael Wright973efa02014-05-13 15:38:56 -07003010 return keyCode;
3011 }
Jeff Brown497a92c2010-09-12 17:55:08 -07003012 } catch (NumberFormatException ex) {
Jeff Brown497a92c2010-09-12 17:55:08 -07003013 }
Siarhei Vishniakoude1f9042018-05-09 09:54:43 -07003014
3015 if (symbolicName.startsWith(LABEL_PREFIX)) {
3016 symbolicName = symbolicName.substring(LABEL_PREFIX.length());
3017 }
3018 int keyCode = nativeKeyCodeFromString(symbolicName);
3019 if (keyCodeIsValid(keyCode)) {
3020 return keyCode;
3021 }
3022 return KEYCODE_UNKNOWN;
3023 }
3024
3025 private static boolean keyCodeIsValid(int keyCode) {
3026 return keyCode >= KEYCODE_UNKNOWN && keyCode <= LAST_KEYCODE;
Jeff Brown497a92c2010-09-12 17:55:08 -07003027 }
3028
3029 /**
3030 * Returns a string that represents the symbolic name of the specified combined meta
3031 * key modifier state flags such as "0", "META_SHIFT_ON",
Jeff Brown6f2fba42011-02-19 01:08:02 -08003032 * "META_ALT_ON|META_SHIFT_ON" or an equivalent numeric constant such as "0x10000000"
3033 * if unknown.
Jeff Brown497a92c2010-09-12 17:55:08 -07003034 *
3035 * @param metaState The meta state.
3036 * @return The symbolic name of the specified combined meta state flags.
3037 * @hide
3038 */
3039 public static String metaStateToString(int metaState) {
3040 if (metaState == 0) {
3041 return "0";
3042 }
3043 StringBuilder result = null;
3044 int i = 0;
3045 while (metaState != 0) {
3046 final boolean isSet = (metaState & 1) != 0;
3047 metaState >>>= 1; // unsigned shift!
3048 if (isSet) {
3049 final String name = META_SYMBOLIC_NAMES[i];
3050 if (result == null) {
3051 if (metaState == 0) {
3052 return name;
3053 }
3054 result = new StringBuilder(name);
3055 } else {
3056 result.append('|');
3057 result.append(name);
3058 }
3059 }
3060 i += 1;
3061 }
3062 return result.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003063 }
3064
3065 public static final Parcelable.Creator<KeyEvent> CREATOR
3066 = new Parcelable.Creator<KeyEvent>() {
Jim Miller07e03842016-06-22 15:18:13 -07003067 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003068 public KeyEvent createFromParcel(Parcel in) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07003069 in.readInt(); // skip token, we already know this is a KeyEvent
3070 return KeyEvent.createFromParcelBody(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071 }
3072
Jim Miller07e03842016-06-22 15:18:13 -07003073 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003074 public KeyEvent[] newArray(int size) {
3075 return new KeyEvent[size];
3076 }
3077 };
RoboErik01fe6612014-02-13 14:19:04 -08003078
Jeff Brown6ec402b2010-07-28 15:48:59 -07003079 /** @hide */
3080 public static KeyEvent createFromParcelBody(Parcel in) {
3081 return new KeyEvent(in);
3082 }
RoboErik01fe6612014-02-13 14:19:04 -08003083
Jeff Brown6ec402b2010-07-28 15:48:59 -07003084 private KeyEvent(Parcel in) {
Jeff Brown91c69ab2011-02-14 17:03:18 -08003085 mDeviceId = in.readInt();
3086 mSource = in.readInt();
Siarhei Vishniakou91fa08f2018-06-08 22:49:30 +01003087 mDisplayId = in.readInt();
Jeff Brown6ec402b2010-07-28 15:48:59 -07003088 mAction = in.readInt();
3089 mKeyCode = in.readInt();
3090 mRepeatCount = in.readInt();
3091 mMetaState = in.readInt();
3092 mScanCode = in.readInt();
3093 mFlags = in.readInt();
3094 mDownTime = in.readLong();
3095 mEventTime = in.readLong();
Siarhei Vishniakoue481d332018-07-18 14:46:00 +01003096 mCharacters = in.readString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003097 }
3098
Jim Miller07e03842016-06-22 15:18:13 -07003099 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003100 public void writeToParcel(Parcel out, int flags) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07003101 out.writeInt(PARCEL_TOKEN_KEY_EVENT);
Jeff Brown91c69ab2011-02-14 17:03:18 -08003102
3103 out.writeInt(mDeviceId);
3104 out.writeInt(mSource);
Siarhei Vishniakou91fa08f2018-06-08 22:49:30 +01003105 out.writeInt(mDisplayId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003106 out.writeInt(mAction);
3107 out.writeInt(mKeyCode);
3108 out.writeInt(mRepeatCount);
3109 out.writeInt(mMetaState);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003110 out.writeInt(mScanCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003111 out.writeInt(mFlags);
3112 out.writeLong(mDownTime);
3113 out.writeLong(mEventTime);
Siarhei Vishniakoue481d332018-07-18 14:46:00 +01003114 out.writeString(mCharacters);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003115 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003116}