1 /* 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 17 package android.view; 18 19 import static android.os.IInputConstants.INPUT_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; 20 import static android.view.Display.INVALID_DISPLAY; 21 22 import android.annotation.FlaggedApi; 23 import android.annotation.IntDef; 24 import android.annotation.NonNull; 25 import android.annotation.Nullable; 26 import android.annotation.TestApi; 27 import android.compat.annotation.UnsupportedAppUsage; 28 import android.os.Build; 29 import android.os.IInputConstants; 30 import android.os.Parcel; 31 import android.os.Parcelable; 32 import android.text.method.MetaKeyKeyListener; 33 import android.util.Log; 34 import android.util.SparseIntArray; 35 import android.view.KeyCharacterMap.KeyData; 36 37 import com.android.hardware.input.Flags; 38 39 import java.lang.annotation.Retention; 40 import java.lang.annotation.RetentionPolicy; 41 import java.util.concurrent.TimeUnit; 42 43 /** 44 * Object used to report key and button events. 45 * <p> 46 * Each key press is described by a sequence of key events. A key press 47 * starts with a key event with {@link #ACTION_DOWN}. If the key is held 48 * sufficiently long that it repeats, then the initial down is followed 49 * additional key events with {@link #ACTION_DOWN} and a non-zero value for 50 * {@link #getRepeatCount()}. The last key event is a {@link #ACTION_UP} 51 * for the key up. If the key press is canceled, the key up event will have the 52 * {@link #FLAG_CANCELED} flag set. 53 * </p><p> 54 * Key events are generally accompanied by a key code ({@link #getKeyCode()}), 55 * scan code ({@link #getScanCode()}) and meta state ({@link #getMetaState()}). 56 * Key code constants are defined in this class. Scan code constants are raw 57 * device-specific codes obtained from the OS and so are not generally meaningful 58 * to applications unless interpreted using the {@link KeyCharacterMap}. 59 * Meta states describe the pressed state of key modifiers 60 * such as {@link #META_SHIFT_ON} or {@link #META_ALT_ON}. 61 * </p><p> 62 * Key codes typically correspond one-to-one with individual keys on an input device. 63 * Many keys and key combinations serve quite different functions on different 64 * input devices so care must be taken when interpreting them. Always use the 65 * {@link KeyCharacterMap} associated with the input device when mapping keys 66 * to characters. Be aware that there may be multiple key input devices active 67 * at the same time and each will have its own key character map. 68 * </p><p> 69 * As soft input methods can use multiple and inventive ways of inputting text, 70 * there is no guarantee that any key press on a soft keyboard will generate a key 71 * event: this is left to the IME's discretion, and in fact sending such events is 72 * discouraged. You should never rely on receiving KeyEvents for any key on a soft 73 * input method. In particular, the default software keyboard will never send any 74 * key event to any application targetting Jelly Bean or later, and will only send 75 * events for some presses of the delete and return keys to applications targetting 76 * Ice Cream Sandwich or earlier. Be aware that other software input methods may 77 * never send key events regardless of the version. Consider using editor actions 78 * like {@link android.view.inputmethod.EditorInfo#IME_ACTION_DONE} if you need 79 * specific interaction with the software keyboard, as it gives more visibility to 80 * the user as to how your application will react to key presses. 81 * </p><p> 82 * When interacting with an IME, the framework may deliver key events 83 * with the special action {@link #ACTION_MULTIPLE} that either specifies 84 * that single repeated key code or a sequence of characters to insert. 85 * </p><p> 86 * In general, the framework cannot guarantee that the key events it delivers 87 * to a view always constitute complete key sequences since some events may be dropped 88 * or modified by containing views before they are delivered. The view implementation 89 * should be prepared to handle {@link #FLAG_CANCELED} and should tolerate anomalous 90 * situations such as receiving a new {@link #ACTION_DOWN} without first having 91 * received an {@link #ACTION_UP} for the prior key press. 92 * </p><p> 93 * Refer to {@link InputDevice} for more information about how different kinds of 94 * input devices and sources represent keys and buttons. 95 * </p> 96 */ 97 public class KeyEvent extends InputEvent implements Parcelable { 98 /** Key code constant: Unknown key code. */ 99 public static final int KEYCODE_UNKNOWN = 0; 100 /** Key code constant: Soft Left key. 101 * Usually situated below the display on phones and used as a multi-function 102 * feature key for selecting a software defined function shown on the bottom left 103 * of the display. */ 104 public static final int KEYCODE_SOFT_LEFT = 1; 105 /** Key code constant: Soft Right key. 106 * Usually situated below the display on phones and used as a multi-function 107 * feature key for selecting a software defined function shown on the bottom right 108 * of the display. */ 109 public static final int KEYCODE_SOFT_RIGHT = 2; 110 /** Key code constant: Home key. 111 * This key is handled by the framework and is never delivered to applications. */ 112 public static final int KEYCODE_HOME = 3; 113 /** Key code constant: Back key. */ 114 public static final int KEYCODE_BACK = 4; 115 /** Key code constant: Call key. */ 116 public static final int KEYCODE_CALL = 5; 117 /** Key code constant: End Call key. */ 118 public static final int KEYCODE_ENDCALL = 6; 119 /** Key code constant: '0' key. */ 120 public static final int KEYCODE_0 = 7; 121 /** Key code constant: '1' key. */ 122 public static final int KEYCODE_1 = 8; 123 /** Key code constant: '2' key. */ 124 public static final int KEYCODE_2 = 9; 125 /** Key code constant: '3' key. */ 126 public static final int KEYCODE_3 = 10; 127 /** Key code constant: '4' key. */ 128 public static final int KEYCODE_4 = 11; 129 /** Key code constant: '5' key. */ 130 public static final int KEYCODE_5 = 12; 131 /** Key code constant: '6' key. */ 132 public static final int KEYCODE_6 = 13; 133 /** Key code constant: '7' key. */ 134 public static final int KEYCODE_7 = 14; 135 /** Key code constant: '8' key. */ 136 public static final int KEYCODE_8 = 15; 137 /** Key code constant: '9' key. */ 138 public static final int KEYCODE_9 = 16; 139 /** Key code constant: '*' key. */ 140 public static final int KEYCODE_STAR = 17; 141 /** Key code constant: '#' key. */ 142 public static final int KEYCODE_POUND = 18; 143 /** Key code constant: Directional Pad Up key. 144 * May also be synthesized from trackball motions. */ 145 public static final int KEYCODE_DPAD_UP = 19; 146 /** Key code constant: Directional Pad Down key. 147 * May also be synthesized from trackball motions. */ 148 public static final int KEYCODE_DPAD_DOWN = 20; 149 /** Key code constant: Directional Pad Left key. 150 * May also be synthesized from trackball motions. */ 151 public static final int KEYCODE_DPAD_LEFT = 21; 152 /** Key code constant: Directional Pad Right key. 153 * May also be synthesized from trackball motions. */ 154 public static final int KEYCODE_DPAD_RIGHT = 22; 155 /** Key code constant: Directional Pad Center key. 156 * May also be synthesized from trackball motions. */ 157 public static final int KEYCODE_DPAD_CENTER = 23; 158 /** Key code constant: Volume Up key. 159 * Adjusts the speaker volume up. */ 160 public static final int KEYCODE_VOLUME_UP = 24; 161 /** Key code constant: Volume Down key. 162 * Adjusts the speaker volume down. */ 163 public static final int KEYCODE_VOLUME_DOWN = 25; 164 /** Key code constant: Power key. */ 165 public static final int KEYCODE_POWER = 26; 166 /** Key code constant: Camera key. 167 * Used to launch a camera application or take pictures. */ 168 public static final int KEYCODE_CAMERA = 27; 169 /** Key code constant: Clear key. */ 170 public static final int KEYCODE_CLEAR = 28; 171 /** Key code constant: 'A' key. */ 172 public static final int KEYCODE_A = 29; 173 /** Key code constant: 'B' key. */ 174 public static final int KEYCODE_B = 30; 175 /** Key code constant: 'C' key. */ 176 public static final int KEYCODE_C = 31; 177 /** Key code constant: 'D' key. */ 178 public static final int KEYCODE_D = 32; 179 /** Key code constant: 'E' key. */ 180 public static final int KEYCODE_E = 33; 181 /** Key code constant: 'F' key. */ 182 public static final int KEYCODE_F = 34; 183 /** Key code constant: 'G' key. */ 184 public static final int KEYCODE_G = 35; 185 /** Key code constant: 'H' key. */ 186 public static final int KEYCODE_H = 36; 187 /** Key code constant: 'I' key. */ 188 public static final int KEYCODE_I = 37; 189 /** Key code constant: 'J' key. */ 190 public static final int KEYCODE_J = 38; 191 /** Key code constant: 'K' key. */ 192 public static final int KEYCODE_K = 39; 193 /** Key code constant: 'L' key. */ 194 public static final int KEYCODE_L = 40; 195 /** Key code constant: 'M' key. */ 196 public static final int KEYCODE_M = 41; 197 /** Key code constant: 'N' key. */ 198 public static final int KEYCODE_N = 42; 199 /** Key code constant: 'O' key. */ 200 public static final int KEYCODE_O = 43; 201 /** Key code constant: 'P' key. */ 202 public static final int KEYCODE_P = 44; 203 /** Key code constant: 'Q' key. */ 204 public static final int KEYCODE_Q = 45; 205 /** Key code constant: 'R' key. */ 206 public static final int KEYCODE_R = 46; 207 /** Key code constant: 'S' key. */ 208 public static final int KEYCODE_S = 47; 209 /** Key code constant: 'T' key. */ 210 public static final int KEYCODE_T = 48; 211 /** Key code constant: 'U' key. */ 212 public static final int KEYCODE_U = 49; 213 /** Key code constant: 'V' key. */ 214 public static final int KEYCODE_V = 50; 215 /** Key code constant: 'W' key. */ 216 public static final int KEYCODE_W = 51; 217 /** Key code constant: 'X' key. */ 218 public static final int KEYCODE_X = 52; 219 /** Key code constant: 'Y' key. */ 220 public static final int KEYCODE_Y = 53; 221 /** Key code constant: 'Z' key. */ 222 public static final int KEYCODE_Z = 54; 223 /** Key code constant: ',' key. */ 224 public static final int KEYCODE_COMMA = 55; 225 /** Key code constant: '.' key. */ 226 public static final int KEYCODE_PERIOD = 56; 227 /** Key code constant: Left Alt modifier key. */ 228 public static final int KEYCODE_ALT_LEFT = 57; 229 /** Key code constant: Right Alt modifier key. */ 230 public static final int KEYCODE_ALT_RIGHT = 58; 231 /** Key code constant: Left Shift modifier key. */ 232 public static final int KEYCODE_SHIFT_LEFT = 59; 233 /** Key code constant: Right Shift modifier key. */ 234 public static final int KEYCODE_SHIFT_RIGHT = 60; 235 /** Key code constant: Tab key. */ 236 public static final int KEYCODE_TAB = 61; 237 /** Key code constant: Space key. */ 238 public static final int KEYCODE_SPACE = 62; 239 /** Key code constant: Symbol modifier key. 240 * Used to enter alternate symbols. */ 241 public static final int KEYCODE_SYM = 63; 242 /** Key code constant: Explorer special function key. 243 * Used to launch a browser application. */ 244 public static final int KEYCODE_EXPLORER = 64; 245 /** Key code constant: Envelope special function key. 246 * Used to launch a mail application. */ 247 public static final int KEYCODE_ENVELOPE = 65; 248 /** Key code constant: Enter key. */ 249 public static final int KEYCODE_ENTER = 66; 250 /** Key code constant: Backspace key. 251 * Deletes characters before the insertion point, unlike {@link #KEYCODE_FORWARD_DEL}. */ 252 public static final int KEYCODE_DEL = 67; 253 /** Key code constant: '`' (backtick) key. */ 254 public static final int KEYCODE_GRAVE = 68; 255 /** Key code constant: '-'. */ 256 public static final int KEYCODE_MINUS = 69; 257 /** Key code constant: '=' key. */ 258 public static final int KEYCODE_EQUALS = 70; 259 /** Key code constant: '[' key. */ 260 public static final int KEYCODE_LEFT_BRACKET = 71; 261 /** Key code constant: ']' key. */ 262 public static final int KEYCODE_RIGHT_BRACKET = 72; 263 /** Key code constant: '\' key. */ 264 public static final int KEYCODE_BACKSLASH = 73; 265 /** Key code constant: ';' key. */ 266 public static final int KEYCODE_SEMICOLON = 74; 267 /** Key code constant: ''' (apostrophe) key. */ 268 public static final int KEYCODE_APOSTROPHE = 75; 269 /** Key code constant: '/' key. */ 270 public static final int KEYCODE_SLASH = 76; 271 /** Key code constant: '@' key. */ 272 public static final int KEYCODE_AT = 77; 273 /** Key code constant: Number modifier key. 274 * Used to enter numeric symbols. 275 * This key is not Num Lock; it is more like {@link #KEYCODE_ALT_LEFT} and is 276 * interpreted as an ALT key by {@link android.text.method.MetaKeyKeyListener}. */ 277 public static final int KEYCODE_NUM = 78; 278 /** Key code constant: Headset Hook key. 279 * Used to hang up calls and stop media. */ 280 public static final int KEYCODE_HEADSETHOOK = 79; 281 /** Key code constant: Camera Focus key. 282 * Used to focus the camera. */ 283 public static final int KEYCODE_FOCUS = 80; // *Camera* focus 284 /** Key code constant: '+' key. */ 285 public static final int KEYCODE_PLUS = 81; 286 /** Key code constant: Menu key. */ 287 public static final int KEYCODE_MENU = 82; 288 /** Key code constant: Notification key. */ 289 public static final int KEYCODE_NOTIFICATION = 83; 290 /** Key code constant: Search key. */ 291 public static final int KEYCODE_SEARCH = 84; 292 /** Key code constant: Play/Pause media key. */ 293 public static final int KEYCODE_MEDIA_PLAY_PAUSE= 85; 294 /** Key code constant: Stop media key. */ 295 public static final int KEYCODE_MEDIA_STOP = 86; 296 /** Key code constant: Play Next media key. */ 297 public static final int KEYCODE_MEDIA_NEXT = 87; 298 /** Key code constant: Play Previous media key. */ 299 public static final int KEYCODE_MEDIA_PREVIOUS = 88; 300 /** Key code constant: Rewind media key. */ 301 public static final int KEYCODE_MEDIA_REWIND = 89; 302 /** Key code constant: Fast Forward media key. */ 303 public static final int KEYCODE_MEDIA_FAST_FORWARD = 90; 304 /** Key code constant: Mute key. 305 * Mute key for the microphone (unlike {@link #KEYCODE_VOLUME_MUTE}, which is the speaker mute 306 * key). */ 307 public static final int KEYCODE_MUTE = 91; 308 /** Key code constant: Page Up key. */ 309 public static final int KEYCODE_PAGE_UP = 92; 310 /** Key code constant: Page Down key. */ 311 public static final int KEYCODE_PAGE_DOWN = 93; 312 /** Key code constant: Picture Symbols modifier key. 313 * Used to switch symbol sets (Emoji, Kao-moji). */ 314 public static final int KEYCODE_PICTSYMBOLS = 94; // switch symbol-sets (Emoji,Kao-moji) 315 /** Key code constant: Switch Charset modifier key. 316 * Used to switch character sets (Kanji, Katakana). */ 317 public static final int KEYCODE_SWITCH_CHARSET = 95; // switch char-sets (Kanji,Katakana) 318 /** Key code constant: A Button key. 319 * On a game controller, the A button should be either the button labeled A 320 * or the first button on the bottom row of controller buttons. */ 321 public static final int KEYCODE_BUTTON_A = 96; 322 /** Key code constant: B Button key. 323 * On a game controller, the B button should be either the button labeled B 324 * or the second button on the bottom row of controller buttons. */ 325 public static final int KEYCODE_BUTTON_B = 97; 326 /** Key code constant: C Button key. 327 * On a game controller, the C button should be either the button labeled C 328 * or the third button on the bottom row of controller buttons. */ 329 public static final int KEYCODE_BUTTON_C = 98; 330 /** Key code constant: X Button key. 331 * On a game controller, the X button should be either the button labeled X 332 * or the first button on the upper row of controller buttons. */ 333 public static final int KEYCODE_BUTTON_X = 99; 334 /** Key code constant: Y Button key. 335 * On a game controller, the Y button should be either the button labeled Y 336 * or the second button on the upper row of controller buttons. */ 337 public static final int KEYCODE_BUTTON_Y = 100; 338 /** Key code constant: Z Button key. 339 * On a game controller, the Z button should be either the button labeled Z 340 * or the third button on the upper row of controller buttons. */ 341 public static final int KEYCODE_BUTTON_Z = 101; 342 /** Key code constant: L1 Button key. 343 * On a game controller, the L1 button should be either the button labeled L1 (or L) 344 * or the top left trigger button. */ 345 public static final int KEYCODE_BUTTON_L1 = 102; 346 /** Key code constant: R1 Button key. 347 * On a game controller, the R1 button should be either the button labeled R1 (or R) 348 * or the top right trigger button. */ 349 public static final int KEYCODE_BUTTON_R1 = 103; 350 /** Key code constant: L2 Button key. 351 * On a game controller, the L2 button should be either the button labeled L2 352 * or the bottom left trigger button. */ 353 public static final int KEYCODE_BUTTON_L2 = 104; 354 /** Key code constant: R2 Button key. 355 * On a game controller, the R2 button should be either the button labeled R2 356 * or the bottom right trigger button. */ 357 public static final int KEYCODE_BUTTON_R2 = 105; 358 /** Key code constant: Left Thumb Button key. 359 * On a game controller, the left thumb button indicates that the left (or only) 360 * joystick is pressed. */ 361 public static final int KEYCODE_BUTTON_THUMBL = 106; 362 /** Key code constant: Right Thumb Button key. 363 * On a game controller, the right thumb button indicates that the right 364 * joystick is pressed. */ 365 public static final int KEYCODE_BUTTON_THUMBR = 107; 366 /** Key code constant: Start Button key. 367 * On a game controller, the button labeled Start. */ 368 public static final int KEYCODE_BUTTON_START = 108; 369 /** Key code constant: Select Button key. 370 * On a game controller, the button labeled Select. */ 371 public static final int KEYCODE_BUTTON_SELECT = 109; 372 /** Key code constant: Mode Button key. 373 * On a game controller, the button labeled Mode. */ 374 public static final int KEYCODE_BUTTON_MODE = 110; 375 /** Key code constant: Escape key. */ 376 public static final int KEYCODE_ESCAPE = 111; 377 /** Key code constant: Forward Delete key. 378 * Deletes characters ahead of the insertion point, unlike {@link #KEYCODE_DEL}. */ 379 public static final int KEYCODE_FORWARD_DEL = 112; 380 /** Key code constant: Left Control modifier key. */ 381 public static final int KEYCODE_CTRL_LEFT = 113; 382 /** Key code constant: Right Control modifier key. */ 383 public static final int KEYCODE_CTRL_RIGHT = 114; 384 /** Key code constant: Caps Lock key. */ 385 public static final int KEYCODE_CAPS_LOCK = 115; 386 /** Key code constant: Scroll Lock key. */ 387 public static final int KEYCODE_SCROLL_LOCK = 116; 388 /** Key code constant: Left Meta modifier key. */ 389 public static final int KEYCODE_META_LEFT = 117; 390 /** Key code constant: Right Meta modifier key. */ 391 public static final int KEYCODE_META_RIGHT = 118; 392 /** Key code constant: Function modifier key. */ 393 public static final int KEYCODE_FUNCTION = 119; 394 /** 395 * Key code constant: System Request / Print Screen key. 396 * 397 * This key is sent to the app first and only if the app doesn't handle it, the framework 398 * handles it (to take a screenshot), unlike {@code KEYCODE_TAKE_SCREENSHOT} which is 399 * fully handled by the framework. 400 */ 401 public static final int KEYCODE_SYSRQ = 120; 402 /** Key code constant: Break / Pause key. */ 403 public static final int KEYCODE_BREAK = 121; 404 /** Key code constant: Home Movement key. 405 * Used for scrolling or moving the cursor around to the start of a line 406 * or to the top of a list. */ 407 public static final int KEYCODE_MOVE_HOME = 122; 408 /** Key code constant: End Movement key. 409 * Used for scrolling or moving the cursor around to the end of a line 410 * or to the bottom of a list. */ 411 public static final int KEYCODE_MOVE_END = 123; 412 /** Key code constant: Insert key. 413 * Toggles insert / overwrite edit mode. */ 414 public static final int KEYCODE_INSERT = 124; 415 /** Key code constant: Forward key. 416 * Navigates forward in the history stack. Complement of {@link #KEYCODE_BACK}. */ 417 public static final int KEYCODE_FORWARD = 125; 418 /** Key code constant: Play media key. */ 419 public static final int KEYCODE_MEDIA_PLAY = 126; 420 /** Key code constant: Pause media key. */ 421 public static final int KEYCODE_MEDIA_PAUSE = 127; 422 /** Key code constant: Close media key. 423 * May be used to close a CD tray, for example. */ 424 public static final int KEYCODE_MEDIA_CLOSE = 128; 425 /** Key code constant: Eject media key. 426 * May be used to eject a CD tray, for example. */ 427 public static final int KEYCODE_MEDIA_EJECT = 129; 428 /** Key code constant: Record media key. */ 429 public static final int KEYCODE_MEDIA_RECORD = 130; 430 /** Key code constant: F1 key. */ 431 public static final int KEYCODE_F1 = 131; 432 /** Key code constant: F2 key. */ 433 public static final int KEYCODE_F2 = 132; 434 /** Key code constant: F3 key. */ 435 public static final int KEYCODE_F3 = 133; 436 /** Key code constant: F4 key. */ 437 public static final int KEYCODE_F4 = 134; 438 /** Key code constant: F5 key. */ 439 public static final int KEYCODE_F5 = 135; 440 /** Key code constant: F6 key. */ 441 public static final int KEYCODE_F6 = 136; 442 /** Key code constant: F7 key. */ 443 public static final int KEYCODE_F7 = 137; 444 /** Key code constant: F8 key. */ 445 public static final int KEYCODE_F8 = 138; 446 /** Key code constant: F9 key. */ 447 public static final int KEYCODE_F9 = 139; 448 /** Key code constant: F10 key. */ 449 public static final int KEYCODE_F10 = 140; 450 /** Key code constant: F11 key. */ 451 public static final int KEYCODE_F11 = 141; 452 /** Key code constant: F12 key. */ 453 public static final int KEYCODE_F12 = 142; 454 /** Key code constant: Num Lock key. 455 * This is the Num Lock key; it is different from {@link #KEYCODE_NUM}. 456 * This key alters the behavior of other keys on the numeric keypad. */ 457 public static final int KEYCODE_NUM_LOCK = 143; 458 /** Key code constant: Numeric keypad '0' key. */ 459 public static final int KEYCODE_NUMPAD_0 = 144; 460 /** Key code constant: Numeric keypad '1' key. */ 461 public static final int KEYCODE_NUMPAD_1 = 145; 462 /** Key code constant: Numeric keypad '2' key. */ 463 public static final int KEYCODE_NUMPAD_2 = 146; 464 /** Key code constant: Numeric keypad '3' key. */ 465 public static final int KEYCODE_NUMPAD_3 = 147; 466 /** Key code constant: Numeric keypad '4' key. */ 467 public static final int KEYCODE_NUMPAD_4 = 148; 468 /** Key code constant: Numeric keypad '5' key. */ 469 public static final int KEYCODE_NUMPAD_5 = 149; 470 /** Key code constant: Numeric keypad '6' key. */ 471 public static final int KEYCODE_NUMPAD_6 = 150; 472 /** Key code constant: Numeric keypad '7' key. */ 473 public static final int KEYCODE_NUMPAD_7 = 151; 474 /** Key code constant: Numeric keypad '8' key. */ 475 public static final int KEYCODE_NUMPAD_8 = 152; 476 /** Key code constant: Numeric keypad '9' key. */ 477 public static final int KEYCODE_NUMPAD_9 = 153; 478 /** Key code constant: Numeric keypad '/' key (for division). */ 479 public static final int KEYCODE_NUMPAD_DIVIDE = 154; 480 /** Key code constant: Numeric keypad '*' key (for multiplication). */ 481 public static final int KEYCODE_NUMPAD_MULTIPLY = 155; 482 /** Key code constant: Numeric keypad '-' key (for subtraction). */ 483 public static final int KEYCODE_NUMPAD_SUBTRACT = 156; 484 /** Key code constant: Numeric keypad '+' key (for addition). */ 485 public static final int KEYCODE_NUMPAD_ADD = 157; 486 /** Key code constant: Numeric keypad '.' key (for decimals or digit grouping). */ 487 public static final int KEYCODE_NUMPAD_DOT = 158; 488 /** Key code constant: Numeric keypad ',' key (for decimals or digit grouping). */ 489 public static final int KEYCODE_NUMPAD_COMMA = 159; 490 /** Key code constant: Numeric keypad Enter key. */ 491 public static final int KEYCODE_NUMPAD_ENTER = 160; 492 /** Key code constant: Numeric keypad '=' key. */ 493 public static final int KEYCODE_NUMPAD_EQUALS = 161; 494 /** Key code constant: Numeric keypad '(' key. */ 495 public static final int KEYCODE_NUMPAD_LEFT_PAREN = 162; 496 /** Key code constant: Numeric keypad ')' key. */ 497 public static final int KEYCODE_NUMPAD_RIGHT_PAREN = 163; 498 /** Key code constant: Volume Mute key. 499 * Mute key for speaker (unlike {@link #KEYCODE_MUTE}, which is the mute key for the 500 * microphone). This key should normally be implemented as a toggle such that the first press 501 * mutes the speaker and the second press restores the original volume. 502 */ 503 public static final int KEYCODE_VOLUME_MUTE = 164; 504 /** Key code constant: Info key. 505 * Common on TV remotes to show additional information related to what is 506 * currently being viewed. */ 507 public static final int KEYCODE_INFO = 165; 508 /** Key code constant: Channel up key. 509 * On TV remotes, increments the television channel. */ 510 public static final int KEYCODE_CHANNEL_UP = 166; 511 /** Key code constant: Channel down key. 512 * On TV remotes, decrements the television channel. */ 513 public static final int KEYCODE_CHANNEL_DOWN = 167; 514 /** Key code constant: Zoom in key. */ 515 public static final int KEYCODE_ZOOM_IN = 168; 516 /** Key code constant: Zoom out key. */ 517 public static final int KEYCODE_ZOOM_OUT = 169; 518 /** Key code constant: TV key. 519 * On TV remotes, switches to viewing live TV. */ 520 public static final int KEYCODE_TV = 170; 521 /** Key code constant: Window key. 522 * On TV remotes, toggles picture-in-picture mode or other windowing functions. 523 * On Android Wear devices, triggers a display offset. */ 524 public static final int KEYCODE_WINDOW = 171; 525 /** Key code constant: Guide key. 526 * On TV remotes, shows a programming guide. */ 527 public static final int KEYCODE_GUIDE = 172; 528 /** Key code constant: DVR key. 529 * On some TV remotes, switches to a DVR mode for recorded shows. */ 530 public static final int KEYCODE_DVR = 173; 531 /** Key code constant: Bookmark key. 532 * On some TV remotes, bookmarks content or web pages. */ 533 public static final int KEYCODE_BOOKMARK = 174; 534 /** Key code constant: Toggle captions key. 535 * Switches the mode for closed-captioning text, for example during television shows. */ 536 public static final int KEYCODE_CAPTIONS = 175; 537 /** Key code constant: Settings key. 538 * Starts the system settings activity. */ 539 public static final int KEYCODE_SETTINGS = 176; 540 /** 541 * Key code constant: TV power key. 542 * On HDMI TV panel devices and Android TV devices that don't support HDMI, toggles the power 543 * state of the device. 544 * On HDMI source devices, toggles the power state of the HDMI-connected TV via HDMI-CEC and 545 * makes the source device follow this power state. 546 */ 547 public static final int KEYCODE_TV_POWER = 177; 548 /** Key code constant: TV input key. 549 * On TV remotes, switches the input on a television screen. */ 550 public static final int KEYCODE_TV_INPUT = 178; 551 /** Key code constant: Set-top-box power key. 552 * On TV remotes, toggles the power on an external Set-top-box. */ 553 public static final int KEYCODE_STB_POWER = 179; 554 /** Key code constant: Set-top-box input key. 555 * On TV remotes, switches the input mode on an external Set-top-box. */ 556 public static final int KEYCODE_STB_INPUT = 180; 557 /** Key code constant: A/V Receiver power key. 558 * On TV remotes, toggles the power on an external A/V Receiver. */ 559 public static final int KEYCODE_AVR_POWER = 181; 560 /** Key code constant: A/V Receiver input key. 561 * On TV remotes, switches the input mode on an external A/V Receiver. */ 562 public static final int KEYCODE_AVR_INPUT = 182; 563 /** Key code constant: Red "programmable" key. 564 * On TV remotes, acts as a contextual/programmable key. */ 565 public static final int KEYCODE_PROG_RED = 183; 566 /** Key code constant: Green "programmable" key. 567 * On TV remotes, actsas a contextual/programmable key. */ 568 public static final int KEYCODE_PROG_GREEN = 184; 569 /** Key code constant: Yellow "programmable" key. 570 * On TV remotes, acts as a contextual/programmable key. */ 571 public static final int KEYCODE_PROG_YELLOW = 185; 572 /** Key code constant: Blue "programmable" key. 573 * On TV remotes, acts as a contextual/programmable key. */ 574 public static final int KEYCODE_PROG_BLUE = 186; 575 /** Key code constant: App switch key. 576 * Should bring up the application switcher dialog. */ 577 public static final int KEYCODE_APP_SWITCH = 187; 578 /** Key code constant: Generic Game Pad Button #1.*/ 579 public static final int KEYCODE_BUTTON_1 = 188; 580 /** Key code constant: Generic Game Pad Button #2.*/ 581 public static final int KEYCODE_BUTTON_2 = 189; 582 /** Key code constant: Generic Game Pad Button #3.*/ 583 public static final int KEYCODE_BUTTON_3 = 190; 584 /** Key code constant: Generic Game Pad Button #4.*/ 585 public static final int KEYCODE_BUTTON_4 = 191; 586 /** Key code constant: Generic Game Pad Button #5.*/ 587 public static final int KEYCODE_BUTTON_5 = 192; 588 /** Key code constant: Generic Game Pad Button #6.*/ 589 public static final int KEYCODE_BUTTON_6 = 193; 590 /** Key code constant: Generic Game Pad Button #7.*/ 591 public static final int KEYCODE_BUTTON_7 = 194; 592 /** Key code constant: Generic Game Pad Button #8.*/ 593 public static final int KEYCODE_BUTTON_8 = 195; 594 /** Key code constant: Generic Game Pad Button #9.*/ 595 public static final int KEYCODE_BUTTON_9 = 196; 596 /** Key code constant: Generic Game Pad Button #10.*/ 597 public static final int KEYCODE_BUTTON_10 = 197; 598 /** Key code constant: Generic Game Pad Button #11.*/ 599 public static final int KEYCODE_BUTTON_11 = 198; 600 /** Key code constant: Generic Game Pad Button #12.*/ 601 public static final int KEYCODE_BUTTON_12 = 199; 602 /** Key code constant: Generic Game Pad Button #13.*/ 603 public static final int KEYCODE_BUTTON_13 = 200; 604 /** Key code constant: Generic Game Pad Button #14.*/ 605 public static final int KEYCODE_BUTTON_14 = 201; 606 /** Key code constant: Generic Game Pad Button #15.*/ 607 public static final int KEYCODE_BUTTON_15 = 202; 608 /** Key code constant: Generic Game Pad Button #16.*/ 609 public static final int KEYCODE_BUTTON_16 = 203; 610 /** Key code constant: Language Switch key. 611 * Toggles the current input language such as switching between English and Japanese on 612 * a QWERTY keyboard. On some devices, the same function may be performed by 613 * pressing Shift+Spacebar. */ 614 public static final int KEYCODE_LANGUAGE_SWITCH = 204; 615 /** Key code constant: Manner Mode key. 616 * Toggles silent or vibrate mode on and off to make the device behave more politely 617 * in certain settings such as on a crowded train. On some devices, the key may only 618 * operate when long-pressed. */ 619 public static final int KEYCODE_MANNER_MODE = 205; 620 /** Key code constant: 3D Mode key. 621 * Toggles the display between 2D and 3D mode. */ 622 public static final int KEYCODE_3D_MODE = 206; 623 /** Key code constant: Contacts special function key. 624 * Used to launch an address book application. */ 625 public static final int KEYCODE_CONTACTS = 207; 626 /** Key code constant: Calendar special function key. 627 * Used to launch a calendar application. */ 628 public static final int KEYCODE_CALENDAR = 208; 629 /** Key code constant: Music special function key. 630 * Used to launch a music player application. */ 631 public static final int KEYCODE_MUSIC = 209; 632 /** Key code constant: Calculator special function key. 633 * Used to launch a calculator application. */ 634 public static final int KEYCODE_CALCULATOR = 210; 635 /** Key code constant: Japanese full-width / half-width key. */ 636 public static final int KEYCODE_ZENKAKU_HANKAKU = 211; 637 /** Key code constant: Japanese alphanumeric key. */ 638 public static final int KEYCODE_EISU = 212; 639 /** Key code constant: Japanese non-conversion key. */ 640 public static final int KEYCODE_MUHENKAN = 213; 641 /** Key code constant: Japanese conversion key. */ 642 public static final int KEYCODE_HENKAN = 214; 643 /** Key code constant: Japanese katakana / hiragana key. */ 644 public static final int KEYCODE_KATAKANA_HIRAGANA = 215; 645 /** Key code constant: Japanese Yen key. */ 646 public static final int KEYCODE_YEN = 216; 647 /** Key code constant: Japanese Ro key. */ 648 public static final int KEYCODE_RO = 217; 649 /** Key code constant: Japanese kana key. */ 650 public static final int KEYCODE_KANA = 218; 651 /** Key code constant: Assist key. 652 * Launches the global assist activity. Not delivered to applications. */ 653 public static final int KEYCODE_ASSIST = 219; 654 /** Key code constant: Brightness Down key. 655 * Adjusts the screen brightness down. */ 656 public static final int KEYCODE_BRIGHTNESS_DOWN = 220; 657 /** Key code constant: Brightness Up key. 658 * Adjusts the screen brightness up. */ 659 public static final int KEYCODE_BRIGHTNESS_UP = 221; 660 /** Key code constant: Audio Track key. 661 * Switches the audio tracks. */ 662 public static final int KEYCODE_MEDIA_AUDIO_TRACK = 222; 663 /** Key code constant: Sleep key. 664 * Puts the device to sleep. Behaves somewhat like {@link #KEYCODE_POWER} but it 665 * has no effect if the device is already asleep. */ 666 public static final int KEYCODE_SLEEP = 223; 667 /** Key code constant: Wakeup key. 668 * Wakes up the device. Behaves somewhat like {@link #KEYCODE_POWER} but it 669 * has no effect if the device is already awake. */ 670 public static final int KEYCODE_WAKEUP = 224; 671 /** Key code constant: Pairing key. 672 * Initiates peripheral pairing mode. Useful for pairing remote control 673 * devices or game controllers, especially if no other input mode is 674 * available. */ 675 public static final int KEYCODE_PAIRING = 225; 676 /** Key code constant: Media Top Menu key. 677 * Goes to the top of media menu. */ 678 public static final int KEYCODE_MEDIA_TOP_MENU = 226; 679 /** Key code constant: '11' key. */ 680 public static final int KEYCODE_11 = 227; 681 /** Key code constant: '12' key. */ 682 public static final int KEYCODE_12 = 228; 683 /** Key code constant: Last Channel key. 684 * Goes to the last viewed channel. */ 685 public static final int KEYCODE_LAST_CHANNEL = 229; 686 /** Key code constant: TV data service key. 687 * Displays data services like weather, sports. */ 688 public static final int KEYCODE_TV_DATA_SERVICE = 230; 689 /** Key code constant: Voice Assist key. 690 * Launches the global voice assist activity. Not delivered to applications. */ 691 public static final int KEYCODE_VOICE_ASSIST = 231; 692 /** Key code constant: Radio key. 693 * Toggles TV service / Radio service. */ 694 public static final int KEYCODE_TV_RADIO_SERVICE = 232; 695 /** Key code constant: Teletext key. 696 * Displays Teletext service. */ 697 public static final int KEYCODE_TV_TELETEXT = 233; 698 /** Key code constant: Number entry key. 699 * Initiates to enter multi-digit channel nubmber when each digit key is assigned 700 * for selecting separate channel. Corresponds to Number Entry Mode (0x1D) of CEC 701 * User Control Code. */ 702 public static final int KEYCODE_TV_NUMBER_ENTRY = 234; 703 /** Key code constant: Analog Terrestrial key. 704 * Switches to analog terrestrial broadcast service. */ 705 public static final int KEYCODE_TV_TERRESTRIAL_ANALOG = 235; 706 /** Key code constant: Digital Terrestrial key. 707 * Switches to digital terrestrial broadcast service. */ 708 public static final int KEYCODE_TV_TERRESTRIAL_DIGITAL = 236; 709 /** Key code constant: Satellite key. 710 * Switches to digital satellite broadcast service. */ 711 public static final int KEYCODE_TV_SATELLITE = 237; 712 /** Key code constant: BS key. 713 * Switches to BS digital satellite broadcasting service available in Japan. */ 714 public static final int KEYCODE_TV_SATELLITE_BS = 238; 715 /** Key code constant: CS key. 716 * Switches to CS digital satellite broadcasting service available in Japan. */ 717 public static final int KEYCODE_TV_SATELLITE_CS = 239; 718 /** Key code constant: BS/CS key. 719 * Toggles between BS and CS digital satellite services. */ 720 public static final int KEYCODE_TV_SATELLITE_SERVICE = 240; 721 /** Key code constant: Toggle Network key. 722 * Toggles selecting broacast services. */ 723 public static final int KEYCODE_TV_NETWORK = 241; 724 /** Key code constant: Antenna/Cable key. 725 * Toggles broadcast input source between antenna and cable. */ 726 public static final int KEYCODE_TV_ANTENNA_CABLE = 242; 727 /** Key code constant: HDMI #1 key. 728 * Switches to HDMI input #1. */ 729 public static final int KEYCODE_TV_INPUT_HDMI_1 = 243; 730 /** Key code constant: HDMI #2 key. 731 * Switches to HDMI input #2. */ 732 public static final int KEYCODE_TV_INPUT_HDMI_2 = 244; 733 /** Key code constant: HDMI #3 key. 734 * Switches to HDMI input #3. */ 735 public static final int KEYCODE_TV_INPUT_HDMI_3 = 245; 736 /** Key code constant: HDMI #4 key. 737 * Switches to HDMI input #4. */ 738 public static final int KEYCODE_TV_INPUT_HDMI_4 = 246; 739 /** Key code constant: Composite #1 key. 740 * Switches to composite video input #1. */ 741 public static final int KEYCODE_TV_INPUT_COMPOSITE_1 = 247; 742 /** Key code constant: Composite #2 key. 743 * Switches to composite video input #2. */ 744 public static final int KEYCODE_TV_INPUT_COMPOSITE_2 = 248; 745 /** Key code constant: Component #1 key. 746 * Switches to component video input #1. */ 747 public static final int KEYCODE_TV_INPUT_COMPONENT_1 = 249; 748 /** Key code constant: Component #2 key. 749 * Switches to component video input #2. */ 750 public static final int KEYCODE_TV_INPUT_COMPONENT_2 = 250; 751 /** Key code constant: VGA #1 key. 752 * Switches to VGA (analog RGB) input #1. */ 753 public static final int KEYCODE_TV_INPUT_VGA_1 = 251; 754 /** Key code constant: Audio description key. 755 * Toggles audio description off / on. */ 756 public static final int KEYCODE_TV_AUDIO_DESCRIPTION = 252; 757 /** Key code constant: Audio description mixing volume up key. 758 * Louden audio description volume as compared with normal audio volume. */ 759 public static final int KEYCODE_TV_AUDIO_DESCRIPTION_MIX_UP = 253; 760 /** Key code constant: Audio description mixing volume down key. 761 * Lessen audio description volume as compared with normal audio volume. */ 762 public static final int KEYCODE_TV_AUDIO_DESCRIPTION_MIX_DOWN = 254; 763 /** Key code constant: Zoom mode key. 764 * Changes Zoom mode (Normal, Full, Zoom, Wide-zoom, etc.) */ 765 public static final int KEYCODE_TV_ZOOM_MODE = 255; 766 /** Key code constant: Contents menu key. 767 * Goes to the title list. Corresponds to Contents Menu (0x0B) of CEC User Control 768 * Code */ 769 public static final int KEYCODE_TV_CONTENTS_MENU = 256; 770 /** Key code constant: Media context menu key. 771 * Goes to the context menu of media contents. Corresponds to Media Context-sensitive 772 * Menu (0x11) of CEC User Control Code. */ 773 public static final int KEYCODE_TV_MEDIA_CONTEXT_MENU = 257; 774 /** Key code constant: Timer programming key. 775 * Goes to the timer recording menu. Corresponds to Timer Programming (0x54) of 776 * CEC User Control Code. */ 777 public static final int KEYCODE_TV_TIMER_PROGRAMMING = 258; 778 /** Key code constant: Help key. */ 779 public static final int KEYCODE_HELP = 259; 780 /** Key code constant: Navigate to previous key. 781 * Goes backward by one item in an ordered collection of items. */ 782 public static final int KEYCODE_NAVIGATE_PREVIOUS = 260; 783 /** Key code constant: Navigate to next key. 784 * Advances to the next item in an ordered collection of items. */ 785 public static final int KEYCODE_NAVIGATE_NEXT = 261; 786 /** Key code constant: Navigate in key. 787 * Activates the item that currently has focus or expands to the next level of a navigation 788 * hierarchy. */ 789 public static final int KEYCODE_NAVIGATE_IN = 262; 790 /** Key code constant: Navigate out key. 791 * Backs out one level of a navigation hierarchy or collapses the item that currently has 792 * focus. */ 793 public static final int KEYCODE_NAVIGATE_OUT = 263; 794 /** Key code constant: Primary stem key for Wear 795 * Main power/reset button on watch. */ 796 public static final int KEYCODE_STEM_PRIMARY = 264; 797 /** Key code constant: Generic stem key 1 for Wear */ 798 public static final int KEYCODE_STEM_1 = 265; 799 /** Key code constant: Generic stem key 2 for Wear */ 800 public static final int KEYCODE_STEM_2 = 266; 801 /** Key code constant: Generic stem key 3 for Wear */ 802 public static final int KEYCODE_STEM_3 = 267; 803 /** Key code constant: Directional Pad Up-Left */ 804 public static final int KEYCODE_DPAD_UP_LEFT = 268; 805 /** Key code constant: Directional Pad Down-Left */ 806 public static final int KEYCODE_DPAD_DOWN_LEFT = 269; 807 /** Key code constant: Directional Pad Up-Right */ 808 public static final int KEYCODE_DPAD_UP_RIGHT = 270; 809 /** Key code constant: Directional Pad Down-Right */ 810 public static final int KEYCODE_DPAD_DOWN_RIGHT = 271; 811 /** Key code constant: Skip forward media key. */ 812 public static final int KEYCODE_MEDIA_SKIP_FORWARD = 272; 813 /** Key code constant: Skip backward media key. */ 814 public static final int KEYCODE_MEDIA_SKIP_BACKWARD = 273; 815 /** Key code constant: Step forward media key. 816 * Steps media forward, one frame at a time. */ 817 public static final int KEYCODE_MEDIA_STEP_FORWARD = 274; 818 /** Key code constant: Step backward media key. 819 * Steps media backward, one frame at a time. */ 820 public static final int KEYCODE_MEDIA_STEP_BACKWARD = 275; 821 /** Key code constant: put device to sleep unless a wakelock is held. */ 822 public static final int KEYCODE_SOFT_SLEEP = 276; 823 /** Key code constant: Cut key. */ 824 public static final int KEYCODE_CUT = 277; 825 /** Key code constant: Copy key. */ 826 public static final int KEYCODE_COPY = 278; 827 /** Key code constant: Paste key. */ 828 public static final int KEYCODE_PASTE = 279; 829 /** Key code constant: Consumed by the system for navigation up */ 830 public static final int KEYCODE_SYSTEM_NAVIGATION_UP = 280; 831 /** Key code constant: Consumed by the system for navigation down */ 832 public static final int KEYCODE_SYSTEM_NAVIGATION_DOWN = 281; 833 /** Key code constant: Consumed by the system for navigation left*/ 834 public static final int KEYCODE_SYSTEM_NAVIGATION_LEFT = 282; 835 /** Key code constant: Consumed by the system for navigation right */ 836 public static final int KEYCODE_SYSTEM_NAVIGATION_RIGHT = 283; 837 /** Key code constant: Show all apps */ 838 public static final int KEYCODE_ALL_APPS = 284; 839 /** Key code constant: Refresh key. */ 840 public static final int KEYCODE_REFRESH = 285; 841 /** Key code constant: Thumbs up key. Apps can use this to let user upvote content. */ 842 public static final int KEYCODE_THUMBS_UP = 286; 843 /** Key code constant: Thumbs down key. Apps can use this to let user downvote content. */ 844 public static final int KEYCODE_THUMBS_DOWN = 287; 845 /** 846 * Key code constant: Used to switch current {@link android.accounts.Account} that is 847 * consuming content. May be consumed by system to set account globally. 848 */ 849 public static final int KEYCODE_PROFILE_SWITCH = 288; 850 /** Key code constant: Video Application key #1. */ 851 public static final int KEYCODE_VIDEO_APP_1 = 289; 852 /** Key code constant: Video Application key #2. */ 853 public static final int KEYCODE_VIDEO_APP_2 = 290; 854 /** Key code constant: Video Application key #3. */ 855 public static final int KEYCODE_VIDEO_APP_3 = 291; 856 /** Key code constant: Video Application key #4. */ 857 public static final int KEYCODE_VIDEO_APP_4 = 292; 858 /** Key code constant: Video Application key #5. */ 859 public static final int KEYCODE_VIDEO_APP_5 = 293; 860 /** Key code constant: Video Application key #6. */ 861 public static final int KEYCODE_VIDEO_APP_6 = 294; 862 /** Key code constant: Video Application key #7. */ 863 public static final int KEYCODE_VIDEO_APP_7 = 295; 864 /** Key code constant: Video Application key #8. */ 865 public static final int KEYCODE_VIDEO_APP_8 = 296; 866 /** Key code constant: Featured Application key #1. */ 867 public static final int KEYCODE_FEATURED_APP_1 = 297; 868 /** Key code constant: Featured Application key #2. */ 869 public static final int KEYCODE_FEATURED_APP_2 = 298; 870 /** Key code constant: Featured Application key #3. */ 871 public static final int KEYCODE_FEATURED_APP_3 = 299; 872 /** Key code constant: Featured Application key #4. */ 873 public static final int KEYCODE_FEATURED_APP_4 = 300; 874 /** Key code constant: Demo Application key #1. */ 875 public static final int KEYCODE_DEMO_APP_1 = 301; 876 /** Key code constant: Demo Application key #2. */ 877 public static final int KEYCODE_DEMO_APP_2 = 302; 878 /** Key code constant: Demo Application key #3. */ 879 public static final int KEYCODE_DEMO_APP_3 = 303; 880 /** Key code constant: Demo Application key #4. */ 881 public static final int KEYCODE_DEMO_APP_4 = 304; 882 /** Key code constant: Keyboard backlight down */ 883 public static final int KEYCODE_KEYBOARD_BACKLIGHT_DOWN = 305; 884 /** Key code constant: Keyboard backlight up */ 885 public static final int KEYCODE_KEYBOARD_BACKLIGHT_UP = 306; 886 /** Key code constant: Keyboard backlight toggle */ 887 public static final int KEYCODE_KEYBOARD_BACKLIGHT_TOGGLE = 307; 888 /** 889 * Key code constant: The primary button on the barrel of a stylus. 890 * This is usually the button closest to the tip of the stylus. 891 */ 892 public static final int KEYCODE_STYLUS_BUTTON_PRIMARY = 308; 893 /** 894 * Key code constant: The secondary button on the barrel of a stylus. 895 * This is usually the second button from the tip of the stylus. 896 */ 897 public static final int KEYCODE_STYLUS_BUTTON_SECONDARY = 309; 898 /** 899 * Key code constant: The tertiary button on the barrel of a stylus. 900 * This is usually the third button from the tip of the stylus. 901 */ 902 public static final int KEYCODE_STYLUS_BUTTON_TERTIARY = 310; 903 /** 904 * Key code constant: A button on the tail end of a stylus. 905 * The use of this button does not usually correspond to the function of an eraser. 906 */ 907 public static final int KEYCODE_STYLUS_BUTTON_TAIL = 311; 908 /** 909 * Key code constant: To open recent apps view (a.k.a. Overview). 910 * This key is handled by the framework and is never delivered to applications. 911 */ 912 public static final int KEYCODE_RECENT_APPS = 312; 913 /** 914 * Key code constant: A button whose usage can be customized by the user through 915 * the system. 916 * User customizable key #1. 917 */ 918 public static final int KEYCODE_MACRO_1 = 313; 919 /** 920 * Key code constant: A button whose usage can be customized by the user through 921 * the system. 922 * User customizable key #2. 923 */ 924 public static final int KEYCODE_MACRO_2 = 314; 925 /** 926 * Key code constant: A button whose usage can be customized by the user through 927 * the system. 928 * User customizable key #3. 929 */ 930 public static final int KEYCODE_MACRO_3 = 315; 931 /** 932 * Key code constant: A button whose usage can be customized by the user through 933 * the system. 934 * User customizable key #4. 935 */ 936 public static final int KEYCODE_MACRO_4 = 316; 937 /** Key code constant: To open emoji picker */ 938 public static final int KEYCODE_EMOJI_PICKER = 317; 939 /** 940 * Key code constant: To take a screenshot 941 * 942 * This key is fully handled by the framework and will not be sent to the foreground app, 943 * unlike {@code KEYCODE_SYSRQ} which is sent to the app first and only if the app 944 * doesn't handle it, the framework handles it (to take a screenshot). 945 */ 946 public static final int KEYCODE_SCREENSHOT = 318; 947 /** Key code constant: To start dictate to an input field */ 948 @FlaggedApi(Flags.FLAG_ENABLE_NEW_25Q2_KEYCODES) 949 public static final int KEYCODE_DICTATE = 319; 950 /** 951 * Key code constant: AC New 952 * 953 * e.g. To create a new instance of a window, open a new tab, etc. 954 */ 955 @FlaggedApi(Flags.FLAG_ENABLE_NEW_25Q2_KEYCODES) 956 public static final int KEYCODE_NEW = 320; 957 /** 958 * Key code constant: AC Close 959 * 960 * e.g. To close current instance of the application window, close the current tab, etc. 961 */ 962 @FlaggedApi(Flags.FLAG_ENABLE_NEW_25Q2_KEYCODES) 963 public static final int KEYCODE_CLOSE = 321; 964 /** Key code constant: To toggle 'Do Not Disturb' mode */ 965 @FlaggedApi(Flags.FLAG_ENABLE_NEW_25Q2_KEYCODES) 966 public static final int KEYCODE_DO_NOT_DISTURB = 322; 967 /** Key code constant: To Print */ 968 @FlaggedApi(Flags.FLAG_ENABLE_NEW_25Q2_KEYCODES) 969 public static final int KEYCODE_PRINT = 323; 970 /** Key code constant: To Lock the screen */ 971 @FlaggedApi(Flags.FLAG_ENABLE_NEW_25Q2_KEYCODES) 972 public static final int KEYCODE_LOCK = 324; 973 /** Key code constant: To toggle fullscreen mode (on the current application) */ 974 @FlaggedApi(Flags.FLAG_ENABLE_NEW_25Q2_KEYCODES) 975 public static final int KEYCODE_FULLSCREEN = 325; 976 /** Key code constant: F13 key. */ 977 @FlaggedApi(Flags.FLAG_ENABLE_NEW_25Q2_KEYCODES) 978 public static final int KEYCODE_F13 = 326; 979 /** Key code constant: F14 key. */ 980 @FlaggedApi(Flags.FLAG_ENABLE_NEW_25Q2_KEYCODES) 981 public static final int KEYCODE_F14 = 327; 982 /** Key code constant: F15 key. */ 983 @FlaggedApi(Flags.FLAG_ENABLE_NEW_25Q2_KEYCODES) 984 public static final int KEYCODE_F15 = 328; 985 /** Key code constant: F16 key. */ 986 @FlaggedApi(Flags.FLAG_ENABLE_NEW_25Q2_KEYCODES) 987 public static final int KEYCODE_F16 = 329; 988 /** Key code constant: F17 key. */ 989 @FlaggedApi(Flags.FLAG_ENABLE_NEW_25Q2_KEYCODES) 990 public static final int KEYCODE_F17 = 330; 991 /** Key code constant: F18 key. */ 992 @FlaggedApi(Flags.FLAG_ENABLE_NEW_25Q2_KEYCODES) 993 public static final int KEYCODE_F18 = 331; 994 /** Key code constant: F19 key. */ 995 @FlaggedApi(Flags.FLAG_ENABLE_NEW_25Q2_KEYCODES) 996 public static final int KEYCODE_F19 = 332; 997 /** Key code constant: F20 key. */ 998 @FlaggedApi(Flags.FLAG_ENABLE_NEW_25Q2_KEYCODES) 999 public static final int KEYCODE_F20 = 333; 1000 /** Key code constant: F21 key. */ 1001 @FlaggedApi(Flags.FLAG_ENABLE_NEW_25Q2_KEYCODES) 1002 public static final int KEYCODE_F21 = 334; 1003 /** Key code constant: F22 key. */ 1004 @FlaggedApi(Flags.FLAG_ENABLE_NEW_25Q2_KEYCODES) 1005 public static final int KEYCODE_F22 = 335; 1006 /** Key code constant: F23 key. */ 1007 @FlaggedApi(Flags.FLAG_ENABLE_NEW_25Q2_KEYCODES) 1008 public static final int KEYCODE_F23 = 336; 1009 /** Key code constant: F24 key. */ 1010 @FlaggedApi(Flags.FLAG_ENABLE_NEW_25Q2_KEYCODES) 1011 public static final int KEYCODE_F24 = 337; 1012 1013 /** 1014 * Integer value of the last KEYCODE. Increases as new keycodes are added to KeyEvent. 1015 * @hide 1016 */ 1017 @TestApi 1018 @SuppressWarnings("FlaggedApi") 1019 public static final int LAST_KEYCODE = KEYCODE_F24; 1020 1021 /** @hide */ 1022 @IntDef(prefix = {"KEYCODE_"}, value = { 1023 KEYCODE_UNKNOWN, 1024 KEYCODE_SOFT_LEFT, 1025 KEYCODE_SOFT_RIGHT, 1026 KEYCODE_HOME, 1027 KEYCODE_BACK, 1028 KEYCODE_CALL, 1029 KEYCODE_ENDCALL, 1030 KEYCODE_0, 1031 KEYCODE_1, 1032 KEYCODE_2, 1033 KEYCODE_3, 1034 KEYCODE_4, 1035 KEYCODE_5, 1036 KEYCODE_6, 1037 KEYCODE_7, 1038 KEYCODE_8, 1039 KEYCODE_9, 1040 KEYCODE_STAR, 1041 KEYCODE_POUND, 1042 KEYCODE_DPAD_UP, 1043 KEYCODE_DPAD_DOWN, 1044 KEYCODE_DPAD_LEFT, 1045 KEYCODE_DPAD_RIGHT, 1046 KEYCODE_DPAD_CENTER, 1047 KEYCODE_VOLUME_UP, 1048 KEYCODE_VOLUME_DOWN, 1049 KEYCODE_POWER, 1050 KEYCODE_CAMERA, 1051 KEYCODE_CLEAR, 1052 KEYCODE_A, 1053 KEYCODE_B, 1054 KEYCODE_C, 1055 KEYCODE_D, 1056 KEYCODE_E, 1057 KEYCODE_F, 1058 KEYCODE_G, 1059 KEYCODE_H, 1060 KEYCODE_I, 1061 KEYCODE_J, 1062 KEYCODE_K, 1063 KEYCODE_L, 1064 KEYCODE_M, 1065 KEYCODE_N, 1066 KEYCODE_O, 1067 KEYCODE_P, 1068 KEYCODE_Q, 1069 KEYCODE_R, 1070 KEYCODE_S, 1071 KEYCODE_T, 1072 KEYCODE_U, 1073 KEYCODE_V, 1074 KEYCODE_W, 1075 KEYCODE_X, 1076 KEYCODE_Y, 1077 KEYCODE_Z, 1078 KEYCODE_COMMA, 1079 KEYCODE_PERIOD, 1080 KEYCODE_ALT_LEFT, 1081 KEYCODE_ALT_RIGHT, 1082 KEYCODE_SHIFT_LEFT, 1083 KEYCODE_SHIFT_RIGHT, 1084 KEYCODE_TAB, 1085 KEYCODE_SPACE, 1086 KEYCODE_SYM, 1087 KEYCODE_EXPLORER, 1088 KEYCODE_ENVELOPE, 1089 KEYCODE_ENTER, 1090 KEYCODE_DEL, 1091 KEYCODE_GRAVE, 1092 KEYCODE_MINUS, 1093 KEYCODE_EQUALS, 1094 KEYCODE_LEFT_BRACKET, 1095 KEYCODE_RIGHT_BRACKET, 1096 KEYCODE_BACKSLASH, 1097 KEYCODE_SEMICOLON, 1098 KEYCODE_APOSTROPHE, 1099 KEYCODE_SLASH, 1100 KEYCODE_AT, 1101 KEYCODE_NUM, 1102 KEYCODE_HEADSETHOOK, 1103 KEYCODE_FOCUS, 1104 KEYCODE_PLUS, 1105 KEYCODE_MENU, 1106 KEYCODE_NOTIFICATION, 1107 KEYCODE_SEARCH, 1108 KEYCODE_MEDIA_PLAY_PAUSE, 1109 KEYCODE_MEDIA_STOP, 1110 KEYCODE_MEDIA_NEXT, 1111 KEYCODE_MEDIA_PREVIOUS, 1112 KEYCODE_MEDIA_REWIND, 1113 KEYCODE_MEDIA_FAST_FORWARD, 1114 KEYCODE_MUTE, 1115 KEYCODE_PAGE_UP, 1116 KEYCODE_PAGE_DOWN, 1117 KEYCODE_PICTSYMBOLS, 1118 KEYCODE_SWITCH_CHARSET, 1119 KEYCODE_BUTTON_A, 1120 KEYCODE_BUTTON_B, 1121 KEYCODE_BUTTON_C, 1122 KEYCODE_BUTTON_X, 1123 KEYCODE_BUTTON_Y, 1124 KEYCODE_BUTTON_Z, 1125 KEYCODE_BUTTON_L1, 1126 KEYCODE_BUTTON_R1, 1127 KEYCODE_BUTTON_L2, 1128 KEYCODE_BUTTON_R2, 1129 KEYCODE_BUTTON_THUMBL, 1130 KEYCODE_BUTTON_THUMBR, 1131 KEYCODE_BUTTON_START, 1132 KEYCODE_BUTTON_SELECT, 1133 KEYCODE_BUTTON_MODE, 1134 KEYCODE_ESCAPE, 1135 KEYCODE_FORWARD_DEL, 1136 KEYCODE_CTRL_LEFT, 1137 KEYCODE_CTRL_RIGHT, 1138 KEYCODE_CAPS_LOCK, 1139 KEYCODE_SCROLL_LOCK, 1140 KEYCODE_META_LEFT, 1141 KEYCODE_META_RIGHT, 1142 KEYCODE_FUNCTION, 1143 KEYCODE_SYSRQ, 1144 KEYCODE_BREAK, 1145 KEYCODE_MOVE_HOME, 1146 KEYCODE_MOVE_END, 1147 KEYCODE_INSERT, 1148 KEYCODE_FORWARD, 1149 KEYCODE_MEDIA_PLAY, 1150 KEYCODE_MEDIA_PAUSE, 1151 KEYCODE_MEDIA_CLOSE, 1152 KEYCODE_MEDIA_EJECT, 1153 KEYCODE_MEDIA_RECORD, 1154 KEYCODE_F1, 1155 KEYCODE_F2, 1156 KEYCODE_F3, 1157 KEYCODE_F4, 1158 KEYCODE_F5, 1159 KEYCODE_F6, 1160 KEYCODE_F7, 1161 KEYCODE_F8, 1162 KEYCODE_F9, 1163 KEYCODE_F10, 1164 KEYCODE_F11, 1165 KEYCODE_F12, 1166 KEYCODE_NUM_LOCK, 1167 KEYCODE_NUMPAD_0, 1168 KEYCODE_NUMPAD_1, 1169 KEYCODE_NUMPAD_2, 1170 KEYCODE_NUMPAD_3, 1171 KEYCODE_NUMPAD_4, 1172 KEYCODE_NUMPAD_5, 1173 KEYCODE_NUMPAD_6, 1174 KEYCODE_NUMPAD_7, 1175 KEYCODE_NUMPAD_8, 1176 KEYCODE_NUMPAD_9, 1177 KEYCODE_NUMPAD_DIVIDE, 1178 KEYCODE_NUMPAD_MULTIPLY, 1179 KEYCODE_NUMPAD_SUBTRACT, 1180 KEYCODE_NUMPAD_ADD, 1181 KEYCODE_NUMPAD_DOT, 1182 KEYCODE_NUMPAD_COMMA, 1183 KEYCODE_NUMPAD_ENTER, 1184 KEYCODE_NUMPAD_EQUALS, 1185 KEYCODE_NUMPAD_LEFT_PAREN, 1186 KEYCODE_NUMPAD_RIGHT_PAREN, 1187 KEYCODE_VOLUME_MUTE, 1188 KEYCODE_INFO, 1189 KEYCODE_CHANNEL_UP, 1190 KEYCODE_CHANNEL_DOWN, 1191 KEYCODE_ZOOM_IN, 1192 KEYCODE_ZOOM_OUT, 1193 KEYCODE_TV, 1194 KEYCODE_WINDOW, 1195 KEYCODE_GUIDE, 1196 KEYCODE_DVR, 1197 KEYCODE_BOOKMARK, 1198 KEYCODE_CAPTIONS, 1199 KEYCODE_SETTINGS, 1200 KEYCODE_TV_POWER, 1201 KEYCODE_TV_INPUT, 1202 KEYCODE_STB_POWER, 1203 KEYCODE_STB_INPUT, 1204 KEYCODE_AVR_POWER, 1205 KEYCODE_AVR_INPUT, 1206 KEYCODE_PROG_RED, 1207 KEYCODE_PROG_GREEN, 1208 KEYCODE_PROG_YELLOW, 1209 KEYCODE_PROG_BLUE, 1210 KEYCODE_APP_SWITCH, 1211 KEYCODE_BUTTON_1, 1212 KEYCODE_BUTTON_2, 1213 KEYCODE_BUTTON_3, 1214 KEYCODE_BUTTON_4, 1215 KEYCODE_BUTTON_5, 1216 KEYCODE_BUTTON_6, 1217 KEYCODE_BUTTON_7, 1218 KEYCODE_BUTTON_8, 1219 KEYCODE_BUTTON_9, 1220 KEYCODE_BUTTON_10, 1221 KEYCODE_BUTTON_11, 1222 KEYCODE_BUTTON_12, 1223 KEYCODE_BUTTON_13, 1224 KEYCODE_BUTTON_14, 1225 KEYCODE_BUTTON_15, 1226 KEYCODE_BUTTON_16, 1227 KEYCODE_LANGUAGE_SWITCH, 1228 KEYCODE_MANNER_MODE, 1229 KEYCODE_3D_MODE, 1230 KEYCODE_CONTACTS, 1231 KEYCODE_CALENDAR, 1232 KEYCODE_MUSIC, 1233 KEYCODE_CALCULATOR, 1234 KEYCODE_ZENKAKU_HANKAKU, 1235 KEYCODE_EISU, 1236 KEYCODE_MUHENKAN, 1237 KEYCODE_HENKAN, 1238 KEYCODE_KATAKANA_HIRAGANA, 1239 KEYCODE_YEN, 1240 KEYCODE_RO, 1241 KEYCODE_KANA, 1242 KEYCODE_ASSIST, 1243 KEYCODE_BRIGHTNESS_DOWN, 1244 KEYCODE_BRIGHTNESS_UP, 1245 KEYCODE_MEDIA_AUDIO_TRACK, 1246 KEYCODE_SLEEP, 1247 KEYCODE_WAKEUP, 1248 KEYCODE_PAIRING, 1249 KEYCODE_MEDIA_TOP_MENU, 1250 KEYCODE_11, 1251 KEYCODE_12, 1252 KEYCODE_LAST_CHANNEL, 1253 KEYCODE_TV_DATA_SERVICE, 1254 KEYCODE_VOICE_ASSIST, 1255 KEYCODE_TV_RADIO_SERVICE, 1256 KEYCODE_TV_TELETEXT, 1257 KEYCODE_TV_NUMBER_ENTRY, 1258 KEYCODE_TV_TERRESTRIAL_ANALOG, 1259 KEYCODE_TV_TERRESTRIAL_DIGITAL, 1260 KEYCODE_TV_SATELLITE, 1261 KEYCODE_TV_SATELLITE_BS, 1262 KEYCODE_TV_SATELLITE_CS, 1263 KEYCODE_TV_SATELLITE_SERVICE, 1264 KEYCODE_TV_NETWORK, 1265 KEYCODE_TV_ANTENNA_CABLE, 1266 KEYCODE_TV_INPUT_HDMI_1, 1267 KEYCODE_TV_INPUT_HDMI_2, 1268 KEYCODE_TV_INPUT_HDMI_3, 1269 KEYCODE_TV_INPUT_HDMI_4, 1270 KEYCODE_TV_INPUT_COMPOSITE_1, 1271 KEYCODE_TV_INPUT_COMPOSITE_2, 1272 KEYCODE_TV_INPUT_COMPONENT_1, 1273 KEYCODE_TV_INPUT_COMPONENT_2, 1274 KEYCODE_TV_INPUT_VGA_1, 1275 KEYCODE_TV_AUDIO_DESCRIPTION, 1276 KEYCODE_TV_AUDIO_DESCRIPTION_MIX_UP, 1277 KEYCODE_TV_AUDIO_DESCRIPTION_MIX_DOWN, 1278 KEYCODE_TV_ZOOM_MODE, 1279 KEYCODE_TV_CONTENTS_MENU, 1280 KEYCODE_TV_MEDIA_CONTEXT_MENU, 1281 KEYCODE_TV_TIMER_PROGRAMMING, 1282 KEYCODE_HELP, 1283 KEYCODE_NAVIGATE_PREVIOUS, 1284 KEYCODE_NAVIGATE_NEXT, 1285 KEYCODE_NAVIGATE_IN, 1286 KEYCODE_NAVIGATE_OUT, 1287 KEYCODE_STEM_PRIMARY, 1288 KEYCODE_STEM_1, 1289 KEYCODE_STEM_2, 1290 KEYCODE_STEM_3, 1291 KEYCODE_DPAD_UP_LEFT, 1292 KEYCODE_DPAD_DOWN_LEFT, 1293 KEYCODE_DPAD_UP_RIGHT, 1294 KEYCODE_DPAD_DOWN_RIGHT, 1295 KEYCODE_MEDIA_SKIP_FORWARD, 1296 KEYCODE_MEDIA_SKIP_BACKWARD, 1297 KEYCODE_MEDIA_STEP_FORWARD, 1298 KEYCODE_MEDIA_STEP_BACKWARD, 1299 KEYCODE_SOFT_SLEEP, 1300 KEYCODE_CUT, 1301 KEYCODE_COPY, 1302 KEYCODE_PASTE, 1303 KEYCODE_SYSTEM_NAVIGATION_UP, 1304 KEYCODE_SYSTEM_NAVIGATION_DOWN, 1305 KEYCODE_SYSTEM_NAVIGATION_LEFT, 1306 KEYCODE_SYSTEM_NAVIGATION_RIGHT, 1307 KEYCODE_ALL_APPS, 1308 KEYCODE_REFRESH, 1309 KEYCODE_THUMBS_UP, 1310 KEYCODE_THUMBS_DOWN, 1311 KEYCODE_PROFILE_SWITCH, 1312 KEYCODE_VIDEO_APP_1, 1313 KEYCODE_VIDEO_APP_2, 1314 KEYCODE_VIDEO_APP_3, 1315 KEYCODE_VIDEO_APP_4, 1316 KEYCODE_VIDEO_APP_5, 1317 KEYCODE_VIDEO_APP_6, 1318 KEYCODE_VIDEO_APP_7, 1319 KEYCODE_VIDEO_APP_8, 1320 KEYCODE_FEATURED_APP_1, 1321 KEYCODE_FEATURED_APP_2, 1322 KEYCODE_FEATURED_APP_3, 1323 KEYCODE_FEATURED_APP_4, 1324 KEYCODE_DEMO_APP_1, 1325 KEYCODE_DEMO_APP_2, 1326 KEYCODE_DEMO_APP_3, 1327 KEYCODE_DEMO_APP_4, 1328 KEYCODE_KEYBOARD_BACKLIGHT_DOWN, 1329 KEYCODE_KEYBOARD_BACKLIGHT_UP, 1330 KEYCODE_KEYBOARD_BACKLIGHT_TOGGLE, 1331 KEYCODE_STYLUS_BUTTON_PRIMARY, 1332 KEYCODE_STYLUS_BUTTON_SECONDARY, 1333 KEYCODE_STYLUS_BUTTON_TERTIARY, 1334 KEYCODE_STYLUS_BUTTON_TAIL, 1335 KEYCODE_RECENT_APPS, 1336 KEYCODE_MACRO_1, 1337 KEYCODE_MACRO_2, 1338 KEYCODE_MACRO_3, 1339 KEYCODE_MACRO_4, 1340 KEYCODE_EMOJI_PICKER, 1341 KEYCODE_SCREENSHOT, 1342 KEYCODE_DICTATE, 1343 KEYCODE_NEW, 1344 KEYCODE_CLOSE, 1345 KEYCODE_DO_NOT_DISTURB, 1346 KEYCODE_PRINT, 1347 KEYCODE_LOCK, 1348 KEYCODE_FULLSCREEN, 1349 KEYCODE_F13, 1350 KEYCODE_F14, 1351 KEYCODE_F15, 1352 KEYCODE_F16, 1353 KEYCODE_F17, 1354 KEYCODE_F18, 1355 KEYCODE_F19, 1356 KEYCODE_F20, 1357 KEYCODE_F21, 1358 KEYCODE_F22, 1359 KEYCODE_F23, 1360 KEYCODE_F24, 1361 }) 1362 @Retention(RetentionPolicy.SOURCE) 1363 @interface KeyCode {} 1364 1365 // NOTE: If you add a new keycode here you must also add it to: 1366 // isSystem() 1367 // isWakeKey() 1368 // frameworks/native/include/android/keycodes.h 1369 // frameworks/native/include/input/InputEventLabels.h 1370 // frameworks/base/core/res/res/values/attrs.xml 1371 // emulator? 1372 // LAST_KEYCODE 1373 // 1374 // Also Android currently does not reserve code ranges for vendor- 1375 // specific key codes. If you have new key codes to have, you 1376 // MUST contribute a patch to the open source project to define 1377 // those new codes. This is intended to maintain a consistent 1378 // set of key code definitions across all Android devices. 1379 1380 // Symbolic names of all metakeys in bit order from least significant to most significant. 1381 // Accordingly there are exactly 32 values in this table. 1382 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 1383 private static final String[] META_SYMBOLIC_NAMES = new String[] { 1384 "META_SHIFT_ON", 1385 "META_ALT_ON", 1386 "META_SYM_ON", 1387 "META_FUNCTION_ON", 1388 "META_ALT_LEFT_ON", 1389 "META_ALT_RIGHT_ON", 1390 "META_SHIFT_LEFT_ON", 1391 "META_SHIFT_RIGHT_ON", 1392 "META_CAP_LOCKED", 1393 "META_ALT_LOCKED", 1394 "META_SYM_LOCKED", 1395 "0x00000800", 1396 "META_CTRL_ON", 1397 "META_CTRL_LEFT_ON", 1398 "META_CTRL_RIGHT_ON", 1399 "0x00008000", 1400 "META_META_ON", 1401 "META_META_LEFT_ON", 1402 "META_META_RIGHT_ON", 1403 "0x00080000", 1404 "META_CAPS_LOCK_ON", 1405 "META_NUM_LOCK_ON", 1406 "META_SCROLL_LOCK_ON", 1407 "0x00800000", 1408 "0x01000000", 1409 "0x02000000", 1410 "0x04000000", 1411 "0x08000000", 1412 "0x10000000", 1413 "0x20000000", 1414 "0x40000000", 1415 "0x80000000", 1416 }; 1417 1418 private static final String LABEL_PREFIX = "KEYCODE_"; 1419 1420 /** 1421 * @deprecated There are now more than MAX_KEYCODE keycodes. 1422 * Use {@link #getMaxKeyCode()} instead. 1423 */ 1424 @Deprecated 1425 public static final int MAX_KEYCODE = 84; 1426 1427 /** 1428 * {@link #getAction} value: the key has been pressed down. 1429 */ 1430 public static final int ACTION_DOWN = 0; 1431 /** 1432 * {@link #getAction} value: the key has been released. 1433 */ 1434 public static final int ACTION_UP = 1; 1435 /** 1436 * @deprecated No longer used by the input system. 1437 * {@link #getAction} value: multiple duplicate key events have 1438 * occurred in a row, or a complex string is being delivered. If the 1439 * key code is not {@link #KEYCODE_UNKNOWN} then the 1440 * {@link #getRepeatCount()} method returns the number of times 1441 * the given key code should be executed. 1442 * Otherwise, if the key code is {@link #KEYCODE_UNKNOWN}, then 1443 * this is a sequence of characters as returned by {@link #getCharacters}. 1444 */ 1445 @Deprecated 1446 public static final int ACTION_MULTIPLE = 2; 1447 1448 /** @hide */ 1449 @IntDef(prefix = {"ACTION_"}, value = { 1450 ACTION_DOWN, 1451 ACTION_UP, 1452 ACTION_MULTIPLE, 1453 }) 1454 @Retention(RetentionPolicy.SOURCE) 1455 @interface Action {} 1456 1457 /** 1458 * SHIFT key locked in CAPS mode. 1459 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API. 1460 * @hide 1461 */ 1462 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 1463 public static final int META_CAP_LOCKED = 0x100; 1464 1465 /** 1466 * ALT key locked. 1467 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API. 1468 * @hide 1469 */ 1470 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 1471 public static final int META_ALT_LOCKED = 0x200; 1472 1473 /** 1474 * SYM key locked. 1475 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API. 1476 * @hide 1477 */ 1478 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 1479 public static final int META_SYM_LOCKED = 0x400; 1480 1481 /** 1482 * Text is in selection mode. 1483 * Reserved for use by {@link MetaKeyKeyListener} for a private unpublished constant 1484 * in its API that is currently being retained for legacy reasons. 1485 * @hide 1486 */ 1487 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 1488 public static final int META_SELECTING = 0x800; 1489 1490 /** 1491 * <p>This mask is used to check whether one of the ALT meta keys is pressed.</p> 1492 * 1493 * @see #isAltPressed() 1494 * @see #getMetaState() 1495 * @see #KEYCODE_ALT_LEFT 1496 * @see #KEYCODE_ALT_RIGHT 1497 */ 1498 public static final int META_ALT_ON = 0x02; 1499 1500 /** 1501 * <p>This mask is used to check whether the left ALT meta key is pressed.</p> 1502 * 1503 * @see #isAltPressed() 1504 * @see #getMetaState() 1505 * @see #KEYCODE_ALT_LEFT 1506 */ 1507 public static final int META_ALT_LEFT_ON = 0x10; 1508 1509 /** 1510 * <p>This mask is used to check whether the right the ALT meta key is pressed.</p> 1511 * 1512 * @see #isAltPressed() 1513 * @see #getMetaState() 1514 * @see #KEYCODE_ALT_RIGHT 1515 */ 1516 public static final int META_ALT_RIGHT_ON = 0x20; 1517 1518 /** 1519 * <p>This mask is used to check whether one of the SHIFT meta keys is pressed.</p> 1520 * 1521 * @see #isShiftPressed() 1522 * @see #getMetaState() 1523 * @see #KEYCODE_SHIFT_LEFT 1524 * @see #KEYCODE_SHIFT_RIGHT 1525 */ 1526 public static final int META_SHIFT_ON = 0x1; 1527 1528 /** 1529 * <p>This mask is used to check whether the left SHIFT meta key is pressed.</p> 1530 * 1531 * @see #isShiftPressed() 1532 * @see #getMetaState() 1533 * @see #KEYCODE_SHIFT_LEFT 1534 */ 1535 public static final int META_SHIFT_LEFT_ON = 0x40; 1536 1537 /** 1538 * <p>This mask is used to check whether the right SHIFT meta key is pressed.</p> 1539 * 1540 * @see #isShiftPressed() 1541 * @see #getMetaState() 1542 * @see #KEYCODE_SHIFT_RIGHT 1543 */ 1544 public static final int META_SHIFT_RIGHT_ON = 0x80; 1545 1546 /** 1547 * <p>This mask is used to check whether the SYM meta key is pressed.</p> 1548 * 1549 * @see #isSymPressed() 1550 * @see #getMetaState() 1551 */ 1552 public static final int META_SYM_ON = 0x4; 1553 1554 /** 1555 * <p>This mask is used to check whether the FUNCTION meta key is pressed.</p> 1556 * 1557 * @see #isFunctionPressed() 1558 * @see #getMetaState() 1559 */ 1560 public static final int META_FUNCTION_ON = 0x8; 1561 1562 /** 1563 * <p>This mask is used to check whether one of the CTRL meta keys is pressed.</p> 1564 * 1565 * @see #isCtrlPressed() 1566 * @see #getMetaState() 1567 * @see #KEYCODE_CTRL_LEFT 1568 * @see #KEYCODE_CTRL_RIGHT 1569 */ 1570 public static final int META_CTRL_ON = 0x1000; 1571 1572 /** 1573 * <p>This mask is used to check whether the left CTRL meta key is pressed.</p> 1574 * 1575 * @see #isCtrlPressed() 1576 * @see #getMetaState() 1577 * @see #KEYCODE_CTRL_LEFT 1578 */ 1579 public static final int META_CTRL_LEFT_ON = 0x2000; 1580 1581 /** 1582 * <p>This mask is used to check whether the right CTRL meta key is pressed.</p> 1583 * 1584 * @see #isCtrlPressed() 1585 * @see #getMetaState() 1586 * @see #KEYCODE_CTRL_RIGHT 1587 */ 1588 public static final int META_CTRL_RIGHT_ON = 0x4000; 1589 1590 /** 1591 * <p>This mask is used to check whether one of the META meta keys is pressed.</p> 1592 * 1593 * @see #isMetaPressed() 1594 * @see #getMetaState() 1595 * @see #KEYCODE_META_LEFT 1596 * @see #KEYCODE_META_RIGHT 1597 */ 1598 public static final int META_META_ON = 0x10000; 1599 1600 /** 1601 * <p>This mask is used to check whether the left META meta key is pressed.</p> 1602 * 1603 * @see #isMetaPressed() 1604 * @see #getMetaState() 1605 * @see #KEYCODE_META_LEFT 1606 */ 1607 public static final int META_META_LEFT_ON = 0x20000; 1608 1609 /** 1610 * <p>This mask is used to check whether the right META meta key is pressed.</p> 1611 * 1612 * @see #isMetaPressed() 1613 * @see #getMetaState() 1614 * @see #KEYCODE_META_RIGHT 1615 */ 1616 public static final int META_META_RIGHT_ON = 0x40000; 1617 1618 /** 1619 * <p>This mask is used to check whether the CAPS LOCK meta key is on.</p> 1620 * 1621 * @see #isCapsLockOn() 1622 * @see #getMetaState() 1623 * @see #KEYCODE_CAPS_LOCK 1624 */ 1625 public static final int META_CAPS_LOCK_ON = 0x100000; 1626 1627 /** 1628 * <p>This mask is used to check whether the NUM LOCK meta key is on.</p> 1629 * 1630 * @see #isNumLockOn() 1631 * @see #getMetaState() 1632 * @see #KEYCODE_NUM_LOCK 1633 */ 1634 public static final int META_NUM_LOCK_ON = 0x200000; 1635 1636 /** 1637 * <p>This mask is used to check whether the SCROLL LOCK meta key is on.</p> 1638 * 1639 * @see #isScrollLockOn() 1640 * @see #getMetaState() 1641 * @see #KEYCODE_SCROLL_LOCK 1642 */ 1643 public static final int META_SCROLL_LOCK_ON = 0x400000; 1644 1645 /** @hide */ 1646 @IntDef(flag = true, prefix = {"META_"}, value = { 1647 META_CAP_LOCKED, 1648 META_ALT_LOCKED, 1649 META_SYM_LOCKED, 1650 META_SELECTING, 1651 META_ALT_ON, 1652 META_ALT_LEFT_ON, 1653 META_ALT_RIGHT_ON, 1654 META_SHIFT_ON, 1655 META_SHIFT_LEFT_ON, 1656 META_SHIFT_RIGHT_ON, 1657 META_SYM_ON, 1658 META_FUNCTION_ON, 1659 META_CTRL_ON, 1660 META_CTRL_LEFT_ON, 1661 META_CTRL_RIGHT_ON, 1662 META_META_ON, 1663 META_META_LEFT_ON, 1664 META_META_RIGHT_ON, 1665 META_CAPS_LOCK_ON, 1666 META_NUM_LOCK_ON, 1667 META_SCROLL_LOCK_ON, 1668 }) 1669 @Retention(RetentionPolicy.SOURCE) 1670 @interface MetaState {} 1671 1672 /** 1673 * This mask is a combination of {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON} 1674 * and {@link #META_SHIFT_RIGHT_ON}. 1675 */ 1676 public static final int META_SHIFT_MASK = META_SHIFT_ON 1677 | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON; 1678 1679 /** 1680 * This mask is a combination of {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON} 1681 * and {@link #META_ALT_RIGHT_ON}. 1682 */ 1683 public static final int META_ALT_MASK = META_ALT_ON 1684 | META_ALT_LEFT_ON | META_ALT_RIGHT_ON; 1685 1686 /** 1687 * This mask is a combination of {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON} 1688 * and {@link #META_CTRL_RIGHT_ON}. 1689 */ 1690 public static final int META_CTRL_MASK = META_CTRL_ON 1691 | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON; 1692 1693 /** 1694 * This mask is a combination of {@link #META_META_ON}, {@link #META_META_LEFT_ON} 1695 * and {@link #META_META_RIGHT_ON}. 1696 */ 1697 public static final int META_META_MASK = META_META_ON 1698 | META_META_LEFT_ON | META_META_RIGHT_ON; 1699 1700 /** 1701 * This mask is set if the device woke because of this key event. 1702 * 1703 * @deprecated This flag will never be set by the system since the system 1704 * consumes all wake keys itself. 1705 */ 1706 @Deprecated 1707 public static final int FLAG_WOKE_HERE = 0x1; 1708 1709 /** 1710 * This mask is set if the key event was generated by a software keyboard. 1711 */ 1712 public static final int FLAG_SOFT_KEYBOARD = 0x2; 1713 1714 /** 1715 * This mask is set if we don't want the key event to cause us to leave 1716 * touch mode. 1717 */ 1718 public static final int FLAG_KEEP_TOUCH_MODE = 0x4; 1719 1720 /** 1721 * This mask is set if an event was known to come from a trusted part 1722 * of the system. That is, the event is known to come from the user, 1723 * and could not have been spoofed by a third party component. 1724 */ 1725 public static final int FLAG_FROM_SYSTEM = 0x8; 1726 1727 /** 1728 * This mask is used for compatibility, to identify enter keys that are 1729 * coming from an IME whose enter key has been auto-labelled "next" or 1730 * "done". This allows TextView to dispatch these as normal enter keys 1731 * for old applications, but still do the appropriate action when 1732 * receiving them. 1733 */ 1734 public static final int FLAG_EDITOR_ACTION = 0x10; 1735 1736 /** 1737 * When associated with up key events, this indicates that the key press 1738 * has been canceled. Typically this is used with virtual touch screen 1739 * keys, where the user can slide from the virtual key area on to the 1740 * display: in that case, the application will receive a canceled up 1741 * event and should not perform the action normally associated with the 1742 * key. Note that for this to work, the application can not perform an 1743 * action for a key until it receives an up or the long press timeout has 1744 * expired. 1745 */ 1746 public static final int FLAG_CANCELED = IInputConstants.INPUT_EVENT_FLAG_CANCELED; 1747 1748 /** 1749 * This key event was generated by a virtual (on-screen) hard key area. 1750 * Typically this is an area of the touchscreen, outside of the regular 1751 * display, dedicated to "hardware" buttons. 1752 */ 1753 public static final int FLAG_VIRTUAL_HARD_KEY = 0x40; 1754 1755 /** 1756 * This flag is set for the first key repeat that occurs after the 1757 * long press timeout. 1758 */ 1759 public static final int FLAG_LONG_PRESS = 0x80; 1760 1761 /** 1762 * Set when a key event has {@link #FLAG_CANCELED} set because a long 1763 * press action was executed while it was down. 1764 */ 1765 public static final int FLAG_CANCELED_LONG_PRESS = 0x100; 1766 1767 /** 1768 * Set for {@link #ACTION_UP} when this event's key code is still being 1769 * tracked from its initial down. That is, somebody requested that tracking 1770 * started on the key down and a long press has not caused 1771 * the tracking to be canceled. 1772 */ 1773 public static final int FLAG_TRACKING = 0x200; 1774 1775 /** 1776 * Set when a key event has been synthesized to implement default behavior 1777 * for an event that the application did not handle. 1778 * Fallback key events are generated by unhandled trackball motions 1779 * (to emulate a directional keypad) and by certain unhandled key presses 1780 * that are declared in the key map (such as special function numeric keypad 1781 * keys when numlock is off). 1782 */ 1783 public static final int FLAG_FALLBACK = 0x400; 1784 1785 /** 1786 * This flag indicates that this event was modified by or generated from an accessibility 1787 * service. Value = 0x800 1788 * @hide 1789 */ 1790 @TestApi 1791 public static final int FLAG_IS_ACCESSIBILITY_EVENT = INPUT_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; 1792 1793 /** 1794 * Signifies that the key is being predispatched. 1795 * @hide 1796 */ 1797 public static final int FLAG_PREDISPATCH = 0x20000000; 1798 1799 /** 1800 * Private control to determine when an app is tracking a key sequence. 1801 * @hide 1802 */ 1803 public static final int FLAG_START_TRACKING = 0x40000000; 1804 1805 /** 1806 * Private flag that indicates when the system has detected that this key event 1807 * may be inconsistent with respect to the sequence of previously delivered key events, 1808 * such as when a key up event is sent but the key was not down. 1809 * 1810 * @hide 1811 * @see #isTainted 1812 * @see #setTainted 1813 */ 1814 public static final int FLAG_TAINTED = IInputConstants.INPUT_EVENT_FLAG_TAINTED; 1815 1816 /** @hide */ 1817 @IntDef(flag = true, prefix = { "FLAG_" }, value = { 1818 FLAG_WOKE_HERE, 1819 FLAG_SOFT_KEYBOARD, 1820 FLAG_KEEP_TOUCH_MODE, 1821 FLAG_FROM_SYSTEM, 1822 FLAG_EDITOR_ACTION, 1823 FLAG_CANCELED, 1824 FLAG_VIRTUAL_HARD_KEY, 1825 FLAG_LONG_PRESS, 1826 FLAG_CANCELED_LONG_PRESS, 1827 FLAG_TRACKING, 1828 FLAG_FALLBACK, 1829 FLAG_IS_ACCESSIBILITY_EVENT, 1830 FLAG_PREDISPATCH, 1831 FLAG_START_TRACKING, 1832 FLAG_TAINTED, 1833 }) 1834 @Retention(RetentionPolicy.SOURCE) 1835 @interface Flag {} 1836 1837 /** 1838 * Returns the maximum keycode. 1839 */ getMaxKeyCode()1840 public static int getMaxKeyCode() { 1841 return LAST_KEYCODE; 1842 } 1843 1844 /** 1845 * Get the character that is produced by putting accent on the character 1846 * c. 1847 * For example, getDeadChar('`', 'e') returns è. 1848 */ getDeadChar(int accent, int c)1849 public static int getDeadChar(int accent, int c) { 1850 return KeyCharacterMap.getDeadChar(accent, c); 1851 } 1852 1853 static final boolean DEBUG = false; 1854 static final String TAG = "KeyEvent"; 1855 1856 private static final int MAX_RECYCLED = 10; 1857 private static final Object gRecyclerLock = new Object(); 1858 private static int gRecyclerUsed; 1859 private static KeyEvent gRecyclerTop; 1860 1861 private KeyEvent mNext; 1862 1863 private int mId; 1864 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 1865 private int mDeviceId; 1866 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) 1867 private int mSource; 1868 private int mDisplayId = INVALID_DISPLAY; 1869 // NOTE: mHmac is private and not used in this class, but it's used on native side / parcel. 1870 private @Nullable byte[] mHmac; 1871 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 1872 @MetaState 1873 private int mMetaState; 1874 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 1875 @Action 1876 private int mAction; 1877 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 1878 private int mKeyCode; 1879 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 1880 private int mScanCode; 1881 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 1882 private int mRepeatCount; 1883 @UnsupportedAppUsage 1884 @Flag 1885 private int mFlags; 1886 /** 1887 * The time when the key initially was pressed, in nanoseconds. Only millisecond precision is 1888 * exposed as public api, so this must always be converted to / from milliseconds when used. 1889 */ 1890 private long mDownTime; 1891 /** 1892 * The time when the current key event occurred. If mAction is ACTION_DOWN, then this is equal 1893 * to mDownTime. Only millisecond precision is exposed as public api, so this must always be 1894 * converted to / from milliseconds when used. 1895 */ 1896 private long mEventTime; 1897 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 1898 private @Nullable String mCharacters; 1899 1900 public interface Callback { 1901 /** 1902 * Called when a key down event has occurred. If you return true, 1903 * you can first call {@link KeyEvent#startTracking() 1904 * KeyEvent.startTracking()} to have the framework track the event 1905 * through its {@link #onKeyUp(int, KeyEvent)} and also call your 1906 * {@link #onKeyLongPress(int, KeyEvent)} if it occurs. 1907 * 1908 * @param keyCode The value in event.getKeyCode(). 1909 * @param event Description of the key event. 1910 * 1911 * @return If you handled the event, return true. If you want to allow 1912 * the event to be handled by the next receiver, return false. 1913 */ onKeyDown(int keyCode, KeyEvent event)1914 boolean onKeyDown(int keyCode, KeyEvent event); 1915 1916 /** 1917 * Called when a long press has occurred. If you return true, 1918 * the final key up will have {@link KeyEvent#FLAG_CANCELED} and 1919 * {@link KeyEvent#FLAG_CANCELED_LONG_PRESS} set. Note that in 1920 * order to receive this callback, someone in the event change 1921 * <em>must</em> return true from {@link #onKeyDown} <em>and</em> 1922 * call {@link KeyEvent#startTracking()} on the event. 1923 * 1924 * @param keyCode The value in event.getKeyCode(). 1925 * @param event Description of the key event. 1926 * 1927 * @return If you handled the event, return true. If you want to allow 1928 * the event to be handled by the next receiver, return false. 1929 */ onKeyLongPress(int keyCode, KeyEvent event)1930 boolean onKeyLongPress(int keyCode, KeyEvent event); 1931 1932 /** 1933 * Called when a key up event has occurred. 1934 * 1935 * @param keyCode The value in event.getKeyCode(). 1936 * @param event Description of the key event. 1937 * 1938 * @return If you handled the event, return true. If you want to allow 1939 * the event to be handled by the next receiver, return false. 1940 */ onKeyUp(int keyCode, KeyEvent event)1941 boolean onKeyUp(int keyCode, KeyEvent event); 1942 1943 /** 1944 * Called when a user's interaction with an analog control, such as 1945 * flinging a trackball, generates simulated down/up events for the same 1946 * key multiple times in quick succession. 1947 * 1948 * @param keyCode The value in event.getKeyCode(). 1949 * @param count Number of pairs as returned by event.getRepeatCount(). 1950 * @param event Description of the key event. 1951 * 1952 * @return If you handled the event, return true. If you want to allow 1953 * the event to be handled by the next receiver, return false. 1954 */ onKeyMultiple(int keyCode, int count, KeyEvent event)1955 boolean onKeyMultiple(int keyCode, int count, KeyEvent event); 1956 } 1957 nativeKeyCodeToString(int keyCode)1958 private static native String nativeKeyCodeToString(int keyCode); nativeKeyCodeFromString(String keyCode)1959 private static native int nativeKeyCodeFromString(String keyCode); nativeNextId()1960 private static native int nativeNextId(); 1961 KeyEvent()1962 private KeyEvent() { 1963 this(/* downTime= */ 0, /* eventTime= */ 0, /* action= */ 0, /* code= */0, /* repeat= */ 0, 1964 /* metaState= */ 0, /* deviceId= */ 0, /* scancode= */ 0, /* flags= */ 0, 1965 /* source= */ 0, /* characters= */ null); 1966 } 1967 1968 /** 1969 * Create a new key event. 1970 * 1971 * @param action Action code: either {@link #ACTION_DOWN}, 1972 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 1973 * @param code The key code. 1974 */ KeyEvent(int action, int code)1975 public KeyEvent(int action, int code) { 1976 this(/* downTime= */ 0, /* eventTime= */ 0, action, code, /* repeat= */ 0, 1977 /* metaState= */ 0, /* deviceId= */ KeyCharacterMap.VIRTUAL_KEYBOARD, 1978 /* scancode= */ 0, /* flags= */ 0, /* source= */ 0, /* characters= */ null); 1979 } 1980 1981 /** 1982 * Create a new key event. 1983 * 1984 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1985 * at which this key code originally went down. 1986 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1987 * at which this event happened. 1988 * @param action Action code: either {@link #ACTION_DOWN}, 1989 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 1990 * @param code The key code. 1991 * @param repeat A repeat count for down events (> 0 if this is after the 1992 * initial down) or event count for multiple events. 1993 */ KeyEvent(long downTime, long eventTime, int action, int code, int repeat)1994 public KeyEvent(long downTime, long eventTime, int action, 1995 int code, int repeat) { 1996 this(downTime, eventTime, action, code, repeat, /* metaState= */ 0, 1997 KeyCharacterMap.VIRTUAL_KEYBOARD, /* scancode= */ 0, /* flags= */ 0, 1998 /* source= */ 0, /* characters= */ null); 1999 } 2000 2001 /** 2002 * Create a new key event. 2003 * 2004 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) 2005 * at which this key code originally went down. 2006 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) 2007 * at which this event happened. 2008 * @param action Action code: either {@link #ACTION_DOWN}, 2009 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 2010 * @param code The key code. 2011 * @param repeat A repeat count for down events (> 0 if this is after the 2012 * initial down) or event count for multiple events. 2013 * @param metaState Flags indicating which meta keys are currently pressed. 2014 */ KeyEvent(long downTime, long eventTime, int action, int code, int repeat, int metaState)2015 public KeyEvent(long downTime, long eventTime, int action, 2016 int code, int repeat, int metaState) { 2017 this(downTime, eventTime, action, code, repeat, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 2018 /* scancode= */ 0, /* flags= */ 0, /* source= */ 0, /* characters= */ null); 2019 } 2020 2021 /** 2022 * Create a new key event. 2023 * 2024 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) 2025 * at which this key code originally went down. 2026 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) 2027 * at which this event happened. 2028 * @param action Action code: either {@link #ACTION_DOWN}, 2029 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 2030 * @param code The key code. 2031 * @param repeat A repeat count for down events (> 0 if this is after the 2032 * initial down) or event count for multiple events. 2033 * @param metaState Flags indicating which meta keys are currently pressed. 2034 * @param deviceId The device ID that generated the key event. 2035 * @param scancode Raw device scan code of the event. 2036 */ KeyEvent(long downTime, long eventTime, int action, int code, int repeat, int metaState, int deviceId, int scancode)2037 public KeyEvent(long downTime, long eventTime, int action, 2038 int code, int repeat, int metaState, 2039 int deviceId, int scancode) { 2040 this(downTime, eventTime, action, code, repeat, metaState, deviceId, scancode, 2041 /* flags= */ 0, /* source= */ 0, /* characters= */ null); 2042 } 2043 2044 /** 2045 * Create a new key event. 2046 * 2047 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) 2048 * at which this key code originally went down. 2049 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) 2050 * at which this event happened. 2051 * @param action Action code: either {@link #ACTION_DOWN}, 2052 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 2053 * @param code The key code. 2054 * @param repeat A repeat count for down events (> 0 if this is after the 2055 * initial down) or event count for multiple events. 2056 * @param metaState Flags indicating which meta keys are currently pressed. 2057 * @param deviceId The device ID that generated the key event. 2058 * @param scancode Raw device scan code of the event. 2059 * @param flags The flags for this key event 2060 */ KeyEvent(long downTime, long eventTime, int action, int code, int repeat, int metaState, int deviceId, int scancode, int flags)2061 public KeyEvent(long downTime, long eventTime, int action, 2062 int code, int repeat, int metaState, 2063 int deviceId, int scancode, int flags) { 2064 this(downTime, eventTime, action, code, repeat, metaState, deviceId, scancode, flags, 2065 /* source= */ 0, /* characters= */ null); 2066 } 2067 2068 /** 2069 * Create a new key event. 2070 * 2071 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) 2072 * at which this key code originally went down. 2073 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) 2074 * at which this event happened. 2075 * @param action Action code: either {@link #ACTION_DOWN}, 2076 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 2077 * @param code The key code. 2078 * @param repeat A repeat count for down events (> 0 if this is after the 2079 * initial down) or event count for multiple events. 2080 * @param metaState Flags indicating which meta keys are currently pressed. 2081 * @param deviceId The device ID that generated the key event. 2082 * @param scancode Raw device scan code of the event. 2083 * @param flags The flags for this key event 2084 * @param source The input source such as {@link InputDevice#SOURCE_KEYBOARD}. 2085 */ KeyEvent(long downTime, long eventTime, int action, int code, int repeat, int metaState, int deviceId, int scancode, int flags, int source)2086 public KeyEvent(long downTime, long eventTime, int action, 2087 int code, int repeat, int metaState, 2088 int deviceId, int scancode, int flags, int source) { 2089 this(downTime, eventTime, action, code, repeat, metaState, deviceId, scancode, flags, 2090 source, /* characters= */ null); 2091 } 2092 KeyEvent(long downTime, long eventTime, int action, int code, int repeat, int metaState, int deviceId, int scancode, int flags, int source, @Nullable String characters)2093 private KeyEvent(long downTime, long eventTime, int action, int code, int repeat, int metaState, 2094 int deviceId, int scancode, int flags, int source, @Nullable String characters) { 2095 // NOTE: this is the canonical constructor, other constructors that takes KeyEvent 2096 // attributes should call it 2097 mId = nativeNextId(); 2098 mDownTime = TimeUnit.NANOSECONDS.convert(downTime, TimeUnit.MILLISECONDS); 2099 mEventTime = TimeUnit.NANOSECONDS.convert(eventTime, TimeUnit.MILLISECONDS); 2100 mAction = action; 2101 mKeyCode = code; 2102 mRepeatCount = repeat; 2103 mMetaState = metaState; 2104 mDeviceId = deviceId; 2105 mScanCode = scancode; 2106 mFlags = flags; 2107 mSource = source; 2108 mCharacters = characters; 2109 } 2110 2111 /** 2112 * Create a new key event for a string of characters. The key code, 2113 * action, repeat count and source will automatically be set to 2114 * {@link #KEYCODE_UNKNOWN}, {@link #ACTION_MULTIPLE}, 0, and 2115 * {@link InputDevice#SOURCE_KEYBOARD} for you. 2116 * 2117 * @param time The time (in {@link android.os.SystemClock#uptimeMillis}) 2118 * at which this event occured. 2119 * @param characters The string of characters. 2120 * @param deviceId The device ID that generated the key event. 2121 * @param flags The flags for this key event 2122 */ KeyEvent(long time, String characters, int deviceId, int flags)2123 public KeyEvent(long time, String characters, int deviceId, int flags) { 2124 this(/* downTime= */ time, /* eventTime= */ time, ACTION_MULTIPLE, KEYCODE_UNKNOWN, 2125 /* repeat= */ 0, /* metaState= */ 0, deviceId, /* scancode= */ 0, flags, 2126 /* source= */ InputDevice.SOURCE_KEYBOARD, characters); 2127 } 2128 2129 /** 2130 * Make an exact copy of an existing key event. 2131 */ KeyEvent(KeyEvent origEvent)2132 public KeyEvent(KeyEvent origEvent) { 2133 this(origEvent, origEvent.mId, origEvent.mEventTime, origEvent.mAction, 2134 origEvent.mRepeatCount, origEvent.mHmac == null ? null : origEvent.mHmac.clone(), 2135 origEvent.mCharacters); 2136 } 2137 2138 /** 2139 * Copy an existing key event, modifying its time and repeat count. 2140 * 2141 * @deprecated Use {@link #changeTimeRepeat(KeyEvent, long, int)} 2142 * instead. 2143 * 2144 * @param origEvent The existing event to be copied. 2145 * @param eventTime The new event time 2146 * (in {@link android.os.SystemClock#uptimeMillis}) of the event. 2147 * @param newRepeat The new repeat count of the event. 2148 */ 2149 @Deprecated KeyEvent(KeyEvent origEvent, long eventTime, int newRepeat)2150 public KeyEvent(KeyEvent origEvent, long eventTime, int newRepeat) { 2151 // Not an exact copy so assign a new ID. 2152 // Don't copy HMAC, it will be invalid because eventTime is changing 2153 this(origEvent, nativeNextId(), 2154 TimeUnit.NANOSECONDS.convert(eventTime, TimeUnit.MILLISECONDS), origEvent.mAction, 2155 newRepeat, /* hmac= */ null, origEvent.mCharacters); 2156 } 2157 2158 // This is the canonical constructor that should be called for constructors that take a KeyEvent KeyEvent(KeyEvent origEvent, int id, long eventTime, int action, int newRepeat, @Nullable byte[] hmac, @Nullable String characters)2159 private KeyEvent(KeyEvent origEvent, int id, long eventTime, int action, int newRepeat, 2160 @Nullable byte[] hmac, @Nullable String characters) { 2161 mId = id; 2162 mDownTime = origEvent.mDownTime; 2163 mEventTime = eventTime; 2164 mAction = action; 2165 mKeyCode = origEvent.mKeyCode; 2166 mRepeatCount = newRepeat; 2167 mMetaState = origEvent.mMetaState; 2168 mDeviceId = origEvent.mDeviceId; 2169 mSource = origEvent.mSource; 2170 mDisplayId = origEvent.mDisplayId; 2171 mHmac = hmac; 2172 mScanCode = origEvent.mScanCode; 2173 mFlags = origEvent.mFlags; 2174 mCharacters = characters; 2175 } 2176 obtain()2177 private static KeyEvent obtain() { 2178 final KeyEvent ev; 2179 synchronized (gRecyclerLock) { 2180 ev = gRecyclerTop; 2181 if (ev == null) { 2182 return new KeyEvent(); 2183 } 2184 gRecyclerTop = ev.mNext; 2185 gRecyclerUsed -= 1; 2186 } 2187 ev.mNext = null; 2188 ev.prepareForReuse(); 2189 return ev; 2190 } 2191 2192 /** 2193 * Obtains a (potentially recycled) key event. Used by native code to create a Java object. 2194 * 2195 * @hide 2196 */ obtain(int id, long downTimeNanos, long eventTimeNanos, int action, int code, int repeat, int metaState, int deviceId, int scancode, int flags, int source, int displayId, @Nullable byte[] hmac, String characters)2197 private static KeyEvent obtain(int id, long downTimeNanos, long eventTimeNanos, int action, 2198 int code, int repeat, int metaState, 2199 int deviceId, int scancode, int flags, int source, int displayId, @Nullable byte[] hmac, 2200 String characters) { 2201 KeyEvent ev = obtain(); 2202 ev.mId = id; 2203 ev.mDownTime = downTimeNanos; 2204 ev.mEventTime = eventTimeNanos; 2205 ev.mAction = action; 2206 ev.mKeyCode = code; 2207 ev.mRepeatCount = repeat; 2208 ev.mMetaState = metaState; 2209 ev.mDeviceId = deviceId; 2210 ev.mScanCode = scancode; 2211 ev.mFlags = flags; 2212 ev.mSource = source; 2213 ev.mDisplayId = displayId; 2214 ev.mHmac = hmac; 2215 ev.mCharacters = characters; 2216 return ev; 2217 } 2218 2219 /** 2220 * Obtains a (potentially recycled) key event. 2221 * 2222 * @hide 2223 */ obtain(long downTime, long eventTime, int action, int code, int repeat, int metaState, int deviceId, int scanCode, int flags, int source, int displayId, String characters)2224 public static KeyEvent obtain(long downTime, long eventTime, int action, 2225 int code, int repeat, int metaState, 2226 int deviceId, int scanCode, int flags, int source, int displayId, String characters) { 2227 downTime = TimeUnit.NANOSECONDS.convert(downTime, TimeUnit.MILLISECONDS); 2228 eventTime = TimeUnit.NANOSECONDS.convert(eventTime, TimeUnit.MILLISECONDS); 2229 return obtain(nativeNextId(), downTime, eventTime, action, code, repeat, metaState, 2230 deviceId, scanCode, flags, source, displayId, null /* hmac */, characters); 2231 } 2232 2233 /** 2234 * Obtains a (potentially recycled) key event. 2235 * 2236 * @hide 2237 */ 2238 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) obtain(long downTime, long eventTime, int action, int code, int repeat, int metaState, int deviceId, int scancode, int flags, int source, String characters)2239 public static KeyEvent obtain(long downTime, long eventTime, int action, 2240 int code, int repeat, int metaState, 2241 int deviceId, int scancode, int flags, int source, String characters) { 2242 // Do not convert downTime and eventTime here. We are calling the obtain method above, 2243 // which will do the conversion. Just specify INVALID_DISPLAY and forward the request. 2244 return obtain(downTime, eventTime, action, code, repeat, metaState, deviceId, scancode, 2245 flags, source, INVALID_DISPLAY, characters); 2246 } 2247 2248 /** 2249 2250 /** 2251 * Obtains a (potentially recycled) copy of another key event. 2252 * 2253 * @hide 2254 */ obtain(KeyEvent other)2255 public static KeyEvent obtain(KeyEvent other) { 2256 KeyEvent ev = obtain(); 2257 ev.mId = other.mId; 2258 ev.mDownTime = other.mDownTime; 2259 ev.mEventTime = other.mEventTime; 2260 ev.mAction = other.mAction; 2261 ev.mKeyCode = other.mKeyCode; 2262 ev.mRepeatCount = other.mRepeatCount; 2263 ev.mMetaState = other.mMetaState; 2264 ev.mDeviceId = other.mDeviceId; 2265 ev.mScanCode = other.mScanCode; 2266 ev.mFlags = other.mFlags; 2267 ev.mSource = other.mSource; 2268 ev.mDisplayId = other.mDisplayId; 2269 ev.mHmac = other.mHmac == null ? null : other.mHmac.clone(); 2270 ev.mCharacters = other.mCharacters; 2271 return ev; 2272 } 2273 2274 /** @hide */ 2275 @Override copy()2276 public KeyEvent copy() { 2277 return obtain(this); 2278 } 2279 2280 /** 2281 * Recycles a key event. 2282 * Key events should only be recycled if they are owned by the system since user 2283 * code expects them to be essentially immutable, "tracking" notwithstanding. 2284 * 2285 * @hide 2286 */ 2287 @Override 2288 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) recycle()2289 public final void recycle() { 2290 super.recycle(); 2291 mCharacters = null; 2292 2293 synchronized (gRecyclerLock) { 2294 if (gRecyclerUsed < MAX_RECYCLED) { 2295 gRecyclerUsed++; 2296 mNext = gRecyclerTop; 2297 gRecyclerTop = this; 2298 } 2299 } 2300 } 2301 2302 /** @hide */ 2303 @Override recycleIfNeededAfterDispatch()2304 public final void recycleIfNeededAfterDispatch() { 2305 // Do nothing. 2306 } 2307 2308 /** @hide */ 2309 @Override getId()2310 public int getId() { 2311 return mId; 2312 } 2313 2314 /** 2315 * Create a new key event that is the same as the given one, but whose 2316 * event time and repeat count are replaced with the given value. 2317 * 2318 * @param event The existing event to be copied. This is not modified. 2319 * @param eventTime The new event time 2320 * (in {@link android.os.SystemClock#uptimeMillis}) of the event. 2321 * @param newRepeat The new repeat count of the event. 2322 */ changeTimeRepeat(KeyEvent event, long eventTime, int newRepeat)2323 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime, 2324 int newRepeat) { 2325 return new KeyEvent(event, eventTime, newRepeat); 2326 } 2327 2328 /** 2329 * Create a new key event that is the same as the given one, but whose 2330 * event time and repeat count are replaced with the given value. 2331 * 2332 * @param event The existing event to be copied. This is not modified. 2333 * @param eventTime The new event time 2334 * (in {@link android.os.SystemClock#uptimeMillis}) of the event. 2335 * @param newRepeat The new repeat count of the event. 2336 * @param newFlags New flags for the event, replacing the entire value 2337 * in the original event. 2338 */ changeTimeRepeat(KeyEvent event, long eventTime, int newRepeat, int newFlags)2339 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime, 2340 int newRepeat, int newFlags) { 2341 KeyEvent ret = new KeyEvent(event); 2342 ret.mId = nativeNextId(); // Not an exact copy so assign a new ID. 2343 ret.mEventTime = TimeUnit.NANOSECONDS.convert(eventTime, TimeUnit.MILLISECONDS); 2344 ret.mRepeatCount = newRepeat; 2345 ret.mFlags = newFlags; 2346 return ret; 2347 } 2348 2349 /** 2350 * Copy an existing key event, modifying its action. 2351 * 2352 * @param origEvent The existing event to be copied. 2353 * @param action The new action code of the event. 2354 */ KeyEvent(KeyEvent origEvent, int action)2355 private KeyEvent(KeyEvent origEvent, int action) { 2356 // Not an exact copy so assign a new ID 2357 // Don't copy the hmac, it will be invalid since action is changing 2358 // Don't copy mCharacters, since one way or the other we'll lose it when changing action. 2359 this(origEvent, nativeNextId(), origEvent.mEventTime, action, origEvent.mRepeatCount, 2360 /* hmac= */ null, /* characters= */ null); 2361 } 2362 2363 /** 2364 * Create a new key event that is the same as the given one, but whose 2365 * action is replaced with the given value. 2366 * 2367 * @param event The existing event to be copied. This is not modified. 2368 * @param action The new action code of the event. 2369 */ changeAction(KeyEvent event, int action)2370 public static KeyEvent changeAction(KeyEvent event, int action) { 2371 return new KeyEvent(event, action); 2372 } 2373 2374 /** 2375 * Create a new key event that is the same as the given one, but whose 2376 * flags are replaced with the given value. 2377 * 2378 * @param event The existing event to be copied. This is not modified. 2379 * @param flags The new flags constant. 2380 */ changeFlags(KeyEvent event, int flags)2381 public static KeyEvent changeFlags(KeyEvent event, int flags) { 2382 event = new KeyEvent(event); 2383 event.mId = nativeNextId(); // Not an exact copy so assign a new ID. 2384 event.mFlags = flags; 2385 return event; 2386 } 2387 2388 /** @hide */ 2389 @Override isTainted()2390 public final boolean isTainted() { 2391 return (mFlags & FLAG_TAINTED) != 0; 2392 } 2393 2394 /** @hide */ 2395 @Override setTainted(boolean tainted)2396 public final void setTainted(boolean tainted) { 2397 mFlags = tainted ? mFlags | FLAG_TAINTED : mFlags & ~FLAG_TAINTED; 2398 } 2399 2400 /** 2401 * Don't use in new code, instead explicitly check 2402 * {@link #getAction()}. 2403 * 2404 * @return If the action is ACTION_DOWN, returns true; else false. 2405 * 2406 * @deprecated 2407 * @hide 2408 */ 2409 @UnsupportedAppUsage isDown()2410 @Deprecated public final boolean isDown() { 2411 return mAction == ACTION_DOWN; 2412 } 2413 2414 /** Is this a system key? System keys can not be used for menu shortcuts. 2415 */ isSystem()2416 public final boolean isSystem() { 2417 return isSystemKey(mKeyCode); 2418 } 2419 2420 /** @hide */ isWakeKey()2421 public final boolean isWakeKey() { 2422 return isWakeKey(mKeyCode); 2423 } 2424 2425 /** 2426 * Returns true if the specified keycode is a gamepad button. 2427 * @return True if the keycode is a gamepad button, such as {@link #KEYCODE_BUTTON_A}. 2428 */ isGamepadButton(int keyCode)2429 public static final boolean isGamepadButton(int keyCode) { 2430 switch (keyCode) { 2431 case KeyEvent.KEYCODE_BUTTON_A: 2432 case KeyEvent.KEYCODE_BUTTON_B: 2433 case KeyEvent.KEYCODE_BUTTON_C: 2434 case KeyEvent.KEYCODE_BUTTON_X: 2435 case KeyEvent.KEYCODE_BUTTON_Y: 2436 case KeyEvent.KEYCODE_BUTTON_Z: 2437 case KeyEvent.KEYCODE_BUTTON_L1: 2438 case KeyEvent.KEYCODE_BUTTON_R1: 2439 case KeyEvent.KEYCODE_BUTTON_L2: 2440 case KeyEvent.KEYCODE_BUTTON_R2: 2441 case KeyEvent.KEYCODE_BUTTON_THUMBL: 2442 case KeyEvent.KEYCODE_BUTTON_THUMBR: 2443 case KeyEvent.KEYCODE_BUTTON_START: 2444 case KeyEvent.KEYCODE_BUTTON_SELECT: 2445 case KeyEvent.KEYCODE_BUTTON_MODE: 2446 case KeyEvent.KEYCODE_BUTTON_1: 2447 case KeyEvent.KEYCODE_BUTTON_2: 2448 case KeyEvent.KEYCODE_BUTTON_3: 2449 case KeyEvent.KEYCODE_BUTTON_4: 2450 case KeyEvent.KEYCODE_BUTTON_5: 2451 case KeyEvent.KEYCODE_BUTTON_6: 2452 case KeyEvent.KEYCODE_BUTTON_7: 2453 case KeyEvent.KEYCODE_BUTTON_8: 2454 case KeyEvent.KEYCODE_BUTTON_9: 2455 case KeyEvent.KEYCODE_BUTTON_10: 2456 case KeyEvent.KEYCODE_BUTTON_11: 2457 case KeyEvent.KEYCODE_BUTTON_12: 2458 case KeyEvent.KEYCODE_BUTTON_13: 2459 case KeyEvent.KEYCODE_BUTTON_14: 2460 case KeyEvent.KEYCODE_BUTTON_15: 2461 case KeyEvent.KEYCODE_BUTTON_16: 2462 return true; 2463 default: 2464 return false; 2465 } 2466 } 2467 2468 /** Whether key will, by default, trigger a click on the focused view. 2469 * @hide 2470 */ 2471 @UnsupportedAppUsage isConfirmKey(int keyCode)2472 public static final boolean isConfirmKey(int keyCode) { 2473 switch (keyCode) { 2474 case KeyEvent.KEYCODE_DPAD_CENTER: 2475 case KeyEvent.KEYCODE_ENTER: 2476 case KeyEvent.KEYCODE_SPACE: 2477 case KeyEvent.KEYCODE_NUMPAD_ENTER: 2478 return true; 2479 default: 2480 return false; 2481 } 2482 } 2483 2484 /** 2485 * Returns whether this key will be sent to the {@link 2486 * android.media.session.MediaSession.Callback} if not handled. 2487 * 2488 * <p>The following key codes are considered {@link android.media.session.MediaSession} keys: 2489 * 2490 * <ul> 2491 * <li>{@link #KEYCODE_MEDIA_PLAY} 2492 * <li>{@link #KEYCODE_MEDIA_PAUSE} 2493 * <li>{@link #KEYCODE_MEDIA_PLAY_PAUSE} 2494 * <li>{@link #KEYCODE_HEADSETHOOK} 2495 * <li>{@link #KEYCODE_MEDIA_STOP} 2496 * <li>{@link #KEYCODE_MEDIA_NEXT} 2497 * <li>{@link #KEYCODE_MEDIA_PREVIOUS} 2498 * <li>{@link #KEYCODE_MEDIA_REWIND} 2499 * <li>{@link #KEYCODE_MEDIA_RECORD} 2500 * <li>{@link #KEYCODE_MEDIA_FAST_FORWARD} 2501 * </ul> 2502 */ isMediaSessionKey(int keyCode)2503 public static final boolean isMediaSessionKey(int keyCode) { 2504 switch (keyCode) { 2505 case KeyEvent.KEYCODE_MEDIA_PLAY: 2506 case KeyEvent.KEYCODE_MEDIA_PAUSE: 2507 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: 2508 case KeyEvent.KEYCODE_HEADSETHOOK: 2509 case KeyEvent.KEYCODE_MEDIA_STOP: 2510 case KeyEvent.KEYCODE_MEDIA_NEXT: 2511 case KeyEvent.KEYCODE_MEDIA_PREVIOUS: 2512 case KeyEvent.KEYCODE_MEDIA_REWIND: 2513 case KeyEvent.KEYCODE_MEDIA_RECORD: 2514 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD: 2515 return true; 2516 } 2517 return false; 2518 } 2519 2520 /** Is this a system key? System keys can not be used for menu shortcuts. 2521 * @hide 2522 */ isSystemKey(int keyCode)2523 public static final boolean isSystemKey(int keyCode) { 2524 switch (keyCode) { 2525 case KeyEvent.KEYCODE_MENU: 2526 case KeyEvent.KEYCODE_SOFT_RIGHT: 2527 case KeyEvent.KEYCODE_HOME: 2528 case KeyEvent.KEYCODE_RECENT_APPS: 2529 case KeyEvent.KEYCODE_BACK: 2530 case KeyEvent.KEYCODE_CALL: 2531 case KeyEvent.KEYCODE_ENDCALL: 2532 case KeyEvent.KEYCODE_VOLUME_UP: 2533 case KeyEvent.KEYCODE_VOLUME_DOWN: 2534 case KeyEvent.KEYCODE_VOLUME_MUTE: 2535 case KeyEvent.KEYCODE_MUTE: 2536 case KeyEvent.KEYCODE_POWER: 2537 case KeyEvent.KEYCODE_HEADSETHOOK: 2538 case KeyEvent.KEYCODE_MEDIA_PLAY: 2539 case KeyEvent.KEYCODE_MEDIA_PAUSE: 2540 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: 2541 case KeyEvent.KEYCODE_MEDIA_STOP: 2542 case KeyEvent.KEYCODE_MEDIA_NEXT: 2543 case KeyEvent.KEYCODE_MEDIA_PREVIOUS: 2544 case KeyEvent.KEYCODE_MEDIA_REWIND: 2545 case KeyEvent.KEYCODE_MEDIA_RECORD: 2546 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD: 2547 case KeyEvent.KEYCODE_CAMERA: 2548 case KeyEvent.KEYCODE_FOCUS: 2549 case KeyEvent.KEYCODE_SEARCH: 2550 case KeyEvent.KEYCODE_BRIGHTNESS_DOWN: 2551 case KeyEvent.KEYCODE_BRIGHTNESS_UP: 2552 case KeyEvent.KEYCODE_KEYBOARD_BACKLIGHT_DOWN: 2553 case KeyEvent.KEYCODE_KEYBOARD_BACKLIGHT_UP: 2554 case KeyEvent.KEYCODE_KEYBOARD_BACKLIGHT_TOGGLE: 2555 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK: 2556 case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_UP: 2557 case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_DOWN: 2558 case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_LEFT: 2559 case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_RIGHT: 2560 case KeyEvent.KEYCODE_STEM_PRIMARY: 2561 return true; 2562 } 2563 2564 return false; 2565 } 2566 2567 /** @hide */ isWakeKey(int keyCode)2568 public static final boolean isWakeKey(int keyCode) { 2569 switch (keyCode) { 2570 case KeyEvent.KEYCODE_CAMERA: 2571 case KeyEvent.KEYCODE_MENU: 2572 case KeyEvent.KEYCODE_PAIRING: 2573 case KeyEvent.KEYCODE_STEM_1: 2574 case KeyEvent.KEYCODE_STEM_2: 2575 case KeyEvent.KEYCODE_STEM_3: 2576 case KeyEvent.KEYCODE_WAKEUP: 2577 case KeyEvent.KEYCODE_STEM_PRIMARY: 2578 return true; 2579 } 2580 return false; 2581 } 2582 2583 /** @hide */ isMetaKey(int keyCode)2584 public static final boolean isMetaKey(int keyCode) { 2585 return keyCode == KeyEvent.KEYCODE_META_LEFT || keyCode == KeyEvent.KEYCODE_META_RIGHT; 2586 } 2587 2588 /** @hide */ isAltKey(int keyCode)2589 public static final boolean isAltKey(int keyCode) { 2590 return keyCode == KeyEvent.KEYCODE_ALT_LEFT || keyCode == KeyEvent.KEYCODE_ALT_RIGHT; 2591 } 2592 2593 /** 2594 * Returns whether the key code passed as argument is allowed for visible background users. 2595 * Visible background users are expected to run on secondary displays with certain limitations 2596 * on system keys. 2597 * 2598 * @hide 2599 */ isVisibleBackgroundUserAllowedKey(int keyCode)2600 public static boolean isVisibleBackgroundUserAllowedKey(int keyCode) { 2601 switch (keyCode) { 2602 case KeyEvent.KEYCODE_POWER: 2603 case KeyEvent.KEYCODE_SLEEP: 2604 case KeyEvent.KEYCODE_WAKEUP: 2605 case KeyEvent.KEYCODE_CALL: 2606 case KeyEvent.KEYCODE_ENDCALL: 2607 case KeyEvent.KEYCODE_ASSIST: 2608 case KeyEvent.KEYCODE_VOICE_ASSIST: 2609 case KeyEvent.KEYCODE_MUTE: 2610 case KeyEvent.KEYCODE_VOLUME_MUTE: 2611 case KeyEvent.KEYCODE_RECENT_APPS: 2612 case KeyEvent.KEYCODE_APP_SWITCH: 2613 case KeyEvent.KEYCODE_NOTIFICATION: 2614 return false; 2615 } 2616 return true; 2617 } 2618 2619 /** {@inheritDoc} */ 2620 @Override getDeviceId()2621 public final int getDeviceId() { 2622 return mDeviceId; 2623 } 2624 2625 /** {@inheritDoc} */ 2626 @Override getSource()2627 public final int getSource() { 2628 return mSource; 2629 } 2630 2631 /** {@inheritDoc} */ 2632 @Override setSource(int source)2633 public final void setSource(int source) { 2634 mSource = source; 2635 } 2636 2637 /** @hide */ 2638 @TestApi 2639 @Override getDisplayId()2640 public final int getDisplayId() { 2641 return mDisplayId; 2642 } 2643 2644 /** @hide */ 2645 @TestApi 2646 @Override setDisplayId(int displayId)2647 public final void setDisplayId(int displayId) { 2648 mDisplayId = displayId; 2649 } 2650 2651 /** 2652 * <p>Returns the state of the meta keys.</p> 2653 * 2654 * @return an integer in which each bit set to 1 represents a pressed 2655 * meta key 2656 * 2657 * @see #isAltPressed() 2658 * @see #isShiftPressed() 2659 * @see #isSymPressed() 2660 * @see #isCtrlPressed() 2661 * @see #isMetaPressed() 2662 * @see #isFunctionPressed() 2663 * @see #isCapsLockOn() 2664 * @see #isNumLockOn() 2665 * @see #isScrollLockOn() 2666 * @see #META_ALT_ON 2667 * @see #META_ALT_LEFT_ON 2668 * @see #META_ALT_RIGHT_ON 2669 * @see #META_SHIFT_ON 2670 * @see #META_SHIFT_LEFT_ON 2671 * @see #META_SHIFT_RIGHT_ON 2672 * @see #META_SYM_ON 2673 * @see #META_FUNCTION_ON 2674 * @see #META_CTRL_ON 2675 * @see #META_CTRL_LEFT_ON 2676 * @see #META_CTRL_RIGHT_ON 2677 * @see #META_META_ON 2678 * @see #META_META_LEFT_ON 2679 * @see #META_META_RIGHT_ON 2680 * @see #META_CAPS_LOCK_ON 2681 * @see #META_NUM_LOCK_ON 2682 * @see #META_SCROLL_LOCK_ON 2683 * @see #getModifiers 2684 */ getMetaState()2685 public final int getMetaState() { 2686 return mMetaState; 2687 } 2688 2689 /** 2690 * Returns the state of the modifier keys. 2691 * <p> 2692 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK}, 2693 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are 2694 * not considered modifier keys. Consequently, this function specifically masks out 2695 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}. 2696 * </p><p> 2697 * The value returned consists of the meta state (from {@link #getMetaState}) 2698 * normalized using {@link #normalizeMetaState(int)} and then masked with 2699 * {@link #getModifierMetaStateMask} so that only valid modifier bits are retained. 2700 * </p> 2701 * 2702 * @return An integer in which each bit set to 1 represents a pressed modifier key. 2703 * @see #getMetaState 2704 */ getModifiers()2705 public final int getModifiers() { 2706 return normalizeMetaState(mMetaState) & META_MODIFIER_MASK; 2707 } 2708 2709 /** 2710 * Modifies the flags of the event. 2711 * 2712 * @param newFlags New flags for the event, replacing the entire value. 2713 * @hide 2714 */ setFlags(int newFlags)2715 public final void setFlags(int newFlags) { 2716 mFlags = newFlags; 2717 } 2718 2719 /** 2720 * Returns the flags for this key event. 2721 * 2722 * @see #FLAG_WOKE_HERE 2723 */ getFlags()2724 public final int getFlags() { 2725 return mFlags; 2726 } 2727 2728 // Mask of all modifier key meta states. Specifically excludes locked keys like caps lock. 2729 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 2730 private static final int META_MODIFIER_MASK = 2731 META_SHIFT_ON | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON 2732 | META_ALT_ON | META_ALT_LEFT_ON | META_ALT_RIGHT_ON 2733 | META_CTRL_ON | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON 2734 | META_META_ON | META_META_LEFT_ON | META_META_RIGHT_ON 2735 | META_SYM_ON | META_FUNCTION_ON; 2736 2737 // Mask of all lock key meta states. 2738 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 2739 private static final int META_LOCK_MASK = 2740 META_CAPS_LOCK_ON | META_NUM_LOCK_ON | META_SCROLL_LOCK_ON; 2741 2742 // Mask of all valid meta states. 2743 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 2744 private static final int META_ALL_MASK = META_MODIFIER_MASK | META_LOCK_MASK; 2745 2746 // Mask of all synthetic meta states that are reserved for API compatibility with 2747 // historical uses in MetaKeyKeyListener. 2748 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 2749 private static final int META_SYNTHETIC_MASK = 2750 META_CAP_LOCKED | META_ALT_LOCKED | META_SYM_LOCKED | META_SELECTING; 2751 2752 // Mask of all meta states that are not valid use in specifying a modifier key. 2753 // These bits are known to be used for purposes other than specifying modifiers. 2754 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 2755 private static final int META_INVALID_MODIFIER_MASK = 2756 META_LOCK_MASK | META_SYNTHETIC_MASK; 2757 2758 /** 2759 * Gets a mask that includes all valid modifier key meta state bits. 2760 * <p> 2761 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK}, 2762 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are 2763 * not considered modifier keys. Consequently, the mask specifically excludes 2764 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}. 2765 * </p> 2766 * 2767 * @return The modifier meta state mask which is a combination of 2768 * {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON}, {@link #META_SHIFT_RIGHT_ON}, 2769 * {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON}, {@link #META_ALT_RIGHT_ON}, 2770 * {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON}, {@link #META_CTRL_RIGHT_ON}, 2771 * {@link #META_META_ON}, {@link #META_META_LEFT_ON}, {@link #META_META_RIGHT_ON}, 2772 * {@link #META_SYM_ON}, {@link #META_FUNCTION_ON}. 2773 */ getModifierMetaStateMask()2774 public static int getModifierMetaStateMask() { 2775 return META_MODIFIER_MASK; 2776 } 2777 2778 /** 2779 * Returns true if this key code is a modifier key. 2780 * <p> 2781 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK}, 2782 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are 2783 * not considered modifier keys. Consequently, this function return false 2784 * for those keys. 2785 * </p> 2786 * 2787 * @return True if the key code is one of 2788 * {@link #KEYCODE_SHIFT_LEFT} {@link #KEYCODE_SHIFT_RIGHT}, 2789 * {@link #KEYCODE_ALT_LEFT}, {@link #KEYCODE_ALT_RIGHT}, 2790 * {@link #KEYCODE_CTRL_LEFT}, {@link #KEYCODE_CTRL_RIGHT}, 2791 * {@link #KEYCODE_META_LEFT}, or {@link #KEYCODE_META_RIGHT}, 2792 * {@link #KEYCODE_SYM}, {@link #KEYCODE_NUM}, {@link #KEYCODE_FUNCTION}. 2793 */ isModifierKey(int keyCode)2794 public static boolean isModifierKey(int keyCode) { 2795 switch (keyCode) { 2796 case KEYCODE_SHIFT_LEFT: 2797 case KEYCODE_SHIFT_RIGHT: 2798 case KEYCODE_ALT_LEFT: 2799 case KEYCODE_ALT_RIGHT: 2800 case KEYCODE_CTRL_LEFT: 2801 case KEYCODE_CTRL_RIGHT: 2802 case KEYCODE_META_LEFT: 2803 case KEYCODE_META_RIGHT: 2804 case KEYCODE_SYM: 2805 case KEYCODE_NUM: 2806 case KEYCODE_FUNCTION: 2807 return true; 2808 default: 2809 return false; 2810 } 2811 } 2812 2813 /** 2814 * Normalizes the specified meta state. 2815 * <p> 2816 * The meta state is normalized such that if either the left or right modifier meta state 2817 * bits are set then the result will also include the universal bit for that modifier. 2818 * </p><p> 2819 * If the specified meta state contains {@link #META_ALT_LEFT_ON} then 2820 * the result will also contain {@link #META_ALT_ON} in addition to {@link #META_ALT_LEFT_ON} 2821 * and the other bits that were specified in the input. The same is process is 2822 * performed for shift, control and meta. 2823 * </p><p> 2824 * If the specified meta state contains synthetic meta states defined by 2825 * {@link MetaKeyKeyListener}, then those states are translated here and the original 2826 * synthetic meta states are removed from the result. 2827 * {@link MetaKeyKeyListener#META_CAP_LOCKED} is translated to {@link #META_CAPS_LOCK_ON}. 2828 * {@link MetaKeyKeyListener#META_ALT_LOCKED} is translated to {@link #META_ALT_ON}. 2829 * {@link MetaKeyKeyListener#META_SYM_LOCKED} is translated to {@link #META_SYM_ON}. 2830 * </p><p> 2831 * Undefined meta state bits are removed. 2832 * </p> 2833 * 2834 * @param metaState The meta state. 2835 * @return The normalized meta state. 2836 */ normalizeMetaState(int metaState)2837 public static int normalizeMetaState(int metaState) { 2838 if ((metaState & (META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON)) != 0) { 2839 metaState |= META_SHIFT_ON; 2840 } 2841 if ((metaState & (META_ALT_LEFT_ON | META_ALT_RIGHT_ON)) != 0) { 2842 metaState |= META_ALT_ON; 2843 } 2844 if ((metaState & (META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON)) != 0) { 2845 metaState |= META_CTRL_ON; 2846 } 2847 if ((metaState & (META_META_LEFT_ON | META_META_RIGHT_ON)) != 0) { 2848 metaState |= META_META_ON; 2849 } 2850 if ((metaState & MetaKeyKeyListener.META_CAP_LOCKED) != 0) { 2851 metaState |= META_CAPS_LOCK_ON; 2852 } 2853 if ((metaState & MetaKeyKeyListener.META_ALT_LOCKED) != 0) { 2854 metaState |= META_ALT_ON; 2855 } 2856 if ((metaState & MetaKeyKeyListener.META_SYM_LOCKED) != 0) { 2857 metaState |= META_SYM_ON; 2858 } 2859 return metaState & META_ALL_MASK; 2860 } 2861 2862 /** 2863 * Returns true if no modifiers keys are pressed according to the specified meta state. 2864 * <p> 2865 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK}, 2866 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are 2867 * not considered modifier keys. Consequently, this function ignores 2868 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}. 2869 * </p><p> 2870 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}. 2871 * </p> 2872 * 2873 * @param metaState The meta state to consider. 2874 * @return True if no modifier keys are pressed. 2875 * @see #hasNoModifiers() 2876 */ metaStateHasNoModifiers(int metaState)2877 public static boolean metaStateHasNoModifiers(int metaState) { 2878 return (normalizeMetaState(metaState) & META_MODIFIER_MASK) == 0; 2879 } 2880 2881 /** 2882 * Returns true if only the specified modifier keys are pressed according to 2883 * the specified meta state. Returns false if a different combination of modifier 2884 * keys are pressed. 2885 * <p> 2886 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK}, 2887 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are 2888 * not considered modifier keys. Consequently, this function ignores 2889 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}. 2890 * </p><p> 2891 * If the specified modifier mask includes directional modifiers, such as 2892 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the 2893 * modifier is pressed on that side. 2894 * If the specified modifier mask includes non-directional modifiers, such as 2895 * {@link #META_SHIFT_ON}, then this method ensures that the modifier 2896 * is pressed on either side. 2897 * If the specified modifier mask includes both directional and non-directional modifiers 2898 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON}, 2899 * then this method throws an illegal argument exception. 2900 * </p> 2901 * 2902 * @param metaState The meta state to consider. 2903 * @param modifiers The meta state of the modifier keys to check. May be a combination 2904 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to 2905 * ensure that no modifier keys are pressed. 2906 * @return True if only the specified modifier keys are pressed. 2907 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers 2908 * @see #hasModifiers 2909 */ metaStateHasModifiers(int metaState, int modifiers)2910 public static boolean metaStateHasModifiers(int metaState, int modifiers) { 2911 // Note: For forward compatibility, we allow the parameter to contain meta states 2912 // that we do not recognize but we explicitly disallow meta states that 2913 // are not valid modifiers. 2914 if ((modifiers & META_INVALID_MODIFIER_MASK) != 0) { 2915 throw new IllegalArgumentException("modifiers must not contain " 2916 + "META_CAPS_LOCK_ON, META_NUM_LOCK_ON, META_SCROLL_LOCK_ON, " 2917 + "META_CAP_LOCKED, META_ALT_LOCKED, META_SYM_LOCKED, " 2918 + "or META_SELECTING"); 2919 } 2920 2921 metaState = normalizeMetaState(metaState) & META_MODIFIER_MASK; 2922 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers, 2923 META_SHIFT_ON, META_SHIFT_LEFT_ON, META_SHIFT_RIGHT_ON); 2924 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers, 2925 META_ALT_ON, META_ALT_LEFT_ON, META_ALT_RIGHT_ON); 2926 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers, 2927 META_CTRL_ON, META_CTRL_LEFT_ON, META_CTRL_RIGHT_ON); 2928 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers, 2929 META_META_ON, META_META_LEFT_ON, META_META_RIGHT_ON); 2930 return metaState == modifiers; 2931 } 2932 metaStateFilterDirectionalModifiers(int metaState, int modifiers, int basic, int left, int right)2933 private static int metaStateFilterDirectionalModifiers(int metaState, 2934 int modifiers, int basic, int left, int right) { 2935 final boolean wantBasic = (modifiers & basic) != 0; 2936 final int directional = left | right; 2937 final boolean wantLeftOrRight = (modifiers & directional) != 0; 2938 2939 if (wantBasic) { 2940 if (wantLeftOrRight) { 2941 throw new IllegalArgumentException("modifiers must not contain " 2942 + metaStateToString(basic) + " combined with " 2943 + metaStateToString(left) + " or " + metaStateToString(right)); 2944 } 2945 return metaState & ~directional; 2946 } else if (wantLeftOrRight) { 2947 return metaState & ~basic; 2948 } else { 2949 return metaState; 2950 } 2951 } 2952 2953 /** 2954 * Returns true if no modifier keys are pressed. 2955 * <p> 2956 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK}, 2957 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are 2958 * not considered modifier keys. Consequently, this function ignores 2959 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}. 2960 * </p><p> 2961 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}. 2962 * </p> 2963 * 2964 * @return True if no modifier keys are pressed. 2965 * @see #metaStateHasNoModifiers 2966 */ hasNoModifiers()2967 public final boolean hasNoModifiers() { 2968 return metaStateHasNoModifiers(mMetaState); 2969 } 2970 2971 /** 2972 * Returns true if only the specified modifiers keys are pressed. 2973 * Returns false if a different combination of modifier keys are pressed. 2974 * <p> 2975 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK}, 2976 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are 2977 * not considered modifier keys. Consequently, this function ignores 2978 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}. 2979 * </p><p> 2980 * If the specified modifier mask includes directional modifiers, such as 2981 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the 2982 * modifier is pressed on that side. 2983 * If the specified modifier mask includes non-directional modifiers, such as 2984 * {@link #META_SHIFT_ON}, then this method ensures that the modifier 2985 * is pressed on either side. 2986 * If the specified modifier mask includes both directional and non-directional modifiers 2987 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON}, 2988 * then this method throws an illegal argument exception. 2989 * </p> 2990 * 2991 * @param modifiers The meta state of the modifier keys to check. May be a combination 2992 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to 2993 * ensure that no modifier keys are pressed. 2994 * @return True if only the specified modifier keys are pressed. 2995 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers 2996 * @see #metaStateHasModifiers 2997 */ hasModifiers(int modifiers)2998 public final boolean hasModifiers(int modifiers) { 2999 return metaStateHasModifiers(mMetaState, modifiers); 3000 } 3001 3002 /** 3003 * <p>Returns the pressed state of the ALT meta key.</p> 3004 * 3005 * @return true if the ALT key is pressed, false otherwise 3006 * 3007 * @see #KEYCODE_ALT_LEFT 3008 * @see #KEYCODE_ALT_RIGHT 3009 * @see #META_ALT_ON 3010 */ isAltPressed()3011 public final boolean isAltPressed() { 3012 return (mMetaState & META_ALT_ON) != 0; 3013 } 3014 3015 /** 3016 * <p>Returns the pressed state of the SHIFT meta key.</p> 3017 * 3018 * @return true if the SHIFT key is pressed, false otherwise 3019 * 3020 * @see #KEYCODE_SHIFT_LEFT 3021 * @see #KEYCODE_SHIFT_RIGHT 3022 * @see #META_SHIFT_ON 3023 */ isShiftPressed()3024 public final boolean isShiftPressed() { 3025 return (mMetaState & META_SHIFT_ON) != 0; 3026 } 3027 3028 /** 3029 * <p>Returns the pressed state of the SYM meta key.</p> 3030 * 3031 * @return true if the SYM key is pressed, false otherwise 3032 * 3033 * @see #KEYCODE_SYM 3034 * @see #META_SYM_ON 3035 */ isSymPressed()3036 public final boolean isSymPressed() { 3037 return (mMetaState & META_SYM_ON) != 0; 3038 } 3039 3040 /** 3041 * <p>Returns the pressed state of the CTRL meta key.</p> 3042 * 3043 * @return true if the CTRL key is pressed, false otherwise 3044 * 3045 * @see #KEYCODE_CTRL_LEFT 3046 * @see #KEYCODE_CTRL_RIGHT 3047 * @see #META_CTRL_ON 3048 */ isCtrlPressed()3049 public final boolean isCtrlPressed() { 3050 return (mMetaState & META_CTRL_ON) != 0; 3051 } 3052 3053 /** 3054 * <p>Returns the pressed state of the META meta key.</p> 3055 * 3056 * @return true if the META key is pressed, false otherwise 3057 * 3058 * @see #KEYCODE_META_LEFT 3059 * @see #KEYCODE_META_RIGHT 3060 * @see #META_META_ON 3061 */ isMetaPressed()3062 public final boolean isMetaPressed() { 3063 return (mMetaState & META_META_ON) != 0; 3064 } 3065 3066 /** 3067 * <p>Returns the pressed state of the FUNCTION meta key.</p> 3068 * 3069 * @return true if the FUNCTION key is pressed, false otherwise 3070 * 3071 * @see #KEYCODE_FUNCTION 3072 * @see #META_FUNCTION_ON 3073 */ isFunctionPressed()3074 public final boolean isFunctionPressed() { 3075 return (mMetaState & META_FUNCTION_ON) != 0; 3076 } 3077 3078 /** 3079 * <p>Returns the locked state of the CAPS LOCK meta key.</p> 3080 * 3081 * @return true if the CAPS LOCK key is on, false otherwise 3082 * 3083 * @see #KEYCODE_CAPS_LOCK 3084 * @see #META_CAPS_LOCK_ON 3085 */ isCapsLockOn()3086 public final boolean isCapsLockOn() { 3087 return (mMetaState & META_CAPS_LOCK_ON) != 0; 3088 } 3089 3090 /** 3091 * <p>Returns the locked state of the NUM LOCK meta key.</p> 3092 * 3093 * @return true if the NUM LOCK key is on, false otherwise 3094 * 3095 * @see #KEYCODE_NUM_LOCK 3096 * @see #META_NUM_LOCK_ON 3097 */ isNumLockOn()3098 public final boolean isNumLockOn() { 3099 return (mMetaState & META_NUM_LOCK_ON) != 0; 3100 } 3101 3102 /** 3103 * <p>Returns the locked state of the SCROLL LOCK meta key.</p> 3104 * 3105 * @return true if the SCROLL LOCK key is on, false otherwise 3106 * 3107 * @see #KEYCODE_SCROLL_LOCK 3108 * @see #META_SCROLL_LOCK_ON 3109 */ isScrollLockOn()3110 public final boolean isScrollLockOn() { 3111 return (mMetaState & META_SCROLL_LOCK_ON) != 0; 3112 } 3113 3114 /** 3115 * Retrieve the action of this key event. May be either 3116 * {@link #ACTION_DOWN}, {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 3117 * 3118 * @return The event action: ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE. 3119 */ getAction()3120 public final int getAction() { 3121 return mAction; 3122 } 3123 3124 /** 3125 * For {@link #ACTION_UP} events, indicates that the event has been 3126 * canceled as per {@link #FLAG_CANCELED}. 3127 */ isCanceled()3128 public final boolean isCanceled() { 3129 return (mFlags&FLAG_CANCELED) != 0; 3130 } 3131 3132 /** 3133 * Set {@link #FLAG_CANCELED} flag for the key event. 3134 * 3135 * @hide 3136 */ 3137 @Override cancel()3138 public final void cancel() { 3139 mFlags |= FLAG_CANCELED; 3140 } 3141 3142 /** 3143 * Call this during {@link Callback#onKeyDown} to have the system track 3144 * the key through its final up (possibly including a long press). Note 3145 * that only one key can be tracked at a time -- if another key down 3146 * event is received while a previous one is being tracked, tracking is 3147 * stopped on the previous event. 3148 */ startTracking()3149 public final void startTracking() { 3150 mFlags |= FLAG_START_TRACKING; 3151 } 3152 3153 /** 3154 * For {@link #ACTION_UP} events, indicates that the event is still being 3155 * tracked from its initial down event as per 3156 * {@link #FLAG_TRACKING}. 3157 */ isTracking()3158 public final boolean isTracking() { 3159 return (mFlags&FLAG_TRACKING) != 0; 3160 } 3161 3162 /** 3163 * For {@link #ACTION_DOWN} events, indicates that the event has been 3164 * canceled as per {@link #FLAG_LONG_PRESS}. 3165 */ isLongPress()3166 public final boolean isLongPress() { 3167 return (mFlags&FLAG_LONG_PRESS) != 0; 3168 } 3169 3170 /** 3171 * Retrieve the key code of the key event. This is the physical key that 3172 * was pressed, <em>not</em> the Unicode character. 3173 * 3174 * @return The key code of the event. 3175 */ getKeyCode()3176 public final int getKeyCode() { 3177 return mKeyCode; 3178 } 3179 3180 /** 3181 * For the special case of a {@link #ACTION_MULTIPLE} event with key 3182 * code of {@link #KEYCODE_UNKNOWN}, this is a raw string of characters 3183 * associated with the event. In all other cases it is null. 3184 * 3185 * @return Returns a String of 1 or more characters associated with 3186 * the event. 3187 * 3188 * @deprecated no longer used by the input system. 3189 */ 3190 @Deprecated getCharacters()3191 public final String getCharacters() { 3192 return mCharacters; 3193 } 3194 3195 /** 3196 * Retrieve the hardware key id of this key event. These values are not 3197 * reliable and vary from device to device. 3198 * 3199 * {@more} 3200 * Mostly this is here for debugging purposes. 3201 */ getScanCode()3202 public final int getScanCode() { 3203 return mScanCode; 3204 } 3205 3206 /** 3207 * Retrieve the repeat count of the event. For key down events, 3208 * this is the number of times the key has repeated with the first 3209 * down starting at 0 and counting up from there. For key up events, 3210 * this is always equal to zero. For multiple key events, 3211 * this is the number of down/up pairs that have occurred. 3212 * 3213 * @return The number of times the key has repeated. 3214 */ getRepeatCount()3215 public final int getRepeatCount() { 3216 return mRepeatCount; 3217 } 3218 3219 /** 3220 * Modifies the down time and the event time of the event. 3221 * 3222 * @param downTime The new down time (in {@link android.os.SystemClock#uptimeMillis}) of the 3223 * event. 3224 * @param eventTime The new event time (in {@link android.os.SystemClock#uptimeMillis}) of the 3225 * event. 3226 * @hide 3227 */ setTime(long downTime, long eventTime)3228 public final void setTime(long downTime, long eventTime) { 3229 mDownTime = TimeUnit.NANOSECONDS.convert(downTime, TimeUnit.MILLISECONDS); 3230 mEventTime = TimeUnit.NANOSECONDS.convert(eventTime, TimeUnit.MILLISECONDS); 3231 } 3232 3233 /** 3234 * Retrieve the time of the most recent key down event, 3235 * in the {@link android.os.SystemClock#uptimeMillis} time base. If this 3236 * is a down event, this will be the same as {@link #getEventTime()}. 3237 * Note that when chording keys, this value is the down time of the 3238 * most recently pressed key, which may <em>not</em> be the same physical 3239 * key of this event. 3240 * 3241 * @return Returns the most recent key down time, in the 3242 * {@link android.os.SystemClock#uptimeMillis} time base 3243 */ getDownTime()3244 public final long getDownTime() { 3245 return TimeUnit.MILLISECONDS.convert(mDownTime, TimeUnit.NANOSECONDS); 3246 } 3247 3248 /** 3249 * Retrieve the time this event occurred, 3250 * in the {@link android.os.SystemClock#uptimeMillis} time base. 3251 * 3252 * @return Returns the time this event occurred, 3253 * in the {@link android.os.SystemClock#uptimeMillis} time base. 3254 */ 3255 @Override getEventTime()3256 public final long getEventTime() { 3257 return TimeUnit.MILLISECONDS.convert(mEventTime, TimeUnit.NANOSECONDS); 3258 } 3259 3260 /** 3261 * Retrieve the time this event occurred, 3262 * in the {@link android.os.SystemClock#uptimeMillis} time base but with 3263 * nanosecond (instead of millisecond) precision. 3264 * <p> 3265 * The value is in nanosecond precision but it may not have nanosecond accuracy. 3266 * </p> 3267 * 3268 * @return Returns the time this event occurred, 3269 * in the {@link android.os.SystemClock#uptimeMillis} time base but with 3270 * nanosecond (instead of millisecond) precision. 3271 * 3272 * @hide 3273 */ 3274 @Override getEventTimeNanos()3275 public final long getEventTimeNanos() { 3276 return mEventTime; 3277 } 3278 3279 /** 3280 * Renamed to {@link #getDeviceId}. 3281 * 3282 * @hide 3283 * @deprecated use {@link #getDeviceId()} instead. 3284 */ 3285 @Deprecated getKeyboardDevice()3286 public final int getKeyboardDevice() { 3287 return mDeviceId; 3288 } 3289 3290 /** 3291 * Gets the {@link KeyCharacterMap} associated with the keyboard device. 3292 * 3293 * @return The associated key character map. 3294 * @throws {@link KeyCharacterMap.UnavailableException} if the key character map 3295 * could not be loaded because it was malformed or the default key character map 3296 * is missing from the system. 3297 * 3298 * @see KeyCharacterMap#load 3299 */ getKeyCharacterMap()3300 public final KeyCharacterMap getKeyCharacterMap() { 3301 return KeyCharacterMap.load(mDeviceId); 3302 } 3303 3304 /** 3305 * Gets the primary character for this key. 3306 * In other words, the label that is physically printed on it. 3307 * 3308 * @return The display label character, or 0 if none (eg. for non-printing keys). 3309 */ getDisplayLabel()3310 public char getDisplayLabel() { 3311 return getKeyCharacterMap().getDisplayLabel(mKeyCode); 3312 } 3313 3314 /** 3315 * Gets the Unicode character generated by the specified key and meta 3316 * key state combination. 3317 * <p> 3318 * Returns the Unicode character that the specified key would produce 3319 * when the specified meta bits (see {@link MetaKeyKeyListener}) 3320 * were active. 3321 * </p><p> 3322 * Returns 0 if the key is not one that is used to type Unicode 3323 * characters. 3324 * </p><p> 3325 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the 3326 * key is a "dead key" that should be combined with another to 3327 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} -- 3328 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}. 3329 * </p> 3330 * 3331 * @return The associated character or combining accent, or 0 if none. 3332 */ getUnicodeChar()3333 public int getUnicodeChar() { 3334 return getUnicodeChar(mMetaState); 3335 } 3336 3337 /** 3338 * Gets the Unicode character generated by the specified key and meta 3339 * key state combination. 3340 * <p> 3341 * Returns the Unicode character that the specified key would produce 3342 * when the specified meta bits (see {@link MetaKeyKeyListener}) 3343 * were active. 3344 * </p><p> 3345 * Returns 0 if the key is not one that is used to type Unicode 3346 * characters. 3347 * </p><p> 3348 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the 3349 * key is a "dead key" that should be combined with another to 3350 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} -- 3351 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}. 3352 * </p> 3353 * 3354 * @param metaState The meta key modifier state. 3355 * @return The associated character or combining accent, or 0 if none. 3356 */ getUnicodeChar(int metaState)3357 public int getUnicodeChar(int metaState) { 3358 return getKeyCharacterMap().get(mKeyCode, metaState); 3359 } 3360 3361 /** 3362 * Get the character conversion data for a given key code. 3363 * 3364 * @param results A {@link KeyCharacterMap.KeyData} instance that will be 3365 * filled with the results. 3366 * @return True if the key was mapped. If the key was not mapped, results is not modified. 3367 * 3368 * @deprecated instead use {@link #getDisplayLabel()}, 3369 * {@link #getNumber()} or {@link #getUnicodeChar(int)}. 3370 */ 3371 @Deprecated getKeyData(KeyData results)3372 public boolean getKeyData(KeyData results) { 3373 return getKeyCharacterMap().getKeyData(mKeyCode, results); 3374 } 3375 3376 /** 3377 * Gets the first character in the character array that can be generated 3378 * by the specified key code. 3379 * <p> 3380 * This is a convenience function that returns the same value as 3381 * {@link #getMatch(char[],int) getMatch(chars, 0)}. 3382 * </p> 3383 * 3384 * @param chars The array of matching characters to consider. 3385 * @return The matching associated character, or 0 if none. 3386 */ getMatch(char[] chars)3387 public char getMatch(char[] chars) { 3388 return getMatch(chars, 0); 3389 } 3390 3391 /** 3392 * Gets the first character in the character array that can be generated 3393 * by the specified key code. If there are multiple choices, prefers 3394 * the one that would be generated with the specified meta key modifier state. 3395 * 3396 * @param chars The array of matching characters to consider. 3397 * @param metaState The preferred meta key modifier state. 3398 * @return The matching associated character, or 0 if none. 3399 */ getMatch(char[] chars, int metaState)3400 public char getMatch(char[] chars, int metaState) { 3401 return getKeyCharacterMap().getMatch(mKeyCode, chars, metaState); 3402 } 3403 3404 /** 3405 * Gets the number or symbol associated with the key. 3406 * <p> 3407 * The character value is returned, not the numeric value. 3408 * If the key is not a number, but is a symbol, the symbol is retuned. 3409 * </p><p> 3410 * This method is intended to to support dial pads and other numeric or 3411 * symbolic entry on keyboards where certain keys serve dual function 3412 * as alphabetic and symbolic keys. This method returns the number 3413 * or symbol associated with the key independent of whether the user 3414 * has pressed the required modifier. 3415 * </p><p> 3416 * For example, on one particular keyboard the keys on the top QWERTY row generate 3417 * numbers when ALT is pressed such that ALT-Q maps to '1'. So for that keyboard 3418 * when {@link #getNumber} is called with {@link KeyEvent#KEYCODE_Q} it returns '1' 3419 * so that the user can type numbers without pressing ALT when it makes sense. 3420 * </p> 3421 * 3422 * @return The associated numeric or symbolic character, or 0 if none. 3423 */ getNumber()3424 public char getNumber() { 3425 return getKeyCharacterMap().getNumber(mKeyCode); 3426 } 3427 3428 /** 3429 * Returns true if this key produces a glyph. 3430 * 3431 * @return True if the key is a printing key. 3432 */ isPrintingKey()3433 public boolean isPrintingKey() { 3434 return getKeyCharacterMap().isPrintingKey(mKeyCode); 3435 } 3436 3437 /** 3438 * @deprecated Use {@link #dispatch(Callback, DispatcherState, Object)} instead. 3439 */ 3440 @Deprecated dispatch(Callback receiver)3441 public final boolean dispatch(Callback receiver) { 3442 return dispatch(receiver, null, null); 3443 } 3444 3445 /** 3446 * Deliver this key event to a {@link Callback} interface. If this is 3447 * an ACTION_MULTIPLE event and it is not handled, then an attempt will 3448 * be made to deliver a single normal event. 3449 * 3450 * @param receiver The Callback that will be given the event. 3451 * @param state State information retained across events. 3452 * @param target The target of the dispatch, for use in tracking. 3453 * 3454 * @return The return value from the Callback method that was called. 3455 */ dispatch(Callback receiver, DispatcherState state, Object target)3456 public final boolean dispatch(Callback receiver, DispatcherState state, 3457 Object target) { 3458 switch (mAction) { 3459 case ACTION_DOWN: { 3460 mFlags &= ~FLAG_START_TRACKING; 3461 if (DEBUG) Log.v(TAG, "Key down to " + target + " in " + state 3462 + ": " + this); 3463 boolean res = receiver.onKeyDown(mKeyCode, this); 3464 if (state != null) { 3465 if (res && mRepeatCount == 0 && (mFlags&FLAG_START_TRACKING) != 0) { 3466 if (DEBUG) Log.v(TAG, " Start tracking!"); 3467 state.startTracking(this, target); 3468 } else if (isLongPress() && state.isTracking(this)) { 3469 try { 3470 if (receiver.onKeyLongPress(mKeyCode, this)) { 3471 if (DEBUG) Log.v(TAG, " Clear from long press!"); 3472 state.performedLongPress(this); 3473 res = true; 3474 } 3475 } catch (AbstractMethodError e) { 3476 } 3477 } 3478 } 3479 return res; 3480 } 3481 case ACTION_UP: 3482 if (DEBUG) Log.v(TAG, "Key up to " + target + " in " + state 3483 + ": " + this); 3484 if (state != null) { 3485 state.handleUpEvent(this); 3486 } 3487 return receiver.onKeyUp(mKeyCode, this); 3488 case ACTION_MULTIPLE: 3489 final int count = mRepeatCount; 3490 final int code = mKeyCode; 3491 if (receiver.onKeyMultiple(code, count, this)) { 3492 return true; 3493 } 3494 if (code != KeyEvent.KEYCODE_UNKNOWN) { 3495 mAction = ACTION_DOWN; 3496 mRepeatCount = 0; 3497 boolean handled = receiver.onKeyDown(code, this); 3498 if (handled) { 3499 mAction = ACTION_UP; 3500 receiver.onKeyUp(code, this); 3501 } 3502 mAction = ACTION_MULTIPLE; 3503 mRepeatCount = count; 3504 return handled; 3505 } 3506 return false; 3507 } 3508 return false; 3509 } 3510 3511 /** 3512 * Use with {@link KeyEvent#dispatch(Callback, DispatcherState, Object)} 3513 * for more advanced key dispatching, such as long presses. 3514 */ 3515 public static class DispatcherState { 3516 int mDownKeyCode; 3517 Object mDownTarget; 3518 SparseIntArray mActiveLongPresses = new SparseIntArray(); 3519 3520 /** 3521 * Reset back to initial state. 3522 */ reset()3523 public void reset() { 3524 if (DEBUG) Log.v(TAG, "Reset: " + this); 3525 mDownKeyCode = 0; 3526 mDownTarget = null; 3527 mActiveLongPresses.clear(); 3528 } 3529 3530 /** 3531 * Stop any tracking associated with this target. 3532 */ reset(Object target)3533 public void reset(Object target) { 3534 if (mDownTarget == target) { 3535 if (DEBUG) Log.v(TAG, "Reset in " + target + ": " + this); 3536 mDownKeyCode = 0; 3537 mDownTarget = null; 3538 } 3539 } 3540 3541 /** 3542 * Start tracking the key code associated with the given event. This 3543 * can only be called on a key down. It will allow you to see any 3544 * long press associated with the key, and will result in 3545 * {@link KeyEvent#isTracking} return true on the long press and up 3546 * events. 3547 * 3548 * <p>This is only needed if you are directly dispatching events, rather 3549 * than handling them in {@link Callback#onKeyDown}. 3550 */ startTracking(KeyEvent event, Object target)3551 public void startTracking(KeyEvent event, Object target) { 3552 if (event.getAction() != ACTION_DOWN) { 3553 throw new IllegalArgumentException( 3554 "Can only start tracking on a down event"); 3555 } 3556 if (DEBUG) Log.v(TAG, "Start trackingt in " + target + ": " + this); 3557 mDownKeyCode = event.getKeyCode(); 3558 mDownTarget = target; 3559 } 3560 3561 /** 3562 * Return true if the key event is for a key code that is currently 3563 * being tracked by the dispatcher. 3564 */ isTracking(KeyEvent event)3565 public boolean isTracking(KeyEvent event) { 3566 return mDownKeyCode == event.getKeyCode(); 3567 } 3568 3569 /** 3570 * Keep track of the given event's key code as having performed an 3571 * action with a long press, so no action should occur on the up. 3572 * <p>This is only needed if you are directly dispatching events, rather 3573 * than handling them in {@link Callback#onKeyLongPress}. 3574 */ performedLongPress(KeyEvent event)3575 public void performedLongPress(KeyEvent event) { 3576 mActiveLongPresses.put(event.getKeyCode(), 1); 3577 } 3578 3579 /** 3580 * Handle key up event to stop tracking. This resets the dispatcher state, 3581 * and updates the key event state based on it. 3582 * <p>This is only needed if you are directly dispatching events, rather 3583 * than handling them in {@link Callback#onKeyUp}. 3584 */ handleUpEvent(KeyEvent event)3585 public void handleUpEvent(KeyEvent event) { 3586 final int keyCode = event.getKeyCode(); 3587 if (DEBUG) Log.v(TAG, "Handle key up " + event + ": " + this); 3588 int index = mActiveLongPresses.indexOfKey(keyCode); 3589 if (index >= 0) { 3590 if (DEBUG) Log.v(TAG, " Index: " + index); 3591 event.mFlags |= FLAG_CANCELED | FLAG_CANCELED_LONG_PRESS; 3592 mActiveLongPresses.removeAt(index); 3593 } 3594 if (mDownKeyCode == keyCode) { 3595 if (DEBUG) Log.v(TAG, " Tracking!"); 3596 event.mFlags |= FLAG_TRACKING; 3597 mDownKeyCode = 0; 3598 mDownTarget = null; 3599 } 3600 } 3601 } 3602 3603 @Override toString()3604 public String toString() { 3605 StringBuilder msg = new StringBuilder(); 3606 msg.append("KeyEvent { action=").append(actionToString(mAction)); 3607 msg.append(", keyCode=").append(keyCodeToString(mKeyCode)); 3608 msg.append(", scanCode=").append(mScanCode); 3609 if (mCharacters != null) { 3610 msg.append(", characters=\"").append(mCharacters).append("\""); 3611 } 3612 msg.append(", metaState=").append(metaStateToString(mMetaState)); 3613 msg.append(", flags=0x").append(Integer.toHexString(mFlags)); 3614 msg.append(", repeatCount=").append(mRepeatCount); 3615 msg.append(", eventTime=").append(mEventTime); 3616 msg.append(", downTime=").append(mDownTime); 3617 msg.append(", deviceId=").append(mDeviceId); 3618 msg.append(", source=0x").append(Integer.toHexString(mSource)); 3619 msg.append(", displayId=").append(mDisplayId); 3620 msg.append(" }"); 3621 return msg.toString(); 3622 } 3623 3624 /** 3625 * Returns a string that represents the symbolic name of the specified action 3626 * such as "ACTION_DOWN", or an equivalent numeric constant such as "35" if unknown. 3627 * 3628 * @param action The action. 3629 * @return The symbolic name of the specified action. 3630 * @hide 3631 */ 3632 @TestApi actionToString(int action)3633 public static String actionToString(int action) { 3634 switch (action) { 3635 case ACTION_DOWN: 3636 return "ACTION_DOWN"; 3637 case ACTION_UP: 3638 return "ACTION_UP"; 3639 case ACTION_MULTIPLE: 3640 return "ACTION_MULTIPLE"; 3641 default: 3642 return Integer.toString(action); 3643 } 3644 } 3645 3646 /** 3647 * Returns a string that represents the symbolic name of the specified keycode 3648 * such as "KEYCODE_A", "KEYCODE_DPAD_UP", or an equivalent numeric constant 3649 * such as "1001" if unknown. 3650 * 3651 * This function is intended to be used mostly for debugging, logging, and testing. It is not 3652 * locale-specific and is not intended to be used in a user-facing manner. 3653 * 3654 * @param keyCode The key code. 3655 * @return The symbolic name of the specified keycode. 3656 * 3657 * @see KeyCharacterMap#getDisplayLabel 3658 */ keyCodeToString(int keyCode)3659 public static String keyCodeToString(int keyCode) { 3660 String symbolicName = nativeKeyCodeToString(keyCode); 3661 return symbolicName != null ? LABEL_PREFIX + symbolicName : Integer.toString(keyCode); 3662 } 3663 3664 /** 3665 * Gets a keycode by its symbolic name such as "KEYCODE_A" or an equivalent 3666 * numeric constant such as "29". For symbolic names, 3667 * starting in {@link android.os.Build.VERSION_CODES#Q} the prefix "KEYCODE_" is optional. 3668 * 3669 * @param symbolicName The symbolic name of the keycode. 3670 * @return The keycode or {@link #KEYCODE_UNKNOWN} if not found. 3671 * @see #keyCodeToString(int) 3672 */ keyCodeFromString(@onNull String symbolicName)3673 public static int keyCodeFromString(@NonNull String symbolicName) { 3674 try { 3675 int keyCode = Integer.parseInt(symbolicName); 3676 if (keyCodeIsValid(keyCode)) { 3677 return keyCode; 3678 } 3679 } catch (NumberFormatException ex) { 3680 } 3681 3682 if (symbolicName.startsWith(LABEL_PREFIX)) { 3683 symbolicName = symbolicName.substring(LABEL_PREFIX.length()); 3684 } 3685 int keyCode = nativeKeyCodeFromString(symbolicName); 3686 if (keyCodeIsValid(keyCode)) { 3687 return keyCode; 3688 } 3689 return KEYCODE_UNKNOWN; 3690 } 3691 keyCodeIsValid(int keyCode)3692 private static boolean keyCodeIsValid(int keyCode) { 3693 return keyCode >= KEYCODE_UNKNOWN && keyCode <= LAST_KEYCODE; 3694 } 3695 3696 /** 3697 * Returns a string that represents the symbolic name of the specified combined meta 3698 * key modifier state flags such as "0", "META_SHIFT_ON", 3699 * "META_ALT_ON|META_SHIFT_ON" or an equivalent numeric constant such as "0x10000000" 3700 * if unknown. 3701 * 3702 * @param metaState The meta state. 3703 * @return The symbolic name of the specified combined meta state flags. 3704 * @hide 3705 */ metaStateToString(int metaState)3706 public static String metaStateToString(int metaState) { 3707 if (metaState == 0) { 3708 return "0"; 3709 } 3710 StringBuilder result = null; 3711 int i = 0; 3712 while (metaState != 0) { 3713 final boolean isSet = (metaState & 1) != 0; 3714 metaState >>>= 1; // unsigned shift! 3715 if (isSet) { 3716 final String name = META_SYMBOLIC_NAMES[i]; 3717 if (result == null) { 3718 if (metaState == 0) { 3719 return name; 3720 } 3721 result = new StringBuilder(name); 3722 } else { 3723 result.append('|'); 3724 result.append(name); 3725 } 3726 } 3727 i += 1; 3728 } 3729 return result.toString(); 3730 } 3731 3732 public static final @android.annotation.NonNull Parcelable.Creator<KeyEvent> CREATOR 3733 = new Parcelable.Creator<KeyEvent>() { 3734 @Override 3735 public KeyEvent createFromParcel(Parcel in) { 3736 in.readInt(); // skip token, we already know this is a KeyEvent 3737 return KeyEvent.createFromParcelBody(in); 3738 } 3739 3740 @Override 3741 public KeyEvent[] newArray(int size) { 3742 return new KeyEvent[size]; 3743 } 3744 }; 3745 3746 /** @hide */ createFromParcelBody(Parcel in)3747 public static KeyEvent createFromParcelBody(Parcel in) { 3748 return new KeyEvent(in); 3749 } 3750 KeyEvent(Parcel in)3751 private KeyEvent(Parcel in) { 3752 // NOTE: ideally this constructor should call the canonical one, but that would require 3753 // changing the order the fields are written to the parcel, which could break native code 3754 mId = in.readInt(); 3755 mDeviceId = in.readInt(); 3756 mSource = in.readInt(); 3757 mDisplayId = in.readInt(); 3758 mHmac = in.createByteArray(); 3759 mAction = in.readInt(); 3760 mKeyCode = in.readInt(); 3761 mRepeatCount = in.readInt(); 3762 mMetaState = in.readInt(); 3763 mScanCode = in.readInt(); 3764 mFlags = in.readInt(); 3765 mDownTime = in.readLong(); 3766 mEventTime = in.readLong(); 3767 mCharacters = in.readString(); 3768 } 3769 3770 @Override writeToParcel(Parcel out, int flags)3771 public void writeToParcel(Parcel out, int flags) { 3772 out.writeInt(PARCEL_TOKEN_KEY_EVENT); 3773 3774 out.writeInt(mId); 3775 out.writeInt(mDeviceId); 3776 out.writeInt(mSource); 3777 out.writeInt(mDisplayId); 3778 out.writeByteArray(mHmac); 3779 out.writeInt(mAction); 3780 out.writeInt(mKeyCode); 3781 out.writeInt(mRepeatCount); 3782 out.writeInt(mMetaState); 3783 out.writeInt(mScanCode); 3784 out.writeInt(mFlags); 3785 out.writeLong(mDownTime); 3786 out.writeLong(mEventTime); 3787 out.writeString(mCharacters); 3788 } 3789 } 3790