The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.view; |
| 18 | |
| 19 | import android.os.Parcel; |
| 20 | import android.os.Parcelable; |
| 21 | import android.view.KeyCharacterMap; |
| 22 | import android.view.KeyCharacterMap.KeyData; |
| 23 | |
| 24 | /** |
| 25 | * Contains constants for key events. |
| 26 | */ |
| 27 | public class KeyEvent implements Parcelable { |
| 28 | // key codes |
| 29 | public static final int KEYCODE_UNKNOWN = 0; |
| 30 | public static final int KEYCODE_SOFT_LEFT = 1; |
| 31 | public static final int KEYCODE_SOFT_RIGHT = 2; |
| 32 | public static final int KEYCODE_HOME = 3; |
| 33 | public static final int KEYCODE_BACK = 4; |
| 34 | public static final int KEYCODE_CALL = 5; |
| 35 | public static final int KEYCODE_ENDCALL = 6; |
| 36 | public static final int KEYCODE_0 = 7; |
| 37 | public static final int KEYCODE_1 = 8; |
| 38 | public static final int KEYCODE_2 = 9; |
| 39 | public static final int KEYCODE_3 = 10; |
| 40 | public static final int KEYCODE_4 = 11; |
| 41 | public static final int KEYCODE_5 = 12; |
| 42 | public static final int KEYCODE_6 = 13; |
| 43 | public static final int KEYCODE_7 = 14; |
| 44 | public static final int KEYCODE_8 = 15; |
| 45 | public static final int KEYCODE_9 = 16; |
| 46 | public static final int KEYCODE_STAR = 17; |
| 47 | public static final int KEYCODE_POUND = 18; |
| 48 | public static final int KEYCODE_DPAD_UP = 19; |
| 49 | public static final int KEYCODE_DPAD_DOWN = 20; |
| 50 | public static final int KEYCODE_DPAD_LEFT = 21; |
| 51 | public static final int KEYCODE_DPAD_RIGHT = 22; |
| 52 | public static final int KEYCODE_DPAD_CENTER = 23; |
| 53 | public static final int KEYCODE_VOLUME_UP = 24; |
| 54 | public static final int KEYCODE_VOLUME_DOWN = 25; |
| 55 | public static final int KEYCODE_POWER = 26; |
| 56 | public static final int KEYCODE_CAMERA = 27; |
| 57 | public static final int KEYCODE_CLEAR = 28; |
| 58 | public static final int KEYCODE_A = 29; |
| 59 | public static final int KEYCODE_B = 30; |
| 60 | public static final int KEYCODE_C = 31; |
| 61 | public static final int KEYCODE_D = 32; |
| 62 | public static final int KEYCODE_E = 33; |
| 63 | public static final int KEYCODE_F = 34; |
| 64 | public static final int KEYCODE_G = 35; |
| 65 | public static final int KEYCODE_H = 36; |
| 66 | public static final int KEYCODE_I = 37; |
| 67 | public static final int KEYCODE_J = 38; |
| 68 | public static final int KEYCODE_K = 39; |
| 69 | public static final int KEYCODE_L = 40; |
| 70 | public static final int KEYCODE_M = 41; |
| 71 | public static final int KEYCODE_N = 42; |
| 72 | public static final int KEYCODE_O = 43; |
| 73 | public static final int KEYCODE_P = 44; |
| 74 | public static final int KEYCODE_Q = 45; |
| 75 | public static final int KEYCODE_R = 46; |
| 76 | public static final int KEYCODE_S = 47; |
| 77 | public static final int KEYCODE_T = 48; |
| 78 | public static final int KEYCODE_U = 49; |
| 79 | public static final int KEYCODE_V = 50; |
| 80 | public static final int KEYCODE_W = 51; |
| 81 | public static final int KEYCODE_X = 52; |
| 82 | public static final int KEYCODE_Y = 53; |
| 83 | public static final int KEYCODE_Z = 54; |
| 84 | public static final int KEYCODE_COMMA = 55; |
| 85 | public static final int KEYCODE_PERIOD = 56; |
| 86 | public static final int KEYCODE_ALT_LEFT = 57; |
| 87 | public static final int KEYCODE_ALT_RIGHT = 58; |
| 88 | public static final int KEYCODE_SHIFT_LEFT = 59; |
| 89 | public static final int KEYCODE_SHIFT_RIGHT = 60; |
| 90 | public static final int KEYCODE_TAB = 61; |
| 91 | public static final int KEYCODE_SPACE = 62; |
| 92 | public static final int KEYCODE_SYM = 63; |
| 93 | public static final int KEYCODE_EXPLORER = 64; |
| 94 | public static final int KEYCODE_ENVELOPE = 65; |
| 95 | public static final int KEYCODE_ENTER = 66; |
| 96 | public static final int KEYCODE_DEL = 67; |
| 97 | public static final int KEYCODE_GRAVE = 68; |
| 98 | public static final int KEYCODE_MINUS = 69; |
| 99 | public static final int KEYCODE_EQUALS = 70; |
| 100 | public static final int KEYCODE_LEFT_BRACKET = 71; |
| 101 | public static final int KEYCODE_RIGHT_BRACKET = 72; |
| 102 | public static final int KEYCODE_BACKSLASH = 73; |
| 103 | public static final int KEYCODE_SEMICOLON = 74; |
| 104 | public static final int KEYCODE_APOSTROPHE = 75; |
| 105 | public static final int KEYCODE_SLASH = 76; |
| 106 | public static final int KEYCODE_AT = 77; |
| 107 | public static final int KEYCODE_NUM = 78; |
| 108 | public static final int KEYCODE_HEADSETHOOK = 79; |
| 109 | public static final int KEYCODE_FOCUS = 80; // *Camera* focus |
| 110 | public static final int KEYCODE_PLUS = 81; |
| 111 | public static final int KEYCODE_MENU = 82; |
| 112 | public static final int KEYCODE_NOTIFICATION = 83; |
| 113 | public static final int KEYCODE_SEARCH = 84; |
Dianne Hackborn | 935ae46 | 2009-04-13 16:11:55 -0700 | [diff] [blame^] | 114 | public static final int KEYCODE_MEDIA_PLAY_PAUSE= 85; |
| 115 | public static final int KEYCODE_MEDIA_STOP = 86; |
| 116 | public static final int KEYCODE_MEDIA_NEXT = 87; |
| 117 | public static final int KEYCODE_MEDIA_PREVIOUS = 88; |
| 118 | public static final int KEYCODE_MEDIA_REWIND = 89; |
| 119 | public static final int KEYCODE_MEDIA_FAST_FORWARD = 90; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | public static final int KEYCODE_MUTE = 91; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | |
| 122 | // NOTE: If you add a new keycode here you must also add it to: |
| 123 | // isSystem() |
| 124 | // frameworks/base/include/ui/KeycodeLabels.h |
| 125 | // tools/puppet_master/PuppetMaster/nav_keys.py |
| 126 | // frameworks/base/core/res/res/values/attrs.xml |
| 127 | // commands/monkey/Monkey.java |
| 128 | // emulator? |
Dianne Hackborn | 935ae46 | 2009-04-13 16:11:55 -0700 | [diff] [blame^] | 129 | // |
| 130 | // Also Android currently does not reserve code ranges for vendor- |
| 131 | // specific key codes. If you have new key codes to have, you |
| 132 | // MUST contribute a patch to the open source project to define |
| 133 | // those new codes. This is intended to maintain a consistent |
| 134 | // set of key code definitions across all Android devices. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | |
Dianne Hackborn | 935ae46 | 2009-04-13 16:11:55 -0700 | [diff] [blame^] | 136 | private static final int LAST_KEYCODE = KEYCODE_MUTE; |
| 137 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | /** |
| 139 | * @deprecated There are now more than MAX_KEYCODE keycodes. |
| 140 | * Use {@link #getMaxKeyCode()} instead. |
| 141 | */ |
| 142 | @Deprecated |
| 143 | public static final int MAX_KEYCODE = 84; |
| 144 | |
| 145 | /** |
| 146 | * {@link #getAction} value: the key has been pressed down. |
| 147 | */ |
| 148 | public static final int ACTION_DOWN = 0; |
| 149 | /** |
| 150 | * {@link #getAction} value: the key has been released. |
| 151 | */ |
| 152 | public static final int ACTION_UP = 1; |
| 153 | /** |
| 154 | * {@link #getAction} value: multiple duplicate key events have |
| 155 | * occurred in a row, or a complex string is being delivered. If the |
| 156 | * key code is not {#link {@link #KEYCODE_UNKNOWN} then the |
| 157 | * {#link {@link #getRepeatCount()} method returns the number of times |
| 158 | * the given key code should be executed. |
| 159 | * Otherwise, if the key code {@link #KEYCODE_UNKNOWN}, then |
| 160 | * this is a sequence of characters as returned by {@link #getCharacters}. |
| 161 | */ |
| 162 | public static final int ACTION_MULTIPLE = 2; |
| 163 | |
| 164 | /** |
| 165 | * <p>This mask is used to check whether one of the ALT meta keys is pressed.</p> |
| 166 | * |
| 167 | * @see #isAltPressed() |
| 168 | * @see #getMetaState() |
| 169 | * @see #KEYCODE_ALT_LEFT |
| 170 | * @see #KEYCODE_ALT_RIGHT |
| 171 | */ |
| 172 | public static final int META_ALT_ON = 0x02; |
| 173 | |
| 174 | /** |
| 175 | * <p>This mask is used to check whether the left ALT meta key is pressed.</p> |
| 176 | * |
| 177 | * @see #isAltPressed() |
| 178 | * @see #getMetaState() |
| 179 | * @see #KEYCODE_ALT_LEFT |
| 180 | */ |
| 181 | public static final int META_ALT_LEFT_ON = 0x10; |
| 182 | |
| 183 | /** |
| 184 | * <p>This mask is used to check whether the right the ALT meta key is pressed.</p> |
| 185 | * |
| 186 | * @see #isAltPressed() |
| 187 | * @see #getMetaState() |
| 188 | * @see #KEYCODE_ALT_RIGHT |
| 189 | */ |
| 190 | public static final int META_ALT_RIGHT_ON = 0x20; |
| 191 | |
| 192 | /** |
| 193 | * <p>This mask is used to check whether one of the SHIFT meta keys is pressed.</p> |
| 194 | * |
| 195 | * @see #isShiftPressed() |
| 196 | * @see #getMetaState() |
| 197 | * @see #KEYCODE_SHIFT_LEFT |
| 198 | * @see #KEYCODE_SHIFT_RIGHT |
| 199 | */ |
| 200 | public static final int META_SHIFT_ON = 0x1; |
| 201 | |
| 202 | /** |
| 203 | * <p>This mask is used to check whether the left SHIFT meta key is pressed.</p> |
| 204 | * |
| 205 | * @see #isShiftPressed() |
| 206 | * @see #getMetaState() |
| 207 | * @see #KEYCODE_SHIFT_LEFT |
| 208 | */ |
| 209 | public static final int META_SHIFT_LEFT_ON = 0x40; |
| 210 | |
| 211 | /** |
| 212 | * <p>This mask is used to check whether the right SHIFT meta key is pressed.</p> |
| 213 | * |
| 214 | * @see #isShiftPressed() |
| 215 | * @see #getMetaState() |
| 216 | * @see #KEYCODE_SHIFT_RIGHT |
| 217 | */ |
| 218 | public static final int META_SHIFT_RIGHT_ON = 0x80; |
| 219 | |
| 220 | /** |
| 221 | * <p>This mask is used to check whether the SYM meta key is pressed.</p> |
| 222 | * |
| 223 | * @see #isSymPressed() |
| 224 | * @see #getMetaState() |
| 225 | */ |
| 226 | public static final int META_SYM_ON = 0x4; |
| 227 | |
| 228 | /** |
| 229 | * This mask is set if the device woke because of this key event. |
| 230 | */ |
| 231 | public static final int FLAG_WOKE_HERE = 0x1; |
| 232 | |
| 233 | /** |
| 234 | * This mask is set if the key event was generated by a software keyboard. |
| 235 | */ |
| 236 | public static final int FLAG_SOFT_KEYBOARD = 0x2; |
| 237 | |
| 238 | /** |
| 239 | * This mask is set if we don't want the key event to cause us to leave |
| 240 | * touch mode. |
| 241 | */ |
| 242 | public static final int FLAG_KEEP_TOUCH_MODE = 0x4; |
| 243 | |
| 244 | /** |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 245 | * This mask is set if an event was known to come from a trusted part |
| 246 | * of the system. That is, the event is known to come from the user, |
| 247 | * and could not have been spoofed by a third party component. |
| 248 | */ |
| 249 | public static final int FLAG_FROM_SYSTEM = 0x8; |
| 250 | |
| 251 | /** |
| 252 | * This mask is used for compatibility, to identify enter keys that are |
| 253 | * coming from an IME whose enter key has been auto-labelled "next" or |
| 254 | * "done". This allows TextView to dispatch these as normal enter keys |
| 255 | * for old applications, but still do the appropriate action when |
| 256 | * receiving them. |
| 257 | */ |
| 258 | public static final int FLAG_EDITOR_ACTION = 0x10; |
| 259 | |
| 260 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | * Returns the maximum keycode. |
| 262 | */ |
| 263 | public static int getMaxKeyCode() { |
| 264 | return LAST_KEYCODE; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Get the character that is produced by putting accent on the character |
| 269 | * c. |
| 270 | * For example, getDeadChar('`', 'e') returns è. |
| 271 | */ |
| 272 | public static int getDeadChar(int accent, int c) { |
| 273 | return KeyCharacterMap.getDeadChar(accent, c); |
| 274 | } |
| 275 | |
| 276 | private int mMetaState; |
| 277 | private int mAction; |
| 278 | private int mKeyCode; |
| 279 | private int mScancode; |
| 280 | private int mRepeatCount; |
| 281 | private int mDeviceId; |
| 282 | private int mFlags; |
| 283 | private long mDownTime; |
| 284 | private long mEventTime; |
| 285 | private String mCharacters; |
| 286 | |
| 287 | public interface Callback { |
| 288 | /** |
| 289 | * Called when a key down event has occurred. |
| 290 | * |
| 291 | * @param keyCode The value in event.getKeyCode(). |
| 292 | * @param event Description of the key event. |
| 293 | * |
| 294 | * @return If you handled the event, return true. If you want to allow |
| 295 | * the event to be handled by the next receiver, return false. |
| 296 | */ |
| 297 | boolean onKeyDown(int keyCode, KeyEvent event); |
| 298 | |
| 299 | /** |
| 300 | * Called when a key up event has occurred. |
| 301 | * |
| 302 | * @param keyCode The value in event.getKeyCode(). |
| 303 | * @param event Description of the key event. |
| 304 | * |
| 305 | * @return If you handled the event, return true. If you want to allow |
| 306 | * the event to be handled by the next receiver, return false. |
| 307 | */ |
| 308 | boolean onKeyUp(int keyCode, KeyEvent event); |
| 309 | |
| 310 | /** |
| 311 | * Called when multiple down/up pairs of the same key have occurred |
| 312 | * in a row. |
| 313 | * |
| 314 | * @param keyCode The value in event.getKeyCode(). |
| 315 | * @param count Number of pairs as returned by event.getRepeatCount(). |
| 316 | * @param event Description of the key event. |
| 317 | * |
| 318 | * @return If you handled the event, return true. If you want to allow |
| 319 | * the event to be handled by the next receiver, return false. |
| 320 | */ |
| 321 | boolean onKeyMultiple(int keyCode, int count, KeyEvent event); |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Create a new key event. |
| 326 | * |
| 327 | * @param action Action code: either {@link #ACTION_DOWN}, |
| 328 | * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. |
| 329 | * @param code The key code. |
| 330 | */ |
| 331 | public KeyEvent(int action, int code) { |
| 332 | mAction = action; |
| 333 | mKeyCode = code; |
| 334 | mRepeatCount = 0; |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Create a new key event. |
| 339 | * |
| 340 | * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) |
| 341 | * at which this key code originally went down. |
| 342 | * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) |
| 343 | * at which this event happened. |
| 344 | * @param action Action code: either {@link #ACTION_DOWN}, |
| 345 | * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. |
| 346 | * @param code The key code. |
| 347 | * @param repeat A repeat count for down events (> 0 if this is after the |
| 348 | * initial down) or event count for multiple events. |
| 349 | */ |
| 350 | public KeyEvent(long downTime, long eventTime, int action, |
| 351 | int code, int repeat) { |
| 352 | mDownTime = downTime; |
| 353 | mEventTime = eventTime; |
| 354 | mAction = action; |
| 355 | mKeyCode = code; |
| 356 | mRepeatCount = repeat; |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Create a new key event. |
| 361 | * |
| 362 | * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) |
| 363 | * at which this key code originally went down. |
| 364 | * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) |
| 365 | * at which this event happened. |
| 366 | * @param action Action code: either {@link #ACTION_DOWN}, |
| 367 | * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. |
| 368 | * @param code The key code. |
| 369 | * @param repeat A repeat count for down events (> 0 if this is after the |
| 370 | * initial down) or event count for multiple events. |
| 371 | * @param metaState Flags indicating which meta keys are currently pressed. |
| 372 | */ |
| 373 | public KeyEvent(long downTime, long eventTime, int action, |
| 374 | int code, int repeat, int metaState) { |
| 375 | mDownTime = downTime; |
| 376 | mEventTime = eventTime; |
| 377 | mAction = action; |
| 378 | mKeyCode = code; |
| 379 | mRepeatCount = repeat; |
| 380 | mMetaState = metaState; |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Create a new key event. |
| 385 | * |
| 386 | * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) |
| 387 | * at which this key code originally went down. |
| 388 | * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) |
| 389 | * at which this event happened. |
| 390 | * @param action Action code: either {@link #ACTION_DOWN}, |
| 391 | * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. |
| 392 | * @param code The key code. |
| 393 | * @param repeat A repeat count for down events (> 0 if this is after the |
| 394 | * initial down) or event count for multiple events. |
| 395 | * @param metaState Flags indicating which meta keys are currently pressed. |
| 396 | * @param device The device ID that generated the key event. |
| 397 | * @param scancode Raw device scan code of the event. |
| 398 | */ |
| 399 | public KeyEvent(long downTime, long eventTime, int action, |
| 400 | int code, int repeat, int metaState, |
| 401 | int device, int scancode) { |
| 402 | mDownTime = downTime; |
| 403 | mEventTime = eventTime; |
| 404 | mAction = action; |
| 405 | mKeyCode = code; |
| 406 | mRepeatCount = repeat; |
| 407 | mMetaState = metaState; |
| 408 | mDeviceId = device; |
| 409 | mScancode = scancode; |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Create a new key event. |
| 414 | * |
| 415 | * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) |
| 416 | * at which this key code originally went down. |
| 417 | * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) |
| 418 | * at which this event happened. |
| 419 | * @param action Action code: either {@link #ACTION_DOWN}, |
| 420 | * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. |
| 421 | * @param code The key code. |
| 422 | * @param repeat A repeat count for down events (> 0 if this is after the |
| 423 | * initial down) or event count for multiple events. |
| 424 | * @param metaState Flags indicating which meta keys are currently pressed. |
| 425 | * @param device The device ID that generated the key event. |
| 426 | * @param scancode Raw device scan code of the event. |
| 427 | * @param flags The flags for this key event |
| 428 | */ |
| 429 | public KeyEvent(long downTime, long eventTime, int action, |
| 430 | int code, int repeat, int metaState, |
| 431 | int device, int scancode, int flags) { |
| 432 | mDownTime = downTime; |
| 433 | mEventTime = eventTime; |
| 434 | mAction = action; |
| 435 | mKeyCode = code; |
| 436 | mRepeatCount = repeat; |
| 437 | mMetaState = metaState; |
| 438 | mDeviceId = device; |
| 439 | mScancode = scancode; |
| 440 | mFlags = flags; |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Create a new key event for a string of characters. The key code, |
| 445 | * action, and repeat could will automatically be set to |
| 446 | * {@link #KEYCODE_UNKNOWN}, {@link #ACTION_MULTIPLE}, and 0 for you. |
| 447 | * |
| 448 | * @param time The time (in {@link android.os.SystemClock#uptimeMillis}) |
| 449 | * at which this event occured. |
| 450 | * @param characters The string of characters. |
| 451 | * @param device The device ID that generated the key event. |
| 452 | * @param flags The flags for this key event |
| 453 | */ |
| 454 | public KeyEvent(long time, String characters, int device, int flags) { |
| 455 | mDownTime = time; |
| 456 | mEventTime = time; |
| 457 | mCharacters = characters; |
| 458 | mAction = ACTION_MULTIPLE; |
| 459 | mKeyCode = KEYCODE_UNKNOWN; |
| 460 | mRepeatCount = 0; |
| 461 | mDeviceId = device; |
| 462 | mFlags = flags; |
| 463 | } |
| 464 | |
| 465 | /** |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 466 | * Make an exact copy of an existing key event. |
| 467 | */ |
| 468 | public KeyEvent(KeyEvent origEvent) { |
| 469 | mDownTime = origEvent.mDownTime; |
| 470 | mEventTime = origEvent.mEventTime; |
| 471 | mAction = origEvent.mAction; |
| 472 | mKeyCode = origEvent.mKeyCode; |
| 473 | mRepeatCount = origEvent.mRepeatCount; |
| 474 | mMetaState = origEvent.mMetaState; |
| 475 | mDeviceId = origEvent.mDeviceId; |
| 476 | mScancode = origEvent.mScancode; |
| 477 | mFlags = origEvent.mFlags; |
| 478 | mCharacters = origEvent.mCharacters; |
| 479 | } |
| 480 | |
| 481 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 482 | * Copy an existing key event, modifying its time and repeat count. |
| 483 | * |
| 484 | * @param origEvent The existing event to be copied. |
| 485 | * @param eventTime The new event time |
| 486 | * (in {@link android.os.SystemClock#uptimeMillis}) of the event. |
| 487 | * @param newRepeat The new repeat count of the event. |
| 488 | */ |
| 489 | public KeyEvent(KeyEvent origEvent, long eventTime, int newRepeat) { |
| 490 | mDownTime = origEvent.mDownTime; |
| 491 | mEventTime = eventTime; |
| 492 | mAction = origEvent.mAction; |
| 493 | mKeyCode = origEvent.mKeyCode; |
| 494 | mRepeatCount = newRepeat; |
| 495 | mMetaState = origEvent.mMetaState; |
| 496 | mDeviceId = origEvent.mDeviceId; |
| 497 | mScancode = origEvent.mScancode; |
| 498 | mFlags = origEvent.mFlags; |
| 499 | mCharacters = origEvent.mCharacters; |
| 500 | } |
| 501 | |
| 502 | /** |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 503 | * Create a new key event that is the same as the given one, but whose |
| 504 | * event time and repeat count are replaced with the given value. |
| 505 | * |
| 506 | * @param event The existing event to be copied. This is not modified. |
| 507 | * @param eventTime The new event time |
| 508 | * (in {@link android.os.SystemClock#uptimeMillis}) of the event. |
| 509 | * @param newRepeat The new repeat count of the event. |
| 510 | */ |
| 511 | public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime, |
| 512 | int newRepeat) { |
| 513 | return new KeyEvent(event, eventTime, newRepeat); |
| 514 | } |
| 515 | |
| 516 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 517 | * Copy an existing key event, modifying its action. |
| 518 | * |
| 519 | * @param origEvent The existing event to be copied. |
| 520 | * @param action The new action code of the event. |
| 521 | */ |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 522 | private KeyEvent(KeyEvent origEvent, int action) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 523 | mDownTime = origEvent.mDownTime; |
| 524 | mEventTime = origEvent.mEventTime; |
| 525 | mAction = action; |
| 526 | mKeyCode = origEvent.mKeyCode; |
| 527 | mRepeatCount = origEvent.mRepeatCount; |
| 528 | mMetaState = origEvent.mMetaState; |
| 529 | mDeviceId = origEvent.mDeviceId; |
| 530 | mScancode = origEvent.mScancode; |
| 531 | mFlags = origEvent.mFlags; |
| 532 | // Don't copy mCharacters, since one way or the other we'll lose it |
| 533 | // when changing the action. |
| 534 | } |
| 535 | |
| 536 | /** |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 537 | * Create a new key event that is the same as the given one, but whose |
| 538 | * action is replaced with the given value. |
| 539 | * |
| 540 | * @param event The existing event to be copied. This is not modified. |
| 541 | * @param action The new action code of the event. |
| 542 | */ |
| 543 | public static KeyEvent changeAction(KeyEvent event, int action) { |
| 544 | return new KeyEvent(event, action); |
| 545 | } |
| 546 | |
| 547 | /** |
| 548 | * Create a new key event that is the same as the given one, but whose |
| 549 | * flags are replaced with the given value. |
| 550 | * |
| 551 | * @param event The existing event to be copied. This is not modified. |
| 552 | * @param flags The new flags constant. |
| 553 | */ |
| 554 | public static KeyEvent changeFlags(KeyEvent event, int flags) { |
| 555 | event = new KeyEvent(event); |
| 556 | event.mFlags = flags; |
| 557 | return event; |
| 558 | } |
| 559 | |
| 560 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 561 | * Don't use in new code, instead explicitly check |
| 562 | * {@link #getAction()}. |
| 563 | * |
| 564 | * @return If the action is ACTION_DOWN, returns true; else false. |
| 565 | * |
| 566 | * @deprecated |
| 567 | * @hide |
| 568 | */ |
| 569 | @Deprecated public final boolean isDown() { |
| 570 | return mAction == ACTION_DOWN; |
| 571 | } |
| 572 | |
| 573 | /** |
| 574 | * Is this a system key? System keys can not be used for menu shortcuts. |
| 575 | * |
| 576 | * TODO: this information should come from a table somewhere. |
| 577 | * TODO: should the dpad keys be here? arguably, because they also shouldn't be menu shortcuts |
| 578 | */ |
| 579 | public final boolean isSystem() { |
| 580 | switch (mKeyCode) { |
| 581 | case KEYCODE_MENU: |
| 582 | case KEYCODE_SOFT_RIGHT: |
| 583 | case KEYCODE_HOME: |
| 584 | case KEYCODE_BACK: |
| 585 | case KEYCODE_CALL: |
| 586 | case KEYCODE_ENDCALL: |
| 587 | case KEYCODE_VOLUME_UP: |
| 588 | case KEYCODE_VOLUME_DOWN: |
| 589 | case KEYCODE_MUTE: |
| 590 | case KEYCODE_POWER: |
| 591 | case KEYCODE_HEADSETHOOK: |
Dianne Hackborn | 935ae46 | 2009-04-13 16:11:55 -0700 | [diff] [blame^] | 592 | case KEYCODE_MEDIA_PLAY_PAUSE: |
| 593 | case KEYCODE_MEDIA_STOP: |
| 594 | case KEYCODE_MEDIA_NEXT: |
| 595 | case KEYCODE_MEDIA_PREVIOUS: |
| 596 | case KEYCODE_MEDIA_REWIND: |
| 597 | case KEYCODE_MEDIA_FAST_FORWARD: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 598 | case KEYCODE_CAMERA: |
| 599 | case KEYCODE_FOCUS: |
| 600 | case KEYCODE_SEARCH: |
| 601 | return true; |
| 602 | default: |
| 603 | return false; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | |
| 608 | /** |
| 609 | * <p>Returns the state of the meta keys.</p> |
| 610 | * |
| 611 | * @return an integer in which each bit set to 1 represents a pressed |
| 612 | * meta key |
| 613 | * |
| 614 | * @see #isAltPressed() |
| 615 | * @see #isShiftPressed() |
| 616 | * @see #isSymPressed() |
| 617 | * @see #META_ALT_ON |
| 618 | * @see #META_SHIFT_ON |
| 619 | * @see #META_SYM_ON |
| 620 | */ |
| 621 | public final int getMetaState() { |
| 622 | return mMetaState; |
| 623 | } |
| 624 | |
| 625 | /** |
| 626 | * Returns the flags for this key event. |
| 627 | * |
| 628 | * @see #FLAG_WOKE_HERE |
| 629 | */ |
| 630 | public final int getFlags() { |
| 631 | return mFlags; |
| 632 | } |
| 633 | |
| 634 | /** |
| 635 | * Returns true if this key code is a modifier key. |
| 636 | * |
| 637 | * @return whether the provided keyCode is one of |
| 638 | * {@link #KEYCODE_SHIFT_LEFT} {@link #KEYCODE_SHIFT_RIGHT}, |
| 639 | * {@link #KEYCODE_ALT_LEFT}, {@link #KEYCODE_ALT_RIGHT} |
| 640 | * or {@link #KEYCODE_SYM}. |
| 641 | */ |
| 642 | public static boolean isModifierKey(int keyCode) { |
| 643 | return keyCode == KEYCODE_SHIFT_LEFT || keyCode == KEYCODE_SHIFT_RIGHT |
| 644 | || keyCode == KEYCODE_ALT_LEFT || keyCode == KEYCODE_ALT_RIGHT |
| 645 | || keyCode == KEYCODE_SYM; |
| 646 | } |
| 647 | |
| 648 | /** |
| 649 | * <p>Returns the pressed state of the ALT meta key.</p> |
| 650 | * |
| 651 | * @return true if the ALT key is pressed, false otherwise |
| 652 | * |
| 653 | * @see #KEYCODE_ALT_LEFT |
| 654 | * @see #KEYCODE_ALT_RIGHT |
| 655 | * @see #META_ALT_ON |
| 656 | */ |
| 657 | public final boolean isAltPressed() { |
| 658 | return (mMetaState & META_ALT_ON) != 0; |
| 659 | } |
| 660 | |
| 661 | /** |
| 662 | * <p>Returns the pressed state of the SHIFT meta key.</p> |
| 663 | * |
| 664 | * @return true if the SHIFT key is pressed, false otherwise |
| 665 | * |
| 666 | * @see #KEYCODE_SHIFT_LEFT |
| 667 | * @see #KEYCODE_SHIFT_RIGHT |
| 668 | * @see #META_SHIFT_ON |
| 669 | */ |
| 670 | public final boolean isShiftPressed() { |
| 671 | return (mMetaState & META_SHIFT_ON) != 0; |
| 672 | } |
| 673 | |
| 674 | /** |
| 675 | * <p>Returns the pressed state of the SYM meta key.</p> |
| 676 | * |
| 677 | * @return true if the SYM key is pressed, false otherwise |
| 678 | * |
| 679 | * @see #KEYCODE_SYM |
| 680 | * @see #META_SYM_ON |
| 681 | */ |
| 682 | public final boolean isSymPressed() { |
| 683 | return (mMetaState & META_SYM_ON) != 0; |
| 684 | } |
| 685 | |
| 686 | /** |
| 687 | * Retrieve the action of this key event. May be either |
| 688 | * {@link #ACTION_DOWN}, {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. |
| 689 | * |
| 690 | * @return The event action: ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE. |
| 691 | */ |
| 692 | public final int getAction() { |
| 693 | return mAction; |
| 694 | } |
| 695 | |
| 696 | /** |
| 697 | * Retrieve the key code of the key event. This is the physical key that |
| 698 | * was pressed, <em>not</em> the Unicode character. |
| 699 | * |
| 700 | * @return The key code of the event. |
| 701 | */ |
| 702 | public final int getKeyCode() { |
| 703 | return mKeyCode; |
| 704 | } |
| 705 | |
| 706 | /** |
| 707 | * For the special case of a {@link #ACTION_MULTIPLE} event with key |
| 708 | * code of {@link #KEYCODE_UNKNOWN}, this is a raw string of characters |
| 709 | * associated with the event. In all other cases it is null. |
| 710 | * |
| 711 | * @return Returns a String of 1 or more characters associated with |
| 712 | * the event. |
| 713 | */ |
| 714 | public final String getCharacters() { |
| 715 | return mCharacters; |
| 716 | } |
| 717 | |
| 718 | /** |
| 719 | * Retrieve the hardware key id of this key event. These values are not |
| 720 | * reliable and vary from device to device. |
| 721 | * |
| 722 | * {@more} |
| 723 | * Mostly this is here for debugging purposes. |
| 724 | */ |
| 725 | public final int getScanCode() { |
| 726 | return mScancode; |
| 727 | } |
| 728 | |
| 729 | /** |
| 730 | * Retrieve the repeat count of the event. For both key up and key down |
| 731 | * events, this is the number of times the key has repeated with the first |
| 732 | * down starting at 0 and counting up from there. For multiple key |
| 733 | * events, this is the number of down/up pairs that have occurred. |
| 734 | * |
| 735 | * @return The number of times the key has repeated. |
| 736 | */ |
| 737 | public final int getRepeatCount() { |
| 738 | return mRepeatCount; |
| 739 | } |
| 740 | |
| 741 | /** |
| 742 | * Retrieve the time of the most recent key down event, |
| 743 | * in the {@link android.os.SystemClock#uptimeMillis} time base. If this |
| 744 | * is a down event, this will be the same as {@link #getEventTime()}. |
| 745 | * Note that when chording keys, this value is the down time of the |
| 746 | * most recently pressed key, which may <em>not</em> be the same physical |
| 747 | * key of this event. |
| 748 | * |
| 749 | * @return Returns the most recent key down time, in the |
| 750 | * {@link android.os.SystemClock#uptimeMillis} time base |
| 751 | */ |
| 752 | public final long getDownTime() { |
| 753 | return mDownTime; |
| 754 | } |
| 755 | |
| 756 | /** |
| 757 | * Retrieve the time this event occurred, |
| 758 | * in the {@link android.os.SystemClock#uptimeMillis} time base. |
| 759 | * |
| 760 | * @return Returns the time this event occurred, |
| 761 | * in the {@link android.os.SystemClock#uptimeMillis} time base. |
| 762 | */ |
| 763 | public final long getEventTime() { |
| 764 | return mEventTime; |
| 765 | } |
| 766 | |
| 767 | /** |
| 768 | * Return the id for the keyboard that this event came from. A device |
| 769 | * id of 0 indicates the event didn't come from a physical device and |
| 770 | * maps to the default keymap. The other numbers are arbitrary and |
| 771 | * you shouldn't depend on the values. |
| 772 | * |
| 773 | * @see KeyCharacterMap#load |
| 774 | */ |
| 775 | public final int getDeviceId() { |
| 776 | return mDeviceId; |
| 777 | } |
| 778 | |
| 779 | /** |
| 780 | * Renamed to {@link #getDeviceId}. |
| 781 | * |
| 782 | * @hide |
| 783 | * @deprecated |
| 784 | */ |
| 785 | public final int getKeyboardDevice() { |
| 786 | return mDeviceId; |
| 787 | } |
| 788 | |
| 789 | /** |
| 790 | * Get the primary character for this key. In other words, the label |
| 791 | * that is physically printed on it. |
| 792 | */ |
| 793 | public char getDisplayLabel() { |
| 794 | return KeyCharacterMap.load(mDeviceId).getDisplayLabel(mKeyCode); |
| 795 | } |
| 796 | |
| 797 | /** |
| 798 | * <p> |
| 799 | * Returns the Unicode character that the key would produce. |
| 800 | * </p><p> |
| 801 | * Returns 0 if the key is not one that is used to type Unicode |
| 802 | * characters. |
| 803 | * </p><p> |
| 804 | * If the return value has bit |
| 805 | * {@link KeyCharacterMap#COMBINING_ACCENT} |
| 806 | * set, the key is a "dead key" that should be combined with another to |
| 807 | * actually produce a character -- see {@link #getDeadChar} -- |
| 808 | * after masking with |
| 809 | * {@link KeyCharacterMap#COMBINING_ACCENT_MASK}. |
| 810 | * </p> |
| 811 | */ |
| 812 | public int getUnicodeChar() { |
| 813 | return getUnicodeChar(mMetaState); |
| 814 | } |
| 815 | |
| 816 | /** |
| 817 | * <p> |
| 818 | * Returns the Unicode character that the key would produce. |
| 819 | * </p><p> |
| 820 | * Returns 0 if the key is not one that is used to type Unicode |
| 821 | * characters. |
| 822 | * </p><p> |
| 823 | * If the return value has bit |
| 824 | * {@link KeyCharacterMap#COMBINING_ACCENT} |
| 825 | * set, the key is a "dead key" that should be combined with another to |
| 826 | * actually produce a character -- see {@link #getDeadChar} -- after masking |
| 827 | * with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}. |
| 828 | * </p> |
| 829 | */ |
| 830 | public int getUnicodeChar(int meta) { |
| 831 | return KeyCharacterMap.load(mDeviceId).get(mKeyCode, meta); |
| 832 | } |
| 833 | |
| 834 | /** |
| 835 | * Get the characters conversion data for the key event.. |
| 836 | * |
| 837 | * @param results a {@link KeyData} that will be filled with the results. |
| 838 | * |
| 839 | * @return whether the key was mapped or not. If the key was not mapped, |
| 840 | * results is not modified. |
| 841 | */ |
| 842 | public boolean getKeyData(KeyData results) { |
| 843 | return KeyCharacterMap.load(mDeviceId).getKeyData(mKeyCode, results); |
| 844 | } |
| 845 | |
| 846 | /** |
| 847 | * The same as {@link #getMatch(char[],int) getMatch(chars, 0)}. |
| 848 | */ |
| 849 | public char getMatch(char[] chars) { |
| 850 | return getMatch(chars, 0); |
| 851 | } |
| 852 | |
| 853 | /** |
| 854 | * If one of the chars in the array can be generated by the keyCode of this |
| 855 | * key event, return the char; otherwise return '\0'. |
| 856 | * @param chars the characters to try to find |
| 857 | * @param modifiers the modifier bits to prefer. If any of these bits |
| 858 | * are set, if there are multiple choices, that could |
| 859 | * work, the one for this modifier will be set. |
| 860 | */ |
| 861 | public char getMatch(char[] chars, int modifiers) { |
| 862 | return KeyCharacterMap.load(mDeviceId).getMatch(mKeyCode, chars, modifiers); |
| 863 | } |
| 864 | |
| 865 | /** |
| 866 | * Gets the number or symbol associated with the key. The character value |
| 867 | * is returned, not the numeric value. If the key is not a number, but is |
| 868 | * a symbol, the symbol is retuned. |
| 869 | */ |
| 870 | public char getNumber() { |
| 871 | return KeyCharacterMap.load(mDeviceId).getNumber(mKeyCode); |
| 872 | } |
| 873 | |
| 874 | /** |
| 875 | * Does the key code of this key produce a glyph? |
| 876 | */ |
| 877 | public boolean isPrintingKey() { |
| 878 | return KeyCharacterMap.load(mDeviceId).isPrintingKey(mKeyCode); |
| 879 | } |
| 880 | |
| 881 | /** |
| 882 | * Deliver this key event to a {@link Callback} interface. If this is |
| 883 | * an ACTION_MULTIPLE event and it is not handled, then an attempt will |
| 884 | * be made to deliver a single normal event. |
| 885 | * |
| 886 | * @param receiver The Callback that will be given the event. |
| 887 | * |
| 888 | * @return The return value from the Callback method that was called. |
| 889 | */ |
| 890 | public final boolean dispatch(Callback receiver) { |
| 891 | switch (mAction) { |
| 892 | case ACTION_DOWN: |
| 893 | return receiver.onKeyDown(mKeyCode, this); |
| 894 | case ACTION_UP: |
| 895 | return receiver.onKeyUp(mKeyCode, this); |
| 896 | case ACTION_MULTIPLE: |
| 897 | final int count = mRepeatCount; |
| 898 | final int code = mKeyCode; |
| 899 | if (receiver.onKeyMultiple(code, count, this)) { |
| 900 | return true; |
| 901 | } |
| 902 | if (code != KeyEvent.KEYCODE_UNKNOWN) { |
| 903 | mAction = ACTION_DOWN; |
| 904 | mRepeatCount = 0; |
| 905 | boolean handled = receiver.onKeyDown(code, this); |
| 906 | if (handled) { |
| 907 | mAction = ACTION_UP; |
| 908 | receiver.onKeyUp(code, this); |
| 909 | } |
| 910 | mAction = ACTION_MULTIPLE; |
| 911 | mRepeatCount = count; |
| 912 | return handled; |
| 913 | } |
| 914 | } |
| 915 | return false; |
| 916 | } |
| 917 | |
| 918 | public String toString() { |
| 919 | return "KeyEvent{action=" + mAction + " code=" + mKeyCode |
| 920 | + " repeat=" + mRepeatCount |
| 921 | + " meta=" + mMetaState + " scancode=" + mScancode |
| 922 | + " mFlags=" + mFlags + "}"; |
| 923 | } |
| 924 | |
| 925 | public static final Parcelable.Creator<KeyEvent> CREATOR |
| 926 | = new Parcelable.Creator<KeyEvent>() { |
| 927 | public KeyEvent createFromParcel(Parcel in) { |
| 928 | return new KeyEvent(in); |
| 929 | } |
| 930 | |
| 931 | public KeyEvent[] newArray(int size) { |
| 932 | return new KeyEvent[size]; |
| 933 | } |
| 934 | }; |
| 935 | |
| 936 | public int describeContents() { |
| 937 | return 0; |
| 938 | } |
| 939 | |
| 940 | public void writeToParcel(Parcel out, int flags) { |
| 941 | out.writeInt(mAction); |
| 942 | out.writeInt(mKeyCode); |
| 943 | out.writeInt(mRepeatCount); |
| 944 | out.writeInt(mMetaState); |
| 945 | out.writeInt(mDeviceId); |
| 946 | out.writeInt(mScancode); |
| 947 | out.writeInt(mFlags); |
| 948 | out.writeLong(mDownTime); |
| 949 | out.writeLong(mEventTime); |
| 950 | } |
| 951 | |
| 952 | private KeyEvent(Parcel in) { |
| 953 | mAction = in.readInt(); |
| 954 | mKeyCode = in.readInt(); |
| 955 | mRepeatCount = in.readInt(); |
| 956 | mMetaState = in.readInt(); |
| 957 | mDeviceId = in.readInt(); |
| 958 | mScancode = in.readInt(); |
| 959 | mFlags = in.readInt(); |
| 960 | mDownTime = in.readLong(); |
| 961 | mEventTime = in.readLong(); |
| 962 | } |
| 963 | } |