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 | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 21 | import android.util.Log; |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 22 | import android.util.SparseIntArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.view.KeyCharacterMap; |
| 24 | import android.view.KeyCharacterMap.KeyData; |
| 25 | |
| 26 | /** |
| 27 | * Contains constants for key events. |
| 28 | */ |
| 29 | public class KeyEvent 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; |
Dianne Hackborn | 935ae46 | 2009-04-13 16:11:55 -0700 | [diff] [blame] | 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; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | public static final int KEYCODE_MUTE = 91; |
Chih-Wei Huang | 4fedd80 | 2009-05-27 15:52:50 +0800 | [diff] [blame] | 123 | public static final int KEYCODE_PAGE_UP = 92; |
| 124 | public static final int KEYCODE_PAGE_DOWN = 93; |
mogimo | b032bc0 | 2009-10-03 03:13:56 +0900 | [diff] [blame] | 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) |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame^] | 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; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | |
| 143 | // NOTE: If you add a new keycode here you must also add it to: |
| 144 | // isSystem() |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 145 | // native/include/android/keycodes.h |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | // frameworks/base/include/ui/KeycodeLabels.h |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame^] | 147 | // external/webkit/WebKit/android/plugins/ANPKeyCodes.h |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | // tools/puppet_master/PuppetMaster/nav_keys.py |
| 149 | // frameworks/base/core/res/res/values/attrs.xml |
| 150 | // commands/monkey/Monkey.java |
| 151 | // emulator? |
Dianne Hackborn | 935ae46 | 2009-04-13 16:11:55 -0700 | [diff] [blame] | 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. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | |
mogimo | b032bc0 | 2009-10-03 03:13:56 +0900 | [diff] [blame] | 159 | private static final int LAST_KEYCODE = KEYCODE_SWITCH_CHARSET; |
Dianne Hackborn | 935ae46 | 2009-04-13 16:11:55 -0700 | [diff] [blame] | 160 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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. |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 182 | * Otherwise, if the key code is {@link #KEYCODE_UNKNOWN}, then |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | /** |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 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 | /** |
Dianne Hackborn | ddca3ee | 2009-07-23 19:01:31 -0700 | [diff] [blame] | 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 | /** |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 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 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 344 | static final boolean DEBUG = false; |
| 345 | static final String TAG = "KeyEvent"; |
| 346 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 347 | private int mMetaState; |
| 348 | private int mAction; |
| 349 | private int mKeyCode; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 350 | private int mScanCode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | private int mRepeatCount; |
| 352 | private int mDeviceId; |
| 353 | private int mFlags; |
| 354 | private long mDownTime; |
| 355 | private long mEventTime; |
| 356 | private String mCharacters; |
| 357 | |
| 358 | public interface Callback { |
| 359 | /** |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 360 | * Called when a key down event has occurred. If you return true, |
| 361 | * you can first call {@link KeyEvent#startTracking() |
| 362 | * KeyEvent.startTracking()} to have the framework track the event |
| 363 | * through its {@link #onKeyUp(int, KeyEvent)} and also call your |
| 364 | * {@link #onKeyLongPress(int, KeyEvent)} if it occurs. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 365 | * |
| 366 | * @param keyCode The value in event.getKeyCode(). |
| 367 | * @param event Description of the key event. |
| 368 | * |
| 369 | * @return If you handled the event, return true. If you want to allow |
| 370 | * the event to be handled by the next receiver, return false. |
| 371 | */ |
| 372 | boolean onKeyDown(int keyCode, KeyEvent event); |
| 373 | |
| 374 | /** |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 375 | * Called when a long press has occurred. If you return true, |
| 376 | * the final key up will have {@link KeyEvent#FLAG_CANCELED} and |
| 377 | * {@link KeyEvent#FLAG_CANCELED_LONG_PRESS} set. Note that in |
| 378 | * order to receive this callback, someone in the event change |
| 379 | * <em>must</em> return true from {@link #onKeyDown} <em>and</em> |
| 380 | * call {@link KeyEvent#startTracking()} on the event. |
| 381 | * |
| 382 | * @param keyCode The value in event.getKeyCode(). |
| 383 | * @param event Description of the key event. |
| 384 | * |
| 385 | * @return If you handled the event, return true. If you want to allow |
| 386 | * the event to be handled by the next receiver, return false. |
| 387 | */ |
| 388 | boolean onKeyLongPress(int keyCode, KeyEvent event); |
| 389 | |
| 390 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | * Called when a key up event has occurred. |
| 392 | * |
| 393 | * @param keyCode The value in event.getKeyCode(). |
| 394 | * @param event Description of the key event. |
| 395 | * |
| 396 | * @return If you handled the event, return true. If you want to allow |
| 397 | * the event to be handled by the next receiver, return false. |
| 398 | */ |
| 399 | boolean onKeyUp(int keyCode, KeyEvent event); |
| 400 | |
| 401 | /** |
| 402 | * Called when multiple down/up pairs of the same key have occurred |
| 403 | * in a row. |
| 404 | * |
| 405 | * @param keyCode The value in event.getKeyCode(). |
| 406 | * @param count Number of pairs as returned by event.getRepeatCount(). |
| 407 | * @param event Description of the key event. |
| 408 | * |
| 409 | * @return If you handled the event, return true. If you want to allow |
| 410 | * the event to be handled by the next receiver, return false. |
| 411 | */ |
| 412 | boolean onKeyMultiple(int keyCode, int count, KeyEvent event); |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * Create a new key event. |
| 417 | * |
| 418 | * @param action Action code: either {@link #ACTION_DOWN}, |
| 419 | * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. |
| 420 | * @param code The key code. |
| 421 | */ |
| 422 | public KeyEvent(int action, int code) { |
| 423 | mAction = action; |
| 424 | mKeyCode = code; |
| 425 | mRepeatCount = 0; |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * Create a new key event. |
| 430 | * |
| 431 | * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) |
| 432 | * at which this key code originally went down. |
| 433 | * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) |
| 434 | * at which this event happened. |
| 435 | * @param action Action code: either {@link #ACTION_DOWN}, |
| 436 | * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. |
| 437 | * @param code The key code. |
| 438 | * @param repeat A repeat count for down events (> 0 if this is after the |
| 439 | * initial down) or event count for multiple events. |
| 440 | */ |
| 441 | public KeyEvent(long downTime, long eventTime, int action, |
| 442 | int code, int repeat) { |
| 443 | mDownTime = downTime; |
| 444 | mEventTime = eventTime; |
| 445 | mAction = action; |
| 446 | mKeyCode = code; |
| 447 | mRepeatCount = repeat; |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * Create a new key event. |
| 452 | * |
| 453 | * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) |
| 454 | * at which this key code originally went down. |
| 455 | * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) |
| 456 | * at which this event happened. |
| 457 | * @param action Action code: either {@link #ACTION_DOWN}, |
| 458 | * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. |
| 459 | * @param code The key code. |
| 460 | * @param repeat A repeat count for down events (> 0 if this is after the |
| 461 | * initial down) or event count for multiple events. |
| 462 | * @param metaState Flags indicating which meta keys are currently pressed. |
| 463 | */ |
| 464 | public KeyEvent(long downTime, long eventTime, int action, |
| 465 | int code, int repeat, int metaState) { |
| 466 | mDownTime = downTime; |
| 467 | mEventTime = eventTime; |
| 468 | mAction = action; |
| 469 | mKeyCode = code; |
| 470 | mRepeatCount = repeat; |
| 471 | mMetaState = metaState; |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * Create a new key event. |
| 476 | * |
| 477 | * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) |
| 478 | * at which this key code originally went down. |
| 479 | * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) |
| 480 | * at which this event happened. |
| 481 | * @param action Action code: either {@link #ACTION_DOWN}, |
| 482 | * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. |
| 483 | * @param code The key code. |
| 484 | * @param repeat A repeat count for down events (> 0 if this is after the |
| 485 | * initial down) or event count for multiple events. |
| 486 | * @param metaState Flags indicating which meta keys are currently pressed. |
| 487 | * @param device The device ID that generated the key event. |
| 488 | * @param scancode Raw device scan code of the event. |
| 489 | */ |
| 490 | public KeyEvent(long downTime, long eventTime, int action, |
| 491 | int code, int repeat, int metaState, |
| 492 | int device, int scancode) { |
| 493 | mDownTime = downTime; |
| 494 | mEventTime = eventTime; |
| 495 | mAction = action; |
| 496 | mKeyCode = code; |
| 497 | mRepeatCount = repeat; |
| 498 | mMetaState = metaState; |
| 499 | mDeviceId = device; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 500 | mScanCode = scancode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | /** |
| 504 | * Create a new key event. |
| 505 | * |
| 506 | * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) |
| 507 | * at which this key code originally went down. |
| 508 | * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) |
| 509 | * at which this event happened. |
| 510 | * @param action Action code: either {@link #ACTION_DOWN}, |
| 511 | * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. |
| 512 | * @param code The key code. |
| 513 | * @param repeat A repeat count for down events (> 0 if this is after the |
| 514 | * initial down) or event count for multiple events. |
| 515 | * @param metaState Flags indicating which meta keys are currently pressed. |
| 516 | * @param device The device ID that generated the key event. |
| 517 | * @param scancode Raw device scan code of the event. |
| 518 | * @param flags The flags for this key event |
| 519 | */ |
| 520 | public KeyEvent(long downTime, long eventTime, int action, |
| 521 | int code, int repeat, int metaState, |
| 522 | int device, int scancode, int flags) { |
| 523 | mDownTime = downTime; |
| 524 | mEventTime = eventTime; |
| 525 | mAction = action; |
| 526 | mKeyCode = code; |
| 527 | mRepeatCount = repeat; |
| 528 | mMetaState = metaState; |
| 529 | mDeviceId = device; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 530 | mScanCode = scancode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 531 | mFlags = flags; |
| 532 | } |
| 533 | |
| 534 | /** |
| 535 | * Create a new key event for a string of characters. The key code, |
| 536 | * action, and repeat could will automatically be set to |
| 537 | * {@link #KEYCODE_UNKNOWN}, {@link #ACTION_MULTIPLE}, and 0 for you. |
| 538 | * |
| 539 | * @param time The time (in {@link android.os.SystemClock#uptimeMillis}) |
| 540 | * at which this event occured. |
| 541 | * @param characters The string of characters. |
| 542 | * @param device The device ID that generated the key event. |
| 543 | * @param flags The flags for this key event |
| 544 | */ |
| 545 | public KeyEvent(long time, String characters, int device, int flags) { |
| 546 | mDownTime = time; |
| 547 | mEventTime = time; |
| 548 | mCharacters = characters; |
| 549 | mAction = ACTION_MULTIPLE; |
| 550 | mKeyCode = KEYCODE_UNKNOWN; |
| 551 | mRepeatCount = 0; |
| 552 | mDeviceId = device; |
| 553 | mFlags = flags; |
| 554 | } |
| 555 | |
| 556 | /** |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 557 | * Make an exact copy of an existing key event. |
| 558 | */ |
| 559 | public KeyEvent(KeyEvent origEvent) { |
| 560 | mDownTime = origEvent.mDownTime; |
| 561 | mEventTime = origEvent.mEventTime; |
| 562 | mAction = origEvent.mAction; |
| 563 | mKeyCode = origEvent.mKeyCode; |
| 564 | mRepeatCount = origEvent.mRepeatCount; |
| 565 | mMetaState = origEvent.mMetaState; |
| 566 | mDeviceId = origEvent.mDeviceId; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 567 | mScanCode = origEvent.mScanCode; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 568 | mFlags = origEvent.mFlags; |
| 569 | mCharacters = origEvent.mCharacters; |
| 570 | } |
| 571 | |
| 572 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 573 | * Copy an existing key event, modifying its time and repeat count. |
| 574 | * |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 575 | * @deprecated Use {@link #changeTimeRepeat(KeyEvent, long, int)} |
| 576 | * instead. |
| 577 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 578 | * @param origEvent The existing event to be copied. |
| 579 | * @param eventTime The new event time |
| 580 | * (in {@link android.os.SystemClock#uptimeMillis}) of the event. |
| 581 | * @param newRepeat The new repeat count of the event. |
| 582 | */ |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 583 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 584 | public KeyEvent(KeyEvent origEvent, long eventTime, int newRepeat) { |
| 585 | mDownTime = origEvent.mDownTime; |
| 586 | mEventTime = eventTime; |
| 587 | mAction = origEvent.mAction; |
| 588 | mKeyCode = origEvent.mKeyCode; |
| 589 | mRepeatCount = newRepeat; |
| 590 | mMetaState = origEvent.mMetaState; |
| 591 | mDeviceId = origEvent.mDeviceId; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 592 | mScanCode = origEvent.mScanCode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 593 | mFlags = origEvent.mFlags; |
| 594 | mCharacters = origEvent.mCharacters; |
| 595 | } |
| 596 | |
| 597 | /** |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 598 | * Create a new key event that is the same as the given one, but whose |
| 599 | * event time and repeat count are replaced with the given value. |
| 600 | * |
| 601 | * @param event The existing event to be copied. This is not modified. |
| 602 | * @param eventTime The new event time |
| 603 | * (in {@link android.os.SystemClock#uptimeMillis}) of the event. |
| 604 | * @param newRepeat The new repeat count of the event. |
| 605 | */ |
| 606 | public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime, |
| 607 | int newRepeat) { |
| 608 | return new KeyEvent(event, eventTime, newRepeat); |
| 609 | } |
| 610 | |
| 611 | /** |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 612 | * Create a new key event that is the same as the given one, but whose |
| 613 | * event time and repeat count are replaced with the given value. |
| 614 | * |
| 615 | * @param event The existing event to be copied. This is not modified. |
| 616 | * @param eventTime The new event time |
| 617 | * (in {@link android.os.SystemClock#uptimeMillis}) of the event. |
| 618 | * @param newRepeat The new repeat count of the event. |
| 619 | * @param newFlags New flags for the event, replacing the entire value |
| 620 | * in the original event. |
| 621 | */ |
| 622 | public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime, |
| 623 | int newRepeat, int newFlags) { |
| 624 | KeyEvent ret = new KeyEvent(event); |
| 625 | ret.mEventTime = eventTime; |
| 626 | ret.mRepeatCount = newRepeat; |
| 627 | ret.mFlags = newFlags; |
| 628 | return ret; |
| 629 | } |
| 630 | |
| 631 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 632 | * Copy an existing key event, modifying its action. |
| 633 | * |
| 634 | * @param origEvent The existing event to be copied. |
| 635 | * @param action The new action code of the event. |
| 636 | */ |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 637 | private KeyEvent(KeyEvent origEvent, int action) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 638 | mDownTime = origEvent.mDownTime; |
| 639 | mEventTime = origEvent.mEventTime; |
| 640 | mAction = action; |
| 641 | mKeyCode = origEvent.mKeyCode; |
| 642 | mRepeatCount = origEvent.mRepeatCount; |
| 643 | mMetaState = origEvent.mMetaState; |
| 644 | mDeviceId = origEvent.mDeviceId; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 645 | mScanCode = origEvent.mScanCode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 646 | mFlags = origEvent.mFlags; |
| 647 | // Don't copy mCharacters, since one way or the other we'll lose it |
| 648 | // when changing the action. |
| 649 | } |
| 650 | |
| 651 | /** |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 652 | * Create a new key event that is the same as the given one, but whose |
| 653 | * action is replaced with the given value. |
| 654 | * |
| 655 | * @param event The existing event to be copied. This is not modified. |
| 656 | * @param action The new action code of the event. |
| 657 | */ |
| 658 | public static KeyEvent changeAction(KeyEvent event, int action) { |
| 659 | return new KeyEvent(event, action); |
| 660 | } |
| 661 | |
| 662 | /** |
| 663 | * Create a new key event that is the same as the given one, but whose |
| 664 | * flags are replaced with the given value. |
| 665 | * |
| 666 | * @param event The existing event to be copied. This is not modified. |
| 667 | * @param flags The new flags constant. |
| 668 | */ |
| 669 | public static KeyEvent changeFlags(KeyEvent event, int flags) { |
| 670 | event = new KeyEvent(event); |
| 671 | event.mFlags = flags; |
| 672 | return event; |
| 673 | } |
| 674 | |
| 675 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 676 | * Don't use in new code, instead explicitly check |
| 677 | * {@link #getAction()}. |
| 678 | * |
| 679 | * @return If the action is ACTION_DOWN, returns true; else false. |
| 680 | * |
| 681 | * @deprecated |
| 682 | * @hide |
| 683 | */ |
| 684 | @Deprecated public final boolean isDown() { |
| 685 | return mAction == ACTION_DOWN; |
| 686 | } |
| 687 | |
| 688 | /** |
| 689 | * Is this a system key? System keys can not be used for menu shortcuts. |
| 690 | * |
| 691 | * TODO: this information should come from a table somewhere. |
| 692 | * TODO: should the dpad keys be here? arguably, because they also shouldn't be menu shortcuts |
| 693 | */ |
| 694 | public final boolean isSystem() { |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 695 | return native_isSystemKey(mKeyCode); |
| 696 | } |
| 697 | |
| 698 | /** @hide */ |
| 699 | public final boolean hasDefaultAction() { |
| 700 | return native_hasDefaultAction(mKeyCode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | |
| 704 | /** |
| 705 | * <p>Returns the state of the meta keys.</p> |
| 706 | * |
| 707 | * @return an integer in which each bit set to 1 represents a pressed |
| 708 | * meta key |
| 709 | * |
| 710 | * @see #isAltPressed() |
| 711 | * @see #isShiftPressed() |
| 712 | * @see #isSymPressed() |
| 713 | * @see #META_ALT_ON |
| 714 | * @see #META_SHIFT_ON |
| 715 | * @see #META_SYM_ON |
| 716 | */ |
| 717 | public final int getMetaState() { |
| 718 | return mMetaState; |
| 719 | } |
| 720 | |
| 721 | /** |
| 722 | * Returns the flags for this key event. |
| 723 | * |
| 724 | * @see #FLAG_WOKE_HERE |
| 725 | */ |
| 726 | public final int getFlags() { |
| 727 | return mFlags; |
| 728 | } |
| 729 | |
| 730 | /** |
| 731 | * Returns true if this key code is a modifier key. |
| 732 | * |
| 733 | * @return whether the provided keyCode is one of |
| 734 | * {@link #KEYCODE_SHIFT_LEFT} {@link #KEYCODE_SHIFT_RIGHT}, |
| 735 | * {@link #KEYCODE_ALT_LEFT}, {@link #KEYCODE_ALT_RIGHT} |
| 736 | * or {@link #KEYCODE_SYM}. |
| 737 | */ |
| 738 | public static boolean isModifierKey(int keyCode) { |
| 739 | return keyCode == KEYCODE_SHIFT_LEFT || keyCode == KEYCODE_SHIFT_RIGHT |
| 740 | || keyCode == KEYCODE_ALT_LEFT || keyCode == KEYCODE_ALT_RIGHT |
| 741 | || keyCode == KEYCODE_SYM; |
| 742 | } |
| 743 | |
| 744 | /** |
| 745 | * <p>Returns the pressed state of the ALT meta key.</p> |
| 746 | * |
| 747 | * @return true if the ALT key is pressed, false otherwise |
| 748 | * |
| 749 | * @see #KEYCODE_ALT_LEFT |
| 750 | * @see #KEYCODE_ALT_RIGHT |
| 751 | * @see #META_ALT_ON |
| 752 | */ |
| 753 | public final boolean isAltPressed() { |
| 754 | return (mMetaState & META_ALT_ON) != 0; |
| 755 | } |
| 756 | |
| 757 | /** |
| 758 | * <p>Returns the pressed state of the SHIFT meta key.</p> |
| 759 | * |
| 760 | * @return true if the SHIFT key is pressed, false otherwise |
| 761 | * |
| 762 | * @see #KEYCODE_SHIFT_LEFT |
| 763 | * @see #KEYCODE_SHIFT_RIGHT |
| 764 | * @see #META_SHIFT_ON |
| 765 | */ |
| 766 | public final boolean isShiftPressed() { |
| 767 | return (mMetaState & META_SHIFT_ON) != 0; |
| 768 | } |
| 769 | |
| 770 | /** |
| 771 | * <p>Returns the pressed state of the SYM meta key.</p> |
| 772 | * |
| 773 | * @return true if the SYM key is pressed, false otherwise |
| 774 | * |
| 775 | * @see #KEYCODE_SYM |
| 776 | * @see #META_SYM_ON |
| 777 | */ |
| 778 | public final boolean isSymPressed() { |
| 779 | return (mMetaState & META_SYM_ON) != 0; |
| 780 | } |
| 781 | |
| 782 | /** |
| 783 | * Retrieve the action of this key event. May be either |
| 784 | * {@link #ACTION_DOWN}, {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. |
| 785 | * |
| 786 | * @return The event action: ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE. |
| 787 | */ |
| 788 | public final int getAction() { |
| 789 | return mAction; |
| 790 | } |
| 791 | |
| 792 | /** |
Dianne Hackborn | ddca3ee | 2009-07-23 19:01:31 -0700 | [diff] [blame] | 793 | * For {@link #ACTION_UP} events, indicates that the event has been |
| 794 | * canceled as per {@link #FLAG_CANCELED}. |
| 795 | */ |
| 796 | public final boolean isCanceled() { |
| 797 | return (mFlags&FLAG_CANCELED) != 0; |
| 798 | } |
| 799 | |
| 800 | /** |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 801 | * Call this during {@link Callback#onKeyDown} to have the system track |
| 802 | * the key through its final up (possibly including a long press). Note |
| 803 | * that only one key can be tracked at a time -- if another key down |
| 804 | * event is received while a previous one is being tracked, tracking is |
| 805 | * stopped on the previous event. |
| 806 | */ |
| 807 | public final void startTracking() { |
| 808 | mFlags |= FLAG_START_TRACKING; |
| 809 | } |
| 810 | |
| 811 | /** |
| 812 | * For {@link #ACTION_UP} events, indicates that the event is still being |
| 813 | * tracked from its initial down event as per |
| 814 | * {@link #FLAG_TRACKING}. |
| 815 | */ |
| 816 | public final boolean isTracking() { |
| 817 | return (mFlags&FLAG_TRACKING) != 0; |
| 818 | } |
| 819 | |
| 820 | /** |
| 821 | * For {@link #ACTION_DOWN} events, indicates that the event has been |
| 822 | * canceled as per {@link #FLAG_LONG_PRESS}. |
| 823 | */ |
| 824 | public final boolean isLongPress() { |
| 825 | return (mFlags&FLAG_LONG_PRESS) != 0; |
| 826 | } |
| 827 | |
| 828 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 829 | * Retrieve the key code of the key event. This is the physical key that |
| 830 | * was pressed, <em>not</em> the Unicode character. |
| 831 | * |
| 832 | * @return The key code of the event. |
| 833 | */ |
| 834 | public final int getKeyCode() { |
| 835 | return mKeyCode; |
| 836 | } |
| 837 | |
| 838 | /** |
| 839 | * For the special case of a {@link #ACTION_MULTIPLE} event with key |
| 840 | * code of {@link #KEYCODE_UNKNOWN}, this is a raw string of characters |
| 841 | * associated with the event. In all other cases it is null. |
| 842 | * |
| 843 | * @return Returns a String of 1 or more characters associated with |
| 844 | * the event. |
| 845 | */ |
| 846 | public final String getCharacters() { |
| 847 | return mCharacters; |
| 848 | } |
| 849 | |
| 850 | /** |
| 851 | * Retrieve the hardware key id of this key event. These values are not |
| 852 | * reliable and vary from device to device. |
| 853 | * |
| 854 | * {@more} |
| 855 | * Mostly this is here for debugging purposes. |
| 856 | */ |
| 857 | public final int getScanCode() { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 858 | return mScanCode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | /** |
| 862 | * Retrieve the repeat count of the event. For both key up and key down |
| 863 | * events, this is the number of times the key has repeated with the first |
| 864 | * down starting at 0 and counting up from there. For multiple key |
| 865 | * events, this is the number of down/up pairs that have occurred. |
| 866 | * |
| 867 | * @return The number of times the key has repeated. |
| 868 | */ |
| 869 | public final int getRepeatCount() { |
| 870 | return mRepeatCount; |
| 871 | } |
| 872 | |
| 873 | /** |
| 874 | * Retrieve the time of the most recent key down event, |
| 875 | * in the {@link android.os.SystemClock#uptimeMillis} time base. If this |
| 876 | * is a down event, this will be the same as {@link #getEventTime()}. |
| 877 | * Note that when chording keys, this value is the down time of the |
| 878 | * most recently pressed key, which may <em>not</em> be the same physical |
| 879 | * key of this event. |
| 880 | * |
| 881 | * @return Returns the most recent key down time, in the |
| 882 | * {@link android.os.SystemClock#uptimeMillis} time base |
| 883 | */ |
| 884 | public final long getDownTime() { |
| 885 | return mDownTime; |
| 886 | } |
| 887 | |
| 888 | /** |
| 889 | * Retrieve the time this event occurred, |
| 890 | * in the {@link android.os.SystemClock#uptimeMillis} time base. |
| 891 | * |
| 892 | * @return Returns the time this event occurred, |
| 893 | * in the {@link android.os.SystemClock#uptimeMillis} time base. |
| 894 | */ |
| 895 | public final long getEventTime() { |
| 896 | return mEventTime; |
| 897 | } |
| 898 | |
| 899 | /** |
| 900 | * Return the id for the keyboard that this event came from. A device |
| 901 | * id of 0 indicates the event didn't come from a physical device and |
| 902 | * maps to the default keymap. The other numbers are arbitrary and |
| 903 | * you shouldn't depend on the values. |
| 904 | * |
| 905 | * @see KeyCharacterMap#load |
| 906 | */ |
| 907 | public final int getDeviceId() { |
| 908 | return mDeviceId; |
| 909 | } |
| 910 | |
| 911 | /** |
| 912 | * Renamed to {@link #getDeviceId}. |
| 913 | * |
| 914 | * @hide |
| 915 | * @deprecated |
| 916 | */ |
| 917 | public final int getKeyboardDevice() { |
| 918 | return mDeviceId; |
| 919 | } |
| 920 | |
| 921 | /** |
| 922 | * Get the primary character for this key. In other words, the label |
| 923 | * that is physically printed on it. |
| 924 | */ |
| 925 | public char getDisplayLabel() { |
| 926 | return KeyCharacterMap.load(mDeviceId).getDisplayLabel(mKeyCode); |
| 927 | } |
| 928 | |
| 929 | /** |
| 930 | * <p> |
| 931 | * Returns the Unicode character that the key would produce. |
| 932 | * </p><p> |
| 933 | * Returns 0 if the key is not one that is used to type Unicode |
| 934 | * characters. |
| 935 | * </p><p> |
| 936 | * If the return value has bit |
| 937 | * {@link KeyCharacterMap#COMBINING_ACCENT} |
| 938 | * set, the key is a "dead key" that should be combined with another to |
| 939 | * actually produce a character -- see {@link #getDeadChar} -- |
| 940 | * after masking with |
| 941 | * {@link KeyCharacterMap#COMBINING_ACCENT_MASK}. |
| 942 | * </p> |
| 943 | */ |
| 944 | public int getUnicodeChar() { |
| 945 | return getUnicodeChar(mMetaState); |
| 946 | } |
| 947 | |
| 948 | /** |
| 949 | * <p> |
| 950 | * Returns the Unicode character that the key would produce. |
| 951 | * </p><p> |
| 952 | * Returns 0 if the key is not one that is used to type Unicode |
| 953 | * characters. |
| 954 | * </p><p> |
| 955 | * If the return value has bit |
| 956 | * {@link KeyCharacterMap#COMBINING_ACCENT} |
| 957 | * set, the key is a "dead key" that should be combined with another to |
| 958 | * actually produce a character -- see {@link #getDeadChar} -- after masking |
| 959 | * with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}. |
| 960 | * </p> |
| 961 | */ |
| 962 | public int getUnicodeChar(int meta) { |
| 963 | return KeyCharacterMap.load(mDeviceId).get(mKeyCode, meta); |
| 964 | } |
| 965 | |
| 966 | /** |
| 967 | * Get the characters conversion data for the key event.. |
| 968 | * |
| 969 | * @param results a {@link KeyData} that will be filled with the results. |
| 970 | * |
| 971 | * @return whether the key was mapped or not. If the key was not mapped, |
| 972 | * results is not modified. |
| 973 | */ |
| 974 | public boolean getKeyData(KeyData results) { |
| 975 | return KeyCharacterMap.load(mDeviceId).getKeyData(mKeyCode, results); |
| 976 | } |
| 977 | |
| 978 | /** |
| 979 | * The same as {@link #getMatch(char[],int) getMatch(chars, 0)}. |
| 980 | */ |
| 981 | public char getMatch(char[] chars) { |
| 982 | return getMatch(chars, 0); |
| 983 | } |
| 984 | |
| 985 | /** |
| 986 | * If one of the chars in the array can be generated by the keyCode of this |
| 987 | * key event, return the char; otherwise return '\0'. |
| 988 | * @param chars the characters to try to find |
| 989 | * @param modifiers the modifier bits to prefer. If any of these bits |
| 990 | * are set, if there are multiple choices, that could |
| 991 | * work, the one for this modifier will be set. |
| 992 | */ |
| 993 | public char getMatch(char[] chars, int modifiers) { |
| 994 | return KeyCharacterMap.load(mDeviceId).getMatch(mKeyCode, chars, modifiers); |
| 995 | } |
| 996 | |
| 997 | /** |
| 998 | * Gets the number or symbol associated with the key. The character value |
| 999 | * is returned, not the numeric value. If the key is not a number, but is |
| 1000 | * a symbol, the symbol is retuned. |
| 1001 | */ |
| 1002 | public char getNumber() { |
| 1003 | return KeyCharacterMap.load(mDeviceId).getNumber(mKeyCode); |
| 1004 | } |
| 1005 | |
| 1006 | /** |
| 1007 | * Does the key code of this key produce a glyph? |
| 1008 | */ |
| 1009 | public boolean isPrintingKey() { |
| 1010 | return KeyCharacterMap.load(mDeviceId).isPrintingKey(mKeyCode); |
| 1011 | } |
| 1012 | |
| 1013 | /** |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 1014 | * @deprecated Use {@link #dispatch(Callback, DispatcherState, Object)} instead. |
| 1015 | */ |
| 1016 | @Deprecated |
| 1017 | public final boolean dispatch(Callback receiver) { |
| 1018 | return dispatch(receiver, null, null); |
| 1019 | } |
| 1020 | |
| 1021 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1022 | * Deliver this key event to a {@link Callback} interface. If this is |
| 1023 | * an ACTION_MULTIPLE event and it is not handled, then an attempt will |
| 1024 | * be made to deliver a single normal event. |
| 1025 | * |
| 1026 | * @param receiver The Callback that will be given the event. |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 1027 | * @param state State information retained across events. |
| 1028 | * @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] | 1029 | * |
| 1030 | * @return The return value from the Callback method that was called. |
| 1031 | */ |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 1032 | public final boolean dispatch(Callback receiver, DispatcherState state, |
| 1033 | Object target) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1034 | switch (mAction) { |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 1035 | case ACTION_DOWN: { |
| 1036 | mFlags &= ~FLAG_START_TRACKING; |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 1037 | if (DEBUG) Log.v(TAG, "Key down to " + target + " in " + state |
| 1038 | + ": " + this); |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 1039 | boolean res = receiver.onKeyDown(mKeyCode, this); |
| 1040 | if (state != null) { |
| 1041 | if (res && mRepeatCount == 0 && (mFlags&FLAG_START_TRACKING) != 0) { |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 1042 | if (DEBUG) Log.v(TAG, " Start tracking!"); |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 1043 | state.startTracking(this, target); |
| 1044 | } else if (isLongPress() && state.isTracking(this)) { |
| 1045 | try { |
| 1046 | if (receiver.onKeyLongPress(mKeyCode, this)) { |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 1047 | if (DEBUG) Log.v(TAG, " Clear from long press!"); |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 1048 | state.performedLongPress(this); |
| 1049 | res = true; |
| 1050 | } |
| 1051 | } catch (AbstractMethodError e) { |
| 1052 | } |
| 1053 | } |
| 1054 | } |
| 1055 | return res; |
| 1056 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1057 | case ACTION_UP: |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 1058 | if (DEBUG) Log.v(TAG, "Key up to " + target + " in " + state |
| 1059 | + ": " + this); |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 1060 | if (state != null) { |
| 1061 | state.handleUpEvent(this); |
| 1062 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1063 | return receiver.onKeyUp(mKeyCode, this); |
| 1064 | case ACTION_MULTIPLE: |
| 1065 | final int count = mRepeatCount; |
| 1066 | final int code = mKeyCode; |
| 1067 | if (receiver.onKeyMultiple(code, count, this)) { |
| 1068 | return true; |
| 1069 | } |
| 1070 | if (code != KeyEvent.KEYCODE_UNKNOWN) { |
| 1071 | mAction = ACTION_DOWN; |
| 1072 | mRepeatCount = 0; |
| 1073 | boolean handled = receiver.onKeyDown(code, this); |
| 1074 | if (handled) { |
| 1075 | mAction = ACTION_UP; |
| 1076 | receiver.onKeyUp(code, this); |
| 1077 | } |
| 1078 | mAction = ACTION_MULTIPLE; |
| 1079 | mRepeatCount = count; |
| 1080 | return handled; |
| 1081 | } |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 1082 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1083 | } |
| 1084 | return false; |
| 1085 | } |
| 1086 | |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 1087 | /** |
| 1088 | * Use with {@link KeyEvent#dispatch(Callback, DispatcherState, Object)} |
| 1089 | * for more advanced key dispatching, such as long presses. |
| 1090 | */ |
| 1091 | public static class DispatcherState { |
| 1092 | int mDownKeyCode; |
| 1093 | Object mDownTarget; |
| 1094 | SparseIntArray mActiveLongPresses = new SparseIntArray(); |
| 1095 | |
| 1096 | /** |
| 1097 | * Reset back to initial state. |
| 1098 | */ |
| 1099 | public void reset() { |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 1100 | if (DEBUG) Log.v(TAG, "Reset: " + this); |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 1101 | mDownKeyCode = 0; |
| 1102 | mDownTarget = null; |
| 1103 | mActiveLongPresses.clear(); |
| 1104 | } |
| 1105 | |
| 1106 | /** |
| 1107 | * Stop any tracking associated with this target. |
| 1108 | */ |
| 1109 | public void reset(Object target) { |
| 1110 | if (mDownTarget == target) { |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 1111 | if (DEBUG) Log.v(TAG, "Reset in " + target + ": " + this); |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 1112 | mDownKeyCode = 0; |
| 1113 | mDownTarget = null; |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | /** |
| 1118 | * Start tracking the key code associated with the given event. This |
| 1119 | * can only be called on a key down. It will allow you to see any |
| 1120 | * long press associated with the key, and will result in |
| 1121 | * {@link KeyEvent#isTracking} return true on the long press and up |
| 1122 | * events. |
| 1123 | * |
| 1124 | * <p>This is only needed if you are directly dispatching events, rather |
| 1125 | * than handling them in {@link Callback#onKeyDown}. |
| 1126 | */ |
| 1127 | public void startTracking(KeyEvent event, Object target) { |
| 1128 | if (event.getAction() != ACTION_DOWN) { |
| 1129 | throw new IllegalArgumentException( |
| 1130 | "Can only start tracking on a down event"); |
| 1131 | } |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 1132 | if (DEBUG) Log.v(TAG, "Start trackingt in " + target + ": " + this); |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 1133 | mDownKeyCode = event.getKeyCode(); |
| 1134 | mDownTarget = target; |
| 1135 | } |
| 1136 | |
| 1137 | /** |
| 1138 | * Return true if the key event is for a key code that is currently |
| 1139 | * being tracked by the dispatcher. |
| 1140 | */ |
| 1141 | public boolean isTracking(KeyEvent event) { |
| 1142 | return mDownKeyCode == event.getKeyCode(); |
| 1143 | } |
| 1144 | |
| 1145 | /** |
| 1146 | * Keep track of the given event's key code as having performed an |
| 1147 | * action with a long press, so no action should occur on the up. |
| 1148 | * <p>This is only needed if you are directly dispatching events, rather |
| 1149 | * than handling them in {@link Callback#onKeyLongPress}. |
| 1150 | */ |
| 1151 | public void performedLongPress(KeyEvent event) { |
| 1152 | mActiveLongPresses.put(event.getKeyCode(), 1); |
| 1153 | } |
| 1154 | |
| 1155 | /** |
| 1156 | * Handle key up event to stop tracking. This resets the dispatcher state, |
| 1157 | * and updates the key event state based on it. |
| 1158 | * <p>This is only needed if you are directly dispatching events, rather |
| 1159 | * than handling them in {@link Callback#onKeyUp}. |
| 1160 | */ |
| 1161 | public void handleUpEvent(KeyEvent event) { |
| 1162 | final int keyCode = event.getKeyCode(); |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 1163 | if (DEBUG) Log.v(TAG, "Handle key up " + event + ": " + this); |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 1164 | int index = mActiveLongPresses.indexOfKey(keyCode); |
| 1165 | if (index >= 0) { |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 1166 | if (DEBUG) Log.v(TAG, " Index: " + index); |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 1167 | event.mFlags |= FLAG_CANCELED | FLAG_CANCELED_LONG_PRESS; |
| 1168 | mActiveLongPresses.removeAt(index); |
| 1169 | } |
| 1170 | if (mDownKeyCode == keyCode) { |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 1171 | if (DEBUG) Log.v(TAG, " Tracking!"); |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 1172 | event.mFlags |= FLAG_TRACKING; |
| 1173 | mDownKeyCode = 0; |
| 1174 | mDownTarget = null; |
| 1175 | } |
| 1176 | } |
| 1177 | } |
| 1178 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1179 | public String toString() { |
| 1180 | return "KeyEvent{action=" + mAction + " code=" + mKeyCode |
| 1181 | + " repeat=" + mRepeatCount |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1182 | + " meta=" + mMetaState + " scancode=" + mScanCode |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1183 | + " mFlags=" + mFlags + "}"; |
| 1184 | } |
| 1185 | |
| 1186 | public static final Parcelable.Creator<KeyEvent> CREATOR |
| 1187 | = new Parcelable.Creator<KeyEvent>() { |
| 1188 | public KeyEvent createFromParcel(Parcel in) { |
| 1189 | return new KeyEvent(in); |
| 1190 | } |
| 1191 | |
| 1192 | public KeyEvent[] newArray(int size) { |
| 1193 | return new KeyEvent[size]; |
| 1194 | } |
| 1195 | }; |
| 1196 | |
| 1197 | public int describeContents() { |
| 1198 | return 0; |
| 1199 | } |
| 1200 | |
| 1201 | public void writeToParcel(Parcel out, int flags) { |
| 1202 | out.writeInt(mAction); |
| 1203 | out.writeInt(mKeyCode); |
| 1204 | out.writeInt(mRepeatCount); |
| 1205 | out.writeInt(mMetaState); |
| 1206 | out.writeInt(mDeviceId); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1207 | out.writeInt(mScanCode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1208 | out.writeInt(mFlags); |
| 1209 | out.writeLong(mDownTime); |
| 1210 | out.writeLong(mEventTime); |
| 1211 | } |
| 1212 | |
| 1213 | private KeyEvent(Parcel in) { |
| 1214 | mAction = in.readInt(); |
| 1215 | mKeyCode = in.readInt(); |
| 1216 | mRepeatCount = in.readInt(); |
| 1217 | mMetaState = in.readInt(); |
| 1218 | mDeviceId = in.readInt(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1219 | mScanCode = in.readInt(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1220 | mFlags = in.readInt(); |
| 1221 | mDownTime = in.readLong(); |
| 1222 | mEventTime = in.readLong(); |
| 1223 | } |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 1224 | |
| 1225 | private native boolean native_isSystemKey(int keyCode); |
| 1226 | private native boolean native_hasDefaultAction(int keyCode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1227 | } |