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