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 android.os.Parcel; 20 import android.os.Parcelable; 21 import android.text.method.MetaKeyKeyListener; 22 import android.util.Log; 23 import android.util.Slog; 24 import android.util.SparseArray; 25 import android.util.SparseIntArray; 26 import android.view.KeyCharacterMap; 27 import android.view.KeyCharacterMap.KeyData; 28 29 /** 30 * Object used to report key and button events. 31 * <p> 32 * Each key press is described by a sequence of key events. A key press 33 * starts with a key event with {@link #ACTION_DOWN}. If the key is held 34 * sufficiently long that it repeats, then the initial down is followed 35 * additional key events with {@link #ACTION_DOWN} and a non-zero value for 36 * {@link #getRepeatCount()}. The last key event is a {@link #ACTION_UP} 37 * for the key up. If the key press is canceled, the key up event will have the 38 * {@link #FLAG_CANCELED} flag set. 39 * </p><p> 40 * Key events are generally accompanied by a key code ({@link #getKeyCode()}), 41 * scan code ({@link #getScanCode()}) and meta state ({@link #getMetaState()}). 42 * Key code constants are defined in this class. Scan code constants are raw 43 * device-specific codes obtained from the OS and so are not generally meaningful 44 * to applications unless interpreted using the {@link KeyCharacterMap}. 45 * Meta states describe the pressed state of key modifiers 46 * such as {@link #META_SHIFT_ON} or {@link #META_ALT_ON}. 47 * </p><p> 48 * Key codes typically correspond one-to-one with individual keys on an input device. 49 * Many keys and key combinations serve quite different functions on different 50 * input devices so care must be taken when interpreting them. Always use the 51 * {@link KeyCharacterMap} associated with the input device when mapping keys 52 * to characters. Be aware that there may be multiple key input devices active 53 * at the same time and each will have its own key character map. 54 * </p><p> 55 * As soft input methods can use multiple and inventive ways of inputting text, 56 * there is no guarantee that any key press on a soft keyboard will generate a key 57 * event: this is left to the IME's discretion, and in fact sending such events is 58 * discouraged. You should never rely on receiving KeyEvents for any key on a soft 59 * input method. In particular, the default software keyboard will never send any 60 * key event to any application targetting Jelly Bean or later, and will only send 61 * events for some presses of the delete and return keys to applications targetting 62 * Ice Cream Sandwich or earlier. Be aware that other software input methods may 63 * never send key events regardless of the version. Consider using editor actions 64 * like {@link android.view.inputmethod.EditorInfo#IME_ACTION_DONE} if you need 65 * specific interaction with the software keyboard, as it gives more visibility to 66 * the user as to how your application will react to key presses. 67 * </p><p> 68 * When interacting with an IME, the framework may deliver key events 69 * with the special action {@link #ACTION_MULTIPLE} that either specifies 70 * that single repeated key code or a sequence of characters to insert. 71 * </p><p> 72 * In general, the framework cannot guarantee that the key events it delivers 73 * to a view always constitute complete key sequences since some events may be dropped 74 * or modified by containing views before they are delivered. The view implementation 75 * should be prepared to handle {@link #FLAG_CANCELED} and should tolerate anomalous 76 * situations such as receiving a new {@link #ACTION_DOWN} without first having 77 * received an {@link #ACTION_UP} for the prior key press. 78 * </p><p> 79 * Refer to {@link InputDevice} for more information about how different kinds of 80 * input devices and sources represent keys and buttons. 81 * </p> 82 */ 83 public class KeyEvent extends InputEvent implements Parcelable { 84 /** Key code constant: Unknown key code. */ 85 public static final int KEYCODE_UNKNOWN = 0; 86 /** Key code constant: Soft Left key. 87 * Usually situated below the display on phones and used as a multi-function 88 * feature key for selecting a software defined function shown on the bottom left 89 * of the display. */ 90 public static final int KEYCODE_SOFT_LEFT = 1; 91 /** Key code constant: Soft Right key. 92 * Usually situated below the display on phones and used as a multi-function 93 * feature key for selecting a software defined function shown on the bottom right 94 * of the display. */ 95 public static final int KEYCODE_SOFT_RIGHT = 2; 96 /** Key code constant: Home key. 97 * This key is handled by the framework and is never delivered to applications. */ 98 public static final int KEYCODE_HOME = 3; 99 /** Key code constant: Back key. */ 100 public static final int KEYCODE_BACK = 4; 101 /** Key code constant: Call key. */ 102 public static final int KEYCODE_CALL = 5; 103 /** Key code constant: End Call key. */ 104 public static final int KEYCODE_ENDCALL = 6; 105 /** Key code constant: '0' key. */ 106 public static final int KEYCODE_0 = 7; 107 /** Key code constant: '1' key. */ 108 public static final int KEYCODE_1 = 8; 109 /** Key code constant: '2' key. */ 110 public static final int KEYCODE_2 = 9; 111 /** Key code constant: '3' key. */ 112 public static final int KEYCODE_3 = 10; 113 /** Key code constant: '4' key. */ 114 public static final int KEYCODE_4 = 11; 115 /** Key code constant: '5' key. */ 116 public static final int KEYCODE_5 = 12; 117 /** Key code constant: '6' key. */ 118 public static final int KEYCODE_6 = 13; 119 /** Key code constant: '7' key. */ 120 public static final int KEYCODE_7 = 14; 121 /** Key code constant: '8' key. */ 122 public static final int KEYCODE_8 = 15; 123 /** Key code constant: '9' key. */ 124 public static final int KEYCODE_9 = 16; 125 /** Key code constant: '*' key. */ 126 public static final int KEYCODE_STAR = 17; 127 /** Key code constant: '#' key. */ 128 public static final int KEYCODE_POUND = 18; 129 /** Key code constant: Directional Pad Up key. 130 * May also be synthesized from trackball motions. */ 131 public static final int KEYCODE_DPAD_UP = 19; 132 /** Key code constant: Directional Pad Down key. 133 * May also be synthesized from trackball motions. */ 134 public static final int KEYCODE_DPAD_DOWN = 20; 135 /** Key code constant: Directional Pad Left key. 136 * May also be synthesized from trackball motions. */ 137 public static final int KEYCODE_DPAD_LEFT = 21; 138 /** Key code constant: Directional Pad Right key. 139 * May also be synthesized from trackball motions. */ 140 public static final int KEYCODE_DPAD_RIGHT = 22; 141 /** Key code constant: Directional Pad Center key. 142 * May also be synthesized from trackball motions. */ 143 public static final int KEYCODE_DPAD_CENTER = 23; 144 /** Key code constant: Volume Up key. 145 * Adjusts the speaker volume up. */ 146 public static final int KEYCODE_VOLUME_UP = 24; 147 /** Key code constant: Volume Down key. 148 * Adjusts the speaker volume down. */ 149 public static final int KEYCODE_VOLUME_DOWN = 25; 150 /** Key code constant: Power key. */ 151 public static final int KEYCODE_POWER = 26; 152 /** Key code constant: Camera key. 153 * Used to launch a camera application or take pictures. */ 154 public static final int KEYCODE_CAMERA = 27; 155 /** Key code constant: Clear key. */ 156 public static final int KEYCODE_CLEAR = 28; 157 /** Key code constant: 'A' key. */ 158 public static final int KEYCODE_A = 29; 159 /** Key code constant: 'B' key. */ 160 public static final int KEYCODE_B = 30; 161 /** Key code constant: 'C' key. */ 162 public static final int KEYCODE_C = 31; 163 /** Key code constant: 'D' key. */ 164 public static final int KEYCODE_D = 32; 165 /** Key code constant: 'E' key. */ 166 public static final int KEYCODE_E = 33; 167 /** Key code constant: 'F' key. */ 168 public static final int KEYCODE_F = 34; 169 /** Key code constant: 'G' key. */ 170 public static final int KEYCODE_G = 35; 171 /** Key code constant: 'H' key. */ 172 public static final int KEYCODE_H = 36; 173 /** Key code constant: 'I' key. */ 174 public static final int KEYCODE_I = 37; 175 /** Key code constant: 'J' key. */ 176 public static final int KEYCODE_J = 38; 177 /** Key code constant: 'K' key. */ 178 public static final int KEYCODE_K = 39; 179 /** Key code constant: 'L' key. */ 180 public static final int KEYCODE_L = 40; 181 /** Key code constant: 'M' key. */ 182 public static final int KEYCODE_M = 41; 183 /** Key code constant: 'N' key. */ 184 public static final int KEYCODE_N = 42; 185 /** Key code constant: 'O' key. */ 186 public static final int KEYCODE_O = 43; 187 /** Key code constant: 'P' key. */ 188 public static final int KEYCODE_P = 44; 189 /** Key code constant: 'Q' key. */ 190 public static final int KEYCODE_Q = 45; 191 /** Key code constant: 'R' key. */ 192 public static final int KEYCODE_R = 46; 193 /** Key code constant: 'S' key. */ 194 public static final int KEYCODE_S = 47; 195 /** Key code constant: 'T' key. */ 196 public static final int KEYCODE_T = 48; 197 /** Key code constant: 'U' key. */ 198 public static final int KEYCODE_U = 49; 199 /** Key code constant: 'V' key. */ 200 public static final int KEYCODE_V = 50; 201 /** Key code constant: 'W' key. */ 202 public static final int KEYCODE_W = 51; 203 /** Key code constant: 'X' key. */ 204 public static final int KEYCODE_X = 52; 205 /** Key code constant: 'Y' key. */ 206 public static final int KEYCODE_Y = 53; 207 /** Key code constant: 'Z' key. */ 208 public static final int KEYCODE_Z = 54; 209 /** Key code constant: ',' key. */ 210 public static final int KEYCODE_COMMA = 55; 211 /** Key code constant: '.' key. */ 212 public static final int KEYCODE_PERIOD = 56; 213 /** Key code constant: Left Alt modifier key. */ 214 public static final int KEYCODE_ALT_LEFT = 57; 215 /** Key code constant: Right Alt modifier key. */ 216 public static final int KEYCODE_ALT_RIGHT = 58; 217 /** Key code constant: Left Shift modifier key. */ 218 public static final int KEYCODE_SHIFT_LEFT = 59; 219 /** Key code constant: Right Shift modifier key. */ 220 public static final int KEYCODE_SHIFT_RIGHT = 60; 221 /** Key code constant: Tab key. */ 222 public static final int KEYCODE_TAB = 61; 223 /** Key code constant: Space key. */ 224 public static final int KEYCODE_SPACE = 62; 225 /** Key code constant: Symbol modifier key. 226 * Used to enter alternate symbols. */ 227 public static final int KEYCODE_SYM = 63; 228 /** Key code constant: Explorer special function key. 229 * Used to launch a browser application. */ 230 public static final int KEYCODE_EXPLORER = 64; 231 /** Key code constant: Envelope special function key. 232 * Used to launch a mail application. */ 233 public static final int KEYCODE_ENVELOPE = 65; 234 /** Key code constant: Enter key. */ 235 public static final int KEYCODE_ENTER = 66; 236 /** Key code constant: Backspace key. 237 * Deletes characters before the insertion point, unlike {@link #KEYCODE_FORWARD_DEL}. */ 238 public static final int KEYCODE_DEL = 67; 239 /** Key code constant: '`' (backtick) key. */ 240 public static final int KEYCODE_GRAVE = 68; 241 /** Key code constant: '-'. */ 242 public static final int KEYCODE_MINUS = 69; 243 /** Key code constant: '=' key. */ 244 public static final int KEYCODE_EQUALS = 70; 245 /** Key code constant: '[' key. */ 246 public static final int KEYCODE_LEFT_BRACKET = 71; 247 /** Key code constant: ']' key. */ 248 public static final int KEYCODE_RIGHT_BRACKET = 72; 249 /** Key code constant: '\' key. */ 250 public static final int KEYCODE_BACKSLASH = 73; 251 /** Key code constant: ';' key. */ 252 public static final int KEYCODE_SEMICOLON = 74; 253 /** Key code constant: ''' (apostrophe) key. */ 254 public static final int KEYCODE_APOSTROPHE = 75; 255 /** Key code constant: '/' key. */ 256 public static final int KEYCODE_SLASH = 76; 257 /** Key code constant: '@' key. */ 258 public static final int KEYCODE_AT = 77; 259 /** Key code constant: Number modifier key. 260 * Used to enter numeric symbols. 261 * This key is not Num Lock; it is more like {@link #KEYCODE_ALT_LEFT} and is 262 * interpreted as an ALT key by {@link android.text.method.MetaKeyKeyListener}. */ 263 public static final int KEYCODE_NUM = 78; 264 /** Key code constant: Headset Hook key. 265 * Used to hang up calls and stop media. */ 266 public static final int KEYCODE_HEADSETHOOK = 79; 267 /** Key code constant: Camera Focus key. 268 * Used to focus the camera. */ 269 public static final int KEYCODE_FOCUS = 80; // *Camera* focus 270 /** Key code constant: '+' key. */ 271 public static final int KEYCODE_PLUS = 81; 272 /** Key code constant: Menu key. */ 273 public static final int KEYCODE_MENU = 82; 274 /** Key code constant: Notification key. */ 275 public static final int KEYCODE_NOTIFICATION = 83; 276 /** Key code constant: Search key. */ 277 public static final int KEYCODE_SEARCH = 84; 278 /** Key code constant: Play/Pause media key. */ 279 public static final int KEYCODE_MEDIA_PLAY_PAUSE= 85; 280 /** Key code constant: Stop media key. */ 281 public static final int KEYCODE_MEDIA_STOP = 86; 282 /** Key code constant: Play Next media key. */ 283 public static final int KEYCODE_MEDIA_NEXT = 87; 284 /** Key code constant: Play Previous media key. */ 285 public static final int KEYCODE_MEDIA_PREVIOUS = 88; 286 /** Key code constant: Rewind media key. */ 287 public static final int KEYCODE_MEDIA_REWIND = 89; 288 /** Key code constant: Fast Forward media key. */ 289 public static final int KEYCODE_MEDIA_FAST_FORWARD = 90; 290 /** Key code constant: Mute key. 291 * Mutes the microphone, unlike {@link #KEYCODE_VOLUME_MUTE}. */ 292 public static final int KEYCODE_MUTE = 91; 293 /** Key code constant: Page Up key. */ 294 public static final int KEYCODE_PAGE_UP = 92; 295 /** Key code constant: Page Down key. */ 296 public static final int KEYCODE_PAGE_DOWN = 93; 297 /** Key code constant: Picture Symbols modifier key. 298 * Used to switch symbol sets (Emoji, Kao-moji). */ 299 public static final int KEYCODE_PICTSYMBOLS = 94; // switch symbol-sets (Emoji,Kao-moji) 300 /** Key code constant: Switch Charset modifier key. 301 * Used to switch character sets (Kanji, Katakana). */ 302 public static final int KEYCODE_SWITCH_CHARSET = 95; // switch char-sets (Kanji,Katakana) 303 /** Key code constant: A Button key. 304 * On a game controller, the A button should be either the button labeled A 305 * or the first button on the bottom row of controller buttons. */ 306 public static final int KEYCODE_BUTTON_A = 96; 307 /** Key code constant: B Button key. 308 * On a game controller, the B button should be either the button labeled B 309 * or the second button on the bottom row of controller buttons. */ 310 public static final int KEYCODE_BUTTON_B = 97; 311 /** Key code constant: C Button key. 312 * On a game controller, the C button should be either the button labeled C 313 * or the third button on the bottom row of controller buttons. */ 314 public static final int KEYCODE_BUTTON_C = 98; 315 /** Key code constant: X Button key. 316 * On a game controller, the X button should be either the button labeled X 317 * or the first button on the upper row of controller buttons. */ 318 public static final int KEYCODE_BUTTON_X = 99; 319 /** Key code constant: Y Button key. 320 * On a game controller, the Y button should be either the button labeled Y 321 * or the second button on the upper row of controller buttons. */ 322 public static final int KEYCODE_BUTTON_Y = 100; 323 /** Key code constant: Z Button key. 324 * On a game controller, the Z button should be either the button labeled Z 325 * or the third button on the upper row of controller buttons. */ 326 public static final int KEYCODE_BUTTON_Z = 101; 327 /** Key code constant: L1 Button key. 328 * On a game controller, the L1 button should be either the button labeled L1 (or L) 329 * or the top left trigger button. */ 330 public static final int KEYCODE_BUTTON_L1 = 102; 331 /** Key code constant: R1 Button key. 332 * On a game controller, the R1 button should be either the button labeled R1 (or R) 333 * or the top right trigger button. */ 334 public static final int KEYCODE_BUTTON_R1 = 103; 335 /** Key code constant: L2 Button key. 336 * On a game controller, the L2 button should be either the button labeled L2 337 * or the bottom left trigger button. */ 338 public static final int KEYCODE_BUTTON_L2 = 104; 339 /** Key code constant: R2 Button key. 340 * On a game controller, the R2 button should be either the button labeled R2 341 * or the bottom right trigger button. */ 342 public static final int KEYCODE_BUTTON_R2 = 105; 343 /** Key code constant: Left Thumb Button key. 344 * On a game controller, the left thumb button indicates that the left (or only) 345 * joystick is pressed. */ 346 public static final int KEYCODE_BUTTON_THUMBL = 106; 347 /** Key code constant: Right Thumb Button key. 348 * On a game controller, the right thumb button indicates that the right 349 * joystick is pressed. */ 350 public static final int KEYCODE_BUTTON_THUMBR = 107; 351 /** Key code constant: Start Button key. 352 * On a game controller, the button labeled Start. */ 353 public static final int KEYCODE_BUTTON_START = 108; 354 /** Key code constant: Select Button key. 355 * On a game controller, the button labeled Select. */ 356 public static final int KEYCODE_BUTTON_SELECT = 109; 357 /** Key code constant: Mode Button key. 358 * On a game controller, the button labeled Mode. */ 359 public static final int KEYCODE_BUTTON_MODE = 110; 360 /** Key code constant: Escape key. */ 361 public static final int KEYCODE_ESCAPE = 111; 362 /** Key code constant: Forward Delete key. 363 * Deletes characters ahead of the insertion point, unlike {@link #KEYCODE_DEL}. */ 364 public static final int KEYCODE_FORWARD_DEL = 112; 365 /** Key code constant: Left Control modifier key. */ 366 public static final int KEYCODE_CTRL_LEFT = 113; 367 /** Key code constant: Right Control modifier key. */ 368 public static final int KEYCODE_CTRL_RIGHT = 114; 369 /** Key code constant: Caps Lock key. */ 370 public static final int KEYCODE_CAPS_LOCK = 115; 371 /** Key code constant: Scroll Lock key. */ 372 public static final int KEYCODE_SCROLL_LOCK = 116; 373 /** Key code constant: Left Meta modifier key. */ 374 public static final int KEYCODE_META_LEFT = 117; 375 /** Key code constant: Right Meta modifier key. */ 376 public static final int KEYCODE_META_RIGHT = 118; 377 /** Key code constant: Function modifier key. */ 378 public static final int KEYCODE_FUNCTION = 119; 379 /** Key code constant: System Request / Print Screen key. */ 380 public static final int KEYCODE_SYSRQ = 120; 381 /** Key code constant: Break / Pause key. */ 382 public static final int KEYCODE_BREAK = 121; 383 /** Key code constant: Home Movement key. 384 * Used for scrolling or moving the cursor around to the start of a line 385 * or to the top of a list. */ 386 public static final int KEYCODE_MOVE_HOME = 122; 387 /** Key code constant: End Movement key. 388 * Used for scrolling or moving the cursor around to the end of a line 389 * or to the bottom of a list. */ 390 public static final int KEYCODE_MOVE_END = 123; 391 /** Key code constant: Insert key. 392 * Toggles insert / overwrite edit mode. */ 393 public static final int KEYCODE_INSERT = 124; 394 /** Key code constant: Forward key. 395 * Navigates forward in the history stack. Complement of {@link #KEYCODE_BACK}. */ 396 public static final int KEYCODE_FORWARD = 125; 397 /** Key code constant: Play media key. */ 398 public static final int KEYCODE_MEDIA_PLAY = 126; 399 /** Key code constant: Pause media key. */ 400 public static final int KEYCODE_MEDIA_PAUSE = 127; 401 /** Key code constant: Close media key. 402 * May be used to close a CD tray, for example. */ 403 public static final int KEYCODE_MEDIA_CLOSE = 128; 404 /** Key code constant: Eject media key. 405 * May be used to eject a CD tray, for example. */ 406 public static final int KEYCODE_MEDIA_EJECT = 129; 407 /** Key code constant: Record media key. */ 408 public static final int KEYCODE_MEDIA_RECORD = 130; 409 /** Key code constant: F1 key. */ 410 public static final int KEYCODE_F1 = 131; 411 /** Key code constant: F2 key. */ 412 public static final int KEYCODE_F2 = 132; 413 /** Key code constant: F3 key. */ 414 public static final int KEYCODE_F3 = 133; 415 /** Key code constant: F4 key. */ 416 public static final int KEYCODE_F4 = 134; 417 /** Key code constant: F5 key. */ 418 public static final int KEYCODE_F5 = 135; 419 /** Key code constant: F6 key. */ 420 public static final int KEYCODE_F6 = 136; 421 /** Key code constant: F7 key. */ 422 public static final int KEYCODE_F7 = 137; 423 /** Key code constant: F8 key. */ 424 public static final int KEYCODE_F8 = 138; 425 /** Key code constant: F9 key. */ 426 public static final int KEYCODE_F9 = 139; 427 /** Key code constant: F10 key. */ 428 public static final int KEYCODE_F10 = 140; 429 /** Key code constant: F11 key. */ 430 public static final int KEYCODE_F11 = 141; 431 /** Key code constant: F12 key. */ 432 public static final int KEYCODE_F12 = 142; 433 /** Key code constant: Num Lock key. 434 * This is the Num Lock key; it is different from {@link #KEYCODE_NUM}. 435 * This key alters the behavior of other keys on the numeric keypad. */ 436 public static final int KEYCODE_NUM_LOCK = 143; 437 /** Key code constant: Numeric keypad '0' key. */ 438 public static final int KEYCODE_NUMPAD_0 = 144; 439 /** Key code constant: Numeric keypad '1' key. */ 440 public static final int KEYCODE_NUMPAD_1 = 145; 441 /** Key code constant: Numeric keypad '2' key. */ 442 public static final int KEYCODE_NUMPAD_2 = 146; 443 /** Key code constant: Numeric keypad '3' key. */ 444 public static final int KEYCODE_NUMPAD_3 = 147; 445 /** Key code constant: Numeric keypad '4' key. */ 446 public static final int KEYCODE_NUMPAD_4 = 148; 447 /** Key code constant: Numeric keypad '5' key. */ 448 public static final int KEYCODE_NUMPAD_5 = 149; 449 /** Key code constant: Numeric keypad '6' key. */ 450 public static final int KEYCODE_NUMPAD_6 = 150; 451 /** Key code constant: Numeric keypad '7' key. */ 452 public static final int KEYCODE_NUMPAD_7 = 151; 453 /** Key code constant: Numeric keypad '8' key. */ 454 public static final int KEYCODE_NUMPAD_8 = 152; 455 /** Key code constant: Numeric keypad '9' key. */ 456 public static final int KEYCODE_NUMPAD_9 = 153; 457 /** Key code constant: Numeric keypad '/' key (for division). */ 458 public static final int KEYCODE_NUMPAD_DIVIDE = 154; 459 /** Key code constant: Numeric keypad '*' key (for multiplication). */ 460 public static final int KEYCODE_NUMPAD_MULTIPLY = 155; 461 /** Key code constant: Numeric keypad '-' key (for subtraction). */ 462 public static final int KEYCODE_NUMPAD_SUBTRACT = 156; 463 /** Key code constant: Numeric keypad '+' key (for addition). */ 464 public static final int KEYCODE_NUMPAD_ADD = 157; 465 /** Key code constant: Numeric keypad '.' key (for decimals or digit grouping). */ 466 public static final int KEYCODE_NUMPAD_DOT = 158; 467 /** Key code constant: Numeric keypad ',' key (for decimals or digit grouping). */ 468 public static final int KEYCODE_NUMPAD_COMMA = 159; 469 /** Key code constant: Numeric keypad Enter key. */ 470 public static final int KEYCODE_NUMPAD_ENTER = 160; 471 /** Key code constant: Numeric keypad '=' key. */ 472 public static final int KEYCODE_NUMPAD_EQUALS = 161; 473 /** Key code constant: Numeric keypad '(' key. */ 474 public static final int KEYCODE_NUMPAD_LEFT_PAREN = 162; 475 /** Key code constant: Numeric keypad ')' key. */ 476 public static final int KEYCODE_NUMPAD_RIGHT_PAREN = 163; 477 /** Key code constant: Volume Mute key. 478 * Mutes the speaker, unlike {@link #KEYCODE_MUTE}. 479 * This key should normally be implemented as a toggle such that the first press 480 * mutes the speaker and the second press restores the original volume. */ 481 public static final int KEYCODE_VOLUME_MUTE = 164; 482 /** Key code constant: Info key. 483 * Common on TV remotes to show additional information related to what is 484 * currently being viewed. */ 485 public static final int KEYCODE_INFO = 165; 486 /** Key code constant: Channel up key. 487 * On TV remotes, increments the television channel. */ 488 public static final int KEYCODE_CHANNEL_UP = 166; 489 /** Key code constant: Channel down key. 490 * On TV remotes, decrements the television channel. */ 491 public static final int KEYCODE_CHANNEL_DOWN = 167; 492 /** Key code constant: Zoom in key. */ 493 public static final int KEYCODE_ZOOM_IN = 168; 494 /** Key code constant: Zoom out key. */ 495 public static final int KEYCODE_ZOOM_OUT = 169; 496 /** Key code constant: TV key. 497 * On TV remotes, switches to viewing live TV. */ 498 public static final int KEYCODE_TV = 170; 499 /** Key code constant: Window key. 500 * On TV remotes, toggles picture-in-picture mode or other windowing functions. */ 501 public static final int KEYCODE_WINDOW = 171; 502 /** Key code constant: Guide key. 503 * On TV remotes, shows a programming guide. */ 504 public static final int KEYCODE_GUIDE = 172; 505 /** Key code constant: DVR key. 506 * On some TV remotes, switches to a DVR mode for recorded shows. */ 507 public static final int KEYCODE_DVR = 173; 508 /** Key code constant: Bookmark key. 509 * On some TV remotes, bookmarks content or web pages. */ 510 public static final int KEYCODE_BOOKMARK = 174; 511 /** Key code constant: Toggle captions key. 512 * Switches the mode for closed-captioning text, for example during television shows. */ 513 public static final int KEYCODE_CAPTIONS = 175; 514 /** Key code constant: Settings key. 515 * Starts the system settings activity. */ 516 public static final int KEYCODE_SETTINGS = 176; 517 /** Key code constant: TV power key. 518 * On TV remotes, toggles the power on a television screen. */ 519 public static final int KEYCODE_TV_POWER = 177; 520 /** Key code constant: TV input key. 521 * On TV remotes, switches the input on a television screen. */ 522 public static final int KEYCODE_TV_INPUT = 178; 523 /** Key code constant: Set-top-box power key. 524 * On TV remotes, toggles the power on an external Set-top-box. */ 525 public static final int KEYCODE_STB_POWER = 179; 526 /** Key code constant: Set-top-box input key. 527 * On TV remotes, switches the input mode on an external Set-top-box. */ 528 public static final int KEYCODE_STB_INPUT = 180; 529 /** Key code constant: A/V Receiver power key. 530 * On TV remotes, toggles the power on an external A/V Receiver. */ 531 public static final int KEYCODE_AVR_POWER = 181; 532 /** Key code constant: A/V Receiver input key. 533 * On TV remotes, switches the input mode on an external A/V Receiver. */ 534 public static final int KEYCODE_AVR_INPUT = 182; 535 /** Key code constant: Red "programmable" key. 536 * On TV remotes, acts as a contextual/programmable key. */ 537 public static final int KEYCODE_PROG_RED = 183; 538 /** Key code constant: Green "programmable" key. 539 * On TV remotes, actsas a contextual/programmable key. */ 540 public static final int KEYCODE_PROG_GREEN = 184; 541 /** Key code constant: Yellow "programmable" key. 542 * On TV remotes, acts as a contextual/programmable key. */ 543 public static final int KEYCODE_PROG_YELLOW = 185; 544 /** Key code constant: Blue "programmable" key. 545 * On TV remotes, acts as a contextual/programmable key. */ 546 public static final int KEYCODE_PROG_BLUE = 186; 547 /** Key code constant: App switch key. 548 * Should bring up the application switcher dialog. */ 549 public static final int KEYCODE_APP_SWITCH = 187; 550 /** Key code constant: Generic Game Pad Button #1.*/ 551 public static final int KEYCODE_BUTTON_1 = 188; 552 /** Key code constant: Generic Game Pad Button #2.*/ 553 public static final int KEYCODE_BUTTON_2 = 189; 554 /** Key code constant: Generic Game Pad Button #3.*/ 555 public static final int KEYCODE_BUTTON_3 = 190; 556 /** Key code constant: Generic Game Pad Button #4.*/ 557 public static final int KEYCODE_BUTTON_4 = 191; 558 /** Key code constant: Generic Game Pad Button #5.*/ 559 public static final int KEYCODE_BUTTON_5 = 192; 560 /** Key code constant: Generic Game Pad Button #6.*/ 561 public static final int KEYCODE_BUTTON_6 = 193; 562 /** Key code constant: Generic Game Pad Button #7.*/ 563 public static final int KEYCODE_BUTTON_7 = 194; 564 /** Key code constant: Generic Game Pad Button #8.*/ 565 public static final int KEYCODE_BUTTON_8 = 195; 566 /** Key code constant: Generic Game Pad Button #9.*/ 567 public static final int KEYCODE_BUTTON_9 = 196; 568 /** Key code constant: Generic Game Pad Button #10.*/ 569 public static final int KEYCODE_BUTTON_10 = 197; 570 /** Key code constant: Generic Game Pad Button #11.*/ 571 public static final int KEYCODE_BUTTON_11 = 198; 572 /** Key code constant: Generic Game Pad Button #12.*/ 573 public static final int KEYCODE_BUTTON_12 = 199; 574 /** Key code constant: Generic Game Pad Button #13.*/ 575 public static final int KEYCODE_BUTTON_13 = 200; 576 /** Key code constant: Generic Game Pad Button #14.*/ 577 public static final int KEYCODE_BUTTON_14 = 201; 578 /** Key code constant: Generic Game Pad Button #15.*/ 579 public static final int KEYCODE_BUTTON_15 = 202; 580 /** Key code constant: Generic Game Pad Button #16.*/ 581 public static final int KEYCODE_BUTTON_16 = 203; 582 /** Key code constant: Language Switch key. 583 * Toggles the current input language such as switching between English and Japanese on 584 * a QWERTY keyboard. On some devices, the same function may be performed by 585 * pressing Shift+Spacebar. */ 586 public static final int KEYCODE_LANGUAGE_SWITCH = 204; 587 /** Key code constant: Manner Mode key. 588 * Toggles silent or vibrate mode on and off to make the device behave more politely 589 * in certain settings such as on a crowded train. On some devices, the key may only 590 * operate when long-pressed. */ 591 public static final int KEYCODE_MANNER_MODE = 205; 592 /** Key code constant: 3D Mode key. 593 * Toggles the display between 2D and 3D mode. */ 594 public static final int KEYCODE_3D_MODE = 206; 595 /** Key code constant: Contacts special function key. 596 * Used to launch an address book application. */ 597 public static final int KEYCODE_CONTACTS = 207; 598 /** Key code constant: Calendar special function key. 599 * Used to launch a calendar application. */ 600 public static final int KEYCODE_CALENDAR = 208; 601 /** Key code constant: Music special function key. 602 * Used to launch a music player application. */ 603 public static final int KEYCODE_MUSIC = 209; 604 /** Key code constant: Calculator special function key. 605 * Used to launch a calculator application. */ 606 public static final int KEYCODE_CALCULATOR = 210; 607 /** Key code constant: Japanese full-width / half-width key. */ 608 public static final int KEYCODE_ZENKAKU_HANKAKU = 211; 609 /** Key code constant: Japanese alphanumeric key. */ 610 public static final int KEYCODE_EISU = 212; 611 /** Key code constant: Japanese non-conversion key. */ 612 public static final int KEYCODE_MUHENKAN = 213; 613 /** Key code constant: Japanese conversion key. */ 614 public static final int KEYCODE_HENKAN = 214; 615 /** Key code constant: Japanese katakana / hiragana key. */ 616 public static final int KEYCODE_KATAKANA_HIRAGANA = 215; 617 /** Key code constant: Japanese Yen key. */ 618 public static final int KEYCODE_YEN = 216; 619 /** Key code constant: Japanese Ro key. */ 620 public static final int KEYCODE_RO = 217; 621 /** Key code constant: Japanese kana key. */ 622 public static final int KEYCODE_KANA = 218; 623 /** Key code constant: Assist key. 624 * Launches the global assist activity. Not delivered to applications. */ 625 public static final int KEYCODE_ASSIST = 219; 626 /** Key code constant: Brightness Down key. 627 * Adjusts the screen brightness down. */ 628 public static final int KEYCODE_BRIGHTNESS_DOWN = 220; 629 /** Key code constant: Brightness Up key. 630 * Adjusts the screen brightness up. */ 631 public static final int KEYCODE_BRIGHTNESS_UP = 221; 632 /** Key code constant: Audio Track key 633 * Switches the audio tracks. */ 634 public static final int KEYCODE_MEDIA_AUDIO_TRACK = 222; 635 636 private static final int LAST_KEYCODE = KEYCODE_MEDIA_AUDIO_TRACK; 637 638 // NOTE: If you add a new keycode here you must also add it to: 639 // isSystem() 640 // frameworks/native/include/android/keycodes.h 641 // frameworks/base/include/androidfw/KeycodeLabels.h 642 // external/webkit/WebKit/android/plugins/ANPKeyCodes.h 643 // frameworks/base/core/res/res/values/attrs.xml 644 // emulator? 645 // LAST_KEYCODE 646 // KEYCODE_SYMBOLIC_NAMES 647 // 648 // Also Android currently does not reserve code ranges for vendor- 649 // specific key codes. If you have new key codes to have, you 650 // MUST contribute a patch to the open source project to define 651 // those new codes. This is intended to maintain a consistent 652 // set of key code definitions across all Android devices. 653 654 // Symbolic names of all key codes. 655 private static final SparseArray<String> KEYCODE_SYMBOLIC_NAMES = new SparseArray<String>(); populateKeycodeSymbolicNames()656 private static void populateKeycodeSymbolicNames() { 657 SparseArray<String> names = KEYCODE_SYMBOLIC_NAMES; 658 names.append(KEYCODE_UNKNOWN, "KEYCODE_UNKNOWN"); 659 names.append(KEYCODE_SOFT_LEFT, "KEYCODE_SOFT_LEFT"); 660 names.append(KEYCODE_SOFT_RIGHT, "KEYCODE_SOFT_RIGHT"); 661 names.append(KEYCODE_HOME, "KEYCODE_HOME"); 662 names.append(KEYCODE_BACK, "KEYCODE_BACK"); 663 names.append(KEYCODE_CALL, "KEYCODE_CALL"); 664 names.append(KEYCODE_ENDCALL, "KEYCODE_ENDCALL"); 665 names.append(KEYCODE_0, "KEYCODE_0"); 666 names.append(KEYCODE_1, "KEYCODE_1"); 667 names.append(KEYCODE_2, "KEYCODE_2"); 668 names.append(KEYCODE_3, "KEYCODE_3"); 669 names.append(KEYCODE_4, "KEYCODE_4"); 670 names.append(KEYCODE_5, "KEYCODE_5"); 671 names.append(KEYCODE_6, "KEYCODE_6"); 672 names.append(KEYCODE_7, "KEYCODE_7"); 673 names.append(KEYCODE_8, "KEYCODE_8"); 674 names.append(KEYCODE_9, "KEYCODE_9"); 675 names.append(KEYCODE_STAR, "KEYCODE_STAR"); 676 names.append(KEYCODE_POUND, "KEYCODE_POUND"); 677 names.append(KEYCODE_DPAD_UP, "KEYCODE_DPAD_UP"); 678 names.append(KEYCODE_DPAD_DOWN, "KEYCODE_DPAD_DOWN"); 679 names.append(KEYCODE_DPAD_LEFT, "KEYCODE_DPAD_LEFT"); 680 names.append(KEYCODE_DPAD_RIGHT, "KEYCODE_DPAD_RIGHT"); 681 names.append(KEYCODE_DPAD_CENTER, "KEYCODE_DPAD_CENTER"); 682 names.append(KEYCODE_VOLUME_UP, "KEYCODE_VOLUME_UP"); 683 names.append(KEYCODE_VOLUME_DOWN, "KEYCODE_VOLUME_DOWN"); 684 names.append(KEYCODE_POWER, "KEYCODE_POWER"); 685 names.append(KEYCODE_CAMERA, "KEYCODE_CAMERA"); 686 names.append(KEYCODE_CLEAR, "KEYCODE_CLEAR"); 687 names.append(KEYCODE_A, "KEYCODE_A"); 688 names.append(KEYCODE_B, "KEYCODE_B"); 689 names.append(KEYCODE_C, "KEYCODE_C"); 690 names.append(KEYCODE_D, "KEYCODE_D"); 691 names.append(KEYCODE_E, "KEYCODE_E"); 692 names.append(KEYCODE_F, "KEYCODE_F"); 693 names.append(KEYCODE_G, "KEYCODE_G"); 694 names.append(KEYCODE_H, "KEYCODE_H"); 695 names.append(KEYCODE_I, "KEYCODE_I"); 696 names.append(KEYCODE_J, "KEYCODE_J"); 697 names.append(KEYCODE_K, "KEYCODE_K"); 698 names.append(KEYCODE_L, "KEYCODE_L"); 699 names.append(KEYCODE_M, "KEYCODE_M"); 700 names.append(KEYCODE_N, "KEYCODE_N"); 701 names.append(KEYCODE_O, "KEYCODE_O"); 702 names.append(KEYCODE_P, "KEYCODE_P"); 703 names.append(KEYCODE_Q, "KEYCODE_Q"); 704 names.append(KEYCODE_R, "KEYCODE_R"); 705 names.append(KEYCODE_S, "KEYCODE_S"); 706 names.append(KEYCODE_T, "KEYCODE_T"); 707 names.append(KEYCODE_U, "KEYCODE_U"); 708 names.append(KEYCODE_V, "KEYCODE_V"); 709 names.append(KEYCODE_W, "KEYCODE_W"); 710 names.append(KEYCODE_X, "KEYCODE_X"); 711 names.append(KEYCODE_Y, "KEYCODE_Y"); 712 names.append(KEYCODE_Z, "KEYCODE_Z"); 713 names.append(KEYCODE_COMMA, "KEYCODE_COMMA"); 714 names.append(KEYCODE_PERIOD, "KEYCODE_PERIOD"); 715 names.append(KEYCODE_ALT_LEFT, "KEYCODE_ALT_LEFT"); 716 names.append(KEYCODE_ALT_RIGHT, "KEYCODE_ALT_RIGHT"); 717 names.append(KEYCODE_SHIFT_LEFT, "KEYCODE_SHIFT_LEFT"); 718 names.append(KEYCODE_SHIFT_RIGHT, "KEYCODE_SHIFT_RIGHT"); 719 names.append(KEYCODE_TAB, "KEYCODE_TAB"); 720 names.append(KEYCODE_SPACE, "KEYCODE_SPACE"); 721 names.append(KEYCODE_SYM, "KEYCODE_SYM"); 722 names.append(KEYCODE_EXPLORER, "KEYCODE_EXPLORER"); 723 names.append(KEYCODE_ENVELOPE, "KEYCODE_ENVELOPE"); 724 names.append(KEYCODE_ENTER, "KEYCODE_ENTER"); 725 names.append(KEYCODE_DEL, "KEYCODE_DEL"); 726 names.append(KEYCODE_GRAVE, "KEYCODE_GRAVE"); 727 names.append(KEYCODE_MINUS, "KEYCODE_MINUS"); 728 names.append(KEYCODE_EQUALS, "KEYCODE_EQUALS"); 729 names.append(KEYCODE_LEFT_BRACKET, "KEYCODE_LEFT_BRACKET"); 730 names.append(KEYCODE_RIGHT_BRACKET, "KEYCODE_RIGHT_BRACKET"); 731 names.append(KEYCODE_BACKSLASH, "KEYCODE_BACKSLASH"); 732 names.append(KEYCODE_SEMICOLON, "KEYCODE_SEMICOLON"); 733 names.append(KEYCODE_APOSTROPHE, "KEYCODE_APOSTROPHE"); 734 names.append(KEYCODE_SLASH, "KEYCODE_SLASH"); 735 names.append(KEYCODE_AT, "KEYCODE_AT"); 736 names.append(KEYCODE_NUM, "KEYCODE_NUM"); 737 names.append(KEYCODE_HEADSETHOOK, "KEYCODE_HEADSETHOOK"); 738 names.append(KEYCODE_FOCUS, "KEYCODE_FOCUS"); 739 names.append(KEYCODE_PLUS, "KEYCODE_PLUS"); 740 names.append(KEYCODE_MENU, "KEYCODE_MENU"); 741 names.append(KEYCODE_NOTIFICATION, "KEYCODE_NOTIFICATION"); 742 names.append(KEYCODE_SEARCH, "KEYCODE_SEARCH"); 743 names.append(KEYCODE_MEDIA_PLAY_PAUSE, "KEYCODE_MEDIA_PLAY_PAUSE"); 744 names.append(KEYCODE_MEDIA_STOP, "KEYCODE_MEDIA_STOP"); 745 names.append(KEYCODE_MEDIA_NEXT, "KEYCODE_MEDIA_NEXT"); 746 names.append(KEYCODE_MEDIA_PREVIOUS, "KEYCODE_MEDIA_PREVIOUS"); 747 names.append(KEYCODE_MEDIA_REWIND, "KEYCODE_MEDIA_REWIND"); 748 names.append(KEYCODE_MEDIA_FAST_FORWARD, "KEYCODE_MEDIA_FAST_FORWARD"); 749 names.append(KEYCODE_MUTE, "KEYCODE_MUTE"); 750 names.append(KEYCODE_PAGE_UP, "KEYCODE_PAGE_UP"); 751 names.append(KEYCODE_PAGE_DOWN, "KEYCODE_PAGE_DOWN"); 752 names.append(KEYCODE_PICTSYMBOLS, "KEYCODE_PICTSYMBOLS"); 753 names.append(KEYCODE_SWITCH_CHARSET, "KEYCODE_SWITCH_CHARSET"); 754 names.append(KEYCODE_BUTTON_A, "KEYCODE_BUTTON_A"); 755 names.append(KEYCODE_BUTTON_B, "KEYCODE_BUTTON_B"); 756 names.append(KEYCODE_BUTTON_C, "KEYCODE_BUTTON_C"); 757 names.append(KEYCODE_BUTTON_X, "KEYCODE_BUTTON_X"); 758 names.append(KEYCODE_BUTTON_Y, "KEYCODE_BUTTON_Y"); 759 names.append(KEYCODE_BUTTON_Z, "KEYCODE_BUTTON_Z"); 760 names.append(KEYCODE_BUTTON_L1, "KEYCODE_BUTTON_L1"); 761 names.append(KEYCODE_BUTTON_R1, "KEYCODE_BUTTON_R1"); 762 names.append(KEYCODE_BUTTON_L2, "KEYCODE_BUTTON_L2"); 763 names.append(KEYCODE_BUTTON_R2, "KEYCODE_BUTTON_R2"); 764 names.append(KEYCODE_BUTTON_THUMBL, "KEYCODE_BUTTON_THUMBL"); 765 names.append(KEYCODE_BUTTON_THUMBR, "KEYCODE_BUTTON_THUMBR"); 766 names.append(KEYCODE_BUTTON_START, "KEYCODE_BUTTON_START"); 767 names.append(KEYCODE_BUTTON_SELECT, "KEYCODE_BUTTON_SELECT"); 768 names.append(KEYCODE_BUTTON_MODE, "KEYCODE_BUTTON_MODE"); 769 names.append(KEYCODE_ESCAPE, "KEYCODE_ESCAPE"); 770 names.append(KEYCODE_FORWARD_DEL, "KEYCODE_FORWARD_DEL"); 771 names.append(KEYCODE_CTRL_LEFT, "KEYCODE_CTRL_LEFT"); 772 names.append(KEYCODE_CTRL_RIGHT, "KEYCODE_CTRL_RIGHT"); 773 names.append(KEYCODE_CAPS_LOCK, "KEYCODE_CAPS_LOCK"); 774 names.append(KEYCODE_SCROLL_LOCK, "KEYCODE_SCROLL_LOCK"); 775 names.append(KEYCODE_META_LEFT, "KEYCODE_META_LEFT"); 776 names.append(KEYCODE_META_RIGHT, "KEYCODE_META_RIGHT"); 777 names.append(KEYCODE_FUNCTION, "KEYCODE_FUNCTION"); 778 names.append(KEYCODE_SYSRQ, "KEYCODE_SYSRQ"); 779 names.append(KEYCODE_BREAK, "KEYCODE_BREAK"); 780 names.append(KEYCODE_MOVE_HOME, "KEYCODE_MOVE_HOME"); 781 names.append(KEYCODE_MOVE_END, "KEYCODE_MOVE_END"); 782 names.append(KEYCODE_INSERT, "KEYCODE_INSERT"); 783 names.append(KEYCODE_FORWARD, "KEYCODE_FORWARD"); 784 names.append(KEYCODE_MEDIA_PLAY, "KEYCODE_MEDIA_PLAY"); 785 names.append(KEYCODE_MEDIA_PAUSE, "KEYCODE_MEDIA_PAUSE"); 786 names.append(KEYCODE_MEDIA_CLOSE, "KEYCODE_MEDIA_CLOSE"); 787 names.append(KEYCODE_MEDIA_EJECT, "KEYCODE_MEDIA_EJECT"); 788 names.append(KEYCODE_MEDIA_RECORD, "KEYCODE_MEDIA_RECORD"); 789 names.append(KEYCODE_F1, "KEYCODE_F1"); 790 names.append(KEYCODE_F2, "KEYCODE_F2"); 791 names.append(KEYCODE_F3, "KEYCODE_F3"); 792 names.append(KEYCODE_F4, "KEYCODE_F4"); 793 names.append(KEYCODE_F5, "KEYCODE_F5"); 794 names.append(KEYCODE_F6, "KEYCODE_F6"); 795 names.append(KEYCODE_F7, "KEYCODE_F7"); 796 names.append(KEYCODE_F8, "KEYCODE_F8"); 797 names.append(KEYCODE_F9, "KEYCODE_F9"); 798 names.append(KEYCODE_F10, "KEYCODE_F10"); 799 names.append(KEYCODE_F11, "KEYCODE_F11"); 800 names.append(KEYCODE_F12, "KEYCODE_F12"); 801 names.append(KEYCODE_NUM_LOCK, "KEYCODE_NUM_LOCK"); 802 names.append(KEYCODE_NUMPAD_0, "KEYCODE_NUMPAD_0"); 803 names.append(KEYCODE_NUMPAD_1, "KEYCODE_NUMPAD_1"); 804 names.append(KEYCODE_NUMPAD_2, "KEYCODE_NUMPAD_2"); 805 names.append(KEYCODE_NUMPAD_3, "KEYCODE_NUMPAD_3"); 806 names.append(KEYCODE_NUMPAD_4, "KEYCODE_NUMPAD_4"); 807 names.append(KEYCODE_NUMPAD_5, "KEYCODE_NUMPAD_5"); 808 names.append(KEYCODE_NUMPAD_6, "KEYCODE_NUMPAD_6"); 809 names.append(KEYCODE_NUMPAD_7, "KEYCODE_NUMPAD_7"); 810 names.append(KEYCODE_NUMPAD_8, "KEYCODE_NUMPAD_8"); 811 names.append(KEYCODE_NUMPAD_9, "KEYCODE_NUMPAD_9"); 812 names.append(KEYCODE_NUMPAD_DIVIDE, "KEYCODE_NUMPAD_DIVIDE"); 813 names.append(KEYCODE_NUMPAD_MULTIPLY, "KEYCODE_NUMPAD_MULTIPLY"); 814 names.append(KEYCODE_NUMPAD_SUBTRACT, "KEYCODE_NUMPAD_SUBTRACT"); 815 names.append(KEYCODE_NUMPAD_ADD, "KEYCODE_NUMPAD_ADD"); 816 names.append(KEYCODE_NUMPAD_DOT, "KEYCODE_NUMPAD_DOT"); 817 names.append(KEYCODE_NUMPAD_COMMA, "KEYCODE_NUMPAD_COMMA"); 818 names.append(KEYCODE_NUMPAD_ENTER, "KEYCODE_NUMPAD_ENTER"); 819 names.append(KEYCODE_NUMPAD_EQUALS, "KEYCODE_NUMPAD_EQUALS"); 820 names.append(KEYCODE_NUMPAD_LEFT_PAREN, "KEYCODE_NUMPAD_LEFT_PAREN"); 821 names.append(KEYCODE_NUMPAD_RIGHT_PAREN, "KEYCODE_NUMPAD_RIGHT_PAREN"); 822 names.append(KEYCODE_VOLUME_MUTE, "KEYCODE_VOLUME_MUTE"); 823 names.append(KEYCODE_INFO, "KEYCODE_INFO"); 824 names.append(KEYCODE_CHANNEL_UP, "KEYCODE_CHANNEL_UP"); 825 names.append(KEYCODE_CHANNEL_DOWN, "KEYCODE_CHANNEL_DOWN"); 826 names.append(KEYCODE_ZOOM_IN, "KEYCODE_ZOOM_IN"); 827 names.append(KEYCODE_ZOOM_OUT, "KEYCODE_ZOOM_OUT"); 828 names.append(KEYCODE_TV, "KEYCODE_TV"); 829 names.append(KEYCODE_WINDOW, "KEYCODE_WINDOW"); 830 names.append(KEYCODE_GUIDE, "KEYCODE_GUIDE"); 831 names.append(KEYCODE_DVR, "KEYCODE_DVR"); 832 names.append(KEYCODE_BOOKMARK, "KEYCODE_BOOKMARK"); 833 names.append(KEYCODE_CAPTIONS, "KEYCODE_CAPTIONS"); 834 names.append(KEYCODE_SETTINGS, "KEYCODE_SETTINGS"); 835 names.append(KEYCODE_TV_POWER, "KEYCODE_TV_POWER"); 836 names.append(KEYCODE_TV_INPUT, "KEYCODE_TV_INPUT"); 837 names.append(KEYCODE_STB_INPUT, "KEYCODE_STB_INPUT"); 838 names.append(KEYCODE_STB_POWER, "KEYCODE_STB_POWER"); 839 names.append(KEYCODE_AVR_POWER, "KEYCODE_AVR_POWER"); 840 names.append(KEYCODE_AVR_INPUT, "KEYCODE_AVR_INPUT"); 841 names.append(KEYCODE_PROG_RED, "KEYCODE_PROG_RED"); 842 names.append(KEYCODE_PROG_GREEN, "KEYCODE_PROG_GREEN"); 843 names.append(KEYCODE_PROG_YELLOW, "KEYCODE_PROG_YELLOW"); 844 names.append(KEYCODE_PROG_BLUE, "KEYCODE_PROG_BLUE"); 845 names.append(KEYCODE_APP_SWITCH, "KEYCODE_APP_SWITCH"); 846 names.append(KEYCODE_BUTTON_1, "KEYCODE_BUTTON_1"); 847 names.append(KEYCODE_BUTTON_2, "KEYCODE_BUTTON_2"); 848 names.append(KEYCODE_BUTTON_3, "KEYCODE_BUTTON_3"); 849 names.append(KEYCODE_BUTTON_4, "KEYCODE_BUTTON_4"); 850 names.append(KEYCODE_BUTTON_5, "KEYCODE_BUTTON_5"); 851 names.append(KEYCODE_BUTTON_6, "KEYCODE_BUTTON_6"); 852 names.append(KEYCODE_BUTTON_7, "KEYCODE_BUTTON_7"); 853 names.append(KEYCODE_BUTTON_8, "KEYCODE_BUTTON_8"); 854 names.append(KEYCODE_BUTTON_9, "KEYCODE_BUTTON_9"); 855 names.append(KEYCODE_BUTTON_10, "KEYCODE_BUTTON_10"); 856 names.append(KEYCODE_BUTTON_11, "KEYCODE_BUTTON_11"); 857 names.append(KEYCODE_BUTTON_12, "KEYCODE_BUTTON_12"); 858 names.append(KEYCODE_BUTTON_13, "KEYCODE_BUTTON_13"); 859 names.append(KEYCODE_BUTTON_14, "KEYCODE_BUTTON_14"); 860 names.append(KEYCODE_BUTTON_15, "KEYCODE_BUTTON_15"); 861 names.append(KEYCODE_BUTTON_16, "KEYCODE_BUTTON_16"); 862 names.append(KEYCODE_LANGUAGE_SWITCH, "KEYCODE_LANGUAGE_SWITCH"); 863 names.append(KEYCODE_MANNER_MODE, "KEYCODE_MANNER_MODE"); 864 names.append(KEYCODE_3D_MODE, "KEYCODE_3D_MODE"); 865 names.append(KEYCODE_CONTACTS, "KEYCODE_CONTACTS"); 866 names.append(KEYCODE_CALENDAR, "KEYCODE_CALENDAR"); 867 names.append(KEYCODE_MUSIC, "KEYCODE_MUSIC"); 868 names.append(KEYCODE_CALCULATOR, "KEYCODE_CALCULATOR"); 869 names.append(KEYCODE_ZENKAKU_HANKAKU, "KEYCODE_ZENKAKU_HANKAKU"); 870 names.append(KEYCODE_EISU, "KEYCODE_EISU"); 871 names.append(KEYCODE_MUHENKAN, "KEYCODE_MUHENKAN"); 872 names.append(KEYCODE_HENKAN, "KEYCODE_HENKAN"); 873 names.append(KEYCODE_KATAKANA_HIRAGANA, "KEYCODE_KATAKANA_HIRAGANA"); 874 names.append(KEYCODE_YEN, "KEYCODE_YEN"); 875 names.append(KEYCODE_RO, "KEYCODE_RO"); 876 names.append(KEYCODE_KANA, "KEYCODE_KANA"); 877 names.append(KEYCODE_ASSIST, "KEYCODE_ASSIST"); 878 names.append(KEYCODE_BRIGHTNESS_DOWN, "KEYCODE_BRIGHTNESS_DOWN"); 879 names.append(KEYCODE_BRIGHTNESS_UP, "KEYCODE_BRIGHTNESS_UP"); 880 names.append(KEYCODE_MEDIA_AUDIO_TRACK, "KEYCODE_MEDIA_AUDIO_TRACK"); 881 }; 882 883 // Symbolic names of all metakeys in bit order from least significant to most significant. 884 // Accordingly there are exactly 32 values in this table. 885 private static final String[] META_SYMBOLIC_NAMES = new String[] { 886 "META_SHIFT_ON", 887 "META_ALT_ON", 888 "META_SYM_ON", 889 "META_FUNCTION_ON", 890 "META_ALT_LEFT_ON", 891 "META_ALT_RIGHT_ON", 892 "META_SHIFT_LEFT_ON", 893 "META_SHIFT_RIGHT_ON", 894 "META_CAP_LOCKED", 895 "META_ALT_LOCKED", 896 "META_SYM_LOCKED", 897 "0x00000800", 898 "META_CTRL_ON", 899 "META_CTRL_LEFT_ON", 900 "META_CTRL_RIGHT_ON", 901 "0x00008000", 902 "META_META_ON", 903 "META_META_LEFT_ON", 904 "META_META_RIGHT_ON", 905 "0x00080000", 906 "META_CAPS_LOCK_ON", 907 "META_NUM_LOCK_ON", 908 "META_SCROLL_LOCK_ON", 909 "0x00800000", 910 "0x01000000", 911 "0x02000000", 912 "0x04000000", 913 "0x08000000", 914 "0x10000000", 915 "0x20000000", 916 "0x40000000", 917 "0x80000000", 918 }; 919 920 /** 921 * @deprecated There are now more than MAX_KEYCODE keycodes. 922 * Use {@link #getMaxKeyCode()} instead. 923 */ 924 @Deprecated 925 public static final int MAX_KEYCODE = 84; 926 927 /** 928 * {@link #getAction} value: the key has been pressed down. 929 */ 930 public static final int ACTION_DOWN = 0; 931 /** 932 * {@link #getAction} value: the key has been released. 933 */ 934 public static final int ACTION_UP = 1; 935 /** 936 * {@link #getAction} value: multiple duplicate key events have 937 * occurred in a row, or a complex string is being delivered. If the 938 * key code is not {#link {@link #KEYCODE_UNKNOWN} then the 939 * {#link {@link #getRepeatCount()} method returns the number of times 940 * the given key code should be executed. 941 * Otherwise, if the key code is {@link #KEYCODE_UNKNOWN}, then 942 * this is a sequence of characters as returned by {@link #getCharacters}. 943 */ 944 public static final int ACTION_MULTIPLE = 2; 945 946 /** 947 * SHIFT key locked in CAPS mode. 948 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API. 949 * @hide 950 */ 951 public static final int META_CAP_LOCKED = 0x100; 952 953 /** 954 * ALT key locked. 955 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API. 956 * @hide 957 */ 958 public static final int META_ALT_LOCKED = 0x200; 959 960 /** 961 * SYM key locked. 962 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API. 963 * @hide 964 */ 965 public static final int META_SYM_LOCKED = 0x400; 966 967 /** 968 * Text is in selection mode. 969 * Reserved for use by {@link MetaKeyKeyListener} for a private unpublished constant 970 * in its API that is currently being retained for legacy reasons. 971 * @hide 972 */ 973 public static final int META_SELECTING = 0x800; 974 975 /** 976 * <p>This mask is used to check whether one of the ALT meta keys is pressed.</p> 977 * 978 * @see #isAltPressed() 979 * @see #getMetaState() 980 * @see #KEYCODE_ALT_LEFT 981 * @see #KEYCODE_ALT_RIGHT 982 */ 983 public static final int META_ALT_ON = 0x02; 984 985 /** 986 * <p>This mask is used to check whether the left ALT meta key is pressed.</p> 987 * 988 * @see #isAltPressed() 989 * @see #getMetaState() 990 * @see #KEYCODE_ALT_LEFT 991 */ 992 public static final int META_ALT_LEFT_ON = 0x10; 993 994 /** 995 * <p>This mask is used to check whether the right the ALT meta key is pressed.</p> 996 * 997 * @see #isAltPressed() 998 * @see #getMetaState() 999 * @see #KEYCODE_ALT_RIGHT 1000 */ 1001 public static final int META_ALT_RIGHT_ON = 0x20; 1002 1003 /** 1004 * <p>This mask is used to check whether one of the SHIFT meta keys is pressed.</p> 1005 * 1006 * @see #isShiftPressed() 1007 * @see #getMetaState() 1008 * @see #KEYCODE_SHIFT_LEFT 1009 * @see #KEYCODE_SHIFT_RIGHT 1010 */ 1011 public static final int META_SHIFT_ON = 0x1; 1012 1013 /** 1014 * <p>This mask is used to check whether the left SHIFT meta key is pressed.</p> 1015 * 1016 * @see #isShiftPressed() 1017 * @see #getMetaState() 1018 * @see #KEYCODE_SHIFT_LEFT 1019 */ 1020 public static final int META_SHIFT_LEFT_ON = 0x40; 1021 1022 /** 1023 * <p>This mask is used to check whether the right SHIFT meta key is pressed.</p> 1024 * 1025 * @see #isShiftPressed() 1026 * @see #getMetaState() 1027 * @see #KEYCODE_SHIFT_RIGHT 1028 */ 1029 public static final int META_SHIFT_RIGHT_ON = 0x80; 1030 1031 /** 1032 * <p>This mask is used to check whether the SYM meta key is pressed.</p> 1033 * 1034 * @see #isSymPressed() 1035 * @see #getMetaState() 1036 */ 1037 public static final int META_SYM_ON = 0x4; 1038 1039 /** 1040 * <p>This mask is used to check whether the FUNCTION meta key is pressed.</p> 1041 * 1042 * @see #isFunctionPressed() 1043 * @see #getMetaState() 1044 */ 1045 public static final int META_FUNCTION_ON = 0x8; 1046 1047 /** 1048 * <p>This mask is used to check whether one of the CTRL meta keys is pressed.</p> 1049 * 1050 * @see #isCtrlPressed() 1051 * @see #getMetaState() 1052 * @see #KEYCODE_CTRL_LEFT 1053 * @see #KEYCODE_CTRL_RIGHT 1054 */ 1055 public static final int META_CTRL_ON = 0x1000; 1056 1057 /** 1058 * <p>This mask is used to check whether the left CTRL meta key is pressed.</p> 1059 * 1060 * @see #isCtrlPressed() 1061 * @see #getMetaState() 1062 * @see #KEYCODE_CTRL_LEFT 1063 */ 1064 public static final int META_CTRL_LEFT_ON = 0x2000; 1065 1066 /** 1067 * <p>This mask is used to check whether the right CTRL meta key is pressed.</p> 1068 * 1069 * @see #isCtrlPressed() 1070 * @see #getMetaState() 1071 * @see #KEYCODE_CTRL_RIGHT 1072 */ 1073 public static final int META_CTRL_RIGHT_ON = 0x4000; 1074 1075 /** 1076 * <p>This mask is used to check whether one of the META meta keys is pressed.</p> 1077 * 1078 * @see #isMetaPressed() 1079 * @see #getMetaState() 1080 * @see #KEYCODE_META_LEFT 1081 * @see #KEYCODE_META_RIGHT 1082 */ 1083 public static final int META_META_ON = 0x10000; 1084 1085 /** 1086 * <p>This mask is used to check whether the left META meta key is pressed.</p> 1087 * 1088 * @see #isMetaPressed() 1089 * @see #getMetaState() 1090 * @see #KEYCODE_META_LEFT 1091 */ 1092 public static final int META_META_LEFT_ON = 0x20000; 1093 1094 /** 1095 * <p>This mask is used to check whether the right META meta key is pressed.</p> 1096 * 1097 * @see #isMetaPressed() 1098 * @see #getMetaState() 1099 * @see #KEYCODE_META_RIGHT 1100 */ 1101 public static final int META_META_RIGHT_ON = 0x40000; 1102 1103 /** 1104 * <p>This mask is used to check whether the CAPS LOCK meta key is on.</p> 1105 * 1106 * @see #isCapsLockOn() 1107 * @see #getMetaState() 1108 * @see #KEYCODE_CAPS_LOCK 1109 */ 1110 public static final int META_CAPS_LOCK_ON = 0x100000; 1111 1112 /** 1113 * <p>This mask is used to check whether the NUM LOCK meta key is on.</p> 1114 * 1115 * @see #isNumLockOn() 1116 * @see #getMetaState() 1117 * @see #KEYCODE_NUM_LOCK 1118 */ 1119 public static final int META_NUM_LOCK_ON = 0x200000; 1120 1121 /** 1122 * <p>This mask is used to check whether the SCROLL LOCK meta key is on.</p> 1123 * 1124 * @see #isScrollLockOn() 1125 * @see #getMetaState() 1126 * @see #KEYCODE_SCROLL_LOCK 1127 */ 1128 public static final int META_SCROLL_LOCK_ON = 0x400000; 1129 1130 /** 1131 * This mask is a combination of {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON} 1132 * and {@link #META_SHIFT_RIGHT_ON}. 1133 */ 1134 public static final int META_SHIFT_MASK = META_SHIFT_ON 1135 | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON; 1136 1137 /** 1138 * This mask is a combination of {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON} 1139 * and {@link #META_ALT_RIGHT_ON}. 1140 */ 1141 public static final int META_ALT_MASK = META_ALT_ON 1142 | META_ALT_LEFT_ON | META_ALT_RIGHT_ON; 1143 1144 /** 1145 * This mask is a combination of {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON} 1146 * and {@link #META_CTRL_RIGHT_ON}. 1147 */ 1148 public static final int META_CTRL_MASK = META_CTRL_ON 1149 | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON; 1150 1151 /** 1152 * This mask is a combination of {@link #META_META_ON}, {@link #META_META_LEFT_ON} 1153 * and {@link #META_META_RIGHT_ON}. 1154 */ 1155 public static final int META_META_MASK = META_META_ON 1156 | META_META_LEFT_ON | META_META_RIGHT_ON; 1157 1158 /** 1159 * This mask is set if the device woke because of this key event. 1160 */ 1161 public static final int FLAG_WOKE_HERE = 0x1; 1162 1163 /** 1164 * This mask is set if the key event was generated by a software keyboard. 1165 */ 1166 public static final int FLAG_SOFT_KEYBOARD = 0x2; 1167 1168 /** 1169 * This mask is set if we don't want the key event to cause us to leave 1170 * touch mode. 1171 */ 1172 public static final int FLAG_KEEP_TOUCH_MODE = 0x4; 1173 1174 /** 1175 * This mask is set if an event was known to come from a trusted part 1176 * of the system. That is, the event is known to come from the user, 1177 * and could not have been spoofed by a third party component. 1178 */ 1179 public static final int FLAG_FROM_SYSTEM = 0x8; 1180 1181 /** 1182 * This mask is used for compatibility, to identify enter keys that are 1183 * coming from an IME whose enter key has been auto-labelled "next" or 1184 * "done". This allows TextView to dispatch these as normal enter keys 1185 * for old applications, but still do the appropriate action when 1186 * receiving them. 1187 */ 1188 public static final int FLAG_EDITOR_ACTION = 0x10; 1189 1190 /** 1191 * When associated with up key events, this indicates that the key press 1192 * has been canceled. Typically this is used with virtual touch screen 1193 * keys, where the user can slide from the virtual key area on to the 1194 * display: in that case, the application will receive a canceled up 1195 * event and should not perform the action normally associated with the 1196 * key. Note that for this to work, the application can not perform an 1197 * action for a key until it receives an up or the long press timeout has 1198 * expired. 1199 */ 1200 public static final int FLAG_CANCELED = 0x20; 1201 1202 /** 1203 * This key event was generated by a virtual (on-screen) hard key area. 1204 * Typically this is an area of the touchscreen, outside of the regular 1205 * display, dedicated to "hardware" buttons. 1206 */ 1207 public static final int FLAG_VIRTUAL_HARD_KEY = 0x40; 1208 1209 /** 1210 * This flag is set for the first key repeat that occurs after the 1211 * long press timeout. 1212 */ 1213 public static final int FLAG_LONG_PRESS = 0x80; 1214 1215 /** 1216 * Set when a key event has {@link #FLAG_CANCELED} set because a long 1217 * press action was executed while it was down. 1218 */ 1219 public static final int FLAG_CANCELED_LONG_PRESS = 0x100; 1220 1221 /** 1222 * Set for {@link #ACTION_UP} when this event's key code is still being 1223 * tracked from its initial down. That is, somebody requested that tracking 1224 * started on the key down and a long press has not caused 1225 * the tracking to be canceled. 1226 */ 1227 public static final int FLAG_TRACKING = 0x200; 1228 1229 /** 1230 * Set when a key event has been synthesized to implement default behavior 1231 * for an event that the application did not handle. 1232 * Fallback key events are generated by unhandled trackball motions 1233 * (to emulate a directional keypad) and by certain unhandled key presses 1234 * that are declared in the key map (such as special function numeric keypad 1235 * keys when numlock is off). 1236 */ 1237 public static final int FLAG_FALLBACK = 0x400; 1238 1239 /** 1240 * Signifies that the key is being predispatched. 1241 * @hide 1242 */ 1243 public static final int FLAG_PREDISPATCH = 0x20000000; 1244 1245 /** 1246 * Private control to determine when an app is tracking a key sequence. 1247 * @hide 1248 */ 1249 public static final int FLAG_START_TRACKING = 0x40000000; 1250 1251 /** 1252 * Private flag that indicates when the system has detected that this key event 1253 * may be inconsistent with respect to the sequence of previously delivered key events, 1254 * such as when a key up event is sent but the key was not down. 1255 * 1256 * @hide 1257 * @see #isTainted 1258 * @see #setTainted 1259 */ 1260 public static final int FLAG_TAINTED = 0x80000000; 1261 1262 /** 1263 * Returns the maximum keycode. 1264 */ getMaxKeyCode()1265 public static int getMaxKeyCode() { 1266 return LAST_KEYCODE; 1267 } 1268 1269 /** 1270 * Get the character that is produced by putting accent on the character 1271 * c. 1272 * For example, getDeadChar('`', 'e') returns è. 1273 */ getDeadChar(int accent, int c)1274 public static int getDeadChar(int accent, int c) { 1275 return KeyCharacterMap.getDeadChar(accent, c); 1276 } 1277 1278 static final boolean DEBUG = false; 1279 static final String TAG = "KeyEvent"; 1280 1281 private static final int MAX_RECYCLED = 10; 1282 private static final Object gRecyclerLock = new Object(); 1283 private static int gRecyclerUsed; 1284 private static KeyEvent gRecyclerTop; 1285 1286 private KeyEvent mNext; 1287 1288 private int mDeviceId; 1289 private int mSource; 1290 private int mMetaState; 1291 private int mAction; 1292 private int mKeyCode; 1293 private int mScanCode; 1294 private int mRepeatCount; 1295 private int mFlags; 1296 private long mDownTime; 1297 private long mEventTime; 1298 private String mCharacters; 1299 1300 public interface Callback { 1301 /** 1302 * Called when a key down event has occurred. If you return true, 1303 * you can first call {@link KeyEvent#startTracking() 1304 * KeyEvent.startTracking()} to have the framework track the event 1305 * through its {@link #onKeyUp(int, KeyEvent)} and also call your 1306 * {@link #onKeyLongPress(int, KeyEvent)} if it occurs. 1307 * 1308 * @param keyCode The value in event.getKeyCode(). 1309 * @param event Description of the key event. 1310 * 1311 * @return If you handled the event, return true. If you want to allow 1312 * the event to be handled by the next receiver, return false. 1313 */ onKeyDown(int keyCode, KeyEvent event)1314 boolean onKeyDown(int keyCode, KeyEvent event); 1315 1316 /** 1317 * Called when a long press has occurred. If you return true, 1318 * the final key up will have {@link KeyEvent#FLAG_CANCELED} and 1319 * {@link KeyEvent#FLAG_CANCELED_LONG_PRESS} set. Note that in 1320 * order to receive this callback, someone in the event change 1321 * <em>must</em> return true from {@link #onKeyDown} <em>and</em> 1322 * call {@link KeyEvent#startTracking()} on the event. 1323 * 1324 * @param keyCode The value in event.getKeyCode(). 1325 * @param event Description of the key event. 1326 * 1327 * @return If you handled the event, return true. If you want to allow 1328 * the event to be handled by the next receiver, return false. 1329 */ onKeyLongPress(int keyCode, KeyEvent event)1330 boolean onKeyLongPress(int keyCode, KeyEvent event); 1331 1332 /** 1333 * Called when a key up event has occurred. 1334 * 1335 * @param keyCode The value in event.getKeyCode(). 1336 * @param event Description of the key event. 1337 * 1338 * @return If you handled the event, return true. If you want to allow 1339 * the event to be handled by the next receiver, return false. 1340 */ onKeyUp(int keyCode, KeyEvent event)1341 boolean onKeyUp(int keyCode, KeyEvent event); 1342 1343 /** 1344 * Called when multiple down/up pairs of the same key have occurred 1345 * in a row. 1346 * 1347 * @param keyCode The value in event.getKeyCode(). 1348 * @param count Number of pairs as returned by event.getRepeatCount(). 1349 * @param event Description of the key event. 1350 * 1351 * @return If you handled the event, return true. If you want to allow 1352 * the event to be handled by the next receiver, return false. 1353 */ onKeyMultiple(int keyCode, int count, KeyEvent event)1354 boolean onKeyMultiple(int keyCode, int count, KeyEvent event); 1355 } 1356 1357 static { populateKeycodeSymbolicNames()1358 populateKeycodeSymbolicNames(); 1359 } 1360 KeyEvent()1361 private KeyEvent() { 1362 } 1363 1364 /** 1365 * Create a new key event. 1366 * 1367 * @param action Action code: either {@link #ACTION_DOWN}, 1368 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 1369 * @param code The key code. 1370 */ KeyEvent(int action, int code)1371 public KeyEvent(int action, int code) { 1372 mAction = action; 1373 mKeyCode = code; 1374 mRepeatCount = 0; 1375 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD; 1376 } 1377 1378 /** 1379 * Create a new key event. 1380 * 1381 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1382 * at which this key code originally went down. 1383 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1384 * at which this event happened. 1385 * @param action Action code: either {@link #ACTION_DOWN}, 1386 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 1387 * @param code The key code. 1388 * @param repeat A repeat count for down events (> 0 if this is after the 1389 * initial down) or event count for multiple events. 1390 */ KeyEvent(long downTime, long eventTime, int action, int code, int repeat)1391 public KeyEvent(long downTime, long eventTime, int action, 1392 int code, int repeat) { 1393 mDownTime = downTime; 1394 mEventTime = eventTime; 1395 mAction = action; 1396 mKeyCode = code; 1397 mRepeatCount = repeat; 1398 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD; 1399 } 1400 1401 /** 1402 * Create a new key event. 1403 * 1404 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1405 * at which this key code originally went down. 1406 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1407 * at which this event happened. 1408 * @param action Action code: either {@link #ACTION_DOWN}, 1409 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 1410 * @param code The key code. 1411 * @param repeat A repeat count for down events (> 0 if this is after the 1412 * initial down) or event count for multiple events. 1413 * @param metaState Flags indicating which meta keys are currently pressed. 1414 */ KeyEvent(long downTime, long eventTime, int action, int code, int repeat, int metaState)1415 public KeyEvent(long downTime, long eventTime, int action, 1416 int code, int repeat, int metaState) { 1417 mDownTime = downTime; 1418 mEventTime = eventTime; 1419 mAction = action; 1420 mKeyCode = code; 1421 mRepeatCount = repeat; 1422 mMetaState = metaState; 1423 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD; 1424 } 1425 1426 /** 1427 * Create a new key event. 1428 * 1429 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1430 * at which this key code originally went down. 1431 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1432 * at which this event happened. 1433 * @param action Action code: either {@link #ACTION_DOWN}, 1434 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 1435 * @param code The key code. 1436 * @param repeat A repeat count for down events (> 0 if this is after the 1437 * initial down) or event count for multiple events. 1438 * @param metaState Flags indicating which meta keys are currently pressed. 1439 * @param deviceId The device ID that generated the key event. 1440 * @param scancode Raw device scan code of the event. 1441 */ KeyEvent(long downTime, long eventTime, int action, int code, int repeat, int metaState, int deviceId, int scancode)1442 public KeyEvent(long downTime, long eventTime, int action, 1443 int code, int repeat, int metaState, 1444 int deviceId, int scancode) { 1445 mDownTime = downTime; 1446 mEventTime = eventTime; 1447 mAction = action; 1448 mKeyCode = code; 1449 mRepeatCount = repeat; 1450 mMetaState = metaState; 1451 mDeviceId = deviceId; 1452 mScanCode = scancode; 1453 } 1454 1455 /** 1456 * Create a new key event. 1457 * 1458 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1459 * at which this key code originally went down. 1460 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1461 * at which this event happened. 1462 * @param action Action code: either {@link #ACTION_DOWN}, 1463 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 1464 * @param code The key code. 1465 * @param repeat A repeat count for down events (> 0 if this is after the 1466 * initial down) or event count for multiple events. 1467 * @param metaState Flags indicating which meta keys are currently pressed. 1468 * @param deviceId The device ID that generated the key event. 1469 * @param scancode Raw device scan code of the event. 1470 * @param flags The flags for this key event 1471 */ KeyEvent(long downTime, long eventTime, int action, int code, int repeat, int metaState, int deviceId, int scancode, int flags)1472 public KeyEvent(long downTime, long eventTime, int action, 1473 int code, int repeat, int metaState, 1474 int deviceId, int scancode, int flags) { 1475 mDownTime = downTime; 1476 mEventTime = eventTime; 1477 mAction = action; 1478 mKeyCode = code; 1479 mRepeatCount = repeat; 1480 mMetaState = metaState; 1481 mDeviceId = deviceId; 1482 mScanCode = scancode; 1483 mFlags = flags; 1484 } 1485 1486 /** 1487 * Create a new key event. 1488 * 1489 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1490 * at which this key code originally went down. 1491 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1492 * at which this event happened. 1493 * @param action Action code: either {@link #ACTION_DOWN}, 1494 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 1495 * @param code The key code. 1496 * @param repeat A repeat count for down events (> 0 if this is after the 1497 * initial down) or event count for multiple events. 1498 * @param metaState Flags indicating which meta keys are currently pressed. 1499 * @param deviceId The device ID that generated the key event. 1500 * @param scancode Raw device scan code of the event. 1501 * @param flags The flags for this key event 1502 * @param source The input source such as {@link InputDevice#SOURCE_KEYBOARD}. 1503 */ KeyEvent(long downTime, long eventTime, int action, int code, int repeat, int metaState, int deviceId, int scancode, int flags, int source)1504 public KeyEvent(long downTime, long eventTime, int action, 1505 int code, int repeat, int metaState, 1506 int deviceId, int scancode, int flags, int source) { 1507 mDownTime = downTime; 1508 mEventTime = eventTime; 1509 mAction = action; 1510 mKeyCode = code; 1511 mRepeatCount = repeat; 1512 mMetaState = metaState; 1513 mDeviceId = deviceId; 1514 mScanCode = scancode; 1515 mFlags = flags; 1516 mSource = source; 1517 } 1518 1519 /** 1520 * Create a new key event for a string of characters. The key code, 1521 * action, repeat count and source will automatically be set to 1522 * {@link #KEYCODE_UNKNOWN}, {@link #ACTION_MULTIPLE}, 0, and 1523 * {@link InputDevice#SOURCE_KEYBOARD} for you. 1524 * 1525 * @param time The time (in {@link android.os.SystemClock#uptimeMillis}) 1526 * at which this event occured. 1527 * @param characters The string of characters. 1528 * @param deviceId The device ID that generated the key event. 1529 * @param flags The flags for this key event 1530 */ KeyEvent(long time, String characters, int deviceId, int flags)1531 public KeyEvent(long time, String characters, int deviceId, int flags) { 1532 mDownTime = time; 1533 mEventTime = time; 1534 mCharacters = characters; 1535 mAction = ACTION_MULTIPLE; 1536 mKeyCode = KEYCODE_UNKNOWN; 1537 mRepeatCount = 0; 1538 mDeviceId = deviceId; 1539 mFlags = flags; 1540 mSource = InputDevice.SOURCE_KEYBOARD; 1541 } 1542 1543 /** 1544 * Make an exact copy of an existing key event. 1545 */ KeyEvent(KeyEvent origEvent)1546 public KeyEvent(KeyEvent origEvent) { 1547 mDownTime = origEvent.mDownTime; 1548 mEventTime = origEvent.mEventTime; 1549 mAction = origEvent.mAction; 1550 mKeyCode = origEvent.mKeyCode; 1551 mRepeatCount = origEvent.mRepeatCount; 1552 mMetaState = origEvent.mMetaState; 1553 mDeviceId = origEvent.mDeviceId; 1554 mSource = origEvent.mSource; 1555 mScanCode = origEvent.mScanCode; 1556 mFlags = origEvent.mFlags; 1557 mCharacters = origEvent.mCharacters; 1558 } 1559 1560 /** 1561 * Copy an existing key event, modifying its time and repeat count. 1562 * 1563 * @deprecated Use {@link #changeTimeRepeat(KeyEvent, long, int)} 1564 * instead. 1565 * 1566 * @param origEvent The existing event to be copied. 1567 * @param eventTime The new event time 1568 * (in {@link android.os.SystemClock#uptimeMillis}) of the event. 1569 * @param newRepeat The new repeat count of the event. 1570 */ 1571 @Deprecated KeyEvent(KeyEvent origEvent, long eventTime, int newRepeat)1572 public KeyEvent(KeyEvent origEvent, long eventTime, int newRepeat) { 1573 mDownTime = origEvent.mDownTime; 1574 mEventTime = eventTime; 1575 mAction = origEvent.mAction; 1576 mKeyCode = origEvent.mKeyCode; 1577 mRepeatCount = newRepeat; 1578 mMetaState = origEvent.mMetaState; 1579 mDeviceId = origEvent.mDeviceId; 1580 mSource = origEvent.mSource; 1581 mScanCode = origEvent.mScanCode; 1582 mFlags = origEvent.mFlags; 1583 mCharacters = origEvent.mCharacters; 1584 } 1585 obtain()1586 private static KeyEvent obtain() { 1587 final KeyEvent ev; 1588 synchronized (gRecyclerLock) { 1589 ev = gRecyclerTop; 1590 if (ev == null) { 1591 return new KeyEvent(); 1592 } 1593 gRecyclerTop = ev.mNext; 1594 gRecyclerUsed -= 1; 1595 } 1596 ev.mNext = null; 1597 ev.prepareForReuse(); 1598 return ev; 1599 } 1600 1601 /** 1602 * Obtains a (potentially recycled) key event. 1603 * 1604 * @hide 1605 */ obtain(long downTime, long eventTime, int action, int code, int repeat, int metaState, int deviceId, int scancode, int flags, int source, String characters)1606 public static KeyEvent obtain(long downTime, long eventTime, int action, 1607 int code, int repeat, int metaState, 1608 int deviceId, int scancode, int flags, int source, String characters) { 1609 KeyEvent ev = obtain(); 1610 ev.mDownTime = downTime; 1611 ev.mEventTime = eventTime; 1612 ev.mAction = action; 1613 ev.mKeyCode = code; 1614 ev.mRepeatCount = repeat; 1615 ev.mMetaState = metaState; 1616 ev.mDeviceId = deviceId; 1617 ev.mScanCode = scancode; 1618 ev.mFlags = flags; 1619 ev.mSource = source; 1620 ev.mCharacters = characters; 1621 return ev; 1622 } 1623 1624 /** 1625 * Obtains a (potentially recycled) copy of another key event. 1626 * 1627 * @hide 1628 */ obtain(KeyEvent other)1629 public static KeyEvent obtain(KeyEvent other) { 1630 KeyEvent ev = obtain(); 1631 ev.mDownTime = other.mDownTime; 1632 ev.mEventTime = other.mEventTime; 1633 ev.mAction = other.mAction; 1634 ev.mKeyCode = other.mKeyCode; 1635 ev.mRepeatCount = other.mRepeatCount; 1636 ev.mMetaState = other.mMetaState; 1637 ev.mDeviceId = other.mDeviceId; 1638 ev.mScanCode = other.mScanCode; 1639 ev.mFlags = other.mFlags; 1640 ev.mSource = other.mSource; 1641 ev.mCharacters = other.mCharacters; 1642 return ev; 1643 } 1644 1645 /** @hide */ 1646 @Override copy()1647 public KeyEvent copy() { 1648 return obtain(this); 1649 } 1650 1651 /** 1652 * Recycles a key event. 1653 * Key events should only be recycled if they are owned by the system since user 1654 * code expects them to be essentially immutable, "tracking" notwithstanding. 1655 * 1656 * @hide 1657 */ 1658 @Override recycle()1659 public final void recycle() { 1660 super.recycle(); 1661 mCharacters = null; 1662 1663 synchronized (gRecyclerLock) { 1664 if (gRecyclerUsed < MAX_RECYCLED) { 1665 gRecyclerUsed++; 1666 mNext = gRecyclerTop; 1667 gRecyclerTop = this; 1668 } 1669 } 1670 } 1671 1672 /** @hide */ 1673 @Override recycleIfNeededAfterDispatch()1674 public final void recycleIfNeededAfterDispatch() { 1675 // Do nothing. 1676 } 1677 1678 /** 1679 * Create a new key event that is the same as the given one, but whose 1680 * event time and repeat count are replaced with the given value. 1681 * 1682 * @param event The existing event to be copied. This is not modified. 1683 * @param eventTime The new event time 1684 * (in {@link android.os.SystemClock#uptimeMillis}) of the event. 1685 * @param newRepeat The new repeat count of the event. 1686 */ changeTimeRepeat(KeyEvent event, long eventTime, int newRepeat)1687 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime, 1688 int newRepeat) { 1689 return new KeyEvent(event, eventTime, newRepeat); 1690 } 1691 1692 /** 1693 * Create a new key event that is the same as the given one, but whose 1694 * event time and repeat count are replaced with the given value. 1695 * 1696 * @param event The existing event to be copied. This is not modified. 1697 * @param eventTime The new event time 1698 * (in {@link android.os.SystemClock#uptimeMillis}) of the event. 1699 * @param newRepeat The new repeat count of the event. 1700 * @param newFlags New flags for the event, replacing the entire value 1701 * in the original event. 1702 */ changeTimeRepeat(KeyEvent event, long eventTime, int newRepeat, int newFlags)1703 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime, 1704 int newRepeat, int newFlags) { 1705 KeyEvent ret = new KeyEvent(event); 1706 ret.mEventTime = eventTime; 1707 ret.mRepeatCount = newRepeat; 1708 ret.mFlags = newFlags; 1709 return ret; 1710 } 1711 1712 /** 1713 * Copy an existing key event, modifying its action. 1714 * 1715 * @param origEvent The existing event to be copied. 1716 * @param action The new action code of the event. 1717 */ KeyEvent(KeyEvent origEvent, int action)1718 private KeyEvent(KeyEvent origEvent, int action) { 1719 mDownTime = origEvent.mDownTime; 1720 mEventTime = origEvent.mEventTime; 1721 mAction = action; 1722 mKeyCode = origEvent.mKeyCode; 1723 mRepeatCount = origEvent.mRepeatCount; 1724 mMetaState = origEvent.mMetaState; 1725 mDeviceId = origEvent.mDeviceId; 1726 mSource = origEvent.mSource; 1727 mScanCode = origEvent.mScanCode; 1728 mFlags = origEvent.mFlags; 1729 // Don't copy mCharacters, since one way or the other we'll lose it 1730 // when changing the action. 1731 } 1732 1733 /** 1734 * Create a new key event that is the same as the given one, but whose 1735 * action is replaced with the given value. 1736 * 1737 * @param event The existing event to be copied. This is not modified. 1738 * @param action The new action code of the event. 1739 */ changeAction(KeyEvent event, int action)1740 public static KeyEvent changeAction(KeyEvent event, int action) { 1741 return new KeyEvent(event, action); 1742 } 1743 1744 /** 1745 * Create a new key event that is the same as the given one, but whose 1746 * flags are replaced with the given value. 1747 * 1748 * @param event The existing event to be copied. This is not modified. 1749 * @param flags The new flags constant. 1750 */ changeFlags(KeyEvent event, int flags)1751 public static KeyEvent changeFlags(KeyEvent event, int flags) { 1752 event = new KeyEvent(event); 1753 event.mFlags = flags; 1754 return event; 1755 } 1756 1757 /** @hide */ 1758 @Override isTainted()1759 public final boolean isTainted() { 1760 return (mFlags & FLAG_TAINTED) != 0; 1761 } 1762 1763 /** @hide */ 1764 @Override setTainted(boolean tainted)1765 public final void setTainted(boolean tainted) { 1766 mFlags = tainted ? mFlags | FLAG_TAINTED : mFlags & ~FLAG_TAINTED; 1767 } 1768 1769 /** 1770 * Don't use in new code, instead explicitly check 1771 * {@link #getAction()}. 1772 * 1773 * @return If the action is ACTION_DOWN, returns true; else false. 1774 * 1775 * @deprecated 1776 * @hide 1777 */ isDown()1778 @Deprecated public final boolean isDown() { 1779 return mAction == ACTION_DOWN; 1780 } 1781 1782 /** 1783 * Is this a system key? System keys can not be used for menu shortcuts. 1784 * 1785 * TODO: this information should come from a table somewhere. 1786 * TODO: should the dpad keys be here? arguably, because they also shouldn't be menu shortcuts 1787 */ isSystem()1788 public final boolean isSystem() { 1789 return native_isSystemKey(mKeyCode); 1790 } 1791 1792 /** @hide */ hasDefaultAction()1793 public final boolean hasDefaultAction() { 1794 return native_hasDefaultAction(mKeyCode); 1795 } 1796 1797 /** 1798 * Returns true if the specified keycode is a gamepad button. 1799 * @return True if the keycode is a gamepad button, such as {@link #KEYCODE_BUTTON_A}. 1800 */ isGamepadButton(int keyCode)1801 public static final boolean isGamepadButton(int keyCode) { 1802 switch (keyCode) { 1803 case KeyEvent.KEYCODE_BUTTON_A: 1804 case KeyEvent.KEYCODE_BUTTON_B: 1805 case KeyEvent.KEYCODE_BUTTON_C: 1806 case KeyEvent.KEYCODE_BUTTON_X: 1807 case KeyEvent.KEYCODE_BUTTON_Y: 1808 case KeyEvent.KEYCODE_BUTTON_Z: 1809 case KeyEvent.KEYCODE_BUTTON_L1: 1810 case KeyEvent.KEYCODE_BUTTON_R1: 1811 case KeyEvent.KEYCODE_BUTTON_L2: 1812 case KeyEvent.KEYCODE_BUTTON_R2: 1813 case KeyEvent.KEYCODE_BUTTON_THUMBL: 1814 case KeyEvent.KEYCODE_BUTTON_THUMBR: 1815 case KeyEvent.KEYCODE_BUTTON_START: 1816 case KeyEvent.KEYCODE_BUTTON_SELECT: 1817 case KeyEvent.KEYCODE_BUTTON_MODE: 1818 case KeyEvent.KEYCODE_BUTTON_1: 1819 case KeyEvent.KEYCODE_BUTTON_2: 1820 case KeyEvent.KEYCODE_BUTTON_3: 1821 case KeyEvent.KEYCODE_BUTTON_4: 1822 case KeyEvent.KEYCODE_BUTTON_5: 1823 case KeyEvent.KEYCODE_BUTTON_6: 1824 case KeyEvent.KEYCODE_BUTTON_7: 1825 case KeyEvent.KEYCODE_BUTTON_8: 1826 case KeyEvent.KEYCODE_BUTTON_9: 1827 case KeyEvent.KEYCODE_BUTTON_10: 1828 case KeyEvent.KEYCODE_BUTTON_11: 1829 case KeyEvent.KEYCODE_BUTTON_12: 1830 case KeyEvent.KEYCODE_BUTTON_13: 1831 case KeyEvent.KEYCODE_BUTTON_14: 1832 case KeyEvent.KEYCODE_BUTTON_15: 1833 case KeyEvent.KEYCODE_BUTTON_16: 1834 return true; 1835 default: 1836 return false; 1837 } 1838 } 1839 1840 /** Whether key will, by default, trigger a click on the focused view. 1841 * @hide 1842 */ isConfirmKey(int keyCode)1843 public static final boolean isConfirmKey(int keyCode) { 1844 switch (keyCode) { 1845 case KeyEvent.KEYCODE_DPAD_CENTER: 1846 case KeyEvent.KEYCODE_ENTER: 1847 return true; 1848 default: 1849 return false; 1850 } 1851 } 1852 1853 /** {@inheritDoc} */ 1854 @Override getDeviceId()1855 public final int getDeviceId() { 1856 return mDeviceId; 1857 } 1858 1859 /** {@inheritDoc} */ 1860 @Override getSource()1861 public final int getSource() { 1862 return mSource; 1863 } 1864 1865 /** {@inheritDoc} */ 1866 @Override setSource(int source)1867 public final void setSource(int source) { 1868 mSource = source; 1869 } 1870 1871 /** 1872 * <p>Returns the state of the meta keys.</p> 1873 * 1874 * @return an integer in which each bit set to 1 represents a pressed 1875 * meta key 1876 * 1877 * @see #isAltPressed() 1878 * @see #isShiftPressed() 1879 * @see #isSymPressed() 1880 * @see #isCtrlPressed() 1881 * @see #isMetaPressed() 1882 * @see #isFunctionPressed() 1883 * @see #isCapsLockOn() 1884 * @see #isNumLockOn() 1885 * @see #isScrollLockOn() 1886 * @see #META_ALT_ON 1887 * @see #META_ALT_LEFT_ON 1888 * @see #META_ALT_RIGHT_ON 1889 * @see #META_SHIFT_ON 1890 * @see #META_SHIFT_LEFT_ON 1891 * @see #META_SHIFT_RIGHT_ON 1892 * @see #META_SYM_ON 1893 * @see #META_FUNCTION_ON 1894 * @see #META_CTRL_ON 1895 * @see #META_CTRL_LEFT_ON 1896 * @see #META_CTRL_RIGHT_ON 1897 * @see #META_META_ON 1898 * @see #META_META_LEFT_ON 1899 * @see #META_META_RIGHT_ON 1900 * @see #META_CAPS_LOCK_ON 1901 * @see #META_NUM_LOCK_ON 1902 * @see #META_SCROLL_LOCK_ON 1903 * @see #getModifiers 1904 */ getMetaState()1905 public final int getMetaState() { 1906 return mMetaState; 1907 } 1908 1909 /** 1910 * Returns the state of the modifier keys. 1911 * <p> 1912 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK}, 1913 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are 1914 * not considered modifier keys. Consequently, this function specifically masks out 1915 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}. 1916 * </p><p> 1917 * The value returned consists of the meta state (from {@link #getMetaState}) 1918 * normalized using {@link #normalizeMetaState(int)} and then masked with 1919 * {@link #getModifierMetaStateMask} so that only valid modifier bits are retained. 1920 * </p> 1921 * 1922 * @return An integer in which each bit set to 1 represents a pressed modifier key. 1923 * @see #getMetaState 1924 */ getModifiers()1925 public final int getModifiers() { 1926 return normalizeMetaState(mMetaState) & META_MODIFIER_MASK; 1927 } 1928 1929 /** 1930 * Returns the flags for this key event. 1931 * 1932 * @see #FLAG_WOKE_HERE 1933 */ getFlags()1934 public final int getFlags() { 1935 return mFlags; 1936 } 1937 1938 // Mask of all modifier key meta states. Specifically excludes locked keys like caps lock. 1939 private static final int META_MODIFIER_MASK = 1940 META_SHIFT_ON | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON 1941 | META_ALT_ON | META_ALT_LEFT_ON | META_ALT_RIGHT_ON 1942 | META_CTRL_ON | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON 1943 | META_META_ON | META_META_LEFT_ON | META_META_RIGHT_ON 1944 | META_SYM_ON | META_FUNCTION_ON; 1945 1946 // Mask of all lock key meta states. 1947 private static final int META_LOCK_MASK = 1948 META_CAPS_LOCK_ON | META_NUM_LOCK_ON | META_SCROLL_LOCK_ON; 1949 1950 // Mask of all valid meta states. 1951 private static final int META_ALL_MASK = META_MODIFIER_MASK | META_LOCK_MASK; 1952 1953 // Mask of all synthetic meta states that are reserved for API compatibility with 1954 // historical uses in MetaKeyKeyListener. 1955 private static final int META_SYNTHETIC_MASK = 1956 META_CAP_LOCKED | META_ALT_LOCKED | META_SYM_LOCKED | META_SELECTING; 1957 1958 // Mask of all meta states that are not valid use in specifying a modifier key. 1959 // These bits are known to be used for purposes other than specifying modifiers. 1960 private static final int META_INVALID_MODIFIER_MASK = 1961 META_LOCK_MASK | META_SYNTHETIC_MASK; 1962 1963 /** 1964 * Gets a mask that includes all valid modifier key meta state bits. 1965 * <p> 1966 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK}, 1967 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are 1968 * not considered modifier keys. Consequently, the mask specifically excludes 1969 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}. 1970 * </p> 1971 * 1972 * @return The modifier meta state mask which is a combination of 1973 * {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON}, {@link #META_SHIFT_RIGHT_ON}, 1974 * {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON}, {@link #META_ALT_RIGHT_ON}, 1975 * {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON}, {@link #META_CTRL_RIGHT_ON}, 1976 * {@link #META_META_ON}, {@link #META_META_LEFT_ON}, {@link #META_META_RIGHT_ON}, 1977 * {@link #META_SYM_ON}, {@link #META_FUNCTION_ON}. 1978 */ getModifierMetaStateMask()1979 public static int getModifierMetaStateMask() { 1980 return META_MODIFIER_MASK; 1981 } 1982 1983 /** 1984 * Returns true if this key code is a modifier key. 1985 * <p> 1986 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK}, 1987 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are 1988 * not considered modifier keys. Consequently, this function return false 1989 * for those keys. 1990 * </p> 1991 * 1992 * @return True if the key code is one of 1993 * {@link #KEYCODE_SHIFT_LEFT} {@link #KEYCODE_SHIFT_RIGHT}, 1994 * {@link #KEYCODE_ALT_LEFT}, {@link #KEYCODE_ALT_RIGHT}, 1995 * {@link #KEYCODE_CTRL_LEFT}, {@link #KEYCODE_CTRL_RIGHT}, 1996 * {@link #KEYCODE_META_LEFT}, or {@link #KEYCODE_META_RIGHT}, 1997 * {@link #KEYCODE_SYM}, {@link #KEYCODE_NUM}, {@link #KEYCODE_FUNCTION}. 1998 */ isModifierKey(int keyCode)1999 public static boolean isModifierKey(int keyCode) { 2000 switch (keyCode) { 2001 case KEYCODE_SHIFT_LEFT: 2002 case KEYCODE_SHIFT_RIGHT: 2003 case KEYCODE_ALT_LEFT: 2004 case KEYCODE_ALT_RIGHT: 2005 case KEYCODE_CTRL_LEFT: 2006 case KEYCODE_CTRL_RIGHT: 2007 case KEYCODE_META_LEFT: 2008 case KEYCODE_META_RIGHT: 2009 case KEYCODE_SYM: 2010 case KEYCODE_NUM: 2011 case KEYCODE_FUNCTION: 2012 return true; 2013 default: 2014 return false; 2015 } 2016 } 2017 2018 /** 2019 * Normalizes the specified meta state. 2020 * <p> 2021 * The meta state is normalized such that if either the left or right modifier meta state 2022 * bits are set then the result will also include the universal bit for that modifier. 2023 * </p><p> 2024 * If the specified meta state contains {@link #META_ALT_LEFT_ON} then 2025 * the result will also contain {@link #META_ALT_ON} in addition to {@link #META_ALT_LEFT_ON} 2026 * and the other bits that were specified in the input. The same is process is 2027 * performed for shift, control and meta. 2028 * </p><p> 2029 * If the specified meta state contains synthetic meta states defined by 2030 * {@link MetaKeyKeyListener}, then those states are translated here and the original 2031 * synthetic meta states are removed from the result. 2032 * {@link MetaKeyKeyListener#META_CAP_LOCKED} is translated to {@link #META_CAPS_LOCK_ON}. 2033 * {@link MetaKeyKeyListener#META_ALT_LOCKED} is translated to {@link #META_ALT_ON}. 2034 * {@link MetaKeyKeyListener#META_SYM_LOCKED} is translated to {@link #META_SYM_ON}. 2035 * </p><p> 2036 * Undefined meta state bits are removed. 2037 * </p> 2038 * 2039 * @param metaState The meta state. 2040 * @return The normalized meta state. 2041 */ normalizeMetaState(int metaState)2042 public static int normalizeMetaState(int metaState) { 2043 if ((metaState & (META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON)) != 0) { 2044 metaState |= META_SHIFT_ON; 2045 } 2046 if ((metaState & (META_ALT_LEFT_ON | META_ALT_RIGHT_ON)) != 0) { 2047 metaState |= META_ALT_ON; 2048 } 2049 if ((metaState & (META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON)) != 0) { 2050 metaState |= META_CTRL_ON; 2051 } 2052 if ((metaState & (META_META_LEFT_ON | META_META_RIGHT_ON)) != 0) { 2053 metaState |= META_META_ON; 2054 } 2055 if ((metaState & MetaKeyKeyListener.META_CAP_LOCKED) != 0) { 2056 metaState |= META_CAPS_LOCK_ON; 2057 } 2058 if ((metaState & MetaKeyKeyListener.META_ALT_LOCKED) != 0) { 2059 metaState |= META_ALT_ON; 2060 } 2061 if ((metaState & MetaKeyKeyListener.META_SYM_LOCKED) != 0) { 2062 metaState |= META_SYM_ON; 2063 } 2064 return metaState & META_ALL_MASK; 2065 } 2066 2067 /** 2068 * Returns true if no modifiers keys are pressed according to the specified meta state. 2069 * <p> 2070 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK}, 2071 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are 2072 * not considered modifier keys. Consequently, this function ignores 2073 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}. 2074 * </p><p> 2075 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}. 2076 * </p> 2077 * 2078 * @param metaState The meta state to consider. 2079 * @return True if no modifier keys are pressed. 2080 * @see #hasNoModifiers() 2081 */ metaStateHasNoModifiers(int metaState)2082 public static boolean metaStateHasNoModifiers(int metaState) { 2083 return (normalizeMetaState(metaState) & META_MODIFIER_MASK) == 0; 2084 } 2085 2086 /** 2087 * Returns true if only the specified modifier keys are pressed according to 2088 * the specified meta state. Returns false if a different combination of modifier 2089 * keys are pressed. 2090 * <p> 2091 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK}, 2092 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are 2093 * not considered modifier keys. Consequently, this function ignores 2094 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}. 2095 * </p><p> 2096 * If the specified modifier mask includes directional modifiers, such as 2097 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the 2098 * modifier is pressed on that side. 2099 * If the specified modifier mask includes non-directional modifiers, such as 2100 * {@link #META_SHIFT_ON}, then this method ensures that the modifier 2101 * is pressed on either side. 2102 * If the specified modifier mask includes both directional and non-directional modifiers 2103 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON}, 2104 * then this method throws an illegal argument exception. 2105 * </p> 2106 * 2107 * @param metaState The meta state to consider. 2108 * @param modifiers The meta state of the modifier keys to check. May be a combination 2109 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to 2110 * ensure that no modifier keys are pressed. 2111 * @return True if only the specified modifier keys are pressed. 2112 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers 2113 * @see #hasModifiers 2114 */ metaStateHasModifiers(int metaState, int modifiers)2115 public static boolean metaStateHasModifiers(int metaState, int modifiers) { 2116 // Note: For forward compatibility, we allow the parameter to contain meta states 2117 // that we do not recognize but we explicitly disallow meta states that 2118 // are not valid modifiers. 2119 if ((modifiers & META_INVALID_MODIFIER_MASK) != 0) { 2120 throw new IllegalArgumentException("modifiers must not contain " 2121 + "META_CAPS_LOCK_ON, META_NUM_LOCK_ON, META_SCROLL_LOCK_ON, " 2122 + "META_CAP_LOCKED, META_ALT_LOCKED, META_SYM_LOCKED, " 2123 + "or META_SELECTING"); 2124 } 2125 2126 metaState = normalizeMetaState(metaState) & META_MODIFIER_MASK; 2127 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers, 2128 META_SHIFT_ON, META_SHIFT_LEFT_ON, META_SHIFT_RIGHT_ON); 2129 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers, 2130 META_ALT_ON, META_ALT_LEFT_ON, META_ALT_RIGHT_ON); 2131 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers, 2132 META_CTRL_ON, META_CTRL_LEFT_ON, META_CTRL_RIGHT_ON); 2133 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers, 2134 META_META_ON, META_META_LEFT_ON, META_META_RIGHT_ON); 2135 return metaState == modifiers; 2136 } 2137 metaStateFilterDirectionalModifiers(int metaState, int modifiers, int basic, int left, int right)2138 private static int metaStateFilterDirectionalModifiers(int metaState, 2139 int modifiers, int basic, int left, int right) { 2140 final boolean wantBasic = (modifiers & basic) != 0; 2141 final int directional = left | right; 2142 final boolean wantLeftOrRight = (modifiers & directional) != 0; 2143 2144 if (wantBasic) { 2145 if (wantLeftOrRight) { 2146 throw new IllegalArgumentException("modifiers must not contain " 2147 + metaStateToString(basic) + " combined with " 2148 + metaStateToString(left) + " or " + metaStateToString(right)); 2149 } 2150 return metaState & ~directional; 2151 } else if (wantLeftOrRight) { 2152 return metaState & ~basic; 2153 } else { 2154 return metaState; 2155 } 2156 } 2157 2158 /** 2159 * Returns true if no modifier keys are pressed. 2160 * <p> 2161 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK}, 2162 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are 2163 * not considered modifier keys. Consequently, this function ignores 2164 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}. 2165 * </p><p> 2166 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}. 2167 * </p> 2168 * 2169 * @return True if no modifier keys are pressed. 2170 * @see #metaStateHasNoModifiers 2171 */ hasNoModifiers()2172 public final boolean hasNoModifiers() { 2173 return metaStateHasNoModifiers(mMetaState); 2174 } 2175 2176 /** 2177 * Returns true if only the specified modifiers keys are pressed. 2178 * Returns false if a different combination of modifier keys are pressed. 2179 * <p> 2180 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK}, 2181 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are 2182 * not considered modifier keys. Consequently, this function ignores 2183 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}. 2184 * </p><p> 2185 * If the specified modifier mask includes directional modifiers, such as 2186 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the 2187 * modifier is pressed on that side. 2188 * If the specified modifier mask includes non-directional modifiers, such as 2189 * {@link #META_SHIFT_ON}, then this method ensures that the modifier 2190 * is pressed on either side. 2191 * If the specified modifier mask includes both directional and non-directional modifiers 2192 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON}, 2193 * then this method throws an illegal argument exception. 2194 * </p> 2195 * 2196 * @param modifiers The meta state of the modifier keys to check. May be a combination 2197 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to 2198 * ensure that no modifier keys are pressed. 2199 * @return True if only the specified modifier keys are pressed. 2200 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers 2201 * @see #metaStateHasModifiers 2202 */ hasModifiers(int modifiers)2203 public final boolean hasModifiers(int modifiers) { 2204 return metaStateHasModifiers(mMetaState, modifiers); 2205 } 2206 2207 /** 2208 * <p>Returns the pressed state of the ALT meta key.</p> 2209 * 2210 * @return true if the ALT key is pressed, false otherwise 2211 * 2212 * @see #KEYCODE_ALT_LEFT 2213 * @see #KEYCODE_ALT_RIGHT 2214 * @see #META_ALT_ON 2215 */ isAltPressed()2216 public final boolean isAltPressed() { 2217 return (mMetaState & META_ALT_ON) != 0; 2218 } 2219 2220 /** 2221 * <p>Returns the pressed state of the SHIFT meta key.</p> 2222 * 2223 * @return true if the SHIFT key is pressed, false otherwise 2224 * 2225 * @see #KEYCODE_SHIFT_LEFT 2226 * @see #KEYCODE_SHIFT_RIGHT 2227 * @see #META_SHIFT_ON 2228 */ isShiftPressed()2229 public final boolean isShiftPressed() { 2230 return (mMetaState & META_SHIFT_ON) != 0; 2231 } 2232 2233 /** 2234 * <p>Returns the pressed state of the SYM meta key.</p> 2235 * 2236 * @return true if the SYM key is pressed, false otherwise 2237 * 2238 * @see #KEYCODE_SYM 2239 * @see #META_SYM_ON 2240 */ isSymPressed()2241 public final boolean isSymPressed() { 2242 return (mMetaState & META_SYM_ON) != 0; 2243 } 2244 2245 /** 2246 * <p>Returns the pressed state of the CTRL meta key.</p> 2247 * 2248 * @return true if the CTRL key is pressed, false otherwise 2249 * 2250 * @see #KEYCODE_CTRL_LEFT 2251 * @see #KEYCODE_CTRL_RIGHT 2252 * @see #META_CTRL_ON 2253 */ isCtrlPressed()2254 public final boolean isCtrlPressed() { 2255 return (mMetaState & META_CTRL_ON) != 0; 2256 } 2257 2258 /** 2259 * <p>Returns the pressed state of the META meta key.</p> 2260 * 2261 * @return true if the META key is pressed, false otherwise 2262 * 2263 * @see #KEYCODE_META_LEFT 2264 * @see #KEYCODE_META_RIGHT 2265 * @see #META_META_ON 2266 */ isMetaPressed()2267 public final boolean isMetaPressed() { 2268 return (mMetaState & META_META_ON) != 0; 2269 } 2270 2271 /** 2272 * <p>Returns the pressed state of the FUNCTION meta key.</p> 2273 * 2274 * @return true if the FUNCTION key is pressed, false otherwise 2275 * 2276 * @see #KEYCODE_FUNCTION 2277 * @see #META_FUNCTION_ON 2278 */ isFunctionPressed()2279 public final boolean isFunctionPressed() { 2280 return (mMetaState & META_FUNCTION_ON) != 0; 2281 } 2282 2283 /** 2284 * <p>Returns the locked state of the CAPS LOCK meta key.</p> 2285 * 2286 * @return true if the CAPS LOCK key is on, false otherwise 2287 * 2288 * @see #KEYCODE_CAPS_LOCK 2289 * @see #META_CAPS_LOCK_ON 2290 */ isCapsLockOn()2291 public final boolean isCapsLockOn() { 2292 return (mMetaState & META_CAPS_LOCK_ON) != 0; 2293 } 2294 2295 /** 2296 * <p>Returns the locked state of the NUM LOCK meta key.</p> 2297 * 2298 * @return true if the NUM LOCK key is on, false otherwise 2299 * 2300 * @see #KEYCODE_NUM_LOCK 2301 * @see #META_NUM_LOCK_ON 2302 */ isNumLockOn()2303 public final boolean isNumLockOn() { 2304 return (mMetaState & META_NUM_LOCK_ON) != 0; 2305 } 2306 2307 /** 2308 * <p>Returns the locked state of the SCROLL LOCK meta key.</p> 2309 * 2310 * @return true if the SCROLL LOCK key is on, false otherwise 2311 * 2312 * @see #KEYCODE_SCROLL_LOCK 2313 * @see #META_SCROLL_LOCK_ON 2314 */ isScrollLockOn()2315 public final boolean isScrollLockOn() { 2316 return (mMetaState & META_SCROLL_LOCK_ON) != 0; 2317 } 2318 2319 /** 2320 * Retrieve the action of this key event. May be either 2321 * {@link #ACTION_DOWN}, {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 2322 * 2323 * @return The event action: ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE. 2324 */ getAction()2325 public final int getAction() { 2326 return mAction; 2327 } 2328 2329 /** 2330 * For {@link #ACTION_UP} events, indicates that the event has been 2331 * canceled as per {@link #FLAG_CANCELED}. 2332 */ isCanceled()2333 public final boolean isCanceled() { 2334 return (mFlags&FLAG_CANCELED) != 0; 2335 } 2336 2337 /** 2338 * Call this during {@link Callback#onKeyDown} to have the system track 2339 * the key through its final up (possibly including a long press). Note 2340 * that only one key can be tracked at a time -- if another key down 2341 * event is received while a previous one is being tracked, tracking is 2342 * stopped on the previous event. 2343 */ startTracking()2344 public final void startTracking() { 2345 mFlags |= FLAG_START_TRACKING; 2346 } 2347 2348 /** 2349 * For {@link #ACTION_UP} events, indicates that the event is still being 2350 * tracked from its initial down event as per 2351 * {@link #FLAG_TRACKING}. 2352 */ isTracking()2353 public final boolean isTracking() { 2354 return (mFlags&FLAG_TRACKING) != 0; 2355 } 2356 2357 /** 2358 * For {@link #ACTION_DOWN} events, indicates that the event has been 2359 * canceled as per {@link #FLAG_LONG_PRESS}. 2360 */ isLongPress()2361 public final boolean isLongPress() { 2362 return (mFlags&FLAG_LONG_PRESS) != 0; 2363 } 2364 2365 /** 2366 * Retrieve the key code of the key event. This is the physical key that 2367 * was pressed, <em>not</em> the Unicode character. 2368 * 2369 * @return The key code of the event. 2370 */ getKeyCode()2371 public final int getKeyCode() { 2372 return mKeyCode; 2373 } 2374 2375 /** 2376 * For the special case of a {@link #ACTION_MULTIPLE} event with key 2377 * code of {@link #KEYCODE_UNKNOWN}, this is a raw string of characters 2378 * associated with the event. In all other cases it is null. 2379 * 2380 * @return Returns a String of 1 or more characters associated with 2381 * the event. 2382 */ getCharacters()2383 public final String getCharacters() { 2384 return mCharacters; 2385 } 2386 2387 /** 2388 * Retrieve the hardware key id of this key event. These values are not 2389 * reliable and vary from device to device. 2390 * 2391 * {@more} 2392 * Mostly this is here for debugging purposes. 2393 */ getScanCode()2394 public final int getScanCode() { 2395 return mScanCode; 2396 } 2397 2398 /** 2399 * Retrieve the repeat count of the event. For both key up and key down 2400 * events, this is the number of times the key has repeated with the first 2401 * down starting at 0 and counting up from there. For multiple key 2402 * events, this is the number of down/up pairs that have occurred. 2403 * 2404 * @return The number of times the key has repeated. 2405 */ getRepeatCount()2406 public final int getRepeatCount() { 2407 return mRepeatCount; 2408 } 2409 2410 /** 2411 * Retrieve the time of the most recent key down event, 2412 * in the {@link android.os.SystemClock#uptimeMillis} time base. If this 2413 * is a down event, this will be the same as {@link #getEventTime()}. 2414 * Note that when chording keys, this value is the down time of the 2415 * most recently pressed key, which may <em>not</em> be the same physical 2416 * key of this event. 2417 * 2418 * @return Returns the most recent key down time, in the 2419 * {@link android.os.SystemClock#uptimeMillis} time base 2420 */ getDownTime()2421 public final long getDownTime() { 2422 return mDownTime; 2423 } 2424 2425 /** 2426 * Retrieve the time this event occurred, 2427 * in the {@link android.os.SystemClock#uptimeMillis} time base. 2428 * 2429 * @return Returns the time this event occurred, 2430 * in the {@link android.os.SystemClock#uptimeMillis} time base. 2431 */ 2432 @Override getEventTime()2433 public final long getEventTime() { 2434 return mEventTime; 2435 } 2436 2437 /** 2438 * Retrieve the time this event occurred, 2439 * in the {@link android.os.SystemClock#uptimeMillis} time base but with 2440 * nanosecond (instead of millisecond) precision. 2441 * <p> 2442 * The value is in nanosecond precision but it may not have nanosecond accuracy. 2443 * </p> 2444 * 2445 * @return Returns the time this event occurred, 2446 * in the {@link android.os.SystemClock#uptimeMillis} time base but with 2447 * nanosecond (instead of millisecond) precision. 2448 * 2449 * @hide 2450 */ 2451 @Override getEventTimeNano()2452 public final long getEventTimeNano() { 2453 return mEventTime * 1000000L; 2454 } 2455 2456 /** 2457 * Renamed to {@link #getDeviceId}. 2458 * 2459 * @hide 2460 * @deprecated use {@link #getDeviceId()} instead. 2461 */ 2462 @Deprecated getKeyboardDevice()2463 public final int getKeyboardDevice() { 2464 return mDeviceId; 2465 } 2466 2467 /** 2468 * Gets the {@link KeyCharacterMap} associated with the keyboard device. 2469 * 2470 * @return The associated key character map. 2471 * @throws {@link KeyCharacterMap.UnavailableException} if the key character map 2472 * could not be loaded because it was malformed or the default key character map 2473 * is missing from the system. 2474 * 2475 * @see KeyCharacterMap#load 2476 */ getKeyCharacterMap()2477 public final KeyCharacterMap getKeyCharacterMap() { 2478 return KeyCharacterMap.load(mDeviceId); 2479 } 2480 2481 /** 2482 * Gets the primary character for this key. 2483 * In other words, the label that is physically printed on it. 2484 * 2485 * @return The display label character, or 0 if none (eg. for non-printing keys). 2486 */ getDisplayLabel()2487 public char getDisplayLabel() { 2488 return getKeyCharacterMap().getDisplayLabel(mKeyCode); 2489 } 2490 2491 /** 2492 * Gets the Unicode character generated by the specified key and meta 2493 * key state combination. 2494 * <p> 2495 * Returns the Unicode character that the specified key would produce 2496 * when the specified meta bits (see {@link MetaKeyKeyListener}) 2497 * were active. 2498 * </p><p> 2499 * Returns 0 if the key is not one that is used to type Unicode 2500 * characters. 2501 * </p><p> 2502 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the 2503 * key is a "dead key" that should be combined with another to 2504 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} -- 2505 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}. 2506 * </p> 2507 * 2508 * @return The associated character or combining accent, or 0 if none. 2509 */ getUnicodeChar()2510 public int getUnicodeChar() { 2511 return getUnicodeChar(mMetaState); 2512 } 2513 2514 /** 2515 * Gets the Unicode character generated by the specified key and meta 2516 * key state combination. 2517 * <p> 2518 * Returns the Unicode character that the specified key would produce 2519 * when the specified meta bits (see {@link MetaKeyKeyListener}) 2520 * were active. 2521 * </p><p> 2522 * Returns 0 if the key is not one that is used to type Unicode 2523 * characters. 2524 * </p><p> 2525 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the 2526 * key is a "dead key" that should be combined with another to 2527 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} -- 2528 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}. 2529 * </p> 2530 * 2531 * @param metaState The meta key modifier state. 2532 * @return The associated character or combining accent, or 0 if none. 2533 */ getUnicodeChar(int metaState)2534 public int getUnicodeChar(int metaState) { 2535 return getKeyCharacterMap().get(mKeyCode, metaState); 2536 } 2537 2538 /** 2539 * Get the character conversion data for a given key code. 2540 * 2541 * @param results A {@link KeyCharacterMap.KeyData} instance that will be 2542 * filled with the results. 2543 * @return True if the key was mapped. If the key was not mapped, results is not modified. 2544 * 2545 * @deprecated instead use {@link #getDisplayLabel()}, 2546 * {@link #getNumber()} or {@link #getUnicodeChar(int)}. 2547 */ 2548 @Deprecated getKeyData(KeyData results)2549 public boolean getKeyData(KeyData results) { 2550 return getKeyCharacterMap().getKeyData(mKeyCode, results); 2551 } 2552 2553 /** 2554 * Gets the first character in the character array that can be generated 2555 * by the specified key code. 2556 * <p> 2557 * This is a convenience function that returns the same value as 2558 * {@link #getMatch(char[],int) getMatch(chars, 0)}. 2559 * </p> 2560 * 2561 * @param chars The array of matching characters to consider. 2562 * @return The matching associated character, or 0 if none. 2563 */ getMatch(char[] chars)2564 public char getMatch(char[] chars) { 2565 return getMatch(chars, 0); 2566 } 2567 2568 /** 2569 * Gets the first character in the character array that can be generated 2570 * by the specified key code. If there are multiple choices, prefers 2571 * the one that would be generated with the specified meta key modifier state. 2572 * 2573 * @param chars The array of matching characters to consider. 2574 * @param metaState The preferred meta key modifier state. 2575 * @return The matching associated character, or 0 if none. 2576 */ getMatch(char[] chars, int metaState)2577 public char getMatch(char[] chars, int metaState) { 2578 return getKeyCharacterMap().getMatch(mKeyCode, chars, metaState); 2579 } 2580 2581 /** 2582 * Gets the number or symbol associated with the key. 2583 * <p> 2584 * The character value is returned, not the numeric value. 2585 * If the key is not a number, but is a symbol, the symbol is retuned. 2586 * </p><p> 2587 * This method is intended to to support dial pads and other numeric or 2588 * symbolic entry on keyboards where certain keys serve dual function 2589 * as alphabetic and symbolic keys. This method returns the number 2590 * or symbol associated with the key independent of whether the user 2591 * has pressed the required modifier. 2592 * </p><p> 2593 * For example, on one particular keyboard the keys on the top QWERTY row generate 2594 * numbers when ALT is pressed such that ALT-Q maps to '1'. So for that keyboard 2595 * when {@link #getNumber} is called with {@link KeyEvent#KEYCODE_Q} it returns '1' 2596 * so that the user can type numbers without pressing ALT when it makes sense. 2597 * </p> 2598 * 2599 * @return The associated numeric or symbolic character, or 0 if none. 2600 */ getNumber()2601 public char getNumber() { 2602 return getKeyCharacterMap().getNumber(mKeyCode); 2603 } 2604 2605 /** 2606 * Returns true if this key produces a glyph. 2607 * 2608 * @return True if the key is a printing key. 2609 */ isPrintingKey()2610 public boolean isPrintingKey() { 2611 return getKeyCharacterMap().isPrintingKey(mKeyCode); 2612 } 2613 2614 /** 2615 * @deprecated Use {@link #dispatch(Callback, DispatcherState, Object)} instead. 2616 */ 2617 @Deprecated dispatch(Callback receiver)2618 public final boolean dispatch(Callback receiver) { 2619 return dispatch(receiver, null, null); 2620 } 2621 2622 /** 2623 * Deliver this key event to a {@link Callback} interface. If this is 2624 * an ACTION_MULTIPLE event and it is not handled, then an attempt will 2625 * be made to deliver a single normal event. 2626 * 2627 * @param receiver The Callback that will be given the event. 2628 * @param state State information retained across events. 2629 * @param target The target of the dispatch, for use in tracking. 2630 * 2631 * @return The return value from the Callback method that was called. 2632 */ dispatch(Callback receiver, DispatcherState state, Object target)2633 public final boolean dispatch(Callback receiver, DispatcherState state, 2634 Object target) { 2635 switch (mAction) { 2636 case ACTION_DOWN: { 2637 mFlags &= ~FLAG_START_TRACKING; 2638 if (DEBUG) Log.v(TAG, "Key down to " + target + " in " + state 2639 + ": " + this); 2640 boolean res = receiver.onKeyDown(mKeyCode, this); 2641 if (state != null) { 2642 if (res && mRepeatCount == 0 && (mFlags&FLAG_START_TRACKING) != 0) { 2643 if (DEBUG) Log.v(TAG, " Start tracking!"); 2644 state.startTracking(this, target); 2645 } else if (isLongPress() && state.isTracking(this)) { 2646 try { 2647 if (receiver.onKeyLongPress(mKeyCode, this)) { 2648 if (DEBUG) Log.v(TAG, " Clear from long press!"); 2649 state.performedLongPress(this); 2650 res = true; 2651 } 2652 } catch (AbstractMethodError e) { 2653 } 2654 } 2655 } 2656 return res; 2657 } 2658 case ACTION_UP: 2659 if (DEBUG) Log.v(TAG, "Key up to " + target + " in " + state 2660 + ": " + this); 2661 if (state != null) { 2662 state.handleUpEvent(this); 2663 } 2664 return receiver.onKeyUp(mKeyCode, this); 2665 case ACTION_MULTIPLE: 2666 final int count = mRepeatCount; 2667 final int code = mKeyCode; 2668 if (receiver.onKeyMultiple(code, count, this)) { 2669 return true; 2670 } 2671 if (code != KeyEvent.KEYCODE_UNKNOWN) { 2672 mAction = ACTION_DOWN; 2673 mRepeatCount = 0; 2674 boolean handled = receiver.onKeyDown(code, this); 2675 if (handled) { 2676 mAction = ACTION_UP; 2677 receiver.onKeyUp(code, this); 2678 } 2679 mAction = ACTION_MULTIPLE; 2680 mRepeatCount = count; 2681 return handled; 2682 } 2683 return false; 2684 } 2685 return false; 2686 } 2687 2688 /** 2689 * Use with {@link KeyEvent#dispatch(Callback, DispatcherState, Object)} 2690 * for more advanced key dispatching, such as long presses. 2691 */ 2692 public static class DispatcherState { 2693 int mDownKeyCode; 2694 Object mDownTarget; 2695 SparseIntArray mActiveLongPresses = new SparseIntArray(); 2696 2697 /** 2698 * Reset back to initial state. 2699 */ reset()2700 public void reset() { 2701 if (DEBUG) Log.v(TAG, "Reset: " + this); 2702 mDownKeyCode = 0; 2703 mDownTarget = null; 2704 mActiveLongPresses.clear(); 2705 } 2706 2707 /** 2708 * Stop any tracking associated with this target. 2709 */ reset(Object target)2710 public void reset(Object target) { 2711 if (mDownTarget == target) { 2712 if (DEBUG) Log.v(TAG, "Reset in " + target + ": " + this); 2713 mDownKeyCode = 0; 2714 mDownTarget = null; 2715 } 2716 } 2717 2718 /** 2719 * Start tracking the key code associated with the given event. This 2720 * can only be called on a key down. It will allow you to see any 2721 * long press associated with the key, and will result in 2722 * {@link KeyEvent#isTracking} return true on the long press and up 2723 * events. 2724 * 2725 * <p>This is only needed if you are directly dispatching events, rather 2726 * than handling them in {@link Callback#onKeyDown}. 2727 */ startTracking(KeyEvent event, Object target)2728 public void startTracking(KeyEvent event, Object target) { 2729 if (event.getAction() != ACTION_DOWN) { 2730 throw new IllegalArgumentException( 2731 "Can only start tracking on a down event"); 2732 } 2733 if (DEBUG) Log.v(TAG, "Start trackingt in " + target + ": " + this); 2734 mDownKeyCode = event.getKeyCode(); 2735 mDownTarget = target; 2736 } 2737 2738 /** 2739 * Return true if the key event is for a key code that is currently 2740 * being tracked by the dispatcher. 2741 */ isTracking(KeyEvent event)2742 public boolean isTracking(KeyEvent event) { 2743 return mDownKeyCode == event.getKeyCode(); 2744 } 2745 2746 /** 2747 * Keep track of the given event's key code as having performed an 2748 * action with a long press, so no action should occur on the up. 2749 * <p>This is only needed if you are directly dispatching events, rather 2750 * than handling them in {@link Callback#onKeyLongPress}. 2751 */ performedLongPress(KeyEvent event)2752 public void performedLongPress(KeyEvent event) { 2753 mActiveLongPresses.put(event.getKeyCode(), 1); 2754 } 2755 2756 /** 2757 * Handle key up event to stop tracking. This resets the dispatcher state, 2758 * and updates the key event state based on it. 2759 * <p>This is only needed if you are directly dispatching events, rather 2760 * than handling them in {@link Callback#onKeyUp}. 2761 */ handleUpEvent(KeyEvent event)2762 public void handleUpEvent(KeyEvent event) { 2763 final int keyCode = event.getKeyCode(); 2764 if (DEBUG) Log.v(TAG, "Handle key up " + event + ": " + this); 2765 int index = mActiveLongPresses.indexOfKey(keyCode); 2766 if (index >= 0) { 2767 if (DEBUG) Log.v(TAG, " Index: " + index); 2768 event.mFlags |= FLAG_CANCELED | FLAG_CANCELED_LONG_PRESS; 2769 mActiveLongPresses.removeAt(index); 2770 } 2771 if (mDownKeyCode == keyCode) { 2772 if (DEBUG) Log.v(TAG, " Tracking!"); 2773 event.mFlags |= FLAG_TRACKING; 2774 mDownKeyCode = 0; 2775 mDownTarget = null; 2776 } 2777 } 2778 } 2779 2780 @Override toString()2781 public String toString() { 2782 StringBuilder msg = new StringBuilder(); 2783 msg.append("KeyEvent { action=").append(actionToString(mAction)); 2784 msg.append(", keyCode=").append(keyCodeToString(mKeyCode)); 2785 msg.append(", scanCode=").append(mScanCode); 2786 if (mCharacters != null) { 2787 msg.append(", characters=\"").append(mCharacters).append("\""); 2788 } 2789 msg.append(", metaState=").append(metaStateToString(mMetaState)); 2790 msg.append(", flags=0x").append(Integer.toHexString(mFlags)); 2791 msg.append(", repeatCount=").append(mRepeatCount); 2792 msg.append(", eventTime=").append(mEventTime); 2793 msg.append(", downTime=").append(mDownTime); 2794 msg.append(", deviceId=").append(mDeviceId); 2795 msg.append(", source=0x").append(Integer.toHexString(mSource)); 2796 msg.append(" }"); 2797 return msg.toString(); 2798 } 2799 2800 /** 2801 * Returns a string that represents the symbolic name of the specified action 2802 * such as "ACTION_DOWN", or an equivalent numeric constant such as "35" if unknown. 2803 * 2804 * @param action The action. 2805 * @return The symbolic name of the specified action. 2806 * @hide 2807 */ actionToString(int action)2808 public static String actionToString(int action) { 2809 switch (action) { 2810 case ACTION_DOWN: 2811 return "ACTION_DOWN"; 2812 case ACTION_UP: 2813 return "ACTION_UP"; 2814 case ACTION_MULTIPLE: 2815 return "ACTION_MULTIPLE"; 2816 default: 2817 return Integer.toString(action); 2818 } 2819 } 2820 2821 /** 2822 * Returns a string that represents the symbolic name of the specified keycode 2823 * such as "KEYCODE_A", "KEYCODE_DPAD_UP", or an equivalent numeric constant 2824 * such as "1001" if unknown. 2825 * 2826 * @param keyCode The key code. 2827 * @return The symbolic name of the specified keycode. 2828 * 2829 * @see KeyCharacterMap#getDisplayLabel 2830 */ keyCodeToString(int keyCode)2831 public static String keyCodeToString(int keyCode) { 2832 String symbolicName = KEYCODE_SYMBOLIC_NAMES.get(keyCode); 2833 return symbolicName != null ? symbolicName : Integer.toString(keyCode); 2834 } 2835 2836 /** 2837 * Gets a keycode by its symbolic name such as "KEYCODE_A" or an equivalent 2838 * numeric constant such as "1001". 2839 * 2840 * @param symbolicName The symbolic name of the keycode. 2841 * @return The keycode or {@link #KEYCODE_UNKNOWN} if not found. 2842 * @see #keycodeToString(int) 2843 */ keyCodeFromString(String symbolicName)2844 public static int keyCodeFromString(String symbolicName) { 2845 if (symbolicName == null) { 2846 throw new IllegalArgumentException("symbolicName must not be null"); 2847 } 2848 2849 final int count = KEYCODE_SYMBOLIC_NAMES.size(); 2850 for (int i = 0; i < count; i++) { 2851 if (symbolicName.equals(KEYCODE_SYMBOLIC_NAMES.valueAt(i))) { 2852 return i; 2853 } 2854 } 2855 2856 try { 2857 return Integer.parseInt(symbolicName, 10); 2858 } catch (NumberFormatException ex) { 2859 return KEYCODE_UNKNOWN; 2860 } 2861 } 2862 2863 /** 2864 * Returns a string that represents the symbolic name of the specified combined meta 2865 * key modifier state flags such as "0", "META_SHIFT_ON", 2866 * "META_ALT_ON|META_SHIFT_ON" or an equivalent numeric constant such as "0x10000000" 2867 * if unknown. 2868 * 2869 * @param metaState The meta state. 2870 * @return The symbolic name of the specified combined meta state flags. 2871 * @hide 2872 */ metaStateToString(int metaState)2873 public static String metaStateToString(int metaState) { 2874 if (metaState == 0) { 2875 return "0"; 2876 } 2877 StringBuilder result = null; 2878 int i = 0; 2879 while (metaState != 0) { 2880 final boolean isSet = (metaState & 1) != 0; 2881 metaState >>>= 1; // unsigned shift! 2882 if (isSet) { 2883 final String name = META_SYMBOLIC_NAMES[i]; 2884 if (result == null) { 2885 if (metaState == 0) { 2886 return name; 2887 } 2888 result = new StringBuilder(name); 2889 } else { 2890 result.append('|'); 2891 result.append(name); 2892 } 2893 } 2894 i += 1; 2895 } 2896 return result.toString(); 2897 } 2898 2899 public static final Parcelable.Creator<KeyEvent> CREATOR 2900 = new Parcelable.Creator<KeyEvent>() { 2901 public KeyEvent createFromParcel(Parcel in) { 2902 in.readInt(); // skip token, we already know this is a KeyEvent 2903 return KeyEvent.createFromParcelBody(in); 2904 } 2905 2906 public KeyEvent[] newArray(int size) { 2907 return new KeyEvent[size]; 2908 } 2909 }; 2910 2911 /** @hide */ createFromParcelBody(Parcel in)2912 public static KeyEvent createFromParcelBody(Parcel in) { 2913 return new KeyEvent(in); 2914 } 2915 KeyEvent(Parcel in)2916 private KeyEvent(Parcel in) { 2917 mDeviceId = in.readInt(); 2918 mSource = in.readInt(); 2919 mAction = in.readInt(); 2920 mKeyCode = in.readInt(); 2921 mRepeatCount = in.readInt(); 2922 mMetaState = in.readInt(); 2923 mScanCode = in.readInt(); 2924 mFlags = in.readInt(); 2925 mDownTime = in.readLong(); 2926 mEventTime = in.readLong(); 2927 } 2928 writeToParcel(Parcel out, int flags)2929 public void writeToParcel(Parcel out, int flags) { 2930 out.writeInt(PARCEL_TOKEN_KEY_EVENT); 2931 2932 out.writeInt(mDeviceId); 2933 out.writeInt(mSource); 2934 out.writeInt(mAction); 2935 out.writeInt(mKeyCode); 2936 out.writeInt(mRepeatCount); 2937 out.writeInt(mMetaState); 2938 out.writeInt(mScanCode); 2939 out.writeInt(mFlags); 2940 out.writeLong(mDownTime); 2941 out.writeLong(mEventTime); 2942 } 2943 native_isSystemKey(int keyCode)2944 private native boolean native_isSystemKey(int keyCode); native_hasDefaultAction(int keyCode)2945 private native boolean native_hasDefaultAction(int keyCode); 2946 } 2947