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