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