KeyEvent.java revision c3fc2d03d58a258c53c9265a70143d4af076b764
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.util.Log; 22import android.util.SparseIntArray; 23import android.view.KeyCharacterMap; 24import android.view.KeyCharacterMap.KeyData; 25 26/** 27 * Contains constants for key events. 28 */ 29public class KeyEvent extends InputEvent implements Parcelable { 30 // key codes 31 public static final int KEYCODE_UNKNOWN = 0; 32 public static final int KEYCODE_SOFT_LEFT = 1; 33 public static final int KEYCODE_SOFT_RIGHT = 2; 34 public static final int KEYCODE_HOME = 3; 35 public static final int KEYCODE_BACK = 4; 36 public static final int KEYCODE_CALL = 5; 37 public static final int KEYCODE_ENDCALL = 6; 38 public static final int KEYCODE_0 = 7; 39 public static final int KEYCODE_1 = 8; 40 public static final int KEYCODE_2 = 9; 41 public static final int KEYCODE_3 = 10; 42 public static final int KEYCODE_4 = 11; 43 public static final int KEYCODE_5 = 12; 44 public static final int KEYCODE_6 = 13; 45 public static final int KEYCODE_7 = 14; 46 public static final int KEYCODE_8 = 15; 47 public static final int KEYCODE_9 = 16; 48 public static final int KEYCODE_STAR = 17; 49 public static final int KEYCODE_POUND = 18; 50 public static final int KEYCODE_DPAD_UP = 19; 51 public static final int KEYCODE_DPAD_DOWN = 20; 52 public static final int KEYCODE_DPAD_LEFT = 21; 53 public static final int KEYCODE_DPAD_RIGHT = 22; 54 public static final int KEYCODE_DPAD_CENTER = 23; 55 public static final int KEYCODE_VOLUME_UP = 24; 56 public static final int KEYCODE_VOLUME_DOWN = 25; 57 public static final int KEYCODE_POWER = 26; 58 public static final int KEYCODE_CAMERA = 27; 59 public static final int KEYCODE_CLEAR = 28; 60 public static final int KEYCODE_A = 29; 61 public static final int KEYCODE_B = 30; 62 public static final int KEYCODE_C = 31; 63 public static final int KEYCODE_D = 32; 64 public static final int KEYCODE_E = 33; 65 public static final int KEYCODE_F = 34; 66 public static final int KEYCODE_G = 35; 67 public static final int KEYCODE_H = 36; 68 public static final int KEYCODE_I = 37; 69 public static final int KEYCODE_J = 38; 70 public static final int KEYCODE_K = 39; 71 public static final int KEYCODE_L = 40; 72 public static final int KEYCODE_M = 41; 73 public static final int KEYCODE_N = 42; 74 public static final int KEYCODE_O = 43; 75 public static final int KEYCODE_P = 44; 76 public static final int KEYCODE_Q = 45; 77 public static final int KEYCODE_R = 46; 78 public static final int KEYCODE_S = 47; 79 public static final int KEYCODE_T = 48; 80 public static final int KEYCODE_U = 49; 81 public static final int KEYCODE_V = 50; 82 public static final int KEYCODE_W = 51; 83 public static final int KEYCODE_X = 52; 84 public static final int KEYCODE_Y = 53; 85 public static final int KEYCODE_Z = 54; 86 public static final int KEYCODE_COMMA = 55; 87 public static final int KEYCODE_PERIOD = 56; 88 public static final int KEYCODE_ALT_LEFT = 57; 89 public static final int KEYCODE_ALT_RIGHT = 58; 90 public static final int KEYCODE_SHIFT_LEFT = 59; 91 public static final int KEYCODE_SHIFT_RIGHT = 60; 92 public static final int KEYCODE_TAB = 61; 93 public static final int KEYCODE_SPACE = 62; 94 public static final int KEYCODE_SYM = 63; 95 public static final int KEYCODE_EXPLORER = 64; 96 public static final int KEYCODE_ENVELOPE = 65; 97 public static final int KEYCODE_ENTER = 66; 98 public static final int KEYCODE_DEL = 67; 99 public static final int KEYCODE_GRAVE = 68; 100 public static final int KEYCODE_MINUS = 69; 101 public static final int KEYCODE_EQUALS = 70; 102 public static final int KEYCODE_LEFT_BRACKET = 71; 103 public static final int KEYCODE_RIGHT_BRACKET = 72; 104 public static final int KEYCODE_BACKSLASH = 73; 105 public static final int KEYCODE_SEMICOLON = 74; 106 public static final int KEYCODE_APOSTROPHE = 75; 107 public static final int KEYCODE_SLASH = 76; 108 public static final int KEYCODE_AT = 77; 109 public static final int KEYCODE_NUM = 78; 110 public static final int KEYCODE_HEADSETHOOK = 79; 111 public static final int KEYCODE_FOCUS = 80; // *Camera* focus 112 public static final int KEYCODE_PLUS = 81; 113 public static final int KEYCODE_MENU = 82; 114 public static final int KEYCODE_NOTIFICATION = 83; 115 public static final int KEYCODE_SEARCH = 84; 116 public static final int KEYCODE_MEDIA_PLAY_PAUSE= 85; 117 public static final int KEYCODE_MEDIA_STOP = 86; 118 public static final int KEYCODE_MEDIA_NEXT = 87; 119 public static final int KEYCODE_MEDIA_PREVIOUS = 88; 120 public static final int KEYCODE_MEDIA_REWIND = 89; 121 public static final int KEYCODE_MEDIA_FAST_FORWARD = 90; 122 public static final int KEYCODE_MUTE = 91; 123 public static final int KEYCODE_PAGE_UP = 92; 124 public static final int KEYCODE_PAGE_DOWN = 93; 125 public static final int KEYCODE_PICTSYMBOLS = 94; // switch symbol-sets (Emoji,Kao-moji) 126 public static final int KEYCODE_SWITCH_CHARSET = 95; // switch char-sets (Kanji,Katakana) 127 public static final int KEYCODE_BUTTON_A = 96; 128 public static final int KEYCODE_BUTTON_B = 97; 129 public static final int KEYCODE_BUTTON_C = 98; 130 public static final int KEYCODE_BUTTON_X = 99; 131 public static final int KEYCODE_BUTTON_Y = 100; 132 public static final int KEYCODE_BUTTON_Z = 101; 133 public static final int KEYCODE_BUTTON_L1 = 102; 134 public static final int KEYCODE_BUTTON_R1 = 103; 135 public static final int KEYCODE_BUTTON_L2 = 104; 136 public static final int KEYCODE_BUTTON_R2 = 105; 137 public static final int KEYCODE_BUTTON_THUMBL = 106; 138 public static final int KEYCODE_BUTTON_THUMBR = 107; 139 public static final int KEYCODE_BUTTON_START = 108; 140 public static final int KEYCODE_BUTTON_SELECT = 109; 141 public static final int KEYCODE_BUTTON_MODE = 110; 142 143 // NOTE: If you add a new keycode here you must also add it to: 144 // isSystem() 145 // native/include/android/keycodes.h 146 // frameworks/base/include/ui/KeycodeLabels.h 147 // external/webkit/WebKit/android/plugins/ANPKeyCodes.h 148 // tools/puppet_master/PuppetMaster/nav_keys.py 149 // frameworks/base/core/res/res/values/attrs.xml 150 // commands/monkey/Monkey.java 151 // emulator? 152 // 153 // Also Android currently does not reserve code ranges for vendor- 154 // specific key codes. If you have new key codes to have, you 155 // MUST contribute a patch to the open source project to define 156 // those new codes. This is intended to maintain a consistent 157 // set of key code definitions across all Android devices. 158 159 private static final int LAST_KEYCODE = KEYCODE_BUTTON_MODE; 160 161 /** 162 * @deprecated There are now more than MAX_KEYCODE keycodes. 163 * Use {@link #getMaxKeyCode()} instead. 164 */ 165 @Deprecated 166 public static final int MAX_KEYCODE = 84; 167 168 /** 169 * {@link #getAction} value: the key has been pressed down. 170 */ 171 public static final int ACTION_DOWN = 0; 172 /** 173 * {@link #getAction} value: the key has been released. 174 */ 175 public static final int ACTION_UP = 1; 176 /** 177 * {@link #getAction} value: multiple duplicate key events have 178 * occurred in a row, or a complex string is being delivered. If the 179 * key code is not {#link {@link #KEYCODE_UNKNOWN} then the 180 * {#link {@link #getRepeatCount()} method returns the number of times 181 * the given key code should be executed. 182 * Otherwise, if the key code is {@link #KEYCODE_UNKNOWN}, then 183 * this is a sequence of characters as returned by {@link #getCharacters}. 184 */ 185 public static final int ACTION_MULTIPLE = 2; 186 187 /** 188 * <p>This mask is used to check whether one of the ALT meta keys is pressed.</p> 189 * 190 * @see #isAltPressed() 191 * @see #getMetaState() 192 * @see #KEYCODE_ALT_LEFT 193 * @see #KEYCODE_ALT_RIGHT 194 */ 195 public static final int META_ALT_ON = 0x02; 196 197 /** 198 * <p>This mask is used to check whether the left ALT meta key is pressed.</p> 199 * 200 * @see #isAltPressed() 201 * @see #getMetaState() 202 * @see #KEYCODE_ALT_LEFT 203 */ 204 public static final int META_ALT_LEFT_ON = 0x10; 205 206 /** 207 * <p>This mask is used to check whether the right the ALT meta key is pressed.</p> 208 * 209 * @see #isAltPressed() 210 * @see #getMetaState() 211 * @see #KEYCODE_ALT_RIGHT 212 */ 213 public static final int META_ALT_RIGHT_ON = 0x20; 214 215 /** 216 * <p>This mask is used to check whether one of the SHIFT meta keys is pressed.</p> 217 * 218 * @see #isShiftPressed() 219 * @see #getMetaState() 220 * @see #KEYCODE_SHIFT_LEFT 221 * @see #KEYCODE_SHIFT_RIGHT 222 */ 223 public static final int META_SHIFT_ON = 0x1; 224 225 /** 226 * <p>This mask is used to check whether the left SHIFT meta key is pressed.</p> 227 * 228 * @see #isShiftPressed() 229 * @see #getMetaState() 230 * @see #KEYCODE_SHIFT_LEFT 231 */ 232 public static final int META_SHIFT_LEFT_ON = 0x40; 233 234 /** 235 * <p>This mask is used to check whether the right SHIFT meta key is pressed.</p> 236 * 237 * @see #isShiftPressed() 238 * @see #getMetaState() 239 * @see #KEYCODE_SHIFT_RIGHT 240 */ 241 public static final int META_SHIFT_RIGHT_ON = 0x80; 242 243 /** 244 * <p>This mask is used to check whether the SYM meta key is pressed.</p> 245 * 246 * @see #isSymPressed() 247 * @see #getMetaState() 248 */ 249 public static final int META_SYM_ON = 0x4; 250 251 /** 252 * This mask is set if the device woke because of this key event. 253 */ 254 public static final int FLAG_WOKE_HERE = 0x1; 255 256 /** 257 * This mask is set if the key event was generated by a software keyboard. 258 */ 259 public static final int FLAG_SOFT_KEYBOARD = 0x2; 260 261 /** 262 * This mask is set if we don't want the key event to cause us to leave 263 * touch mode. 264 */ 265 public static final int FLAG_KEEP_TOUCH_MODE = 0x4; 266 267 /** 268 * This mask is set if an event was known to come from a trusted part 269 * of the system. That is, the event is known to come from the user, 270 * and could not have been spoofed by a third party component. 271 */ 272 public static final int FLAG_FROM_SYSTEM = 0x8; 273 274 /** 275 * This mask is used for compatibility, to identify enter keys that are 276 * coming from an IME whose enter key has been auto-labelled "next" or 277 * "done". This allows TextView to dispatch these as normal enter keys 278 * for old applications, but still do the appropriate action when 279 * receiving them. 280 */ 281 public static final int FLAG_EDITOR_ACTION = 0x10; 282 283 /** 284 * When associated with up key events, this indicates that the key press 285 * has been canceled. Typically this is used with virtual touch screen 286 * keys, where the user can slide from the virtual key area on to the 287 * display: in that case, the application will receive a canceled up 288 * event and should not perform the action normally associated with the 289 * key. Note that for this to work, the application can not perform an 290 * action for a key until it receives an up or the long press timeout has 291 * expired. 292 */ 293 public static final int FLAG_CANCELED = 0x20; 294 295 /** 296 * This key event was generated by a virtual (on-screen) hard key area. 297 * Typically this is an area of the touchscreen, outside of the regular 298 * display, dedicated to "hardware" buttons. 299 */ 300 public static final int FLAG_VIRTUAL_HARD_KEY = 0x40; 301 302 /** 303 * This flag is set for the first key repeat that occurs after the 304 * long press timeout. 305 */ 306 public static final int FLAG_LONG_PRESS = 0x80; 307 308 /** 309 * Set when a key event has {@link #FLAG_CANCELED} set because a long 310 * press action was executed while it was down. 311 */ 312 public static final int FLAG_CANCELED_LONG_PRESS = 0x100; 313 314 /** 315 * Set for {@link #ACTION_UP} when this event's key code is still being 316 * tracked from its initial down. That is, somebody requested that tracking 317 * started on the key down and a long press has not caused 318 * the tracking to be canceled. 319 */ 320 public static final int FLAG_TRACKING = 0x200; 321 322 /** 323 * Private control to determine when an app is tracking a key sequence. 324 * @hide 325 */ 326 public static final int FLAG_START_TRACKING = 0x40000000; 327 328 /** 329 * Returns the maximum keycode. 330 */ 331 public static int getMaxKeyCode() { 332 return LAST_KEYCODE; 333 } 334 335 /** 336 * Get the character that is produced by putting accent on the character 337 * c. 338 * For example, getDeadChar('`', 'e') returns è. 339 */ 340 public static int getDeadChar(int accent, int c) { 341 return KeyCharacterMap.getDeadChar(accent, c); 342 } 343 344 static final boolean DEBUG = false; 345 static final String TAG = "KeyEvent"; 346 347 private int mMetaState; 348 private int mAction; 349 private int mKeyCode; 350 private int mScanCode; 351 private int mRepeatCount; 352 private int mFlags; 353 private long mDownTime; 354 private long mEventTime; 355 private String mCharacters; 356 357 public interface Callback { 358 /** 359 * Called when a key down event has occurred. If you return true, 360 * you can first call {@link KeyEvent#startTracking() 361 * KeyEvent.startTracking()} to have the framework track the event 362 * through its {@link #onKeyUp(int, KeyEvent)} and also call your 363 * {@link #onKeyLongPress(int, KeyEvent)} if it occurs. 364 * 365 * @param keyCode The value in event.getKeyCode(). 366 * @param event Description of the key event. 367 * 368 * @return If you handled the event, return true. If you want to allow 369 * the event to be handled by the next receiver, return false. 370 */ 371 boolean onKeyDown(int keyCode, KeyEvent event); 372 373 /** 374 * Called when a long press has occurred. If you return true, 375 * the final key up will have {@link KeyEvent#FLAG_CANCELED} and 376 * {@link KeyEvent#FLAG_CANCELED_LONG_PRESS} set. Note that in 377 * order to receive this callback, someone in the event change 378 * <em>must</em> return true from {@link #onKeyDown} <em>and</em> 379 * call {@link KeyEvent#startTracking()} on the event. 380 * 381 * @param keyCode The value in event.getKeyCode(). 382 * @param event Description of the key event. 383 * 384 * @return If you handled the event, return true. If you want to allow 385 * the event to be handled by the next receiver, return false. 386 */ 387 boolean onKeyLongPress(int keyCode, KeyEvent event); 388 389 /** 390 * Called when a key up event has occurred. 391 * 392 * @param keyCode The value in event.getKeyCode(). 393 * @param event Description of the key event. 394 * 395 * @return If you handled the event, return true. If you want to allow 396 * the event to be handled by the next receiver, return false. 397 */ 398 boolean onKeyUp(int keyCode, KeyEvent event); 399 400 /** 401 * Called when multiple down/up pairs of the same key have occurred 402 * in a row. 403 * 404 * @param keyCode The value in event.getKeyCode(). 405 * @param count Number of pairs as returned by event.getRepeatCount(). 406 * @param event Description of the key event. 407 * 408 * @return If you handled the event, return true. If you want to allow 409 * the event to be handled by the next receiver, return false. 410 */ 411 boolean onKeyMultiple(int keyCode, int count, KeyEvent event); 412 } 413 414 /** 415 * Create a new key event. 416 * 417 * @param action Action code: either {@link #ACTION_DOWN}, 418 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 419 * @param code The key code. 420 */ 421 public KeyEvent(int action, int code) { 422 mAction = action; 423 mKeyCode = code; 424 mRepeatCount = 0; 425 } 426 427 /** 428 * Create a new key event. 429 * 430 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) 431 * at which this key code originally went down. 432 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) 433 * at which this event happened. 434 * @param action Action code: either {@link #ACTION_DOWN}, 435 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 436 * @param code The key code. 437 * @param repeat A repeat count for down events (> 0 if this is after the 438 * initial down) or event count for multiple events. 439 */ 440 public KeyEvent(long downTime, long eventTime, int action, 441 int code, int repeat) { 442 mDownTime = downTime; 443 mEventTime = eventTime; 444 mAction = action; 445 mKeyCode = code; 446 mRepeatCount = repeat; 447 } 448 449 /** 450 * Create a new key event. 451 * 452 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) 453 * at which this key code originally went down. 454 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) 455 * at which this event happened. 456 * @param action Action code: either {@link #ACTION_DOWN}, 457 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 458 * @param code The key code. 459 * @param repeat A repeat count for down events (> 0 if this is after the 460 * initial down) or event count for multiple events. 461 * @param metaState Flags indicating which meta keys are currently pressed. 462 */ 463 public KeyEvent(long downTime, long eventTime, int action, 464 int code, int repeat, int metaState) { 465 mDownTime = downTime; 466 mEventTime = eventTime; 467 mAction = action; 468 mKeyCode = code; 469 mRepeatCount = repeat; 470 mMetaState = metaState; 471 } 472 473 /** 474 * Create a new key event. 475 * 476 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) 477 * at which this key code originally went down. 478 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) 479 * at which this event happened. 480 * @param action Action code: either {@link #ACTION_DOWN}, 481 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 482 * @param code The key code. 483 * @param repeat A repeat count for down events (> 0 if this is after the 484 * initial down) or event count for multiple events. 485 * @param metaState Flags indicating which meta keys are currently pressed. 486 * @param deviceId The device ID that generated the key event. 487 * @param scancode Raw device scan code of the event. 488 */ 489 public KeyEvent(long downTime, long eventTime, int action, 490 int code, int repeat, int metaState, 491 int deviceId, int scancode) { 492 mDownTime = downTime; 493 mEventTime = eventTime; 494 mAction = action; 495 mKeyCode = code; 496 mRepeatCount = repeat; 497 mMetaState = metaState; 498 mDeviceId = deviceId; 499 mScanCode = scancode; 500 } 501 502 /** 503 * Create a new key event. 504 * 505 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) 506 * at which this key code originally went down. 507 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) 508 * at which this event happened. 509 * @param action Action code: either {@link #ACTION_DOWN}, 510 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 511 * @param code The key code. 512 * @param repeat A repeat count for down events (> 0 if this is after the 513 * initial down) or event count for multiple events. 514 * @param metaState Flags indicating which meta keys are currently pressed. 515 * @param deviceId The device ID that generated the key event. 516 * @param scancode Raw device scan code of the event. 517 * @param flags The flags for this key event 518 */ 519 public KeyEvent(long downTime, long eventTime, int action, 520 int code, int repeat, int metaState, 521 int deviceId, int scancode, int flags) { 522 mDownTime = downTime; 523 mEventTime = eventTime; 524 mAction = action; 525 mKeyCode = code; 526 mRepeatCount = repeat; 527 mMetaState = metaState; 528 mDeviceId = deviceId; 529 mScanCode = scancode; 530 mFlags = flags; 531 } 532 533 /** 534 * Create a new key event. 535 * 536 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) 537 * at which this key code originally went down. 538 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) 539 * at which this event happened. 540 * @param action Action code: either {@link #ACTION_DOWN}, 541 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 542 * @param code The key code. 543 * @param repeat A repeat count for down events (> 0 if this is after the 544 * initial down) or event count for multiple events. 545 * @param metaState Flags indicating which meta keys are currently pressed. 546 * @param deviceId The device ID that generated the key event. 547 * @param scancode Raw device scan code of the event. 548 * @param flags The flags for this key event 549 * @param source The input source such as {@link InputDevice#SOURCE_KEYBOARD}. 550 */ 551 public KeyEvent(long downTime, long eventTime, int action, 552 int code, int repeat, int metaState, 553 int deviceId, int scancode, int flags, int source) { 554 mDownTime = downTime; 555 mEventTime = eventTime; 556 mAction = action; 557 mKeyCode = code; 558 mRepeatCount = repeat; 559 mMetaState = metaState; 560 mDeviceId = deviceId; 561 mScanCode = scancode; 562 mFlags = flags; 563 mSource = source; 564 } 565 566 /** 567 * Create a new key event for a string of characters. The key code, 568 * action, repeat count and source will automatically be set to 569 * {@link #KEYCODE_UNKNOWN}, {@link #ACTION_MULTIPLE}, 0, and 570 * {@link InputDevice#SOURCE_KEYBOARD} for you. 571 * 572 * @param time The time (in {@link android.os.SystemClock#uptimeMillis}) 573 * at which this event occured. 574 * @param characters The string of characters. 575 * @param deviceId The device ID that generated the key event. 576 * @param flags The flags for this key event 577 */ 578 public KeyEvent(long time, String characters, int deviceId, int flags) { 579 mDownTime = time; 580 mEventTime = time; 581 mCharacters = characters; 582 mAction = ACTION_MULTIPLE; 583 mKeyCode = KEYCODE_UNKNOWN; 584 mRepeatCount = 0; 585 mDeviceId = deviceId; 586 mFlags = flags; 587 mSource = InputDevice.SOURCE_KEYBOARD; 588 } 589 590 /** 591 * Make an exact copy of an existing key event. 592 */ 593 public KeyEvent(KeyEvent origEvent) { 594 mDownTime = origEvent.mDownTime; 595 mEventTime = origEvent.mEventTime; 596 mAction = origEvent.mAction; 597 mKeyCode = origEvent.mKeyCode; 598 mRepeatCount = origEvent.mRepeatCount; 599 mMetaState = origEvent.mMetaState; 600 mDeviceId = origEvent.mDeviceId; 601 mSource = origEvent.mSource; 602 mScanCode = origEvent.mScanCode; 603 mFlags = origEvent.mFlags; 604 mCharacters = origEvent.mCharacters; 605 } 606 607 /** 608 * Copy an existing key event, modifying its time and repeat count. 609 * 610 * @deprecated Use {@link #changeTimeRepeat(KeyEvent, long, int)} 611 * instead. 612 * 613 * @param origEvent The existing event to be copied. 614 * @param eventTime The new event time 615 * (in {@link android.os.SystemClock#uptimeMillis}) of the event. 616 * @param newRepeat The new repeat count of the event. 617 */ 618 @Deprecated 619 public KeyEvent(KeyEvent origEvent, long eventTime, int newRepeat) { 620 mDownTime = origEvent.mDownTime; 621 mEventTime = eventTime; 622 mAction = origEvent.mAction; 623 mKeyCode = origEvent.mKeyCode; 624 mRepeatCount = newRepeat; 625 mMetaState = origEvent.mMetaState; 626 mDeviceId = origEvent.mDeviceId; 627 mSource = origEvent.mSource; 628 mScanCode = origEvent.mScanCode; 629 mFlags = origEvent.mFlags; 630 mCharacters = origEvent.mCharacters; 631 } 632 633 /** 634 * Create a new key event that is the same as the given one, but whose 635 * event time and repeat count are replaced with the given value. 636 * 637 * @param event The existing event to be copied. This is not modified. 638 * @param eventTime The new event time 639 * (in {@link android.os.SystemClock#uptimeMillis}) of the event. 640 * @param newRepeat The new repeat count of the event. 641 */ 642 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime, 643 int newRepeat) { 644 return new KeyEvent(event, eventTime, newRepeat); 645 } 646 647 /** 648 * Create a new key event that is the same as the given one, but whose 649 * event time and repeat count are replaced with the given value. 650 * 651 * @param event The existing event to be copied. This is not modified. 652 * @param eventTime The new event time 653 * (in {@link android.os.SystemClock#uptimeMillis}) of the event. 654 * @param newRepeat The new repeat count of the event. 655 * @param newFlags New flags for the event, replacing the entire value 656 * in the original event. 657 */ 658 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime, 659 int newRepeat, int newFlags) { 660 KeyEvent ret = new KeyEvent(event); 661 ret.mEventTime = eventTime; 662 ret.mRepeatCount = newRepeat; 663 ret.mFlags = newFlags; 664 return ret; 665 } 666 667 /** 668 * Copy an existing key event, modifying its action. 669 * 670 * @param origEvent The existing event to be copied. 671 * @param action The new action code of the event. 672 */ 673 private KeyEvent(KeyEvent origEvent, int action) { 674 mDownTime = origEvent.mDownTime; 675 mEventTime = origEvent.mEventTime; 676 mAction = action; 677 mKeyCode = origEvent.mKeyCode; 678 mRepeatCount = origEvent.mRepeatCount; 679 mMetaState = origEvent.mMetaState; 680 mDeviceId = origEvent.mDeviceId; 681 mSource = origEvent.mSource; 682 mScanCode = origEvent.mScanCode; 683 mFlags = origEvent.mFlags; 684 // Don't copy mCharacters, since one way or the other we'll lose it 685 // when changing the action. 686 } 687 688 /** 689 * Create a new key event that is the same as the given one, but whose 690 * action is replaced with the given value. 691 * 692 * @param event The existing event to be copied. This is not modified. 693 * @param action The new action code of the event. 694 */ 695 public static KeyEvent changeAction(KeyEvent event, int action) { 696 return new KeyEvent(event, action); 697 } 698 699 /** 700 * Create a new key event that is the same as the given one, but whose 701 * flags are replaced with the given value. 702 * 703 * @param event The existing event to be copied. This is not modified. 704 * @param flags The new flags constant. 705 */ 706 public static KeyEvent changeFlags(KeyEvent event, int flags) { 707 event = new KeyEvent(event); 708 event.mFlags = flags; 709 return event; 710 } 711 712 /** 713 * Don't use in new code, instead explicitly check 714 * {@link #getAction()}. 715 * 716 * @return If the action is ACTION_DOWN, returns true; else false. 717 * 718 * @deprecated 719 * @hide 720 */ 721 @Deprecated public final boolean isDown() { 722 return mAction == ACTION_DOWN; 723 } 724 725 /** 726 * Is this a system key? System keys can not be used for menu shortcuts. 727 * 728 * TODO: this information should come from a table somewhere. 729 * TODO: should the dpad keys be here? arguably, because they also shouldn't be menu shortcuts 730 */ 731 public final boolean isSystem() { 732 return native_isSystemKey(mKeyCode); 733 } 734 735 /** @hide */ 736 public final boolean hasDefaultAction() { 737 return native_hasDefaultAction(mKeyCode); 738 } 739 740 741 /** 742 * <p>Returns the state of the meta keys.</p> 743 * 744 * @return an integer in which each bit set to 1 represents a pressed 745 * meta key 746 * 747 * @see #isAltPressed() 748 * @see #isShiftPressed() 749 * @see #isSymPressed() 750 * @see #META_ALT_ON 751 * @see #META_SHIFT_ON 752 * @see #META_SYM_ON 753 */ 754 public final int getMetaState() { 755 return mMetaState; 756 } 757 758 /** 759 * Returns the flags for this key event. 760 * 761 * @see #FLAG_WOKE_HERE 762 */ 763 public final int getFlags() { 764 return mFlags; 765 } 766 767 /** 768 * Returns true if this key code is a modifier key. 769 * 770 * @return whether the provided keyCode is one of 771 * {@link #KEYCODE_SHIFT_LEFT} {@link #KEYCODE_SHIFT_RIGHT}, 772 * {@link #KEYCODE_ALT_LEFT}, {@link #KEYCODE_ALT_RIGHT} 773 * or {@link #KEYCODE_SYM}. 774 */ 775 public static boolean isModifierKey(int keyCode) { 776 return keyCode == KEYCODE_SHIFT_LEFT || keyCode == KEYCODE_SHIFT_RIGHT 777 || keyCode == KEYCODE_ALT_LEFT || keyCode == KEYCODE_ALT_RIGHT 778 || keyCode == KEYCODE_SYM; 779 } 780 781 /** 782 * <p>Returns the pressed state of the ALT meta key.</p> 783 * 784 * @return true if the ALT key is pressed, false otherwise 785 * 786 * @see #KEYCODE_ALT_LEFT 787 * @see #KEYCODE_ALT_RIGHT 788 * @see #META_ALT_ON 789 */ 790 public final boolean isAltPressed() { 791 return (mMetaState & META_ALT_ON) != 0; 792 } 793 794 /** 795 * <p>Returns the pressed state of the SHIFT meta key.</p> 796 * 797 * @return true if the SHIFT key is pressed, false otherwise 798 * 799 * @see #KEYCODE_SHIFT_LEFT 800 * @see #KEYCODE_SHIFT_RIGHT 801 * @see #META_SHIFT_ON 802 */ 803 public final boolean isShiftPressed() { 804 return (mMetaState & META_SHIFT_ON) != 0; 805 } 806 807 /** 808 * <p>Returns the pressed state of the SYM meta key.</p> 809 * 810 * @return true if the SYM key is pressed, false otherwise 811 * 812 * @see #KEYCODE_SYM 813 * @see #META_SYM_ON 814 */ 815 public final boolean isSymPressed() { 816 return (mMetaState & META_SYM_ON) != 0; 817 } 818 819 /** 820 * Retrieve the action of this key event. May be either 821 * {@link #ACTION_DOWN}, {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 822 * 823 * @return The event action: ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE. 824 */ 825 public final int getAction() { 826 return mAction; 827 } 828 829 /** 830 * For {@link #ACTION_UP} events, indicates that the event has been 831 * canceled as per {@link #FLAG_CANCELED}. 832 */ 833 public final boolean isCanceled() { 834 return (mFlags&FLAG_CANCELED) != 0; 835 } 836 837 /** 838 * Call this during {@link Callback#onKeyDown} to have the system track 839 * the key through its final up (possibly including a long press). Note 840 * that only one key can be tracked at a time -- if another key down 841 * event is received while a previous one is being tracked, tracking is 842 * stopped on the previous event. 843 */ 844 public final void startTracking() { 845 mFlags |= FLAG_START_TRACKING; 846 } 847 848 /** 849 * For {@link #ACTION_UP} events, indicates that the event is still being 850 * tracked from its initial down event as per 851 * {@link #FLAG_TRACKING}. 852 */ 853 public final boolean isTracking() { 854 return (mFlags&FLAG_TRACKING) != 0; 855 } 856 857 /** 858 * For {@link #ACTION_DOWN} events, indicates that the event has been 859 * canceled as per {@link #FLAG_LONG_PRESS}. 860 */ 861 public final boolean isLongPress() { 862 return (mFlags&FLAG_LONG_PRESS) != 0; 863 } 864 865 /** 866 * Retrieve the key code of the key event. This is the physical key that 867 * was pressed, <em>not</em> the Unicode character. 868 * 869 * @return The key code of the event. 870 */ 871 public final int getKeyCode() { 872 return mKeyCode; 873 } 874 875 /** 876 * For the special case of a {@link #ACTION_MULTIPLE} event with key 877 * code of {@link #KEYCODE_UNKNOWN}, this is a raw string of characters 878 * associated with the event. In all other cases it is null. 879 * 880 * @return Returns a String of 1 or more characters associated with 881 * the event. 882 */ 883 public final String getCharacters() { 884 return mCharacters; 885 } 886 887 /** 888 * Retrieve the hardware key id of this key event. These values are not 889 * reliable and vary from device to device. 890 * 891 * {@more} 892 * Mostly this is here for debugging purposes. 893 */ 894 public final int getScanCode() { 895 return mScanCode; 896 } 897 898 /** 899 * Retrieve the repeat count of the event. For both key up and key down 900 * events, this is the number of times the key has repeated with the first 901 * down starting at 0 and counting up from there. For multiple key 902 * events, this is the number of down/up pairs that have occurred. 903 * 904 * @return The number of times the key has repeated. 905 */ 906 public final int getRepeatCount() { 907 return mRepeatCount; 908 } 909 910 /** 911 * Retrieve the time of the most recent key down event, 912 * in the {@link android.os.SystemClock#uptimeMillis} time base. If this 913 * is a down event, this will be the same as {@link #getEventTime()}. 914 * Note that when chording keys, this value is the down time of the 915 * most recently pressed key, which may <em>not</em> be the same physical 916 * key of this event. 917 * 918 * @return Returns the most recent key down time, in the 919 * {@link android.os.SystemClock#uptimeMillis} time base 920 */ 921 public final long getDownTime() { 922 return mDownTime; 923 } 924 925 /** 926 * Retrieve the time this event occurred, 927 * in the {@link android.os.SystemClock#uptimeMillis} time base. 928 * 929 * @return Returns the time this event occurred, 930 * in the {@link android.os.SystemClock#uptimeMillis} time base. 931 */ 932 public final long getEventTime() { 933 return mEventTime; 934 } 935 936 /** 937 * Renamed to {@link #getDeviceId}. 938 * 939 * @hide 940 * @deprecated 941 */ 942 public final int getKeyboardDevice() { 943 return mDeviceId; 944 } 945 946 /** 947 * Get the primary character for this key. In other words, the label 948 * that is physically printed on it. 949 */ 950 public char getDisplayLabel() { 951 return KeyCharacterMap.load(mDeviceId).getDisplayLabel(mKeyCode); 952 } 953 954 /** 955 * <p> 956 * Returns the Unicode character that the key would produce. 957 * </p><p> 958 * Returns 0 if the key is not one that is used to type Unicode 959 * characters. 960 * </p><p> 961 * If the return value has bit 962 * {@link KeyCharacterMap#COMBINING_ACCENT} 963 * set, the key is a "dead key" that should be combined with another to 964 * actually produce a character -- see {@link #getDeadChar} -- 965 * after masking with 966 * {@link KeyCharacterMap#COMBINING_ACCENT_MASK}. 967 * </p> 968 */ 969 public int getUnicodeChar() { 970 return getUnicodeChar(mMetaState); 971 } 972 973 /** 974 * <p> 975 * Returns the Unicode character that the key would produce. 976 * </p><p> 977 * Returns 0 if the key is not one that is used to type Unicode 978 * characters. 979 * </p><p> 980 * If the return value has bit 981 * {@link KeyCharacterMap#COMBINING_ACCENT} 982 * set, the key is a "dead key" that should be combined with another to 983 * actually produce a character -- see {@link #getDeadChar} -- after masking 984 * with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}. 985 * </p> 986 */ 987 public int getUnicodeChar(int meta) { 988 return KeyCharacterMap.load(mDeviceId).get(mKeyCode, meta); 989 } 990 991 /** 992 * Get the characters conversion data for the key event.. 993 * 994 * @param results a {@link KeyData} that will be filled with the results. 995 * 996 * @return whether the key was mapped or not. If the key was not mapped, 997 * results is not modified. 998 */ 999 public boolean getKeyData(KeyData results) { 1000 return KeyCharacterMap.load(mDeviceId).getKeyData(mKeyCode, results); 1001 } 1002 1003 /** 1004 * The same as {@link #getMatch(char[],int) getMatch(chars, 0)}. 1005 */ 1006 public char getMatch(char[] chars) { 1007 return getMatch(chars, 0); 1008 } 1009 1010 /** 1011 * If one of the chars in the array can be generated by the keyCode of this 1012 * key event, return the char; otherwise return '\0'. 1013 * @param chars the characters to try to find 1014 * @param modifiers the modifier bits to prefer. If any of these bits 1015 * are set, if there are multiple choices, that could 1016 * work, the one for this modifier will be set. 1017 */ 1018 public char getMatch(char[] chars, int modifiers) { 1019 return KeyCharacterMap.load(mDeviceId).getMatch(mKeyCode, chars, modifiers); 1020 } 1021 1022 /** 1023 * Gets the number or symbol associated with the key. The character value 1024 * is returned, not the numeric value. If the key is not a number, but is 1025 * a symbol, the symbol is retuned. 1026 */ 1027 public char getNumber() { 1028 return KeyCharacterMap.load(mDeviceId).getNumber(mKeyCode); 1029 } 1030 1031 /** 1032 * Does the key code of this key produce a glyph? 1033 */ 1034 public boolean isPrintingKey() { 1035 return KeyCharacterMap.load(mDeviceId).isPrintingKey(mKeyCode); 1036 } 1037 1038 /** 1039 * @deprecated Use {@link #dispatch(Callback, DispatcherState, Object)} instead. 1040 */ 1041 @Deprecated 1042 public final boolean dispatch(Callback receiver) { 1043 return dispatch(receiver, null, null); 1044 } 1045 1046 /** 1047 * Deliver this key event to a {@link Callback} interface. If this is 1048 * an ACTION_MULTIPLE event and it is not handled, then an attempt will 1049 * be made to deliver a single normal event. 1050 * 1051 * @param receiver The Callback that will be given the event. 1052 * @param state State information retained across events. 1053 * @param target The target of the dispatch, for use in tracking. 1054 * 1055 * @return The return value from the Callback method that was called. 1056 */ 1057 public final boolean dispatch(Callback receiver, DispatcherState state, 1058 Object target) { 1059 switch (mAction) { 1060 case ACTION_DOWN: { 1061 mFlags &= ~FLAG_START_TRACKING; 1062 if (DEBUG) Log.v(TAG, "Key down to " + target + " in " + state 1063 + ": " + this); 1064 boolean res = receiver.onKeyDown(mKeyCode, this); 1065 if (state != null) { 1066 if (res && mRepeatCount == 0 && (mFlags&FLAG_START_TRACKING) != 0) { 1067 if (DEBUG) Log.v(TAG, " Start tracking!"); 1068 state.startTracking(this, target); 1069 } else if (isLongPress() && state.isTracking(this)) { 1070 try { 1071 if (receiver.onKeyLongPress(mKeyCode, this)) { 1072 if (DEBUG) Log.v(TAG, " Clear from long press!"); 1073 state.performedLongPress(this); 1074 res = true; 1075 } 1076 } catch (AbstractMethodError e) { 1077 } 1078 } 1079 } 1080 return res; 1081 } 1082 case ACTION_UP: 1083 if (DEBUG) Log.v(TAG, "Key up to " + target + " in " + state 1084 + ": " + this); 1085 if (state != null) { 1086 state.handleUpEvent(this); 1087 } 1088 return receiver.onKeyUp(mKeyCode, this); 1089 case ACTION_MULTIPLE: 1090 final int count = mRepeatCount; 1091 final int code = mKeyCode; 1092 if (receiver.onKeyMultiple(code, count, this)) { 1093 return true; 1094 } 1095 if (code != KeyEvent.KEYCODE_UNKNOWN) { 1096 mAction = ACTION_DOWN; 1097 mRepeatCount = 0; 1098 boolean handled = receiver.onKeyDown(code, this); 1099 if (handled) { 1100 mAction = ACTION_UP; 1101 receiver.onKeyUp(code, this); 1102 } 1103 mAction = ACTION_MULTIPLE; 1104 mRepeatCount = count; 1105 return handled; 1106 } 1107 return false; 1108 } 1109 return false; 1110 } 1111 1112 /** 1113 * Use with {@link KeyEvent#dispatch(Callback, DispatcherState, Object)} 1114 * for more advanced key dispatching, such as long presses. 1115 */ 1116 public static class DispatcherState { 1117 int mDownKeyCode; 1118 Object mDownTarget; 1119 SparseIntArray mActiveLongPresses = new SparseIntArray(); 1120 1121 /** 1122 * Reset back to initial state. 1123 */ 1124 public void reset() { 1125 if (DEBUG) Log.v(TAG, "Reset: " + this); 1126 mDownKeyCode = 0; 1127 mDownTarget = null; 1128 mActiveLongPresses.clear(); 1129 } 1130 1131 /** 1132 * Stop any tracking associated with this target. 1133 */ 1134 public void reset(Object target) { 1135 if (mDownTarget == target) { 1136 if (DEBUG) Log.v(TAG, "Reset in " + target + ": " + this); 1137 mDownKeyCode = 0; 1138 mDownTarget = null; 1139 } 1140 } 1141 1142 /** 1143 * Start tracking the key code associated with the given event. This 1144 * can only be called on a key down. It will allow you to see any 1145 * long press associated with the key, and will result in 1146 * {@link KeyEvent#isTracking} return true on the long press and up 1147 * events. 1148 * 1149 * <p>This is only needed if you are directly dispatching events, rather 1150 * than handling them in {@link Callback#onKeyDown}. 1151 */ 1152 public void startTracking(KeyEvent event, Object target) { 1153 if (event.getAction() != ACTION_DOWN) { 1154 throw new IllegalArgumentException( 1155 "Can only start tracking on a down event"); 1156 } 1157 if (DEBUG) Log.v(TAG, "Start trackingt in " + target + ": " + this); 1158 mDownKeyCode = event.getKeyCode(); 1159 mDownTarget = target; 1160 } 1161 1162 /** 1163 * Return true if the key event is for a key code that is currently 1164 * being tracked by the dispatcher. 1165 */ 1166 public boolean isTracking(KeyEvent event) { 1167 return mDownKeyCode == event.getKeyCode(); 1168 } 1169 1170 /** 1171 * Keep track of the given event's key code as having performed an 1172 * action with a long press, so no action should occur on the up. 1173 * <p>This is only needed if you are directly dispatching events, rather 1174 * than handling them in {@link Callback#onKeyLongPress}. 1175 */ 1176 public void performedLongPress(KeyEvent event) { 1177 mActiveLongPresses.put(event.getKeyCode(), 1); 1178 } 1179 1180 /** 1181 * Handle key up event to stop tracking. This resets the dispatcher state, 1182 * and updates the key event state based on it. 1183 * <p>This is only needed if you are directly dispatching events, rather 1184 * than handling them in {@link Callback#onKeyUp}. 1185 */ 1186 public void handleUpEvent(KeyEvent event) { 1187 final int keyCode = event.getKeyCode(); 1188 if (DEBUG) Log.v(TAG, "Handle key up " + event + ": " + this); 1189 int index = mActiveLongPresses.indexOfKey(keyCode); 1190 if (index >= 0) { 1191 if (DEBUG) Log.v(TAG, " Index: " + index); 1192 event.mFlags |= FLAG_CANCELED | FLAG_CANCELED_LONG_PRESS; 1193 mActiveLongPresses.removeAt(index); 1194 } 1195 if (mDownKeyCode == keyCode) { 1196 if (DEBUG) Log.v(TAG, " Tracking!"); 1197 event.mFlags |= FLAG_TRACKING; 1198 mDownKeyCode = 0; 1199 mDownTarget = null; 1200 } 1201 } 1202 } 1203 1204 public String toString() { 1205 return "KeyEvent{action=" + mAction + " code=" + mKeyCode 1206 + " repeat=" + mRepeatCount 1207 + " meta=" + mMetaState + " scancode=" + mScanCode 1208 + " mFlags=" + mFlags + "}"; 1209 } 1210 1211 public static final Parcelable.Creator<KeyEvent> CREATOR 1212 = new Parcelable.Creator<KeyEvent>() { 1213 public KeyEvent createFromParcel(Parcel in) { 1214 in.readInt(); // skip token, we already know this is a KeyEvent 1215 return KeyEvent.createFromParcelBody(in); 1216 } 1217 1218 public KeyEvent[] newArray(int size) { 1219 return new KeyEvent[size]; 1220 } 1221 }; 1222 1223 /** @hide */ 1224 public static KeyEvent createFromParcelBody(Parcel in) { 1225 return new KeyEvent(in); 1226 } 1227 1228 private KeyEvent(Parcel in) { 1229 readBaseFromParcel(in); 1230 1231 mAction = in.readInt(); 1232 mKeyCode = in.readInt(); 1233 mRepeatCount = in.readInt(); 1234 mMetaState = in.readInt(); 1235 mScanCode = in.readInt(); 1236 mFlags = in.readInt(); 1237 mDownTime = in.readLong(); 1238 mEventTime = in.readLong(); 1239 } 1240 1241 public void writeToParcel(Parcel out, int flags) { 1242 out.writeInt(PARCEL_TOKEN_KEY_EVENT); 1243 1244 writeBaseToParcel(out); 1245 1246 out.writeInt(mAction); 1247 out.writeInt(mKeyCode); 1248 out.writeInt(mRepeatCount); 1249 out.writeInt(mMetaState); 1250 out.writeInt(mScanCode); 1251 out.writeInt(mFlags); 1252 out.writeLong(mDownTime); 1253 out.writeLong(mEventTime); 1254 } 1255 1256 private native boolean native_isSystemKey(int keyCode); 1257 private native boolean native_hasDefaultAction(int keyCode); 1258} 1259