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 upper 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 upper 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 upper 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 lower 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 lower 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 lower 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 627 private static final int LAST_KEYCODE = KEYCODE_ASSIST; 628 629 // NOTE: If you add a new keycode here you must also add it to: 630 // isSystem() 631 // native/include/android/keycodes.h 632 // frameworks/base/include/ui/KeycodeLabels.h 633 // external/webkit/WebKit/android/plugins/ANPKeyCodes.h 634 // frameworks/base/core/res/res/values/attrs.xml 635 // emulator? 636 // LAST_KEYCODE 637 // KEYCODE_SYMBOLIC_NAMES 638 // 639 // Also Android currently does not reserve code ranges for vendor- 640 // specific key codes. If you have new key codes to have, you 641 // MUST contribute a patch to the open source project to define 642 // those new codes. This is intended to maintain a consistent 643 // set of key code definitions across all Android devices. 644 645 // Symbolic names of all key codes. 646 private static final SparseArray<String> KEYCODE_SYMBOLIC_NAMES = new SparseArray<String>(); populateKeycodeSymbolicNames()647 private static void populateKeycodeSymbolicNames() { 648 SparseArray<String> names = KEYCODE_SYMBOLIC_NAMES; 649 names.append(KEYCODE_UNKNOWN, "KEYCODE_UNKNOWN"); 650 names.append(KEYCODE_SOFT_LEFT, "KEYCODE_SOFT_LEFT"); 651 names.append(KEYCODE_SOFT_RIGHT, "KEYCODE_SOFT_RIGHT"); 652 names.append(KEYCODE_HOME, "KEYCODE_HOME"); 653 names.append(KEYCODE_BACK, "KEYCODE_BACK"); 654 names.append(KEYCODE_CALL, "KEYCODE_CALL"); 655 names.append(KEYCODE_ENDCALL, "KEYCODE_ENDCALL"); 656 names.append(KEYCODE_0, "KEYCODE_0"); 657 names.append(KEYCODE_1, "KEYCODE_1"); 658 names.append(KEYCODE_2, "KEYCODE_2"); 659 names.append(KEYCODE_3, "KEYCODE_3"); 660 names.append(KEYCODE_4, "KEYCODE_4"); 661 names.append(KEYCODE_5, "KEYCODE_5"); 662 names.append(KEYCODE_6, "KEYCODE_6"); 663 names.append(KEYCODE_7, "KEYCODE_7"); 664 names.append(KEYCODE_8, "KEYCODE_8"); 665 names.append(KEYCODE_9, "KEYCODE_9"); 666 names.append(KEYCODE_STAR, "KEYCODE_STAR"); 667 names.append(KEYCODE_POUND, "KEYCODE_POUND"); 668 names.append(KEYCODE_DPAD_UP, "KEYCODE_DPAD_UP"); 669 names.append(KEYCODE_DPAD_DOWN, "KEYCODE_DPAD_DOWN"); 670 names.append(KEYCODE_DPAD_LEFT, "KEYCODE_DPAD_LEFT"); 671 names.append(KEYCODE_DPAD_RIGHT, "KEYCODE_DPAD_RIGHT"); 672 names.append(KEYCODE_DPAD_CENTER, "KEYCODE_DPAD_CENTER"); 673 names.append(KEYCODE_VOLUME_UP, "KEYCODE_VOLUME_UP"); 674 names.append(KEYCODE_VOLUME_DOWN, "KEYCODE_VOLUME_DOWN"); 675 names.append(KEYCODE_POWER, "KEYCODE_POWER"); 676 names.append(KEYCODE_CAMERA, "KEYCODE_CAMERA"); 677 names.append(KEYCODE_CLEAR, "KEYCODE_CLEAR"); 678 names.append(KEYCODE_A, "KEYCODE_A"); 679 names.append(KEYCODE_B, "KEYCODE_B"); 680 names.append(KEYCODE_C, "KEYCODE_C"); 681 names.append(KEYCODE_D, "KEYCODE_D"); 682 names.append(KEYCODE_E, "KEYCODE_E"); 683 names.append(KEYCODE_F, "KEYCODE_F"); 684 names.append(KEYCODE_G, "KEYCODE_G"); 685 names.append(KEYCODE_H, "KEYCODE_H"); 686 names.append(KEYCODE_I, "KEYCODE_I"); 687 names.append(KEYCODE_J, "KEYCODE_J"); 688 names.append(KEYCODE_K, "KEYCODE_K"); 689 names.append(KEYCODE_L, "KEYCODE_L"); 690 names.append(KEYCODE_M, "KEYCODE_M"); 691 names.append(KEYCODE_N, "KEYCODE_N"); 692 names.append(KEYCODE_O, "KEYCODE_O"); 693 names.append(KEYCODE_P, "KEYCODE_P"); 694 names.append(KEYCODE_Q, "KEYCODE_Q"); 695 names.append(KEYCODE_R, "KEYCODE_R"); 696 names.append(KEYCODE_S, "KEYCODE_S"); 697 names.append(KEYCODE_T, "KEYCODE_T"); 698 names.append(KEYCODE_U, "KEYCODE_U"); 699 names.append(KEYCODE_V, "KEYCODE_V"); 700 names.append(KEYCODE_W, "KEYCODE_W"); 701 names.append(KEYCODE_X, "KEYCODE_X"); 702 names.append(KEYCODE_Y, "KEYCODE_Y"); 703 names.append(KEYCODE_Z, "KEYCODE_Z"); 704 names.append(KEYCODE_COMMA, "KEYCODE_COMMA"); 705 names.append(KEYCODE_PERIOD, "KEYCODE_PERIOD"); 706 names.append(KEYCODE_ALT_LEFT, "KEYCODE_ALT_LEFT"); 707 names.append(KEYCODE_ALT_RIGHT, "KEYCODE_ALT_RIGHT"); 708 names.append(KEYCODE_SHIFT_LEFT, "KEYCODE_SHIFT_LEFT"); 709 names.append(KEYCODE_SHIFT_RIGHT, "KEYCODE_SHIFT_RIGHT"); 710 names.append(KEYCODE_TAB, "KEYCODE_TAB"); 711 names.append(KEYCODE_SPACE, "KEYCODE_SPACE"); 712 names.append(KEYCODE_SYM, "KEYCODE_SYM"); 713 names.append(KEYCODE_EXPLORER, "KEYCODE_EXPLORER"); 714 names.append(KEYCODE_ENVELOPE, "KEYCODE_ENVELOPE"); 715 names.append(KEYCODE_ENTER, "KEYCODE_ENTER"); 716 names.append(KEYCODE_DEL, "KEYCODE_DEL"); 717 names.append(KEYCODE_GRAVE, "KEYCODE_GRAVE"); 718 names.append(KEYCODE_MINUS, "KEYCODE_MINUS"); 719 names.append(KEYCODE_EQUALS, "KEYCODE_EQUALS"); 720 names.append(KEYCODE_LEFT_BRACKET, "KEYCODE_LEFT_BRACKET"); 721 names.append(KEYCODE_RIGHT_BRACKET, "KEYCODE_RIGHT_BRACKET"); 722 names.append(KEYCODE_BACKSLASH, "KEYCODE_BACKSLASH"); 723 names.append(KEYCODE_SEMICOLON, "KEYCODE_SEMICOLON"); 724 names.append(KEYCODE_APOSTROPHE, "KEYCODE_APOSTROPHE"); 725 names.append(KEYCODE_SLASH, "KEYCODE_SLASH"); 726 names.append(KEYCODE_AT, "KEYCODE_AT"); 727 names.append(KEYCODE_NUM, "KEYCODE_NUM"); 728 names.append(KEYCODE_HEADSETHOOK, "KEYCODE_HEADSETHOOK"); 729 names.append(KEYCODE_FOCUS, "KEYCODE_FOCUS"); 730 names.append(KEYCODE_PLUS, "KEYCODE_PLUS"); 731 names.append(KEYCODE_MENU, "KEYCODE_MENU"); 732 names.append(KEYCODE_NOTIFICATION, "KEYCODE_NOTIFICATION"); 733 names.append(KEYCODE_SEARCH, "KEYCODE_SEARCH"); 734 names.append(KEYCODE_MEDIA_PLAY_PAUSE, "KEYCODE_MEDIA_PLAY_PAUSE"); 735 names.append(KEYCODE_MEDIA_STOP, "KEYCODE_MEDIA_STOP"); 736 names.append(KEYCODE_MEDIA_NEXT, "KEYCODE_MEDIA_NEXT"); 737 names.append(KEYCODE_MEDIA_PREVIOUS, "KEYCODE_MEDIA_PREVIOUS"); 738 names.append(KEYCODE_MEDIA_REWIND, "KEYCODE_MEDIA_REWIND"); 739 names.append(KEYCODE_MEDIA_FAST_FORWARD, "KEYCODE_MEDIA_FAST_FORWARD"); 740 names.append(KEYCODE_MUTE, "KEYCODE_MUTE"); 741 names.append(KEYCODE_PAGE_UP, "KEYCODE_PAGE_UP"); 742 names.append(KEYCODE_PAGE_DOWN, "KEYCODE_PAGE_DOWN"); 743 names.append(KEYCODE_PICTSYMBOLS, "KEYCODE_PICTSYMBOLS"); 744 names.append(KEYCODE_SWITCH_CHARSET, "KEYCODE_SWITCH_CHARSET"); 745 names.append(KEYCODE_BUTTON_A, "KEYCODE_BUTTON_A"); 746 names.append(KEYCODE_BUTTON_B, "KEYCODE_BUTTON_B"); 747 names.append(KEYCODE_BUTTON_C, "KEYCODE_BUTTON_C"); 748 names.append(KEYCODE_BUTTON_X, "KEYCODE_BUTTON_X"); 749 names.append(KEYCODE_BUTTON_Y, "KEYCODE_BUTTON_Y"); 750 names.append(KEYCODE_BUTTON_Z, "KEYCODE_BUTTON_Z"); 751 names.append(KEYCODE_BUTTON_L1, "KEYCODE_BUTTON_L1"); 752 names.append(KEYCODE_BUTTON_R1, "KEYCODE_BUTTON_R1"); 753 names.append(KEYCODE_BUTTON_L2, "KEYCODE_BUTTON_L2"); 754 names.append(KEYCODE_BUTTON_R2, "KEYCODE_BUTTON_R2"); 755 names.append(KEYCODE_BUTTON_THUMBL, "KEYCODE_BUTTON_THUMBL"); 756 names.append(KEYCODE_BUTTON_THUMBR, "KEYCODE_BUTTON_THUMBR"); 757 names.append(KEYCODE_BUTTON_START, "KEYCODE_BUTTON_START"); 758 names.append(KEYCODE_BUTTON_SELECT, "KEYCODE_BUTTON_SELECT"); 759 names.append(KEYCODE_BUTTON_MODE, "KEYCODE_BUTTON_MODE"); 760 names.append(KEYCODE_ESCAPE, "KEYCODE_ESCAPE"); 761 names.append(KEYCODE_FORWARD_DEL, "KEYCODE_FORWARD_DEL"); 762 names.append(KEYCODE_CTRL_LEFT, "KEYCODE_CTRL_LEFT"); 763 names.append(KEYCODE_CTRL_RIGHT, "KEYCODE_CTRL_RIGHT"); 764 names.append(KEYCODE_CAPS_LOCK, "KEYCODE_CAPS_LOCK"); 765 names.append(KEYCODE_SCROLL_LOCK, "KEYCODE_SCROLL_LOCK"); 766 names.append(KEYCODE_META_LEFT, "KEYCODE_META_LEFT"); 767 names.append(KEYCODE_META_RIGHT, "KEYCODE_META_RIGHT"); 768 names.append(KEYCODE_FUNCTION, "KEYCODE_FUNCTION"); 769 names.append(KEYCODE_SYSRQ, "KEYCODE_SYSRQ"); 770 names.append(KEYCODE_BREAK, "KEYCODE_BREAK"); 771 names.append(KEYCODE_MOVE_HOME, "KEYCODE_MOVE_HOME"); 772 names.append(KEYCODE_MOVE_END, "KEYCODE_MOVE_END"); 773 names.append(KEYCODE_INSERT, "KEYCODE_INSERT"); 774 names.append(KEYCODE_FORWARD, "KEYCODE_FORWARD"); 775 names.append(KEYCODE_MEDIA_PLAY, "KEYCODE_MEDIA_PLAY"); 776 names.append(KEYCODE_MEDIA_PAUSE, "KEYCODE_MEDIA_PAUSE"); 777 names.append(KEYCODE_MEDIA_CLOSE, "KEYCODE_MEDIA_CLOSE"); 778 names.append(KEYCODE_MEDIA_EJECT, "KEYCODE_MEDIA_EJECT"); 779 names.append(KEYCODE_MEDIA_RECORD, "KEYCODE_MEDIA_RECORD"); 780 names.append(KEYCODE_F1, "KEYCODE_F1"); 781 names.append(KEYCODE_F2, "KEYCODE_F2"); 782 names.append(KEYCODE_F3, "KEYCODE_F3"); 783 names.append(KEYCODE_F4, "KEYCODE_F4"); 784 names.append(KEYCODE_F5, "KEYCODE_F5"); 785 names.append(KEYCODE_F6, "KEYCODE_F6"); 786 names.append(KEYCODE_F7, "KEYCODE_F7"); 787 names.append(KEYCODE_F8, "KEYCODE_F8"); 788 names.append(KEYCODE_F9, "KEYCODE_F9"); 789 names.append(KEYCODE_F10, "KEYCODE_F10"); 790 names.append(KEYCODE_F11, "KEYCODE_F11"); 791 names.append(KEYCODE_F12, "KEYCODE_F12"); 792 names.append(KEYCODE_NUM_LOCK, "KEYCODE_NUM_LOCK"); 793 names.append(KEYCODE_NUMPAD_0, "KEYCODE_NUMPAD_0"); 794 names.append(KEYCODE_NUMPAD_1, "KEYCODE_NUMPAD_1"); 795 names.append(KEYCODE_NUMPAD_2, "KEYCODE_NUMPAD_2"); 796 names.append(KEYCODE_NUMPAD_3, "KEYCODE_NUMPAD_3"); 797 names.append(KEYCODE_NUMPAD_4, "KEYCODE_NUMPAD_4"); 798 names.append(KEYCODE_NUMPAD_5, "KEYCODE_NUMPAD_5"); 799 names.append(KEYCODE_NUMPAD_6, "KEYCODE_NUMPAD_6"); 800 names.append(KEYCODE_NUMPAD_7, "KEYCODE_NUMPAD_7"); 801 names.append(KEYCODE_NUMPAD_8, "KEYCODE_NUMPAD_8"); 802 names.append(KEYCODE_NUMPAD_9, "KEYCODE_NUMPAD_9"); 803 names.append(KEYCODE_NUMPAD_DIVIDE, "KEYCODE_NUMPAD_DIVIDE"); 804 names.append(KEYCODE_NUMPAD_MULTIPLY, "KEYCODE_NUMPAD_MULTIPLY"); 805 names.append(KEYCODE_NUMPAD_SUBTRACT, "KEYCODE_NUMPAD_SUBTRACT"); 806 names.append(KEYCODE_NUMPAD_ADD, "KEYCODE_NUMPAD_ADD"); 807 names.append(KEYCODE_NUMPAD_DOT, "KEYCODE_NUMPAD_DOT"); 808 names.append(KEYCODE_NUMPAD_COMMA, "KEYCODE_NUMPAD_COMMA"); 809 names.append(KEYCODE_NUMPAD_ENTER, "KEYCODE_NUMPAD_ENTER"); 810 names.append(KEYCODE_NUMPAD_EQUALS, "KEYCODE_NUMPAD_EQUALS"); 811 names.append(KEYCODE_NUMPAD_LEFT_PAREN, "KEYCODE_NUMPAD_LEFT_PAREN"); 812 names.append(KEYCODE_NUMPAD_RIGHT_PAREN, "KEYCODE_NUMPAD_RIGHT_PAREN"); 813 names.append(KEYCODE_VOLUME_MUTE, "KEYCODE_VOLUME_MUTE"); 814 names.append(KEYCODE_INFO, "KEYCODE_INFO"); 815 names.append(KEYCODE_CHANNEL_UP, "KEYCODE_CHANNEL_UP"); 816 names.append(KEYCODE_CHANNEL_DOWN, "KEYCODE_CHANNEL_DOWN"); 817 names.append(KEYCODE_ZOOM_IN, "KEYCODE_ZOOM_IN"); 818 names.append(KEYCODE_ZOOM_OUT, "KEYCODE_ZOOM_OUT"); 819 names.append(KEYCODE_TV, "KEYCODE_TV"); 820 names.append(KEYCODE_WINDOW, "KEYCODE_WINDOW"); 821 names.append(KEYCODE_GUIDE, "KEYCODE_GUIDE"); 822 names.append(KEYCODE_DVR, "KEYCODE_DVR"); 823 names.append(KEYCODE_BOOKMARK, "KEYCODE_BOOKMARK"); 824 names.append(KEYCODE_CAPTIONS, "KEYCODE_CAPTIONS"); 825 names.append(KEYCODE_SETTINGS, "KEYCODE_SETTINGS"); 826 names.append(KEYCODE_TV_POWER, "KEYCODE_TV_POWER"); 827 names.append(KEYCODE_TV_INPUT, "KEYCODE_TV_INPUT"); 828 names.append(KEYCODE_STB_INPUT, "KEYCODE_STB_INPUT"); 829 names.append(KEYCODE_STB_POWER, "KEYCODE_STB_POWER"); 830 names.append(KEYCODE_AVR_POWER, "KEYCODE_AVR_POWER"); 831 names.append(KEYCODE_AVR_INPUT, "KEYCODE_AVR_INPUT"); 832 names.append(KEYCODE_PROG_RED, "KEYCODE_PROG_RED"); 833 names.append(KEYCODE_PROG_GREEN, "KEYCODE_PROG_GREEN"); 834 names.append(KEYCODE_PROG_YELLOW, "KEYCODE_PROG_YELLOW"); 835 names.append(KEYCODE_PROG_BLUE, "KEYCODE_PROG_BLUE"); 836 names.append(KEYCODE_APP_SWITCH, "KEYCODE_APP_SWITCH"); 837 names.append(KEYCODE_BUTTON_1, "KEYCODE_BUTTON_1"); 838 names.append(KEYCODE_BUTTON_2, "KEYCODE_BUTTON_2"); 839 names.append(KEYCODE_BUTTON_3, "KEYCODE_BUTTON_3"); 840 names.append(KEYCODE_BUTTON_4, "KEYCODE_BUTTON_4"); 841 names.append(KEYCODE_BUTTON_5, "KEYCODE_BUTTON_5"); 842 names.append(KEYCODE_BUTTON_6, "KEYCODE_BUTTON_6"); 843 names.append(KEYCODE_BUTTON_7, "KEYCODE_BUTTON_7"); 844 names.append(KEYCODE_BUTTON_8, "KEYCODE_BUTTON_8"); 845 names.append(KEYCODE_BUTTON_9, "KEYCODE_BUTTON_9"); 846 names.append(KEYCODE_BUTTON_10, "KEYCODE_BUTTON_10"); 847 names.append(KEYCODE_BUTTON_11, "KEYCODE_BUTTON_11"); 848 names.append(KEYCODE_BUTTON_12, "KEYCODE_BUTTON_12"); 849 names.append(KEYCODE_BUTTON_13, "KEYCODE_BUTTON_13"); 850 names.append(KEYCODE_BUTTON_14, "KEYCODE_BUTTON_14"); 851 names.append(KEYCODE_BUTTON_15, "KEYCODE_BUTTON_15"); 852 names.append(KEYCODE_BUTTON_16, "KEYCODE_BUTTON_16"); 853 names.append(KEYCODE_LANGUAGE_SWITCH, "KEYCODE_LANGUAGE_SWITCH"); 854 names.append(KEYCODE_MANNER_MODE, "KEYCODE_MANNER_MODE"); 855 names.append(KEYCODE_3D_MODE, "KEYCODE_3D_MODE"); 856 names.append(KEYCODE_CONTACTS, "KEYCODE_CONTACTS"); 857 names.append(KEYCODE_CALENDAR, "KEYCODE_CALENDAR"); 858 names.append(KEYCODE_MUSIC, "KEYCODE_MUSIC"); 859 names.append(KEYCODE_CALCULATOR, "KEYCODE_CALCULATOR"); 860 names.append(KEYCODE_ZENKAKU_HANKAKU, "KEYCODE_ZENKAKU_HANKAKU"); 861 names.append(KEYCODE_EISU, "KEYCODE_EISU"); 862 names.append(KEYCODE_MUHENKAN, "KEYCODE_MUHENKAN"); 863 names.append(KEYCODE_HENKAN, "KEYCODE_HENKAN"); 864 names.append(KEYCODE_KATAKANA_HIRAGANA, "KEYCODE_KATAKANA_HIRAGANA"); 865 names.append(KEYCODE_YEN, "KEYCODE_YEN"); 866 names.append(KEYCODE_RO, "KEYCODE_RO"); 867 names.append(KEYCODE_KANA, "KEYCODE_KANA"); 868 names.append(KEYCODE_ASSIST, "KEYCODE_ASSIST"); 869 }; 870 871 // Symbolic names of all metakeys in bit order from least significant to most significant. 872 // Accordingly there are exactly 32 values in this table. 873 private static final String[] META_SYMBOLIC_NAMES = new String[] { 874 "META_SHIFT_ON", 875 "META_ALT_ON", 876 "META_SYM_ON", 877 "META_FUNCTION_ON", 878 "META_ALT_LEFT_ON", 879 "META_ALT_RIGHT_ON", 880 "META_SHIFT_LEFT_ON", 881 "META_SHIFT_RIGHT_ON", 882 "META_CAP_LOCKED", 883 "META_ALT_LOCKED", 884 "META_SYM_LOCKED", 885 "0x00000800", 886 "META_CTRL_ON", 887 "META_CTRL_LEFT_ON", 888 "META_CTRL_RIGHT_ON", 889 "0x00008000", 890 "META_META_ON", 891 "META_META_LEFT_ON", 892 "META_META_RIGHT_ON", 893 "0x00080000", 894 "META_CAPS_LOCK_ON", 895 "META_NUM_LOCK_ON", 896 "META_SCROLL_LOCK_ON", 897 "0x00800000", 898 "0x01000000", 899 "0x02000000", 900 "0x04000000", 901 "0x08000000", 902 "0x10000000", 903 "0x20000000", 904 "0x40000000", 905 "0x80000000", 906 }; 907 908 /** 909 * @deprecated There are now more than MAX_KEYCODE keycodes. 910 * Use {@link #getMaxKeyCode()} instead. 911 */ 912 @Deprecated 913 public static final int MAX_KEYCODE = 84; 914 915 /** 916 * {@link #getAction} value: the key has been pressed down. 917 */ 918 public static final int ACTION_DOWN = 0; 919 /** 920 * {@link #getAction} value: the key has been released. 921 */ 922 public static final int ACTION_UP = 1; 923 /** 924 * {@link #getAction} value: multiple duplicate key events have 925 * occurred in a row, or a complex string is being delivered. If the 926 * key code is not {#link {@link #KEYCODE_UNKNOWN} then the 927 * {#link {@link #getRepeatCount()} method returns the number of times 928 * the given key code should be executed. 929 * Otherwise, if the key code is {@link #KEYCODE_UNKNOWN}, then 930 * this is a sequence of characters as returned by {@link #getCharacters}. 931 */ 932 public static final int ACTION_MULTIPLE = 2; 933 934 /** 935 * SHIFT key locked in CAPS mode. 936 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API. 937 * @hide 938 */ 939 public static final int META_CAP_LOCKED = 0x100; 940 941 /** 942 * ALT key locked. 943 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API. 944 * @hide 945 */ 946 public static final int META_ALT_LOCKED = 0x200; 947 948 /** 949 * SYM key locked. 950 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API. 951 * @hide 952 */ 953 public static final int META_SYM_LOCKED = 0x400; 954 955 /** 956 * Text is in selection mode. 957 * Reserved for use by {@link MetaKeyKeyListener} for a private unpublished constant 958 * in its API that is currently being retained for legacy reasons. 959 * @hide 960 */ 961 public static final int META_SELECTING = 0x800; 962 963 /** 964 * <p>This mask is used to check whether one of the ALT meta keys is pressed.</p> 965 * 966 * @see #isAltPressed() 967 * @see #getMetaState() 968 * @see #KEYCODE_ALT_LEFT 969 * @see #KEYCODE_ALT_RIGHT 970 */ 971 public static final int META_ALT_ON = 0x02; 972 973 /** 974 * <p>This mask is used to check whether the left ALT meta key is pressed.</p> 975 * 976 * @see #isAltPressed() 977 * @see #getMetaState() 978 * @see #KEYCODE_ALT_LEFT 979 */ 980 public static final int META_ALT_LEFT_ON = 0x10; 981 982 /** 983 * <p>This mask is used to check whether the right the ALT meta key is pressed.</p> 984 * 985 * @see #isAltPressed() 986 * @see #getMetaState() 987 * @see #KEYCODE_ALT_RIGHT 988 */ 989 public static final int META_ALT_RIGHT_ON = 0x20; 990 991 /** 992 * <p>This mask is used to check whether one of the SHIFT meta keys is pressed.</p> 993 * 994 * @see #isShiftPressed() 995 * @see #getMetaState() 996 * @see #KEYCODE_SHIFT_LEFT 997 * @see #KEYCODE_SHIFT_RIGHT 998 */ 999 public static final int META_SHIFT_ON = 0x1; 1000 1001 /** 1002 * <p>This mask is used to check whether the left SHIFT meta key is pressed.</p> 1003 * 1004 * @see #isShiftPressed() 1005 * @see #getMetaState() 1006 * @see #KEYCODE_SHIFT_LEFT 1007 */ 1008 public static final int META_SHIFT_LEFT_ON = 0x40; 1009 1010 /** 1011 * <p>This mask is used to check whether the right SHIFT meta key is pressed.</p> 1012 * 1013 * @see #isShiftPressed() 1014 * @see #getMetaState() 1015 * @see #KEYCODE_SHIFT_RIGHT 1016 */ 1017 public static final int META_SHIFT_RIGHT_ON = 0x80; 1018 1019 /** 1020 * <p>This mask is used to check whether the SYM meta key is pressed.</p> 1021 * 1022 * @see #isSymPressed() 1023 * @see #getMetaState() 1024 */ 1025 public static final int META_SYM_ON = 0x4; 1026 1027 /** 1028 * <p>This mask is used to check whether the FUNCTION meta key is pressed.</p> 1029 * 1030 * @see #isFunctionPressed() 1031 * @see #getMetaState() 1032 */ 1033 public static final int META_FUNCTION_ON = 0x8; 1034 1035 /** 1036 * <p>This mask is used to check whether one of the CTRL meta keys is pressed.</p> 1037 * 1038 * @see #isCtrlPressed() 1039 * @see #getMetaState() 1040 * @see #KEYCODE_CTRL_LEFT 1041 * @see #KEYCODE_CTRL_RIGHT 1042 */ 1043 public static final int META_CTRL_ON = 0x1000; 1044 1045 /** 1046 * <p>This mask is used to check whether the left CTRL meta key is pressed.</p> 1047 * 1048 * @see #isCtrlPressed() 1049 * @see #getMetaState() 1050 * @see #KEYCODE_CTRL_LEFT 1051 */ 1052 public static final int META_CTRL_LEFT_ON = 0x2000; 1053 1054 /** 1055 * <p>This mask is used to check whether the right CTRL meta key is pressed.</p> 1056 * 1057 * @see #isCtrlPressed() 1058 * @see #getMetaState() 1059 * @see #KEYCODE_CTRL_RIGHT 1060 */ 1061 public static final int META_CTRL_RIGHT_ON = 0x4000; 1062 1063 /** 1064 * <p>This mask is used to check whether one of the META meta keys is pressed.</p> 1065 * 1066 * @see #isMetaPressed() 1067 * @see #getMetaState() 1068 * @see #KEYCODE_META_LEFT 1069 * @see #KEYCODE_META_RIGHT 1070 */ 1071 public static final int META_META_ON = 0x10000; 1072 1073 /** 1074 * <p>This mask is used to check whether the left META meta key is pressed.</p> 1075 * 1076 * @see #isMetaPressed() 1077 * @see #getMetaState() 1078 * @see #KEYCODE_META_LEFT 1079 */ 1080 public static final int META_META_LEFT_ON = 0x20000; 1081 1082 /** 1083 * <p>This mask is used to check whether the right META meta key is pressed.</p> 1084 * 1085 * @see #isMetaPressed() 1086 * @see #getMetaState() 1087 * @see #KEYCODE_META_RIGHT 1088 */ 1089 public static final int META_META_RIGHT_ON = 0x40000; 1090 1091 /** 1092 * <p>This mask is used to check whether the CAPS LOCK meta key is on.</p> 1093 * 1094 * @see #isCapsLockOn() 1095 * @see #getMetaState() 1096 * @see #KEYCODE_CAPS_LOCK 1097 */ 1098 public static final int META_CAPS_LOCK_ON = 0x100000; 1099 1100 /** 1101 * <p>This mask is used to check whether the NUM LOCK meta key is on.</p> 1102 * 1103 * @see #isNumLockOn() 1104 * @see #getMetaState() 1105 * @see #KEYCODE_NUM_LOCK 1106 */ 1107 public static final int META_NUM_LOCK_ON = 0x200000; 1108 1109 /** 1110 * <p>This mask is used to check whether the SCROLL LOCK meta key is on.</p> 1111 * 1112 * @see #isScrollLockOn() 1113 * @see #getMetaState() 1114 * @see #KEYCODE_SCROLL_LOCK 1115 */ 1116 public static final int META_SCROLL_LOCK_ON = 0x400000; 1117 1118 /** 1119 * This mask is a combination of {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON} 1120 * and {@link #META_SHIFT_RIGHT_ON}. 1121 */ 1122 public static final int META_SHIFT_MASK = META_SHIFT_ON 1123 | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON; 1124 1125 /** 1126 * This mask is a combination of {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON} 1127 * and {@link #META_ALT_RIGHT_ON}. 1128 */ 1129 public static final int META_ALT_MASK = META_ALT_ON 1130 | META_ALT_LEFT_ON | META_ALT_RIGHT_ON; 1131 1132 /** 1133 * This mask is a combination of {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON} 1134 * and {@link #META_CTRL_RIGHT_ON}. 1135 */ 1136 public static final int META_CTRL_MASK = META_CTRL_ON 1137 | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON; 1138 1139 /** 1140 * This mask is a combination of {@link #META_META_ON}, {@link #META_META_LEFT_ON} 1141 * and {@link #META_META_RIGHT_ON}. 1142 */ 1143 public static final int META_META_MASK = META_META_ON 1144 | META_META_LEFT_ON | META_META_RIGHT_ON; 1145 1146 /** 1147 * This mask is set if the device woke because of this key event. 1148 */ 1149 public static final int FLAG_WOKE_HERE = 0x1; 1150 1151 /** 1152 * This mask is set if the key event was generated by a software keyboard. 1153 */ 1154 public static final int FLAG_SOFT_KEYBOARD = 0x2; 1155 1156 /** 1157 * This mask is set if we don't want the key event to cause us to leave 1158 * touch mode. 1159 */ 1160 public static final int FLAG_KEEP_TOUCH_MODE = 0x4; 1161 1162 /** 1163 * This mask is set if an event was known to come from a trusted part 1164 * of the system. That is, the event is known to come from the user, 1165 * and could not have been spoofed by a third party component. 1166 */ 1167 public static final int FLAG_FROM_SYSTEM = 0x8; 1168 1169 /** 1170 * This mask is used for compatibility, to identify enter keys that are 1171 * coming from an IME whose enter key has been auto-labelled "next" or 1172 * "done". This allows TextView to dispatch these as normal enter keys 1173 * for old applications, but still do the appropriate action when 1174 * receiving them. 1175 */ 1176 public static final int FLAG_EDITOR_ACTION = 0x10; 1177 1178 /** 1179 * When associated with up key events, this indicates that the key press 1180 * has been canceled. Typically this is used with virtual touch screen 1181 * keys, where the user can slide from the virtual key area on to the 1182 * display: in that case, the application will receive a canceled up 1183 * event and should not perform the action normally associated with the 1184 * key. Note that for this to work, the application can not perform an 1185 * action for a key until it receives an up or the long press timeout has 1186 * expired. 1187 */ 1188 public static final int FLAG_CANCELED = 0x20; 1189 1190 /** 1191 * This key event was generated by a virtual (on-screen) hard key area. 1192 * Typically this is an area of the touchscreen, outside of the regular 1193 * display, dedicated to "hardware" buttons. 1194 */ 1195 public static final int FLAG_VIRTUAL_HARD_KEY = 0x40; 1196 1197 /** 1198 * This flag is set for the first key repeat that occurs after the 1199 * long press timeout. 1200 */ 1201 public static final int FLAG_LONG_PRESS = 0x80; 1202 1203 /** 1204 * Set when a key event has {@link #FLAG_CANCELED} set because a long 1205 * press action was executed while it was down. 1206 */ 1207 public static final int FLAG_CANCELED_LONG_PRESS = 0x100; 1208 1209 /** 1210 * Set for {@link #ACTION_UP} when this event's key code is still being 1211 * tracked from its initial down. That is, somebody requested that tracking 1212 * started on the key down and a long press has not caused 1213 * the tracking to be canceled. 1214 */ 1215 public static final int FLAG_TRACKING = 0x200; 1216 1217 /** 1218 * Set when a key event has been synthesized to implement default behavior 1219 * for an event that the application did not handle. 1220 * Fallback key events are generated by unhandled trackball motions 1221 * (to emulate a directional keypad) and by certain unhandled key presses 1222 * that are declared in the key map (such as special function numeric keypad 1223 * keys when numlock is off). 1224 */ 1225 public static final int FLAG_FALLBACK = 0x400; 1226 1227 /** 1228 * Private control to determine when an app is tracking a key sequence. 1229 * @hide 1230 */ 1231 public static final int FLAG_START_TRACKING = 0x40000000; 1232 1233 /** 1234 * Private flag that indicates when the system has detected that this key event 1235 * may be inconsistent with respect to the sequence of previously delivered key events, 1236 * such as when a key up event is sent but the key was not down. 1237 * 1238 * @hide 1239 * @see #isTainted 1240 * @see #setTainted 1241 */ 1242 public static final int FLAG_TAINTED = 0x80000000; 1243 1244 /** 1245 * Returns the maximum keycode. 1246 */ getMaxKeyCode()1247 public static int getMaxKeyCode() { 1248 return LAST_KEYCODE; 1249 } 1250 1251 /** 1252 * Get the character that is produced by putting accent on the character 1253 * c. 1254 * For example, getDeadChar('`', 'e') returns è. 1255 */ getDeadChar(int accent, int c)1256 public static int getDeadChar(int accent, int c) { 1257 return KeyCharacterMap.getDeadChar(accent, c); 1258 } 1259 1260 static final boolean DEBUG = false; 1261 static final String TAG = "KeyEvent"; 1262 1263 private static final int MAX_RECYCLED = 10; 1264 private static final Object gRecyclerLock = new Object(); 1265 private static int gRecyclerUsed; 1266 private static KeyEvent gRecyclerTop; 1267 1268 private KeyEvent mNext; 1269 1270 private int mDeviceId; 1271 private int mSource; 1272 private int mMetaState; 1273 private int mAction; 1274 private int mKeyCode; 1275 private int mScanCode; 1276 private int mRepeatCount; 1277 private int mFlags; 1278 private long mDownTime; 1279 private long mEventTime; 1280 private String mCharacters; 1281 1282 public interface Callback { 1283 /** 1284 * Called when a key down event has occurred. If you return true, 1285 * you can first call {@link KeyEvent#startTracking() 1286 * KeyEvent.startTracking()} to have the framework track the event 1287 * through its {@link #onKeyUp(int, KeyEvent)} and also call your 1288 * {@link #onKeyLongPress(int, KeyEvent)} if it occurs. 1289 * 1290 * @param keyCode The value in event.getKeyCode(). 1291 * @param event Description of the key event. 1292 * 1293 * @return If you handled the event, return true. If you want to allow 1294 * the event to be handled by the next receiver, return false. 1295 */ onKeyDown(int keyCode, KeyEvent event)1296 boolean onKeyDown(int keyCode, KeyEvent event); 1297 1298 /** 1299 * Called when a long press has occurred. If you return true, 1300 * the final key up will have {@link KeyEvent#FLAG_CANCELED} and 1301 * {@link KeyEvent#FLAG_CANCELED_LONG_PRESS} set. Note that in 1302 * order to receive this callback, someone in the event change 1303 * <em>must</em> return true from {@link #onKeyDown} <em>and</em> 1304 * call {@link KeyEvent#startTracking()} on the event. 1305 * 1306 * @param keyCode The value in event.getKeyCode(). 1307 * @param event Description of the key event. 1308 * 1309 * @return If you handled the event, return true. If you want to allow 1310 * the event to be handled by the next receiver, return false. 1311 */ onKeyLongPress(int keyCode, KeyEvent event)1312 boolean onKeyLongPress(int keyCode, KeyEvent event); 1313 1314 /** 1315 * Called when a key up event has occurred. 1316 * 1317 * @param keyCode The value in event.getKeyCode(). 1318 * @param event Description of the key event. 1319 * 1320 * @return If you handled the event, return true. If you want to allow 1321 * the event to be handled by the next receiver, return false. 1322 */ onKeyUp(int keyCode, KeyEvent event)1323 boolean onKeyUp(int keyCode, KeyEvent event); 1324 1325 /** 1326 * Called when multiple down/up pairs of the same key have occurred 1327 * in a row. 1328 * 1329 * @param keyCode The value in event.getKeyCode(). 1330 * @param count Number of pairs as returned by event.getRepeatCount(). 1331 * @param event Description of the key event. 1332 * 1333 * @return If you handled the event, return true. If you want to allow 1334 * the event to be handled by the next receiver, return false. 1335 */ onKeyMultiple(int keyCode, int count, KeyEvent event)1336 boolean onKeyMultiple(int keyCode, int count, KeyEvent event); 1337 } 1338 1339 static { populateKeycodeSymbolicNames()1340 populateKeycodeSymbolicNames(); 1341 } 1342 KeyEvent()1343 private KeyEvent() { 1344 } 1345 1346 /** 1347 * Create a new key event. 1348 * 1349 * @param action Action code: either {@link #ACTION_DOWN}, 1350 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 1351 * @param code The key code. 1352 */ KeyEvent(int action, int code)1353 public KeyEvent(int action, int code) { 1354 mAction = action; 1355 mKeyCode = code; 1356 mRepeatCount = 0; 1357 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD; 1358 } 1359 1360 /** 1361 * Create a new key event. 1362 * 1363 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1364 * at which this key code originally went down. 1365 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1366 * at which this event happened. 1367 * @param action Action code: either {@link #ACTION_DOWN}, 1368 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 1369 * @param code The key code. 1370 * @param repeat A repeat count for down events (> 0 if this is after the 1371 * initial down) or event count for multiple events. 1372 */ KeyEvent(long downTime, long eventTime, int action, int code, int repeat)1373 public KeyEvent(long downTime, long eventTime, int action, 1374 int code, int repeat) { 1375 mDownTime = downTime; 1376 mEventTime = eventTime; 1377 mAction = action; 1378 mKeyCode = code; 1379 mRepeatCount = repeat; 1380 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD; 1381 } 1382 1383 /** 1384 * Create a new key event. 1385 * 1386 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1387 * at which this key code originally went down. 1388 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1389 * at which this event happened. 1390 * @param action Action code: either {@link #ACTION_DOWN}, 1391 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 1392 * @param code The key code. 1393 * @param repeat A repeat count for down events (> 0 if this is after the 1394 * initial down) or event count for multiple events. 1395 * @param metaState Flags indicating which meta keys are currently pressed. 1396 */ KeyEvent(long downTime, long eventTime, int action, int code, int repeat, int metaState)1397 public KeyEvent(long downTime, long eventTime, int action, 1398 int code, int repeat, int metaState) { 1399 mDownTime = downTime; 1400 mEventTime = eventTime; 1401 mAction = action; 1402 mKeyCode = code; 1403 mRepeatCount = repeat; 1404 mMetaState = metaState; 1405 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD; 1406 } 1407 1408 /** 1409 * Create a new key event. 1410 * 1411 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1412 * at which this key code originally went down. 1413 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1414 * at which this event happened. 1415 * @param action Action code: either {@link #ACTION_DOWN}, 1416 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 1417 * @param code The key code. 1418 * @param repeat A repeat count for down events (> 0 if this is after the 1419 * initial down) or event count for multiple events. 1420 * @param metaState Flags indicating which meta keys are currently pressed. 1421 * @param deviceId The device ID that generated the key event. 1422 * @param scancode Raw device scan code of the event. 1423 */ KeyEvent(long downTime, long eventTime, int action, int code, int repeat, int metaState, int deviceId, int scancode)1424 public KeyEvent(long downTime, long eventTime, int action, 1425 int code, int repeat, int metaState, 1426 int deviceId, int scancode) { 1427 mDownTime = downTime; 1428 mEventTime = eventTime; 1429 mAction = action; 1430 mKeyCode = code; 1431 mRepeatCount = repeat; 1432 mMetaState = metaState; 1433 mDeviceId = deviceId; 1434 mScanCode = scancode; 1435 } 1436 1437 /** 1438 * Create a new key event. 1439 * 1440 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1441 * at which this key code originally went down. 1442 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1443 * at which this event happened. 1444 * @param action Action code: either {@link #ACTION_DOWN}, 1445 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 1446 * @param code The key code. 1447 * @param repeat A repeat count for down events (> 0 if this is after the 1448 * initial down) or event count for multiple events. 1449 * @param metaState Flags indicating which meta keys are currently pressed. 1450 * @param deviceId The device ID that generated the key event. 1451 * @param scancode Raw device scan code of the event. 1452 * @param flags The flags for this key event 1453 */ KeyEvent(long downTime, long eventTime, int action, int code, int repeat, int metaState, int deviceId, int scancode, int flags)1454 public KeyEvent(long downTime, long eventTime, int action, 1455 int code, int repeat, int metaState, 1456 int deviceId, int scancode, int flags) { 1457 mDownTime = downTime; 1458 mEventTime = eventTime; 1459 mAction = action; 1460 mKeyCode = code; 1461 mRepeatCount = repeat; 1462 mMetaState = metaState; 1463 mDeviceId = deviceId; 1464 mScanCode = scancode; 1465 mFlags = flags; 1466 } 1467 1468 /** 1469 * Create a new key event. 1470 * 1471 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1472 * at which this key code originally went down. 1473 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1474 * at which this event happened. 1475 * @param action Action code: either {@link #ACTION_DOWN}, 1476 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 1477 * @param code The key code. 1478 * @param repeat A repeat count for down events (> 0 if this is after the 1479 * initial down) or event count for multiple events. 1480 * @param metaState Flags indicating which meta keys are currently pressed. 1481 * @param deviceId The device ID that generated the key event. 1482 * @param scancode Raw device scan code of the event. 1483 * @param flags The flags for this key event 1484 * @param source The input source such as {@link InputDevice#SOURCE_KEYBOARD}. 1485 */ KeyEvent(long downTime, long eventTime, int action, int code, int repeat, int metaState, int deviceId, int scancode, int flags, int source)1486 public KeyEvent(long downTime, long eventTime, int action, 1487 int code, int repeat, int metaState, 1488 int deviceId, int scancode, int flags, int source) { 1489 mDownTime = downTime; 1490 mEventTime = eventTime; 1491 mAction = action; 1492 mKeyCode = code; 1493 mRepeatCount = repeat; 1494 mMetaState = metaState; 1495 mDeviceId = deviceId; 1496 mScanCode = scancode; 1497 mFlags = flags; 1498 mSource = source; 1499 } 1500 1501 /** 1502 * Create a new key event for a string of characters. The key code, 1503 * action, repeat count and source will automatically be set to 1504 * {@link #KEYCODE_UNKNOWN}, {@link #ACTION_MULTIPLE}, 0, and 1505 * {@link InputDevice#SOURCE_KEYBOARD} for you. 1506 * 1507 * @param time The time (in {@link android.os.SystemClock#uptimeMillis}) 1508 * at which this event occured. 1509 * @param characters The string of characters. 1510 * @param deviceId The device ID that generated the key event. 1511 * @param flags The flags for this key event 1512 */ KeyEvent(long time, String characters, int deviceId, int flags)1513 public KeyEvent(long time, String characters, int deviceId, int flags) { 1514 mDownTime = time; 1515 mEventTime = time; 1516 mCharacters = characters; 1517 mAction = ACTION_MULTIPLE; 1518 mKeyCode = KEYCODE_UNKNOWN; 1519 mRepeatCount = 0; 1520 mDeviceId = deviceId; 1521 mFlags = flags; 1522 mSource = InputDevice.SOURCE_KEYBOARD; 1523 } 1524 1525 /** 1526 * Make an exact copy of an existing key event. 1527 */ KeyEvent(KeyEvent origEvent)1528 public KeyEvent(KeyEvent origEvent) { 1529 mDownTime = origEvent.mDownTime; 1530 mEventTime = origEvent.mEventTime; 1531 mAction = origEvent.mAction; 1532 mKeyCode = origEvent.mKeyCode; 1533 mRepeatCount = origEvent.mRepeatCount; 1534 mMetaState = origEvent.mMetaState; 1535 mDeviceId = origEvent.mDeviceId; 1536 mSource = origEvent.mSource; 1537 mScanCode = origEvent.mScanCode; 1538 mFlags = origEvent.mFlags; 1539 mCharacters = origEvent.mCharacters; 1540 } 1541 1542 /** 1543 * Copy an existing key event, modifying its time and repeat count. 1544 * 1545 * @deprecated Use {@link #changeTimeRepeat(KeyEvent, long, int)} 1546 * instead. 1547 * 1548 * @param origEvent The existing event to be copied. 1549 * @param eventTime The new event time 1550 * (in {@link android.os.SystemClock#uptimeMillis}) of the event. 1551 * @param newRepeat The new repeat count of the event. 1552 */ 1553 @Deprecated KeyEvent(KeyEvent origEvent, long eventTime, int newRepeat)1554 public KeyEvent(KeyEvent origEvent, long eventTime, int newRepeat) { 1555 mDownTime = origEvent.mDownTime; 1556 mEventTime = eventTime; 1557 mAction = origEvent.mAction; 1558 mKeyCode = origEvent.mKeyCode; 1559 mRepeatCount = newRepeat; 1560 mMetaState = origEvent.mMetaState; 1561 mDeviceId = origEvent.mDeviceId; 1562 mSource = origEvent.mSource; 1563 mScanCode = origEvent.mScanCode; 1564 mFlags = origEvent.mFlags; 1565 mCharacters = origEvent.mCharacters; 1566 } 1567 obtain()1568 private static KeyEvent obtain() { 1569 final KeyEvent ev; 1570 synchronized (gRecyclerLock) { 1571 ev = gRecyclerTop; 1572 if (ev == null) { 1573 return new KeyEvent(); 1574 } 1575 gRecyclerTop = ev.mNext; 1576 gRecyclerUsed -= 1; 1577 } 1578 ev.mNext = null; 1579 ev.prepareForReuse(); 1580 return ev; 1581 } 1582 1583 /** 1584 * Obtains a (potentially recycled) key event. 1585 * 1586 * @hide 1587 */ obtain(long downTime, long eventTime, int action, int code, int repeat, int metaState, int deviceId, int scancode, int flags, int source, String characters)1588 public static KeyEvent obtain(long downTime, long eventTime, int action, 1589 int code, int repeat, int metaState, 1590 int deviceId, int scancode, int flags, int source, String characters) { 1591 KeyEvent ev = obtain(); 1592 ev.mDownTime = downTime; 1593 ev.mEventTime = eventTime; 1594 ev.mAction = action; 1595 ev.mKeyCode = code; 1596 ev.mRepeatCount = repeat; 1597 ev.mMetaState = metaState; 1598 ev.mDeviceId = deviceId; 1599 ev.mScanCode = scancode; 1600 ev.mFlags = flags; 1601 ev.mSource = source; 1602 ev.mCharacters = characters; 1603 return ev; 1604 } 1605 1606 /** 1607 * Obtains a (potentially recycled) copy of another key event. 1608 * 1609 * @hide 1610 */ obtain(KeyEvent other)1611 public static KeyEvent obtain(KeyEvent other) { 1612 KeyEvent ev = obtain(); 1613 ev.mDownTime = other.mDownTime; 1614 ev.mEventTime = other.mEventTime; 1615 ev.mAction = other.mAction; 1616 ev.mKeyCode = other.mKeyCode; 1617 ev.mRepeatCount = other.mRepeatCount; 1618 ev.mMetaState = other.mMetaState; 1619 ev.mDeviceId = other.mDeviceId; 1620 ev.mScanCode = other.mScanCode; 1621 ev.mFlags = other.mFlags; 1622 ev.mSource = other.mSource; 1623 ev.mCharacters = other.mCharacters; 1624 return ev; 1625 } 1626 1627 /** @hide */ 1628 @Override copy()1629 public KeyEvent copy() { 1630 return obtain(this); 1631 } 1632 1633 /** 1634 * Recycles a key event. 1635 * Key events should only be recycled if they are owned by the system since user 1636 * code expects them to be essentially immutable, "tracking" notwithstanding. 1637 * 1638 * @hide 1639 */ 1640 @Override recycle()1641 public final void recycle() { 1642 super.recycle(); 1643 mCharacters = null; 1644 1645 synchronized (gRecyclerLock) { 1646 if (gRecyclerUsed < MAX_RECYCLED) { 1647 gRecyclerUsed++; 1648 mNext = gRecyclerTop; 1649 gRecyclerTop = this; 1650 } 1651 } 1652 } 1653 1654 /** @hide */ 1655 @Override recycleIfNeededAfterDispatch()1656 public final void recycleIfNeededAfterDispatch() { 1657 // Do nothing. 1658 } 1659 1660 /** 1661 * Create a new key event that is the same as the given one, but whose 1662 * event time and repeat count are replaced with the given value. 1663 * 1664 * @param event The existing event to be copied. This is not modified. 1665 * @param eventTime The new event time 1666 * (in {@link android.os.SystemClock#uptimeMillis}) of the event. 1667 * @param newRepeat The new repeat count of the event. 1668 */ changeTimeRepeat(KeyEvent event, long eventTime, int newRepeat)1669 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime, 1670 int newRepeat) { 1671 return new KeyEvent(event, eventTime, newRepeat); 1672 } 1673 1674 /** 1675 * Create a new key event that is the same as the given one, but whose 1676 * event time and repeat count are replaced with the given value. 1677 * 1678 * @param event The existing event to be copied. This is not modified. 1679 * @param eventTime The new event time 1680 * (in {@link android.os.SystemClock#uptimeMillis}) of the event. 1681 * @param newRepeat The new repeat count of the event. 1682 * @param newFlags New flags for the event, replacing the entire value 1683 * in the original event. 1684 */ changeTimeRepeat(KeyEvent event, long eventTime, int newRepeat, int newFlags)1685 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime, 1686 int newRepeat, int newFlags) { 1687 KeyEvent ret = new KeyEvent(event); 1688 ret.mEventTime = eventTime; 1689 ret.mRepeatCount = newRepeat; 1690 ret.mFlags = newFlags; 1691 return ret; 1692 } 1693 1694 /** 1695 * Copy an existing key event, modifying its action. 1696 * 1697 * @param origEvent The existing event to be copied. 1698 * @param action The new action code of the event. 1699 */ KeyEvent(KeyEvent origEvent, int action)1700 private KeyEvent(KeyEvent origEvent, int action) { 1701 mDownTime = origEvent.mDownTime; 1702 mEventTime = origEvent.mEventTime; 1703 mAction = action; 1704 mKeyCode = origEvent.mKeyCode; 1705 mRepeatCount = origEvent.mRepeatCount; 1706 mMetaState = origEvent.mMetaState; 1707 mDeviceId = origEvent.mDeviceId; 1708 mSource = origEvent.mSource; 1709 mScanCode = origEvent.mScanCode; 1710 mFlags = origEvent.mFlags; 1711 // Don't copy mCharacters, since one way or the other we'll lose it 1712 // when changing the action. 1713 } 1714 1715 /** 1716 * Create a new key event that is the same as the given one, but whose 1717 * action is replaced with the given value. 1718 * 1719 * @param event The existing event to be copied. This is not modified. 1720 * @param action The new action code of the event. 1721 */ changeAction(KeyEvent event, int action)1722 public static KeyEvent changeAction(KeyEvent event, int action) { 1723 return new KeyEvent(event, action); 1724 } 1725 1726 /** 1727 * Create a new key event that is the same as the given one, but whose 1728 * flags are replaced with the given value. 1729 * 1730 * @param event The existing event to be copied. This is not modified. 1731 * @param flags The new flags constant. 1732 */ changeFlags(KeyEvent event, int flags)1733 public static KeyEvent changeFlags(KeyEvent event, int flags) { 1734 event = new KeyEvent(event); 1735 event.mFlags = flags; 1736 return event; 1737 } 1738 1739 /** @hide */ 1740 @Override isTainted()1741 public final boolean isTainted() { 1742 return (mFlags & FLAG_TAINTED) != 0; 1743 } 1744 1745 /** @hide */ 1746 @Override setTainted(boolean tainted)1747 public final void setTainted(boolean tainted) { 1748 mFlags = tainted ? mFlags | FLAG_TAINTED : mFlags & ~FLAG_TAINTED; 1749 } 1750 1751 /** 1752 * Don't use in new code, instead explicitly check 1753 * {@link #getAction()}. 1754 * 1755 * @return If the action is ACTION_DOWN, returns true; else false. 1756 * 1757 * @deprecated 1758 * @hide 1759 */ isDown()1760 @Deprecated public final boolean isDown() { 1761 return mAction == ACTION_DOWN; 1762 } 1763 1764 /** 1765 * Is this a system key? System keys can not be used for menu shortcuts. 1766 * 1767 * TODO: this information should come from a table somewhere. 1768 * TODO: should the dpad keys be here? arguably, because they also shouldn't be menu shortcuts 1769 */ isSystem()1770 public final boolean isSystem() { 1771 return native_isSystemKey(mKeyCode); 1772 } 1773 1774 /** @hide */ hasDefaultAction()1775 public final boolean hasDefaultAction() { 1776 return native_hasDefaultAction(mKeyCode); 1777 } 1778 1779 /** 1780 * Returns true if the specified keycode is a gamepad button. 1781 * @return True if the keycode is a gamepad button, such as {@link #KEYCODE_BUTTON_A}. 1782 */ isGamepadButton(int keyCode)1783 public static final boolean isGamepadButton(int keyCode) { 1784 switch (keyCode) { 1785 case KeyEvent.KEYCODE_BUTTON_A: 1786 case KeyEvent.KEYCODE_BUTTON_B: 1787 case KeyEvent.KEYCODE_BUTTON_C: 1788 case KeyEvent.KEYCODE_BUTTON_X: 1789 case KeyEvent.KEYCODE_BUTTON_Y: 1790 case KeyEvent.KEYCODE_BUTTON_Z: 1791 case KeyEvent.KEYCODE_BUTTON_L1: 1792 case KeyEvent.KEYCODE_BUTTON_R1: 1793 case KeyEvent.KEYCODE_BUTTON_L2: 1794 case KeyEvent.KEYCODE_BUTTON_R2: 1795 case KeyEvent.KEYCODE_BUTTON_THUMBL: 1796 case KeyEvent.KEYCODE_BUTTON_THUMBR: 1797 case KeyEvent.KEYCODE_BUTTON_START: 1798 case KeyEvent.KEYCODE_BUTTON_SELECT: 1799 case KeyEvent.KEYCODE_BUTTON_MODE: 1800 case KeyEvent.KEYCODE_BUTTON_1: 1801 case KeyEvent.KEYCODE_BUTTON_2: 1802 case KeyEvent.KEYCODE_BUTTON_3: 1803 case KeyEvent.KEYCODE_BUTTON_4: 1804 case KeyEvent.KEYCODE_BUTTON_5: 1805 case KeyEvent.KEYCODE_BUTTON_6: 1806 case KeyEvent.KEYCODE_BUTTON_7: 1807 case KeyEvent.KEYCODE_BUTTON_8: 1808 case KeyEvent.KEYCODE_BUTTON_9: 1809 case KeyEvent.KEYCODE_BUTTON_10: 1810 case KeyEvent.KEYCODE_BUTTON_11: 1811 case KeyEvent.KEYCODE_BUTTON_12: 1812 case KeyEvent.KEYCODE_BUTTON_13: 1813 case KeyEvent.KEYCODE_BUTTON_14: 1814 case KeyEvent.KEYCODE_BUTTON_15: 1815 case KeyEvent.KEYCODE_BUTTON_16: 1816 return true; 1817 default: 1818 return false; 1819 } 1820 } 1821 1822 /** {@inheritDoc} */ 1823 @Override getDeviceId()1824 public final int getDeviceId() { 1825 return mDeviceId; 1826 } 1827 1828 /** {@inheritDoc} */ 1829 @Override getSource()1830 public final int getSource() { 1831 return mSource; 1832 } 1833 1834 /** {@inheritDoc} */ 1835 @Override setSource(int source)1836 public final void setSource(int source) { 1837 mSource = source; 1838 } 1839 1840 /** 1841 * <p>Returns the state of the meta keys.</p> 1842 * 1843 * @return an integer in which each bit set to 1 represents a pressed 1844 * meta key 1845 * 1846 * @see #isAltPressed() 1847 * @see #isShiftPressed() 1848 * @see #isSymPressed() 1849 * @see #isCtrlPressed() 1850 * @see #isMetaPressed() 1851 * @see #isFunctionPressed() 1852 * @see #isCapsLockOn() 1853 * @see #isNumLockOn() 1854 * @see #isScrollLockOn() 1855 * @see #META_ALT_ON 1856 * @see #META_ALT_LEFT_ON 1857 * @see #META_ALT_RIGHT_ON 1858 * @see #META_SHIFT_ON 1859 * @see #META_SHIFT_LEFT_ON 1860 * @see #META_SHIFT_RIGHT_ON 1861 * @see #META_SYM_ON 1862 * @see #META_FUNCTION_ON 1863 * @see #META_CTRL_ON 1864 * @see #META_CTRL_LEFT_ON 1865 * @see #META_CTRL_RIGHT_ON 1866 * @see #META_META_ON 1867 * @see #META_META_LEFT_ON 1868 * @see #META_META_RIGHT_ON 1869 * @see #META_CAPS_LOCK_ON 1870 * @see #META_NUM_LOCK_ON 1871 * @see #META_SCROLL_LOCK_ON 1872 * @see #getModifiers 1873 */ getMetaState()1874 public final int getMetaState() { 1875 return mMetaState; 1876 } 1877 1878 /** 1879 * Returns the state of the modifier keys. 1880 * <p> 1881 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK}, 1882 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are 1883 * not considered modifier keys. Consequently, this function specifically masks out 1884 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}. 1885 * </p><p> 1886 * The value returned consists of the meta state (from {@link #getMetaState}) 1887 * normalized using {@link #normalizeMetaState(int)} and then masked with 1888 * {@link #getModifierMetaStateMask} so that only valid modifier bits are retained. 1889 * </p> 1890 * 1891 * @return An integer in which each bit set to 1 represents a pressed modifier key. 1892 * @see #getMetaState 1893 */ getModifiers()1894 public final int getModifiers() { 1895 return normalizeMetaState(mMetaState) & META_MODIFIER_MASK; 1896 } 1897 1898 /** 1899 * Returns the flags for this key event. 1900 * 1901 * @see #FLAG_WOKE_HERE 1902 */ getFlags()1903 public final int getFlags() { 1904 return mFlags; 1905 } 1906 1907 // Mask of all modifier key meta states. Specifically excludes locked keys like caps lock. 1908 private static final int META_MODIFIER_MASK = 1909 META_SHIFT_ON | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON 1910 | META_ALT_ON | META_ALT_LEFT_ON | META_ALT_RIGHT_ON 1911 | META_CTRL_ON | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON 1912 | META_META_ON | META_META_LEFT_ON | META_META_RIGHT_ON 1913 | META_SYM_ON | META_FUNCTION_ON; 1914 1915 // Mask of all lock key meta states. 1916 private static final int META_LOCK_MASK = 1917 META_CAPS_LOCK_ON | META_NUM_LOCK_ON | META_SCROLL_LOCK_ON; 1918 1919 // Mask of all valid meta states. 1920 private static final int META_ALL_MASK = META_MODIFIER_MASK | META_LOCK_MASK; 1921 1922 // Mask of all synthetic meta states that are reserved for API compatibility with 1923 // historical uses in MetaKeyKeyListener. 1924 private static final int META_SYNTHETIC_MASK = 1925 META_CAP_LOCKED | META_ALT_LOCKED | META_SYM_LOCKED | META_SELECTING; 1926 1927 // Mask of all meta states that are not valid use in specifying a modifier key. 1928 // These bits are known to be used for purposes other than specifying modifiers. 1929 private static final int META_INVALID_MODIFIER_MASK = 1930 META_LOCK_MASK | META_SYNTHETIC_MASK; 1931 1932 /** 1933 * Gets a mask that includes all valid modifier key meta state bits. 1934 * <p> 1935 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK}, 1936 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are 1937 * not considered modifier keys. Consequently, the mask specifically excludes 1938 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}. 1939 * </p> 1940 * 1941 * @return The modifier meta state mask which is a combination of 1942 * {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON}, {@link #META_SHIFT_RIGHT_ON}, 1943 * {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON}, {@link #META_ALT_RIGHT_ON}, 1944 * {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON}, {@link #META_CTRL_RIGHT_ON}, 1945 * {@link #META_META_ON}, {@link #META_META_LEFT_ON}, {@link #META_META_RIGHT_ON}, 1946 * {@link #META_SYM_ON}, {@link #META_FUNCTION_ON}. 1947 */ getModifierMetaStateMask()1948 public static int getModifierMetaStateMask() { 1949 return META_MODIFIER_MASK; 1950 } 1951 1952 /** 1953 * Returns true if this key code is a modifier key. 1954 * <p> 1955 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK}, 1956 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are 1957 * not considered modifier keys. Consequently, this function return false 1958 * for those keys. 1959 * </p> 1960 * 1961 * @return True if the key code is one of 1962 * {@link #KEYCODE_SHIFT_LEFT} {@link #KEYCODE_SHIFT_RIGHT}, 1963 * {@link #KEYCODE_ALT_LEFT}, {@link #KEYCODE_ALT_RIGHT}, 1964 * {@link #KEYCODE_CTRL_LEFT}, {@link #KEYCODE_CTRL_RIGHT}, 1965 * {@link #KEYCODE_META_LEFT}, or {@link #KEYCODE_META_RIGHT}, 1966 * {@link #KEYCODE_SYM}, {@link #KEYCODE_NUM}, {@link #KEYCODE_FUNCTION}. 1967 */ isModifierKey(int keyCode)1968 public static boolean isModifierKey(int keyCode) { 1969 switch (keyCode) { 1970 case KEYCODE_SHIFT_LEFT: 1971 case KEYCODE_SHIFT_RIGHT: 1972 case KEYCODE_ALT_LEFT: 1973 case KEYCODE_ALT_RIGHT: 1974 case KEYCODE_CTRL_LEFT: 1975 case KEYCODE_CTRL_RIGHT: 1976 case KEYCODE_META_LEFT: 1977 case KEYCODE_META_RIGHT: 1978 case KEYCODE_SYM: 1979 case KEYCODE_NUM: 1980 case KEYCODE_FUNCTION: 1981 return true; 1982 default: 1983 return false; 1984 } 1985 } 1986 1987 /** 1988 * Normalizes the specified meta state. 1989 * <p> 1990 * The meta state is normalized such that if either the left or right modifier meta state 1991 * bits are set then the result will also include the universal bit for that modifier. 1992 * </p><p> 1993 * If the specified meta state contains {@link #META_ALT_LEFT_ON} then 1994 * the result will also contain {@link #META_ALT_ON} in addition to {@link #META_ALT_LEFT_ON} 1995 * and the other bits that were specified in the input. The same is process is 1996 * performed for shift, control and meta. 1997 * </p><p> 1998 * If the specified meta state contains synthetic meta states defined by 1999 * {@link MetaKeyKeyListener}, then those states are translated here and the original 2000 * synthetic meta states are removed from the result. 2001 * {@link MetaKeyKeyListener#META_CAP_LOCKED} is translated to {@link #META_CAPS_LOCK_ON}. 2002 * {@link MetaKeyKeyListener#META_ALT_LOCKED} is translated to {@link #META_ALT_ON}. 2003 * {@link MetaKeyKeyListener#META_SYM_LOCKED} is translated to {@link #META_SYM_ON}. 2004 * </p><p> 2005 * Undefined meta state bits are removed. 2006 * </p> 2007 * 2008 * @param metaState The meta state. 2009 * @return The normalized meta state. 2010 */ normalizeMetaState(int metaState)2011 public static int normalizeMetaState(int metaState) { 2012 if ((metaState & (META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON)) != 0) { 2013 metaState |= META_SHIFT_ON; 2014 } 2015 if ((metaState & (META_ALT_LEFT_ON | META_ALT_RIGHT_ON)) != 0) { 2016 metaState |= META_ALT_ON; 2017 } 2018 if ((metaState & (META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON)) != 0) { 2019 metaState |= META_CTRL_ON; 2020 } 2021 if ((metaState & (META_META_LEFT_ON | META_META_RIGHT_ON)) != 0) { 2022 metaState |= META_META_ON; 2023 } 2024 if ((metaState & MetaKeyKeyListener.META_CAP_LOCKED) != 0) { 2025 metaState |= META_CAPS_LOCK_ON; 2026 } 2027 if ((metaState & MetaKeyKeyListener.META_ALT_LOCKED) != 0) { 2028 metaState |= META_ALT_ON; 2029 } 2030 if ((metaState & MetaKeyKeyListener.META_SYM_LOCKED) != 0) { 2031 metaState |= META_SYM_ON; 2032 } 2033 return metaState & META_ALL_MASK; 2034 } 2035 2036 /** 2037 * Returns true if no modifiers keys are pressed according to the specified meta state. 2038 * <p> 2039 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK}, 2040 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are 2041 * not considered modifier keys. Consequently, this function ignores 2042 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}. 2043 * </p><p> 2044 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}. 2045 * </p> 2046 * 2047 * @param metaState The meta state to consider. 2048 * @return True if no modifier keys are pressed. 2049 * @see #hasNoModifiers() 2050 */ metaStateHasNoModifiers(int metaState)2051 public static boolean metaStateHasNoModifiers(int metaState) { 2052 return (normalizeMetaState(metaState) & META_MODIFIER_MASK) == 0; 2053 } 2054 2055 /** 2056 * Returns true if only the specified modifier keys are pressed according to 2057 * the specified meta state. Returns false if a different combination of modifier 2058 * keys are pressed. 2059 * <p> 2060 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK}, 2061 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are 2062 * not considered modifier keys. Consequently, this function ignores 2063 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}. 2064 * </p><p> 2065 * If the specified modifier mask includes directional modifiers, such as 2066 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the 2067 * modifier is pressed on that side. 2068 * If the specified modifier mask includes non-directional modifiers, such as 2069 * {@link #META_SHIFT_ON}, then this method ensures that the modifier 2070 * is pressed on either side. 2071 * If the specified modifier mask includes both directional and non-directional modifiers 2072 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON}, 2073 * then this method throws an illegal argument exception. 2074 * </p> 2075 * 2076 * @param metaState The meta state to consider. 2077 * @param modifiers The meta state of the modifier keys to check. May be a combination 2078 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to 2079 * ensure that no modifier keys are pressed. 2080 * @return True if only the specified modifier keys are pressed. 2081 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers 2082 * @see #hasModifiers 2083 */ metaStateHasModifiers(int metaState, int modifiers)2084 public static boolean metaStateHasModifiers(int metaState, int modifiers) { 2085 // Note: For forward compatibility, we allow the parameter to contain meta states 2086 // that we do not recognize but we explicitly disallow meta states that 2087 // are not valid modifiers. 2088 if ((modifiers & META_INVALID_MODIFIER_MASK) != 0) { 2089 throw new IllegalArgumentException("modifiers must not contain " 2090 + "META_CAPS_LOCK_ON, META_NUM_LOCK_ON, META_SCROLL_LOCK_ON, " 2091 + "META_CAP_LOCKED, META_ALT_LOCKED, META_SYM_LOCKED, " 2092 + "or META_SELECTING"); 2093 } 2094 2095 metaState = normalizeMetaState(metaState) & META_MODIFIER_MASK; 2096 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers, 2097 META_SHIFT_ON, META_SHIFT_LEFT_ON, META_SHIFT_RIGHT_ON); 2098 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers, 2099 META_ALT_ON, META_ALT_LEFT_ON, META_ALT_RIGHT_ON); 2100 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers, 2101 META_CTRL_ON, META_CTRL_LEFT_ON, META_CTRL_RIGHT_ON); 2102 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers, 2103 META_META_ON, META_META_LEFT_ON, META_META_RIGHT_ON); 2104 return metaState == modifiers; 2105 } 2106 metaStateFilterDirectionalModifiers(int metaState, int modifiers, int basic, int left, int right)2107 private static int metaStateFilterDirectionalModifiers(int metaState, 2108 int modifiers, int basic, int left, int right) { 2109 final boolean wantBasic = (modifiers & basic) != 0; 2110 final int directional = left | right; 2111 final boolean wantLeftOrRight = (modifiers & directional) != 0; 2112 2113 if (wantBasic) { 2114 if (wantLeftOrRight) { 2115 throw new IllegalArgumentException("modifiers must not contain " 2116 + metaStateToString(basic) + " combined with " 2117 + metaStateToString(left) + " or " + metaStateToString(right)); 2118 } 2119 return metaState & ~directional; 2120 } else if (wantLeftOrRight) { 2121 return metaState & ~basic; 2122 } else { 2123 return metaState; 2124 } 2125 } 2126 2127 /** 2128 * Returns true if no modifier keys are pressed. 2129 * <p> 2130 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK}, 2131 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are 2132 * not considered modifier keys. Consequently, this function ignores 2133 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}. 2134 * </p><p> 2135 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}. 2136 * </p> 2137 * 2138 * @return True if no modifier keys are pressed. 2139 * @see #metaStateHasNoModifiers 2140 */ hasNoModifiers()2141 public final boolean hasNoModifiers() { 2142 return metaStateHasNoModifiers(mMetaState); 2143 } 2144 2145 /** 2146 * Returns true if only the specified modifiers keys are pressed. 2147 * Returns false if a different combination of modifier keys are pressed. 2148 * <p> 2149 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK}, 2150 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are 2151 * not considered modifier keys. Consequently, this function ignores 2152 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}. 2153 * </p><p> 2154 * If the specified modifier mask includes directional modifiers, such as 2155 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the 2156 * modifier is pressed on that side. 2157 * If the specified modifier mask includes non-directional modifiers, such as 2158 * {@link #META_SHIFT_ON}, then this method ensures that the modifier 2159 * is pressed on either side. 2160 * If the specified modifier mask includes both directional and non-directional modifiers 2161 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON}, 2162 * then this method throws an illegal argument exception. 2163 * </p> 2164 * 2165 * @param modifiers The meta state of the modifier keys to check. May be a combination 2166 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to 2167 * ensure that no modifier keys are pressed. 2168 * @return True if only the specified modifier keys are pressed. 2169 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers 2170 * @see #metaStateHasModifiers 2171 */ hasModifiers(int modifiers)2172 public final boolean hasModifiers(int modifiers) { 2173 return metaStateHasModifiers(mMetaState, modifiers); 2174 } 2175 2176 /** 2177 * <p>Returns the pressed state of the ALT meta key.</p> 2178 * 2179 * @return true if the ALT key is pressed, false otherwise 2180 * 2181 * @see #KEYCODE_ALT_LEFT 2182 * @see #KEYCODE_ALT_RIGHT 2183 * @see #META_ALT_ON 2184 */ isAltPressed()2185 public final boolean isAltPressed() { 2186 return (mMetaState & META_ALT_ON) != 0; 2187 } 2188 2189 /** 2190 * <p>Returns the pressed state of the SHIFT meta key.</p> 2191 * 2192 * @return true if the SHIFT key is pressed, false otherwise 2193 * 2194 * @see #KEYCODE_SHIFT_LEFT 2195 * @see #KEYCODE_SHIFT_RIGHT 2196 * @see #META_SHIFT_ON 2197 */ isShiftPressed()2198 public final boolean isShiftPressed() { 2199 return (mMetaState & META_SHIFT_ON) != 0; 2200 } 2201 2202 /** 2203 * <p>Returns the pressed state of the SYM meta key.</p> 2204 * 2205 * @return true if the SYM key is pressed, false otherwise 2206 * 2207 * @see #KEYCODE_SYM 2208 * @see #META_SYM_ON 2209 */ isSymPressed()2210 public final boolean isSymPressed() { 2211 return (mMetaState & META_SYM_ON) != 0; 2212 } 2213 2214 /** 2215 * <p>Returns the pressed state of the CTRL meta key.</p> 2216 * 2217 * @return true if the CTRL key is pressed, false otherwise 2218 * 2219 * @see #KEYCODE_CTRL_LEFT 2220 * @see #KEYCODE_CTRL_RIGHT 2221 * @see #META_CTRL_ON 2222 */ isCtrlPressed()2223 public final boolean isCtrlPressed() { 2224 return (mMetaState & META_CTRL_ON) != 0; 2225 } 2226 2227 /** 2228 * <p>Returns the pressed state of the META meta key.</p> 2229 * 2230 * @return true if the META key is pressed, false otherwise 2231 * 2232 * @see #KEYCODE_META_LEFT 2233 * @see #KEYCODE_META_RIGHT 2234 * @see #META_META_ON 2235 */ isMetaPressed()2236 public final boolean isMetaPressed() { 2237 return (mMetaState & META_META_ON) != 0; 2238 } 2239 2240 /** 2241 * <p>Returns the pressed state of the FUNCTION meta key.</p> 2242 * 2243 * @return true if the FUNCTION key is pressed, false otherwise 2244 * 2245 * @see #KEYCODE_FUNCTION 2246 * @see #META_FUNCTION_ON 2247 */ isFunctionPressed()2248 public final boolean isFunctionPressed() { 2249 return (mMetaState & META_FUNCTION_ON) != 0; 2250 } 2251 2252 /** 2253 * <p>Returns the locked state of the CAPS LOCK meta key.</p> 2254 * 2255 * @return true if the CAPS LOCK key is on, false otherwise 2256 * 2257 * @see #KEYCODE_CAPS_LOCK 2258 * @see #META_CAPS_LOCK_ON 2259 */ isCapsLockOn()2260 public final boolean isCapsLockOn() { 2261 return (mMetaState & META_CAPS_LOCK_ON) != 0; 2262 } 2263 2264 /** 2265 * <p>Returns the locked state of the NUM LOCK meta key.</p> 2266 * 2267 * @return true if the NUM LOCK key is on, false otherwise 2268 * 2269 * @see #KEYCODE_NUM_LOCK 2270 * @see #META_NUM_LOCK_ON 2271 */ isNumLockOn()2272 public final boolean isNumLockOn() { 2273 return (mMetaState & META_NUM_LOCK_ON) != 0; 2274 } 2275 2276 /** 2277 * <p>Returns the locked state of the SCROLL LOCK meta key.</p> 2278 * 2279 * @return true if the SCROLL LOCK key is on, false otherwise 2280 * 2281 * @see #KEYCODE_SCROLL_LOCK 2282 * @see #META_SCROLL_LOCK_ON 2283 */ isScrollLockOn()2284 public final boolean isScrollLockOn() { 2285 return (mMetaState & META_SCROLL_LOCK_ON) != 0; 2286 } 2287 2288 /** 2289 * Retrieve the action of this key event. May be either 2290 * {@link #ACTION_DOWN}, {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 2291 * 2292 * @return The event action: ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE. 2293 */ getAction()2294 public final int getAction() { 2295 return mAction; 2296 } 2297 2298 /** 2299 * For {@link #ACTION_UP} events, indicates that the event has been 2300 * canceled as per {@link #FLAG_CANCELED}. 2301 */ isCanceled()2302 public final boolean isCanceled() { 2303 return (mFlags&FLAG_CANCELED) != 0; 2304 } 2305 2306 /** 2307 * Call this during {@link Callback#onKeyDown} to have the system track 2308 * the key through its final up (possibly including a long press). Note 2309 * that only one key can be tracked at a time -- if another key down 2310 * event is received while a previous one is being tracked, tracking is 2311 * stopped on the previous event. 2312 */ startTracking()2313 public final void startTracking() { 2314 mFlags |= FLAG_START_TRACKING; 2315 } 2316 2317 /** 2318 * For {@link #ACTION_UP} events, indicates that the event is still being 2319 * tracked from its initial down event as per 2320 * {@link #FLAG_TRACKING}. 2321 */ isTracking()2322 public final boolean isTracking() { 2323 return (mFlags&FLAG_TRACKING) != 0; 2324 } 2325 2326 /** 2327 * For {@link #ACTION_DOWN} events, indicates that the event has been 2328 * canceled as per {@link #FLAG_LONG_PRESS}. 2329 */ isLongPress()2330 public final boolean isLongPress() { 2331 return (mFlags&FLAG_LONG_PRESS) != 0; 2332 } 2333 2334 /** 2335 * Retrieve the key code of the key event. This is the physical key that 2336 * was pressed, <em>not</em> the Unicode character. 2337 * 2338 * @return The key code of the event. 2339 */ getKeyCode()2340 public final int getKeyCode() { 2341 return mKeyCode; 2342 } 2343 2344 /** 2345 * For the special case of a {@link #ACTION_MULTIPLE} event with key 2346 * code of {@link #KEYCODE_UNKNOWN}, this is a raw string of characters 2347 * associated with the event. In all other cases it is null. 2348 * 2349 * @return Returns a String of 1 or more characters associated with 2350 * the event. 2351 */ getCharacters()2352 public final String getCharacters() { 2353 return mCharacters; 2354 } 2355 2356 /** 2357 * Retrieve the hardware key id of this key event. These values are not 2358 * reliable and vary from device to device. 2359 * 2360 * {@more} 2361 * Mostly this is here for debugging purposes. 2362 */ getScanCode()2363 public final int getScanCode() { 2364 return mScanCode; 2365 } 2366 2367 /** 2368 * Retrieve the repeat count of the event. For both key up and key down 2369 * events, this is the number of times the key has repeated with the first 2370 * down starting at 0 and counting up from there. For multiple key 2371 * events, this is the number of down/up pairs that have occurred. 2372 * 2373 * @return The number of times the key has repeated. 2374 */ getRepeatCount()2375 public final int getRepeatCount() { 2376 return mRepeatCount; 2377 } 2378 2379 /** 2380 * Retrieve the time of the most recent key down event, 2381 * in the {@link android.os.SystemClock#uptimeMillis} time base. If this 2382 * is a down event, this will be the same as {@link #getEventTime()}. 2383 * Note that when chording keys, this value is the down time of the 2384 * most recently pressed key, which may <em>not</em> be the same physical 2385 * key of this event. 2386 * 2387 * @return Returns the most recent key down time, in the 2388 * {@link android.os.SystemClock#uptimeMillis} time base 2389 */ getDownTime()2390 public final long getDownTime() { 2391 return mDownTime; 2392 } 2393 2394 /** 2395 * Retrieve the time this event occurred, 2396 * in the {@link android.os.SystemClock#uptimeMillis} time base. 2397 * 2398 * @return Returns the time this event occurred, 2399 * in the {@link android.os.SystemClock#uptimeMillis} time base. 2400 */ 2401 @Override getEventTime()2402 public final long getEventTime() { 2403 return mEventTime; 2404 } 2405 2406 /** 2407 * Retrieve the time this event occurred, 2408 * in the {@link android.os.SystemClock#uptimeMillis} time base but with 2409 * nanosecond (instead of millisecond) precision. 2410 * <p> 2411 * The value is in nanosecond precision but it may not have nanosecond accuracy. 2412 * </p> 2413 * 2414 * @return Returns the time this event occurred, 2415 * in the {@link android.os.SystemClock#uptimeMillis} time base but with 2416 * nanosecond (instead of millisecond) precision. 2417 * 2418 * @hide 2419 */ 2420 @Override getEventTimeNano()2421 public final long getEventTimeNano() { 2422 return mEventTime * 1000000L; 2423 } 2424 2425 /** 2426 * Renamed to {@link #getDeviceId}. 2427 * 2428 * @hide 2429 * @deprecated use {@link #getDeviceId()} instead. 2430 */ 2431 @Deprecated getKeyboardDevice()2432 public final int getKeyboardDevice() { 2433 return mDeviceId; 2434 } 2435 2436 /** 2437 * Gets the {@link KeyCharacterMap} associated with the keyboard device. 2438 * 2439 * @return The associated key character map. 2440 * @throws {@link KeyCharacterMap.UnavailableException} if the key character map 2441 * could not be loaded because it was malformed or the default key character map 2442 * is missing from the system. 2443 * 2444 * @see KeyCharacterMap#load 2445 */ getKeyCharacterMap()2446 public final KeyCharacterMap getKeyCharacterMap() { 2447 return KeyCharacterMap.load(mDeviceId); 2448 } 2449 2450 /** 2451 * Gets the primary character for this key. 2452 * In other words, the label that is physically printed on it. 2453 * 2454 * @return The display label character, or 0 if none (eg. for non-printing keys). 2455 */ getDisplayLabel()2456 public char getDisplayLabel() { 2457 return getKeyCharacterMap().getDisplayLabel(mKeyCode); 2458 } 2459 2460 /** 2461 * Gets the Unicode character generated by the specified key and meta 2462 * key state combination. 2463 * <p> 2464 * Returns the Unicode character that the specified key would produce 2465 * when the specified meta bits (see {@link MetaKeyKeyListener}) 2466 * were active. 2467 * </p><p> 2468 * Returns 0 if the key is not one that is used to type Unicode 2469 * characters. 2470 * </p><p> 2471 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the 2472 * key is a "dead key" that should be combined with another to 2473 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} -- 2474 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}. 2475 * </p> 2476 * 2477 * @return The associated character or combining accent, or 0 if none. 2478 */ getUnicodeChar()2479 public int getUnicodeChar() { 2480 return getUnicodeChar(mMetaState); 2481 } 2482 2483 /** 2484 * Gets the Unicode character generated by the specified key and meta 2485 * key state combination. 2486 * <p> 2487 * Returns the Unicode character that the specified key would produce 2488 * when the specified meta bits (see {@link MetaKeyKeyListener}) 2489 * were active. 2490 * </p><p> 2491 * Returns 0 if the key is not one that is used to type Unicode 2492 * characters. 2493 * </p><p> 2494 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the 2495 * key is a "dead key" that should be combined with another to 2496 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} -- 2497 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}. 2498 * </p> 2499 * 2500 * @param metaState The meta key modifier state. 2501 * @return The associated character or combining accent, or 0 if none. 2502 */ getUnicodeChar(int metaState)2503 public int getUnicodeChar(int metaState) { 2504 return getKeyCharacterMap().get(mKeyCode, metaState); 2505 } 2506 2507 /** 2508 * Get the character conversion data for a given key code. 2509 * 2510 * @param results A {@link KeyCharacterMap.KeyData} instance that will be 2511 * filled with the results. 2512 * @return True if the key was mapped. If the key was not mapped, results is not modified. 2513 * 2514 * @deprecated instead use {@link #getDisplayLabel()}, 2515 * {@link #getNumber()} or {@link #getUnicodeChar(int)}. 2516 */ 2517 @Deprecated getKeyData(KeyData results)2518 public boolean getKeyData(KeyData results) { 2519 return getKeyCharacterMap().getKeyData(mKeyCode, results); 2520 } 2521 2522 /** 2523 * Gets the first character in the character array that can be generated 2524 * by the specified key code. 2525 * <p> 2526 * This is a convenience function that returns the same value as 2527 * {@link #getMatch(char[],int) getMatch(chars, 0)}. 2528 * </p> 2529 * 2530 * @param chars The array of matching characters to consider. 2531 * @return The matching associated character, or 0 if none. 2532 */ getMatch(char[] chars)2533 public char getMatch(char[] chars) { 2534 return getMatch(chars, 0); 2535 } 2536 2537 /** 2538 * Gets the first character in the character array that can be generated 2539 * by the specified key code. If there are multiple choices, prefers 2540 * the one that would be generated with the specified meta key modifier state. 2541 * 2542 * @param chars The array of matching characters to consider. 2543 * @param metaState The preferred meta key modifier state. 2544 * @return The matching associated character, or 0 if none. 2545 */ getMatch(char[] chars, int metaState)2546 public char getMatch(char[] chars, int metaState) { 2547 return getKeyCharacterMap().getMatch(mKeyCode, chars, metaState); 2548 } 2549 2550 /** 2551 * Gets the number or symbol associated with the key. 2552 * <p> 2553 * The character value is returned, not the numeric value. 2554 * If the key is not a number, but is a symbol, the symbol is retuned. 2555 * </p><p> 2556 * This method is intended to to support dial pads and other numeric or 2557 * symbolic entry on keyboards where certain keys serve dual function 2558 * as alphabetic and symbolic keys. This method returns the number 2559 * or symbol associated with the key independent of whether the user 2560 * has pressed the required modifier. 2561 * </p><p> 2562 * For example, on one particular keyboard the keys on the top QWERTY row generate 2563 * numbers when ALT is pressed such that ALT-Q maps to '1'. So for that keyboard 2564 * when {@link #getNumber} is called with {@link KeyEvent#KEYCODE_Q} it returns '1' 2565 * so that the user can type numbers without pressing ALT when it makes sense. 2566 * </p> 2567 * 2568 * @return The associated numeric or symbolic character, or 0 if none. 2569 */ getNumber()2570 public char getNumber() { 2571 return getKeyCharacterMap().getNumber(mKeyCode); 2572 } 2573 2574 /** 2575 * Returns true if this key produces a glyph. 2576 * 2577 * @return True if the key is a printing key. 2578 */ isPrintingKey()2579 public boolean isPrintingKey() { 2580 return getKeyCharacterMap().isPrintingKey(mKeyCode); 2581 } 2582 2583 /** 2584 * @deprecated Use {@link #dispatch(Callback, DispatcherState, Object)} instead. 2585 */ 2586 @Deprecated dispatch(Callback receiver)2587 public final boolean dispatch(Callback receiver) { 2588 return dispatch(receiver, null, null); 2589 } 2590 2591 /** 2592 * Deliver this key event to a {@link Callback} interface. If this is 2593 * an ACTION_MULTIPLE event and it is not handled, then an attempt will 2594 * be made to deliver a single normal event. 2595 * 2596 * @param receiver The Callback that will be given the event. 2597 * @param state State information retained across events. 2598 * @param target The target of the dispatch, for use in tracking. 2599 * 2600 * @return The return value from the Callback method that was called. 2601 */ dispatch(Callback receiver, DispatcherState state, Object target)2602 public final boolean dispatch(Callback receiver, DispatcherState state, 2603 Object target) { 2604 switch (mAction) { 2605 case ACTION_DOWN: { 2606 mFlags &= ~FLAG_START_TRACKING; 2607 if (DEBUG) Log.v(TAG, "Key down to " + target + " in " + state 2608 + ": " + this); 2609 boolean res = receiver.onKeyDown(mKeyCode, this); 2610 if (state != null) { 2611 if (res && mRepeatCount == 0 && (mFlags&FLAG_START_TRACKING) != 0) { 2612 if (DEBUG) Log.v(TAG, " Start tracking!"); 2613 state.startTracking(this, target); 2614 } else if (isLongPress() && state.isTracking(this)) { 2615 try { 2616 if (receiver.onKeyLongPress(mKeyCode, this)) { 2617 if (DEBUG) Log.v(TAG, " Clear from long press!"); 2618 state.performedLongPress(this); 2619 res = true; 2620 } 2621 } catch (AbstractMethodError e) { 2622 } 2623 } 2624 } 2625 return res; 2626 } 2627 case ACTION_UP: 2628 if (DEBUG) Log.v(TAG, "Key up to " + target + " in " + state 2629 + ": " + this); 2630 if (state != null) { 2631 state.handleUpEvent(this); 2632 } 2633 return receiver.onKeyUp(mKeyCode, this); 2634 case ACTION_MULTIPLE: 2635 final int count = mRepeatCount; 2636 final int code = mKeyCode; 2637 if (receiver.onKeyMultiple(code, count, this)) { 2638 return true; 2639 } 2640 if (code != KeyEvent.KEYCODE_UNKNOWN) { 2641 mAction = ACTION_DOWN; 2642 mRepeatCount = 0; 2643 boolean handled = receiver.onKeyDown(code, this); 2644 if (handled) { 2645 mAction = ACTION_UP; 2646 receiver.onKeyUp(code, this); 2647 } 2648 mAction = ACTION_MULTIPLE; 2649 mRepeatCount = count; 2650 return handled; 2651 } 2652 return false; 2653 } 2654 return false; 2655 } 2656 2657 /** 2658 * Use with {@link KeyEvent#dispatch(Callback, DispatcherState, Object)} 2659 * for more advanced key dispatching, such as long presses. 2660 */ 2661 public static class DispatcherState { 2662 int mDownKeyCode; 2663 Object mDownTarget; 2664 SparseIntArray mActiveLongPresses = new SparseIntArray(); 2665 2666 /** 2667 * Reset back to initial state. 2668 */ reset()2669 public void reset() { 2670 if (DEBUG) Log.v(TAG, "Reset: " + this); 2671 mDownKeyCode = 0; 2672 mDownTarget = null; 2673 mActiveLongPresses.clear(); 2674 } 2675 2676 /** 2677 * Stop any tracking associated with this target. 2678 */ reset(Object target)2679 public void reset(Object target) { 2680 if (mDownTarget == target) { 2681 if (DEBUG) Log.v(TAG, "Reset in " + target + ": " + this); 2682 mDownKeyCode = 0; 2683 mDownTarget = null; 2684 } 2685 } 2686 2687 /** 2688 * Start tracking the key code associated with the given event. This 2689 * can only be called on a key down. It will allow you to see any 2690 * long press associated with the key, and will result in 2691 * {@link KeyEvent#isTracking} return true on the long press and up 2692 * events. 2693 * 2694 * <p>This is only needed if you are directly dispatching events, rather 2695 * than handling them in {@link Callback#onKeyDown}. 2696 */ startTracking(KeyEvent event, Object target)2697 public void startTracking(KeyEvent event, Object target) { 2698 if (event.getAction() != ACTION_DOWN) { 2699 throw new IllegalArgumentException( 2700 "Can only start tracking on a down event"); 2701 } 2702 if (DEBUG) Log.v(TAG, "Start trackingt in " + target + ": " + this); 2703 mDownKeyCode = event.getKeyCode(); 2704 mDownTarget = target; 2705 } 2706 2707 /** 2708 * Return true if the key event is for a key code that is currently 2709 * being tracked by the dispatcher. 2710 */ isTracking(KeyEvent event)2711 public boolean isTracking(KeyEvent event) { 2712 return mDownKeyCode == event.getKeyCode(); 2713 } 2714 2715 /** 2716 * Keep track of the given event's key code as having performed an 2717 * action with a long press, so no action should occur on the up. 2718 * <p>This is only needed if you are directly dispatching events, rather 2719 * than handling them in {@link Callback#onKeyLongPress}. 2720 */ performedLongPress(KeyEvent event)2721 public void performedLongPress(KeyEvent event) { 2722 mActiveLongPresses.put(event.getKeyCode(), 1); 2723 } 2724 2725 /** 2726 * Handle key up event to stop tracking. This resets the dispatcher state, 2727 * and updates the key event state based on it. 2728 * <p>This is only needed if you are directly dispatching events, rather 2729 * than handling them in {@link Callback#onKeyUp}. 2730 */ handleUpEvent(KeyEvent event)2731 public void handleUpEvent(KeyEvent event) { 2732 final int keyCode = event.getKeyCode(); 2733 if (DEBUG) Log.v(TAG, "Handle key up " + event + ": " + this); 2734 int index = mActiveLongPresses.indexOfKey(keyCode); 2735 if (index >= 0) { 2736 if (DEBUG) Log.v(TAG, " Index: " + index); 2737 event.mFlags |= FLAG_CANCELED | FLAG_CANCELED_LONG_PRESS; 2738 mActiveLongPresses.removeAt(index); 2739 } 2740 if (mDownKeyCode == keyCode) { 2741 if (DEBUG) Log.v(TAG, " Tracking!"); 2742 event.mFlags |= FLAG_TRACKING; 2743 mDownKeyCode = 0; 2744 mDownTarget = null; 2745 } 2746 } 2747 } 2748 2749 @Override toString()2750 public String toString() { 2751 StringBuilder msg = new StringBuilder(); 2752 msg.append("KeyEvent { action=").append(actionToString(mAction)); 2753 msg.append(", keyCode=").append(keyCodeToString(mKeyCode)); 2754 msg.append(", scanCode=").append(mScanCode); 2755 if (mCharacters != null) { 2756 msg.append(", characters=\"").append(mCharacters).append("\""); 2757 } 2758 msg.append(", metaState=").append(metaStateToString(mMetaState)); 2759 msg.append(", flags=0x").append(Integer.toHexString(mFlags)); 2760 msg.append(", repeatCount=").append(mRepeatCount); 2761 msg.append(", eventTime=").append(mEventTime); 2762 msg.append(", downTime=").append(mDownTime); 2763 msg.append(", deviceId=").append(mDeviceId); 2764 msg.append(", source=0x").append(Integer.toHexString(mSource)); 2765 msg.append(" }"); 2766 return msg.toString(); 2767 } 2768 2769 /** 2770 * Returns a string that represents the symbolic name of the specified action 2771 * such as "ACTION_DOWN", or an equivalent numeric constant such as "35" if unknown. 2772 * 2773 * @param action The action. 2774 * @return The symbolic name of the specified action. 2775 * @hide 2776 */ actionToString(int action)2777 public static String actionToString(int action) { 2778 switch (action) { 2779 case ACTION_DOWN: 2780 return "ACTION_DOWN"; 2781 case ACTION_UP: 2782 return "ACTION_UP"; 2783 case ACTION_MULTIPLE: 2784 return "ACTION_MULTIPLE"; 2785 default: 2786 return Integer.toString(action); 2787 } 2788 } 2789 2790 /** 2791 * Returns a string that represents the symbolic name of the specified keycode 2792 * such as "KEYCODE_A", "KEYCODE_DPAD_UP", or an equivalent numeric constant 2793 * such as "1001" if unknown. 2794 * 2795 * @param keyCode The key code. 2796 * @return The symbolic name of the specified keycode. 2797 * 2798 * @see KeyCharacterMap#getDisplayLabel 2799 */ keyCodeToString(int keyCode)2800 public static String keyCodeToString(int keyCode) { 2801 String symbolicName = KEYCODE_SYMBOLIC_NAMES.get(keyCode); 2802 return symbolicName != null ? symbolicName : Integer.toString(keyCode); 2803 } 2804 2805 /** 2806 * Gets a keycode by its symbolic name such as "KEYCODE_A" or an equivalent 2807 * numeric constant such as "1001". 2808 * 2809 * @param symbolicName The symbolic name of the keycode. 2810 * @return The keycode or {@link #KEYCODE_UNKNOWN} if not found. 2811 * @see #keycodeToString 2812 */ keyCodeFromString(String symbolicName)2813 public static int keyCodeFromString(String symbolicName) { 2814 if (symbolicName == null) { 2815 throw new IllegalArgumentException("symbolicName must not be null"); 2816 } 2817 2818 final int count = KEYCODE_SYMBOLIC_NAMES.size(); 2819 for (int i = 0; i < count; i++) { 2820 if (symbolicName.equals(KEYCODE_SYMBOLIC_NAMES.valueAt(i))) { 2821 return i; 2822 } 2823 } 2824 2825 try { 2826 return Integer.parseInt(symbolicName, 10); 2827 } catch (NumberFormatException ex) { 2828 return KEYCODE_UNKNOWN; 2829 } 2830 } 2831 2832 /** 2833 * Returns a string that represents the symbolic name of the specified combined meta 2834 * key modifier state flags such as "0", "META_SHIFT_ON", 2835 * "META_ALT_ON|META_SHIFT_ON" or an equivalent numeric constant such as "0x10000000" 2836 * if unknown. 2837 * 2838 * @param metaState The meta state. 2839 * @return The symbolic name of the specified combined meta state flags. 2840 * @hide 2841 */ metaStateToString(int metaState)2842 public static String metaStateToString(int metaState) { 2843 if (metaState == 0) { 2844 return "0"; 2845 } 2846 StringBuilder result = null; 2847 int i = 0; 2848 while (metaState != 0) { 2849 final boolean isSet = (metaState & 1) != 0; 2850 metaState >>>= 1; // unsigned shift! 2851 if (isSet) { 2852 final String name = META_SYMBOLIC_NAMES[i]; 2853 if (result == null) { 2854 if (metaState == 0) { 2855 return name; 2856 } 2857 result = new StringBuilder(name); 2858 } else { 2859 result.append('|'); 2860 result.append(name); 2861 } 2862 } 2863 i += 1; 2864 } 2865 return result.toString(); 2866 } 2867 2868 public static final Parcelable.Creator<KeyEvent> CREATOR 2869 = new Parcelable.Creator<KeyEvent>() { 2870 public KeyEvent createFromParcel(Parcel in) { 2871 in.readInt(); // skip token, we already know this is a KeyEvent 2872 return KeyEvent.createFromParcelBody(in); 2873 } 2874 2875 public KeyEvent[] newArray(int size) { 2876 return new KeyEvent[size]; 2877 } 2878 }; 2879 2880 /** @hide */ createFromParcelBody(Parcel in)2881 public static KeyEvent createFromParcelBody(Parcel in) { 2882 return new KeyEvent(in); 2883 } 2884 KeyEvent(Parcel in)2885 private KeyEvent(Parcel in) { 2886 mDeviceId = in.readInt(); 2887 mSource = in.readInt(); 2888 mAction = in.readInt(); 2889 mKeyCode = in.readInt(); 2890 mRepeatCount = in.readInt(); 2891 mMetaState = in.readInt(); 2892 mScanCode = in.readInt(); 2893 mFlags = in.readInt(); 2894 mDownTime = in.readLong(); 2895 mEventTime = in.readLong(); 2896 } 2897 writeToParcel(Parcel out, int flags)2898 public void writeToParcel(Parcel out, int flags) { 2899 out.writeInt(PARCEL_TOKEN_KEY_EVENT); 2900 2901 out.writeInt(mDeviceId); 2902 out.writeInt(mSource); 2903 out.writeInt(mAction); 2904 out.writeInt(mKeyCode); 2905 out.writeInt(mRepeatCount); 2906 out.writeInt(mMetaState); 2907 out.writeInt(mScanCode); 2908 out.writeInt(mFlags); 2909 out.writeLong(mDownTime); 2910 out.writeLong(mEventTime); 2911 } 2912 native_isSystemKey(int keyCode)2913 private native boolean native_isSystemKey(int keyCode); native_hasDefaultAction(int keyCode)2914 private native boolean native_hasDefaultAction(int keyCode); 2915 } 2916