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