KeyEvent.java revision 91c69ab01539f7ba28708f41ec1835cc2920d0a0
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 /** Key code constant: Generic Game Pad Button #1.*/ 537 public static final int KEYCODE_BUTTON_1 = 188; 538 /** Key code constant: Generic Game Pad Button #2.*/ 539 public static final int KEYCODE_BUTTON_2 = 189; 540 /** Key code constant: Generic Game Pad Button #3.*/ 541 public static final int KEYCODE_BUTTON_3 = 190; 542 /** Key code constant: Generic Game Pad Button #4.*/ 543 public static final int KEYCODE_BUTTON_4 = 191; 544 /** Key code constant: Generic Game Pad Button #5.*/ 545 public static final int KEYCODE_BUTTON_5 = 192; 546 /** Key code constant: Generic Game Pad Button #6.*/ 547 public static final int KEYCODE_BUTTON_6 = 193; 548 /** Key code constant: Generic Game Pad Button #7.*/ 549 public static final int KEYCODE_BUTTON_7 = 194; 550 /** Key code constant: Generic Game Pad Button #8.*/ 551 public static final int KEYCODE_BUTTON_8 = 195; 552 /** Key code constant: Generic Game Pad Button #9.*/ 553 public static final int KEYCODE_BUTTON_9 = 196; 554 /** Key code constant: Generic Game Pad Button #10.*/ 555 public static final int KEYCODE_BUTTON_10 = 197; 556 /** Key code constant: Generic Game Pad Button #11.*/ 557 public static final int KEYCODE_BUTTON_11 = 198; 558 /** Key code constant: Generic Game Pad Button #12.*/ 559 public static final int KEYCODE_BUTTON_12 = 199; 560 /** Key code constant: Generic Game Pad Button #13.*/ 561 public static final int KEYCODE_BUTTON_13 = 200; 562 /** Key code constant: Generic Game Pad Button #14.*/ 563 public static final int KEYCODE_BUTTON_14 = 201; 564 /** Key code constant: Generic Game Pad Button #15.*/ 565 public static final int KEYCODE_BUTTON_15 = 202; 566 /** Key code constant: Generic Game Pad Button #16.*/ 567 public static final int KEYCODE_BUTTON_16 = 203; 568 569 private static final int LAST_KEYCODE = KEYCODE_BUTTON_16; 570 571 // NOTE: If you add a new keycode here you must also add it to: 572 // isSystem() 573 // native/include/android/keycodes.h 574 // frameworks/base/include/ui/KeycodeLabels.h 575 // external/webkit/WebKit/android/plugins/ANPKeyCodes.h 576 // frameworks/base/core/res/res/values/attrs.xml 577 // emulator? 578 // 579 // Also Android currently does not reserve code ranges for vendor- 580 // specific key codes. If you have new key codes to have, you 581 // MUST contribute a patch to the open source project to define 582 // those new codes. This is intended to maintain a consistent 583 // set of key code definitions across all Android devices. 584 585 // Symbolic names of all keys indexed by keycode. 586 // There should be exactly LAST_KEYCODE + 1 entries in this table. 587 private static final String[] KEYCODE_SYMBOLIC_NAMES = new String[] { 588 "KEYCODE_UNKNOWN", 589 "KEYCODE_SOFT_LEFT", 590 "KEYCODE_SOFT_RIGHT", 591 "KEYCODE_HOME", 592 "KEYCODE_BACK", 593 "KEYCODE_CALL", 594 "KEYCODE_ENDCALL", 595 "KEYCODE_0", 596 "KEYCODE_1", 597 "KEYCODE_2", 598 "KEYCODE_3", 599 "KEYCODE_4", 600 "KEYCODE_5", 601 "KEYCODE_6", 602 "KEYCODE_7", 603 "KEYCODE_8", 604 "KEYCODE_9", 605 "KEYCODE_STAR", 606 "KEYCODE_POUND", 607 "KEYCODE_DPAD_UP", 608 "KEYCODE_DPAD_DOWN", 609 "KEYCODE_DPAD_LEFT", 610 "KEYCODE_DPAD_RIGHT", 611 "KEYCODE_DPAD_CENTER", 612 "KEYCODE_VOLUME_UP", 613 "KEYCODE_VOLUME_DOWN", 614 "KEYCODE_POWER", 615 "KEYCODE_CAMERA", 616 "KEYCODE_CLEAR", 617 "KEYCODE_A", 618 "KEYCODE_B", 619 "KEYCODE_C", 620 "KEYCODE_D", 621 "KEYCODE_E", 622 "KEYCODE_F", 623 "KEYCODE_G", 624 "KEYCODE_H", 625 "KEYCODE_I", 626 "KEYCODE_J", 627 "KEYCODE_K", 628 "KEYCODE_L", 629 "KEYCODE_M", 630 "KEYCODE_N", 631 "KEYCODE_O", 632 "KEYCODE_P", 633 "KEYCODE_Q", 634 "KEYCODE_R", 635 "KEYCODE_S", 636 "KEYCODE_T", 637 "KEYCODE_U", 638 "KEYCODE_V", 639 "KEYCODE_W", 640 "KEYCODE_X", 641 "KEYCODE_Y", 642 "KEYCODE_Z", 643 "KEYCODE_COMMA", 644 "KEYCODE_PERIOD", 645 "KEYCODE_ALT_LEFT", 646 "KEYCODE_ALT_RIGHT", 647 "KEYCODE_SHIFT_LEFT", 648 "KEYCODE_SHIFT_RIGHT", 649 "KEYCODE_TAB", 650 "KEYCODE_SPACE", 651 "KEYCODE_SYM", 652 "KEYCODE_EXPLORER", 653 "KEYCODE_ENVELOPE", 654 "KEYCODE_ENTER", 655 "KEYCODE_DEL", 656 "KEYCODE_GRAVE", 657 "KEYCODE_MINUS", 658 "KEYCODE_EQUALS", 659 "KEYCODE_LEFT_BRACKET", 660 "KEYCODE_RIGHT_BRACKET", 661 "KEYCODE_BACKSLASH", 662 "KEYCODE_SEMICOLON", 663 "KEYCODE_APOSTROPHE", 664 "KEYCODE_SLASH", 665 "KEYCODE_AT", 666 "KEYCODE_NUM", 667 "KEYCODE_HEADSETHOOK", 668 "KEYCODE_FOCUS", 669 "KEYCODE_PLUS", 670 "KEYCODE_MENU", 671 "KEYCODE_NOTIFICATION", 672 "KEYCODE_SEARCH", 673 "KEYCODE_MEDIA_PLAY_PAUSE", 674 "KEYCODE_MEDIA_STOP", 675 "KEYCODE_MEDIA_NEXT", 676 "KEYCODE_MEDIA_PREVIOUS", 677 "KEYCODE_MEDIA_REWIND", 678 "KEYCODE_MEDIA_FAST_FORWARD", 679 "KEYCODE_MUTE", 680 "KEYCODE_PAGE_UP", 681 "KEYCODE_PAGE_DOWN", 682 "KEYCODE_PICTSYMBOLS", 683 "KEYCODE_SWITCH_CHARSET", 684 "KEYCODE_BUTTON_A", 685 "KEYCODE_BUTTON_B", 686 "KEYCODE_BUTTON_C", 687 "KEYCODE_BUTTON_X", 688 "KEYCODE_BUTTON_Y", 689 "KEYCODE_BUTTON_Z", 690 "KEYCODE_BUTTON_L1", 691 "KEYCODE_BUTTON_R1", 692 "KEYCODE_BUTTON_L2", 693 "KEYCODE_BUTTON_R2", 694 "KEYCODE_BUTTON_THUMBL", 695 "KEYCODE_BUTTON_THUMBR", 696 "KEYCODE_BUTTON_START", 697 "KEYCODE_BUTTON_SELECT", 698 "KEYCODE_BUTTON_MODE", 699 "KEYCODE_ESCAPE", 700 "KEYCODE_FORWARD_DEL", 701 "KEYCODE_CTRL_LEFT", 702 "KEYCODE_CTRL_RIGHT", 703 "KEYCODE_CAPS_LOCK", 704 "KEYCODE_SCROLL_LOCK", 705 "KEYCODE_META_LEFT", 706 "KEYCODE_META_RIGHT", 707 "KEYCODE_FUNCTION", 708 "KEYCODE_SYSRQ", 709 "KEYCODE_BREAK", 710 "KEYCODE_MOVE_HOME", 711 "KEYCODE_MOVE_END", 712 "KEYCODE_INSERT", 713 "KEYCODE_FORWARD", 714 "KEYCODE_MEDIA_PLAY", 715 "KEYCODE_MEDIA_PAUSE", 716 "KEYCODE_MEDIA_CLOSE", 717 "KEYCODE_MEDIA_EJECT", 718 "KEYCODE_MEDIA_RECORD", 719 "KEYCODE_F1", 720 "KEYCODE_F2", 721 "KEYCODE_F3", 722 "KEYCODE_F4", 723 "KEYCODE_F5", 724 "KEYCODE_F6", 725 "KEYCODE_F7", 726 "KEYCODE_F8", 727 "KEYCODE_F9", 728 "KEYCODE_F10", 729 "KEYCODE_F11", 730 "KEYCODE_F12", 731 "KEYCODE_NUM_LOCK", 732 "KEYCODE_NUMPAD_0", 733 "KEYCODE_NUMPAD_1", 734 "KEYCODE_NUMPAD_2", 735 "KEYCODE_NUMPAD_3", 736 "KEYCODE_NUMPAD_4", 737 "KEYCODE_NUMPAD_5", 738 "KEYCODE_NUMPAD_6", 739 "KEYCODE_NUMPAD_7", 740 "KEYCODE_NUMPAD_8", 741 "KEYCODE_NUMPAD_9", 742 "KEYCODE_NUMPAD_DIVIDE", 743 "KEYCODE_NUMPAD_MULTIPLY", 744 "KEYCODE_MUMPAD_SUBTRACT", 745 "KEYCODE_NUMPAD_ADD", 746 "KEYCODE_NUMPAD_DOT", 747 "KEYCODE_NUMPAD_COMMA", 748 "KEYCODE_NUMPAD_ENTER", 749 "KEYCODE_NUMPAD_EQUALS", 750 "KEYCODE_NUMPAD_LEFT_PAREN", 751 "KEYCODE_NUMPAD_RIGHT_PAREN", 752 "KEYCODE_VOLUME_MUTE", 753 "KEYCODE_INFO", 754 "KEYCODE_CHANNEL_UP", 755 "KEYCODE_CHANNEL_DOWN", 756 "KEYCODE_ZOOM_IN", 757 "KEYCODE_ZOOM_OUT", 758 "KEYCODE_TV", 759 "KEYCODE_WINDOW", 760 "KEYCODE_GUIDE", 761 "KEYCODE_DVR", 762 "KEYCODE_BOOKMARK", 763 "KEYCODE_CAPTIONS", 764 "KEYCODE_SETTINGS", 765 "KEYCODE_TV_POWER", 766 "KEYCODE_TV_INPUT", 767 "KEYCODE_STB_INPUT", 768 "KEYCODE_STB_POWER", 769 "KEYCODE_AVR_POWER", 770 "KEYCODE_AVR_INPUT", 771 "KEYCODE_PROG_RED", 772 "KEYCODE_PROG_GREEN", 773 "KEYCODE_PROG_YELLOW", 774 "KEYCODE_PROG_BLUE", 775 "KEYCODE_APP_SWITCH", 776 "KEYCODE_BUTTON_1", 777 "KEYCODE_BUTTON_2", 778 "KEYCODE_BUTTON_3", 779 "KEYCODE_BUTTON_4", 780 "KEYCODE_BUTTON_5", 781 "KEYCODE_BUTTON_6", 782 "KEYCODE_BUTTON_7", 783 "KEYCODE_BUTTON_8", 784 "KEYCODE_BUTTON_9", 785 "KEYCODE_BUTTON_10", 786 "KEYCODE_BUTTON_11", 787 "KEYCODE_BUTTON_12", 788 "KEYCODE_BUTTON_13", 789 "KEYCODE_BUTTON_14", 790 "KEYCODE_BUTTON_15", 791 "KEYCODE_BUTTON_16", 792 }; 793 794 // Symbolic names of all metakeys in bit order from least significant to most significant. 795 // Accordingly there are exactly 32 values in this table. 796 private static final String[] META_SYMBOLIC_NAMES = new String[] { 797 "META_SHIFT_ON", 798 "META_ALT_ON", 799 "META_SYM_ON", 800 "META_FUNCTION_ON", 801 "META_ALT_LEFT_ON", 802 "META_ALT_RIGHT_ON", 803 "META_SHIFT_LEFT_ON", 804 "META_SHIFT_RIGHT_ON", 805 "META_CAP_LOCKED", 806 "META_ALT_LOCKED", 807 "META_SYM_LOCKED", 808 "0x00000800", 809 "META_CTRL_ON", 810 "META_CTRL_LEFT_ON", 811 "META_CTRL_RIGHT_ON", 812 "0x00008000", 813 "META_META_ON", 814 "META_META_LEFT_ON", 815 "META_META_RIGHT_ON", 816 "0x00080000", 817 "META_CAPS_LOCK_ON", 818 "META_NUM_LOCK_ON", 819 "META_SCROLL_LOCK_ON", 820 "0x00800000", 821 "0x01000000", 822 "0x02000000", 823 "0x04000000", 824 "0x08000000", 825 "0x10000000", 826 "0x20000000", 827 "0x40000000", 828 "0x80000000", 829 }; 830 831 /** 832 * @deprecated There are now more than MAX_KEYCODE keycodes. 833 * Use {@link #getMaxKeyCode()} instead. 834 */ 835 @Deprecated 836 public static final int MAX_KEYCODE = 84; 837 838 /** 839 * {@link #getAction} value: the key has been pressed down. 840 */ 841 public static final int ACTION_DOWN = 0; 842 /** 843 * {@link #getAction} value: the key has been released. 844 */ 845 public static final int ACTION_UP = 1; 846 /** 847 * {@link #getAction} value: multiple duplicate key events have 848 * occurred in a row, or a complex string is being delivered. If the 849 * key code is not {#link {@link #KEYCODE_UNKNOWN} then the 850 * {#link {@link #getRepeatCount()} method returns the number of times 851 * the given key code should be executed. 852 * Otherwise, if the key code is {@link #KEYCODE_UNKNOWN}, then 853 * this is a sequence of characters as returned by {@link #getCharacters}. 854 */ 855 public static final int ACTION_MULTIPLE = 2; 856 857 /** 858 * SHIFT key locked in CAPS mode. 859 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API. 860 * @hide 861 */ 862 public static final int META_CAP_LOCKED = 0x100; 863 864 /** 865 * ALT key locked. 866 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API. 867 * @hide 868 */ 869 public static final int META_ALT_LOCKED = 0x200; 870 871 /** 872 * SYM key locked. 873 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API. 874 * @hide 875 */ 876 public static final int META_SYM_LOCKED = 0x400; 877 878 /** 879 * Text is in selection mode. 880 * Reserved for use by {@link MetaKeyKeyListener} for a private unpublished constant 881 * in its API that is currently being retained for legacy reasons. 882 * @hide 883 */ 884 public static final int META_SELECTING = 0x800; 885 886 /** 887 * <p>This mask is used to check whether one of the ALT meta keys is pressed.</p> 888 * 889 * @see #isAltPressed() 890 * @see #getMetaState() 891 * @see #KEYCODE_ALT_LEFT 892 * @see #KEYCODE_ALT_RIGHT 893 */ 894 public static final int META_ALT_ON = 0x02; 895 896 /** 897 * <p>This mask is used to check whether the left ALT meta key is pressed.</p> 898 * 899 * @see #isAltPressed() 900 * @see #getMetaState() 901 * @see #KEYCODE_ALT_LEFT 902 */ 903 public static final int META_ALT_LEFT_ON = 0x10; 904 905 /** 906 * <p>This mask is used to check whether the right the ALT meta key is pressed.</p> 907 * 908 * @see #isAltPressed() 909 * @see #getMetaState() 910 * @see #KEYCODE_ALT_RIGHT 911 */ 912 public static final int META_ALT_RIGHT_ON = 0x20; 913 914 /** 915 * <p>This mask is used to check whether one of the SHIFT meta keys is pressed.</p> 916 * 917 * @see #isShiftPressed() 918 * @see #getMetaState() 919 * @see #KEYCODE_SHIFT_LEFT 920 * @see #KEYCODE_SHIFT_RIGHT 921 */ 922 public static final int META_SHIFT_ON = 0x1; 923 924 /** 925 * <p>This mask is used to check whether the left SHIFT meta key is pressed.</p> 926 * 927 * @see #isShiftPressed() 928 * @see #getMetaState() 929 * @see #KEYCODE_SHIFT_LEFT 930 */ 931 public static final int META_SHIFT_LEFT_ON = 0x40; 932 933 /** 934 * <p>This mask is used to check whether the right SHIFT meta key is pressed.</p> 935 * 936 * @see #isShiftPressed() 937 * @see #getMetaState() 938 * @see #KEYCODE_SHIFT_RIGHT 939 */ 940 public static final int META_SHIFT_RIGHT_ON = 0x80; 941 942 /** 943 * <p>This mask is used to check whether the SYM meta key is pressed.</p> 944 * 945 * @see #isSymPressed() 946 * @see #getMetaState() 947 */ 948 public static final int META_SYM_ON = 0x4; 949 950 /** 951 * <p>This mask is used to check whether the FUNCTION meta key is pressed.</p> 952 * 953 * @see #isFunctionPressed() 954 * @see #getMetaState() 955 */ 956 public static final int META_FUNCTION_ON = 0x8; 957 958 /** 959 * <p>This mask is used to check whether one of the CTRL meta keys is pressed.</p> 960 * 961 * @see #isCtrlPressed() 962 * @see #getMetaState() 963 * @see #KEYCODE_CTRL_LEFT 964 * @see #KEYCODE_CTRL_RIGHT 965 */ 966 public static final int META_CTRL_ON = 0x1000; 967 968 /** 969 * <p>This mask is used to check whether the left CTRL meta key is pressed.</p> 970 * 971 * @see #isCtrlPressed() 972 * @see #getMetaState() 973 * @see #KEYCODE_CTRL_LEFT 974 */ 975 public static final int META_CTRL_LEFT_ON = 0x2000; 976 977 /** 978 * <p>This mask is used to check whether the right CTRL meta key is pressed.</p> 979 * 980 * @see #isCtrlPressed() 981 * @see #getMetaState() 982 * @see #KEYCODE_CTRL_RIGHT 983 */ 984 public static final int META_CTRL_RIGHT_ON = 0x4000; 985 986 /** 987 * <p>This mask is used to check whether one of the META meta keys is pressed.</p> 988 * 989 * @see #isMetaPressed() 990 * @see #getMetaState() 991 * @see #KEYCODE_META_LEFT 992 * @see #KEYCODE_META_RIGHT 993 */ 994 public static final int META_META_ON = 0x10000; 995 996 /** 997 * <p>This mask is used to check whether the left META meta key is pressed.</p> 998 * 999 * @see #isMetaPressed() 1000 * @see #getMetaState() 1001 * @see #KEYCODE_META_LEFT 1002 */ 1003 public static final int META_META_LEFT_ON = 0x20000; 1004 1005 /** 1006 * <p>This mask is used to check whether the right META meta key is pressed.</p> 1007 * 1008 * @see #isMetaPressed() 1009 * @see #getMetaState() 1010 * @see #KEYCODE_META_RIGHT 1011 */ 1012 public static final int META_META_RIGHT_ON = 0x40000; 1013 1014 /** 1015 * <p>This mask is used to check whether the CAPS LOCK meta key is on.</p> 1016 * 1017 * @see #isCapsLockOn() 1018 * @see #getMetaState() 1019 * @see #KEYCODE_CAPS_LOCK 1020 */ 1021 public static final int META_CAPS_LOCK_ON = 0x100000; 1022 1023 /** 1024 * <p>This mask is used to check whether the NUM LOCK meta key is on.</p> 1025 * 1026 * @see #isNumLockOn() 1027 * @see #getMetaState() 1028 * @see #KEYCODE_NUM_LOCK 1029 */ 1030 public static final int META_NUM_LOCK_ON = 0x200000; 1031 1032 /** 1033 * <p>This mask is used to check whether the SCROLL LOCK meta key is on.</p> 1034 * 1035 * @see #isScrollLockOn() 1036 * @see #getMetaState() 1037 * @see #KEYCODE_SCROLL_LOCK 1038 */ 1039 public static final int META_SCROLL_LOCK_ON = 0x400000; 1040 1041 /** 1042 * This mask is a combination of {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON} 1043 * and {@link #META_SHIFT_RIGHT_ON}. 1044 */ 1045 public static final int META_SHIFT_MASK = META_SHIFT_ON 1046 | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON; 1047 1048 /** 1049 * This mask is a combination of {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON} 1050 * and {@link #META_ALT_RIGHT_ON}. 1051 */ 1052 public static final int META_ALT_MASK = META_ALT_ON 1053 | META_ALT_LEFT_ON | META_ALT_RIGHT_ON; 1054 1055 /** 1056 * This mask is a combination of {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON} 1057 * and {@link #META_CTRL_RIGHT_ON}. 1058 */ 1059 public static final int META_CTRL_MASK = META_CTRL_ON 1060 | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON; 1061 1062 /** 1063 * This mask is a combination of {@link #META_META_ON}, {@link #META_META_LEFT_ON} 1064 * and {@link #META_META_RIGHT_ON}. 1065 */ 1066 public static final int META_META_MASK = META_META_ON 1067 | META_META_LEFT_ON | META_META_RIGHT_ON; 1068 1069 /** 1070 * This mask is set if the device woke because of this key event. 1071 */ 1072 public static final int FLAG_WOKE_HERE = 0x1; 1073 1074 /** 1075 * This mask is set if the key event was generated by a software keyboard. 1076 */ 1077 public static final int FLAG_SOFT_KEYBOARD = 0x2; 1078 1079 /** 1080 * This mask is set if we don't want the key event to cause us to leave 1081 * touch mode. 1082 */ 1083 public static final int FLAG_KEEP_TOUCH_MODE = 0x4; 1084 1085 /** 1086 * This mask is set if an event was known to come from a trusted part 1087 * of the system. That is, the event is known to come from the user, 1088 * and could not have been spoofed by a third party component. 1089 */ 1090 public static final int FLAG_FROM_SYSTEM = 0x8; 1091 1092 /** 1093 * This mask is used for compatibility, to identify enter keys that are 1094 * coming from an IME whose enter key has been auto-labelled "next" or 1095 * "done". This allows TextView to dispatch these as normal enter keys 1096 * for old applications, but still do the appropriate action when 1097 * receiving them. 1098 */ 1099 public static final int FLAG_EDITOR_ACTION = 0x10; 1100 1101 /** 1102 * When associated with up key events, this indicates that the key press 1103 * has been canceled. Typically this is used with virtual touch screen 1104 * keys, where the user can slide from the virtual key area on to the 1105 * display: in that case, the application will receive a canceled up 1106 * event and should not perform the action normally associated with the 1107 * key. Note that for this to work, the application can not perform an 1108 * action for a key until it receives an up or the long press timeout has 1109 * expired. 1110 */ 1111 public static final int FLAG_CANCELED = 0x20; 1112 1113 /** 1114 * This key event was generated by a virtual (on-screen) hard key area. 1115 * Typically this is an area of the touchscreen, outside of the regular 1116 * display, dedicated to "hardware" buttons. 1117 */ 1118 public static final int FLAG_VIRTUAL_HARD_KEY = 0x40; 1119 1120 /** 1121 * This flag is set for the first key repeat that occurs after the 1122 * long press timeout. 1123 */ 1124 public static final int FLAG_LONG_PRESS = 0x80; 1125 1126 /** 1127 * Set when a key event has {@link #FLAG_CANCELED} set because a long 1128 * press action was executed while it was down. 1129 */ 1130 public static final int FLAG_CANCELED_LONG_PRESS = 0x100; 1131 1132 /** 1133 * Set for {@link #ACTION_UP} when this event's key code is still being 1134 * tracked from its initial down. That is, somebody requested that tracking 1135 * started on the key down and a long press has not caused 1136 * the tracking to be canceled. 1137 */ 1138 public static final int FLAG_TRACKING = 0x200; 1139 1140 /** 1141 * Set when a key event has been synthesized to implement default behavior 1142 * for an event that the application did not handle. 1143 * Fallback key events are generated by unhandled trackball motions 1144 * (to emulate a directional keypad) and by certain unhandled key presses 1145 * that are declared in the key map (such as special function numeric keypad 1146 * keys when numlock is off). 1147 */ 1148 public static final int FLAG_FALLBACK = 0x400; 1149 1150 /** 1151 * Private control to determine when an app is tracking a key sequence. 1152 * @hide 1153 */ 1154 public static final int FLAG_START_TRACKING = 0x40000000; 1155 1156 /** 1157 * Returns the maximum keycode. 1158 */ 1159 public static int getMaxKeyCode() { 1160 return LAST_KEYCODE; 1161 } 1162 1163 /** 1164 * Get the character that is produced by putting accent on the character 1165 * c. 1166 * For example, getDeadChar('`', 'e') returns è. 1167 */ 1168 public static int getDeadChar(int accent, int c) { 1169 return KeyCharacterMap.getDeadChar(accent, c); 1170 } 1171 1172 static final boolean DEBUG = false; 1173 static final String TAG = "KeyEvent"; 1174 1175 private static final int MAX_RECYCLED = 10; 1176 private static final Object gRecyclerLock = new Object(); 1177 private static int gRecyclerUsed; 1178 private static KeyEvent gRecyclerTop; 1179 1180 private KeyEvent mNext; 1181 private boolean mRecycled; 1182 1183 private int mDeviceId; 1184 private int mSource; 1185 private int mMetaState; 1186 private int mAction; 1187 private int mKeyCode; 1188 private int mScanCode; 1189 private int mRepeatCount; 1190 private int mFlags; 1191 private long mDownTime; 1192 private long mEventTime; 1193 private String mCharacters; 1194 1195 public interface Callback { 1196 /** 1197 * Called when a key down event has occurred. If you return true, 1198 * you can first call {@link KeyEvent#startTracking() 1199 * KeyEvent.startTracking()} to have the framework track the event 1200 * through its {@link #onKeyUp(int, KeyEvent)} and also call your 1201 * {@link #onKeyLongPress(int, KeyEvent)} if it occurs. 1202 * 1203 * @param keyCode The value in event.getKeyCode(). 1204 * @param event Description of the key event. 1205 * 1206 * @return If you handled the event, return true. If you want to allow 1207 * the event to be handled by the next receiver, return false. 1208 */ 1209 boolean onKeyDown(int keyCode, KeyEvent event); 1210 1211 /** 1212 * Called when a long press has occurred. If you return true, 1213 * the final key up will have {@link KeyEvent#FLAG_CANCELED} and 1214 * {@link KeyEvent#FLAG_CANCELED_LONG_PRESS} set. Note that in 1215 * order to receive this callback, someone in the event change 1216 * <em>must</em> return true from {@link #onKeyDown} <em>and</em> 1217 * call {@link KeyEvent#startTracking()} on the event. 1218 * 1219 * @param keyCode The value in event.getKeyCode(). 1220 * @param event Description of the key event. 1221 * 1222 * @return If you handled the event, return true. If you want to allow 1223 * the event to be handled by the next receiver, return false. 1224 */ 1225 boolean onKeyLongPress(int keyCode, KeyEvent event); 1226 1227 /** 1228 * Called when a key up event has occurred. 1229 * 1230 * @param keyCode The value in event.getKeyCode(). 1231 * @param event Description of the key event. 1232 * 1233 * @return If you handled the event, return true. If you want to allow 1234 * the event to be handled by the next receiver, return false. 1235 */ 1236 boolean onKeyUp(int keyCode, KeyEvent event); 1237 1238 /** 1239 * Called when multiple down/up pairs of the same key have occurred 1240 * in a row. 1241 * 1242 * @param keyCode The value in event.getKeyCode(). 1243 * @param count Number of pairs as returned by event.getRepeatCount(). 1244 * @param event Description of the key event. 1245 * 1246 * @return If you handled the event, return true. If you want to allow 1247 * the event to be handled by the next receiver, return false. 1248 */ 1249 boolean onKeyMultiple(int keyCode, int count, KeyEvent event); 1250 } 1251 1252 static { 1253 if (META_SYMBOLIC_NAMES.length != 32) { 1254 throw new IllegalStateException( 1255 "META_SYMBOLIC_NAMES array should contain exactly 32 entries."); 1256 } 1257 if (KEYCODE_SYMBOLIC_NAMES.length != LAST_KEYCODE + 1) { 1258 throw new IllegalStateException( 1259 "KEYCODE_SYMBOLIC_NAMES array is out of sync with the keycode constants."); 1260 } 1261 } 1262 1263 private KeyEvent() { 1264 } 1265 1266 /** 1267 * Create a new key event. 1268 * 1269 * @param action Action code: either {@link #ACTION_DOWN}, 1270 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 1271 * @param code The key code. 1272 */ 1273 public KeyEvent(int action, int code) { 1274 mAction = action; 1275 mKeyCode = code; 1276 mRepeatCount = 0; 1277 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD; 1278 } 1279 1280 /** 1281 * Create a new key event. 1282 * 1283 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1284 * at which this key code originally went down. 1285 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1286 * at which this event happened. 1287 * @param action Action code: either {@link #ACTION_DOWN}, 1288 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 1289 * @param code The key code. 1290 * @param repeat A repeat count for down events (> 0 if this is after the 1291 * initial down) or event count for multiple events. 1292 */ 1293 public KeyEvent(long downTime, long eventTime, int action, 1294 int code, int repeat) { 1295 mDownTime = downTime; 1296 mEventTime = eventTime; 1297 mAction = action; 1298 mKeyCode = code; 1299 mRepeatCount = repeat; 1300 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD; 1301 } 1302 1303 /** 1304 * Create a new key event. 1305 * 1306 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1307 * at which this key code originally went down. 1308 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1309 * at which this event happened. 1310 * @param action Action code: either {@link #ACTION_DOWN}, 1311 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 1312 * @param code The key code. 1313 * @param repeat A repeat count for down events (> 0 if this is after the 1314 * initial down) or event count for multiple events. 1315 * @param metaState Flags indicating which meta keys are currently pressed. 1316 */ 1317 public KeyEvent(long downTime, long eventTime, int action, 1318 int code, int repeat, int metaState) { 1319 mDownTime = downTime; 1320 mEventTime = eventTime; 1321 mAction = action; 1322 mKeyCode = code; 1323 mRepeatCount = repeat; 1324 mMetaState = metaState; 1325 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD; 1326 } 1327 1328 /** 1329 * Create a new key event. 1330 * 1331 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1332 * at which this key code originally went down. 1333 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1334 * at which this event happened. 1335 * @param action Action code: either {@link #ACTION_DOWN}, 1336 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 1337 * @param code The key code. 1338 * @param repeat A repeat count for down events (> 0 if this is after the 1339 * initial down) or event count for multiple events. 1340 * @param metaState Flags indicating which meta keys are currently pressed. 1341 * @param deviceId The device ID that generated the key event. 1342 * @param scancode Raw device scan code of the event. 1343 */ 1344 public KeyEvent(long downTime, long eventTime, int action, 1345 int code, int repeat, int metaState, 1346 int deviceId, int scancode) { 1347 mDownTime = downTime; 1348 mEventTime = eventTime; 1349 mAction = action; 1350 mKeyCode = code; 1351 mRepeatCount = repeat; 1352 mMetaState = metaState; 1353 mDeviceId = deviceId; 1354 mScanCode = scancode; 1355 } 1356 1357 /** 1358 * Create a new key event. 1359 * 1360 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1361 * at which this key code originally went down. 1362 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1363 * at which this event happened. 1364 * @param action Action code: either {@link #ACTION_DOWN}, 1365 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 1366 * @param code The key code. 1367 * @param repeat A repeat count for down events (> 0 if this is after the 1368 * initial down) or event count for multiple events. 1369 * @param metaState Flags indicating which meta keys are currently pressed. 1370 * @param deviceId The device ID that generated the key event. 1371 * @param scancode Raw device scan code of the event. 1372 * @param flags The flags for this key event 1373 */ 1374 public KeyEvent(long downTime, long eventTime, int action, 1375 int code, int repeat, int metaState, 1376 int deviceId, int scancode, int flags) { 1377 mDownTime = downTime; 1378 mEventTime = eventTime; 1379 mAction = action; 1380 mKeyCode = code; 1381 mRepeatCount = repeat; 1382 mMetaState = metaState; 1383 mDeviceId = deviceId; 1384 mScanCode = scancode; 1385 mFlags = flags; 1386 } 1387 1388 /** 1389 * Create a new key event. 1390 * 1391 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1392 * at which this key code originally went down. 1393 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1394 * at which this event happened. 1395 * @param action Action code: either {@link #ACTION_DOWN}, 1396 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 1397 * @param code The key code. 1398 * @param repeat A repeat count for down events (> 0 if this is after the 1399 * initial down) or event count for multiple events. 1400 * @param metaState Flags indicating which meta keys are currently pressed. 1401 * @param deviceId The device ID that generated the key event. 1402 * @param scancode Raw device scan code of the event. 1403 * @param flags The flags for this key event 1404 * @param source The input source such as {@link InputDevice#SOURCE_KEYBOARD}. 1405 */ 1406 public KeyEvent(long downTime, long eventTime, int action, 1407 int code, int repeat, int metaState, 1408 int deviceId, int scancode, int flags, int source) { 1409 mDownTime = downTime; 1410 mEventTime = eventTime; 1411 mAction = action; 1412 mKeyCode = code; 1413 mRepeatCount = repeat; 1414 mMetaState = metaState; 1415 mDeviceId = deviceId; 1416 mScanCode = scancode; 1417 mFlags = flags; 1418 mSource = source; 1419 } 1420 1421 /** 1422 * Create a new key event for a string of characters. The key code, 1423 * action, repeat count and source will automatically be set to 1424 * {@link #KEYCODE_UNKNOWN}, {@link #ACTION_MULTIPLE}, 0, and 1425 * {@link InputDevice#SOURCE_KEYBOARD} for you. 1426 * 1427 * @param time The time (in {@link android.os.SystemClock#uptimeMillis}) 1428 * at which this event occured. 1429 * @param characters The string of characters. 1430 * @param deviceId The device ID that generated the key event. 1431 * @param flags The flags for this key event 1432 */ 1433 public KeyEvent(long time, String characters, int deviceId, int flags) { 1434 mDownTime = time; 1435 mEventTime = time; 1436 mCharacters = characters; 1437 mAction = ACTION_MULTIPLE; 1438 mKeyCode = KEYCODE_UNKNOWN; 1439 mRepeatCount = 0; 1440 mDeviceId = deviceId; 1441 mFlags = flags; 1442 mSource = InputDevice.SOURCE_KEYBOARD; 1443 } 1444 1445 /** 1446 * Make an exact copy of an existing key event. 1447 */ 1448 public KeyEvent(KeyEvent origEvent) { 1449 mDownTime = origEvent.mDownTime; 1450 mEventTime = origEvent.mEventTime; 1451 mAction = origEvent.mAction; 1452 mKeyCode = origEvent.mKeyCode; 1453 mRepeatCount = origEvent.mRepeatCount; 1454 mMetaState = origEvent.mMetaState; 1455 mDeviceId = origEvent.mDeviceId; 1456 mSource = origEvent.mSource; 1457 mScanCode = origEvent.mScanCode; 1458 mFlags = origEvent.mFlags; 1459 mCharacters = origEvent.mCharacters; 1460 } 1461 1462 /** 1463 * Copy an existing key event, modifying its time and repeat count. 1464 * 1465 * @deprecated Use {@link #changeTimeRepeat(KeyEvent, long, int)} 1466 * instead. 1467 * 1468 * @param origEvent The existing event to be copied. 1469 * @param eventTime The new event time 1470 * (in {@link android.os.SystemClock#uptimeMillis}) of the event. 1471 * @param newRepeat The new repeat count of the event. 1472 */ 1473 @Deprecated 1474 public KeyEvent(KeyEvent origEvent, long eventTime, int newRepeat) { 1475 mDownTime = origEvent.mDownTime; 1476 mEventTime = eventTime; 1477 mAction = origEvent.mAction; 1478 mKeyCode = origEvent.mKeyCode; 1479 mRepeatCount = newRepeat; 1480 mMetaState = origEvent.mMetaState; 1481 mDeviceId = origEvent.mDeviceId; 1482 mSource = origEvent.mSource; 1483 mScanCode = origEvent.mScanCode; 1484 mFlags = origEvent.mFlags; 1485 mCharacters = origEvent.mCharacters; 1486 } 1487 1488 private static KeyEvent obtain() { 1489 final KeyEvent ev; 1490 synchronized (gRecyclerLock) { 1491 ev = gRecyclerTop; 1492 if (ev == null) { 1493 return new KeyEvent(); 1494 } 1495 gRecyclerTop = ev.mNext; 1496 gRecyclerUsed -= 1; 1497 } 1498 ev.mRecycled = false; 1499 ev.mNext = null; 1500 return ev; 1501 } 1502 1503 /** 1504 * Obtains a (potentially recycled) key event. 1505 * 1506 * @hide 1507 */ 1508 public static KeyEvent obtain(long downTime, long eventTime, int action, 1509 int code, int repeat, int metaState, 1510 int deviceId, int scancode, int flags, int source, String characters) { 1511 KeyEvent ev = obtain(); 1512 ev.mDownTime = downTime; 1513 ev.mEventTime = eventTime; 1514 ev.mAction = action; 1515 ev.mKeyCode = code; 1516 ev.mRepeatCount = repeat; 1517 ev.mMetaState = metaState; 1518 ev.mDeviceId = deviceId; 1519 ev.mScanCode = scancode; 1520 ev.mFlags = flags; 1521 ev.mSource = source; 1522 ev.mCharacters = characters; 1523 return ev; 1524 } 1525 1526 /** 1527 * Recycles a key event. 1528 * Key events should only be recycled if they are owned by the system since user 1529 * code expects them to be essentially immutable, "tracking" notwithstanding. 1530 * 1531 * @hide 1532 */ 1533 public final void recycle() { 1534 if (mRecycled) { 1535 throw new RuntimeException(toString() + " recycled twice!"); 1536 } 1537 mRecycled = true; 1538 mCharacters = null; 1539 1540 synchronized (gRecyclerLock) { 1541 if (gRecyclerUsed < MAX_RECYCLED) { 1542 gRecyclerUsed++; 1543 mNext = gRecyclerTop; 1544 gRecyclerTop = this; 1545 } 1546 } 1547 } 1548 1549 /** 1550 * Create a new key event that is the same as the given one, but whose 1551 * event time and repeat count are replaced with the given value. 1552 * 1553 * @param event The existing event to be copied. This is not modified. 1554 * @param eventTime The new event time 1555 * (in {@link android.os.SystemClock#uptimeMillis}) of the event. 1556 * @param newRepeat The new repeat count of the event. 1557 */ 1558 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime, 1559 int newRepeat) { 1560 return new KeyEvent(event, eventTime, newRepeat); 1561 } 1562 1563 /** 1564 * Create a new key event that is the same as the given one, but whose 1565 * event time and repeat count are replaced with the given value. 1566 * 1567 * @param event The existing event to be copied. This is not modified. 1568 * @param eventTime The new event time 1569 * (in {@link android.os.SystemClock#uptimeMillis}) of the event. 1570 * @param newRepeat The new repeat count of the event. 1571 * @param newFlags New flags for the event, replacing the entire value 1572 * in the original event. 1573 */ 1574 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime, 1575 int newRepeat, int newFlags) { 1576 KeyEvent ret = new KeyEvent(event); 1577 ret.mEventTime = eventTime; 1578 ret.mRepeatCount = newRepeat; 1579 ret.mFlags = newFlags; 1580 return ret; 1581 } 1582 1583 /** 1584 * Copy an existing key event, modifying its action. 1585 * 1586 * @param origEvent The existing event to be copied. 1587 * @param action The new action code of the event. 1588 */ 1589 private KeyEvent(KeyEvent origEvent, int action) { 1590 mDownTime = origEvent.mDownTime; 1591 mEventTime = origEvent.mEventTime; 1592 mAction = action; 1593 mKeyCode = origEvent.mKeyCode; 1594 mRepeatCount = origEvent.mRepeatCount; 1595 mMetaState = origEvent.mMetaState; 1596 mDeviceId = origEvent.mDeviceId; 1597 mSource = origEvent.mSource; 1598 mScanCode = origEvent.mScanCode; 1599 mFlags = origEvent.mFlags; 1600 // Don't copy mCharacters, since one way or the other we'll lose it 1601 // when changing the action. 1602 } 1603 1604 /** 1605 * Create a new key event that is the same as the given one, but whose 1606 * action is replaced with the given value. 1607 * 1608 * @param event The existing event to be copied. This is not modified. 1609 * @param action The new action code of the event. 1610 */ 1611 public static KeyEvent changeAction(KeyEvent event, int action) { 1612 return new KeyEvent(event, action); 1613 } 1614 1615 /** 1616 * Create a new key event that is the same as the given one, but whose 1617 * flags are replaced with the given value. 1618 * 1619 * @param event The existing event to be copied. This is not modified. 1620 * @param flags The new flags constant. 1621 */ 1622 public static KeyEvent changeFlags(KeyEvent event, int flags) { 1623 event = new KeyEvent(event); 1624 event.mFlags = flags; 1625 return event; 1626 } 1627 1628 /** 1629 * Don't use in new code, instead explicitly check 1630 * {@link #getAction()}. 1631 * 1632 * @return If the action is ACTION_DOWN, returns true; else false. 1633 * 1634 * @deprecated 1635 * @hide 1636 */ 1637 @Deprecated public final boolean isDown() { 1638 return mAction == ACTION_DOWN; 1639 } 1640 1641 /** 1642 * Is this a system key? System keys can not be used for menu shortcuts. 1643 * 1644 * TODO: this information should come from a table somewhere. 1645 * TODO: should the dpad keys be here? arguably, because they also shouldn't be menu shortcuts 1646 */ 1647 public final boolean isSystem() { 1648 return native_isSystemKey(mKeyCode); 1649 } 1650 1651 /** @hide */ 1652 public final boolean hasDefaultAction() { 1653 return native_hasDefaultAction(mKeyCode); 1654 } 1655 1656 /** {@inheritDoc} */ 1657 @Override 1658 public final int getDeviceId() { 1659 return mDeviceId; 1660 } 1661 1662 /** {@inheritDoc} */ 1663 @Override 1664 public final int getSource() { 1665 return mSource; 1666 } 1667 1668 /** {@inheritDoc} */ 1669 @Override 1670 public final void setSource(int source) { 1671 mSource = source; 1672 } 1673 1674 /** 1675 * <p>Returns the state of the meta keys.</p> 1676 * 1677 * @return an integer in which each bit set to 1 represents a pressed 1678 * meta key 1679 * 1680 * @see #isAltPressed() 1681 * @see #isShiftPressed() 1682 * @see #isSymPressed() 1683 * @see #isCtrlPressed() 1684 * @see #isMetaPressed() 1685 * @see #isFunctionPressed() 1686 * @see #isCapsLockOn() 1687 * @see #isNumLockOn() 1688 * @see #isScrollLockOn() 1689 * @see #META_ALT_ON 1690 * @see #META_ALT_LEFT_ON 1691 * @see #META_ALT_RIGHT_ON 1692 * @see #META_SHIFT_ON 1693 * @see #META_SHIFT_LEFT_ON 1694 * @see #META_SHIFT_RIGHT_ON 1695 * @see #META_SYM_ON 1696 * @see #META_FUNCTION_ON 1697 * @see #META_CTRL_ON 1698 * @see #META_CTRL_LEFT_ON 1699 * @see #META_CTRL_RIGHT_ON 1700 * @see #META_META_ON 1701 * @see #META_META_LEFT_ON 1702 * @see #META_META_RIGHT_ON 1703 * @see #META_CAPS_LOCK_ON 1704 * @see #META_NUM_LOCK_ON 1705 * @see #META_SCROLL_LOCK_ON 1706 */ 1707 public final int getMetaState() { 1708 return mMetaState; 1709 } 1710 1711 /** 1712 * Returns the flags for this key event. 1713 * 1714 * @see #FLAG_WOKE_HERE 1715 */ 1716 public final int getFlags() { 1717 return mFlags; 1718 } 1719 1720 // Mask of all modifier key meta states. Specifically excludes locked keys like caps lock. 1721 private static final int META_MODIFIER_MASK = 1722 META_SHIFT_ON | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON 1723 | META_ALT_ON | META_ALT_LEFT_ON | META_ALT_RIGHT_ON 1724 | META_CTRL_ON | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON 1725 | META_META_ON | META_META_LEFT_ON | META_META_RIGHT_ON 1726 | META_SYM_ON | META_FUNCTION_ON; 1727 1728 // Mask of all lock key meta states. 1729 private static final int META_LOCK_MASK = 1730 META_CAPS_LOCK_ON | META_NUM_LOCK_ON | META_SCROLL_LOCK_ON; 1731 1732 // Mask of all valid meta states. 1733 private static final int META_ALL_MASK = META_MODIFIER_MASK | META_LOCK_MASK; 1734 1735 // Mask of all synthetic meta states that are reserved for API compatibility with 1736 // historical uses in MetaKeyKeyListener. 1737 private static final int META_SYNTHETIC_MASK = 1738 META_CAP_LOCKED | META_ALT_LOCKED | META_SYM_LOCKED | META_SELECTING; 1739 1740 // Mask of all meta states that are not valid use in specifying a modifier key. 1741 // These bits are known to be used for purposes other than specifying modifiers. 1742 private static final int META_INVALID_MODIFIER_MASK = 1743 META_LOCK_MASK | META_SYNTHETIC_MASK; 1744 1745 /** 1746 * Gets a mask that includes all valid modifier key meta state bits. 1747 * <p> 1748 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK}, 1749 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are 1750 * not considered modifier keys. Consequently, the mask specifically excludes 1751 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}. 1752 * </p> 1753 * 1754 * @return The modifier meta state mask which is a combination of 1755 * {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON}, {@link #META_SHIFT_RIGHT_ON}, 1756 * {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON}, {@link #META_ALT_RIGHT_ON}, 1757 * {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON}, {@link #META_CTRL_RIGHT_ON}, 1758 * {@link #META_META_ON}, {@link #META_META_LEFT_ON}, {@link #META_META_RIGHT_ON}, 1759 * {@link #META_SYM_ON}, {@link #META_FUNCTION_ON}. 1760 */ 1761 public static int getModifierMetaStateMask() { 1762 return META_MODIFIER_MASK; 1763 } 1764 1765 /** 1766 * Returns true if this key code is a modifier key. 1767 * <p> 1768 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK}, 1769 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are 1770 * not considered modifier keys. Consequently, this function return false 1771 * for those keys. 1772 * </p> 1773 * 1774 * @return True if the key code is one of 1775 * {@link #KEYCODE_SHIFT_LEFT} {@link #KEYCODE_SHIFT_RIGHT}, 1776 * {@link #KEYCODE_ALT_LEFT}, {@link #KEYCODE_ALT_RIGHT}, 1777 * {@link #KEYCODE_CTRL_LEFT}, {@link #KEYCODE_CTRL_RIGHT}, 1778 * {@link #KEYCODE_META_LEFT}, or {@link #KEYCODE_META_RIGHT}, 1779 * {@link #KEYCODE_SYM}, {@link #KEYCODE_NUM}, {@link #KEYCODE_FUNCTION}. 1780 */ 1781 public static boolean isModifierKey(int keyCode) { 1782 switch (keyCode) { 1783 case KEYCODE_SHIFT_LEFT: 1784 case KEYCODE_SHIFT_RIGHT: 1785 case KEYCODE_ALT_LEFT: 1786 case KEYCODE_ALT_RIGHT: 1787 case KEYCODE_CTRL_LEFT: 1788 case KEYCODE_CTRL_RIGHT: 1789 case KEYCODE_META_LEFT: 1790 case KEYCODE_META_RIGHT: 1791 case KEYCODE_SYM: 1792 case KEYCODE_NUM: 1793 case KEYCODE_FUNCTION: 1794 return true; 1795 default: 1796 return false; 1797 } 1798 } 1799 1800 /** 1801 * Normalizes the specified meta state. 1802 * <p> 1803 * The meta state is normalized such that if either the left or right modifier meta state 1804 * bits are set then the result will also include the universal bit for that modifier. 1805 * </p><p> 1806 * If the specified meta state contains {@link #META_ALT_LEFT_ON} then 1807 * the result will also contain {@link #META_ALT_ON} in addition to {@link #META_ALT_LEFT_ON} 1808 * and the other bits that were specified in the input. The same is process is 1809 * performed for shift, control and meta. 1810 * </p><p> 1811 * If the specified meta state contains synthetic meta states defined by 1812 * {@link MetaKeyKeyListener}, then those states are translated here and the original 1813 * synthetic meta states are removed from the result. 1814 * {@link MetaKeyKeyListener#META_CAP_LOCKED} is translated to {@link #META_CAPS_LOCK_ON}. 1815 * {@link MetaKeyKeyListener#META_ALT_LOCKED} is translated to {@link #META_ALT_ON}. 1816 * {@link MetaKeyKeyListener#META_SYM_LOCKED} is translated to {@link #META_SYM_ON}. 1817 * </p><p> 1818 * Undefined meta state bits are removed. 1819 * </p> 1820 * 1821 * @param metaState The meta state. 1822 * @return The normalized meta state. 1823 */ 1824 public static int normalizeMetaState(int metaState) { 1825 if ((metaState & (META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON)) != 0) { 1826 metaState |= META_SHIFT_ON; 1827 } 1828 if ((metaState & (META_ALT_LEFT_ON | META_ALT_RIGHT_ON)) != 0) { 1829 metaState |= META_ALT_ON; 1830 } 1831 if ((metaState & (META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON)) != 0) { 1832 metaState |= META_CTRL_ON; 1833 } 1834 if ((metaState & (META_META_LEFT_ON | META_META_RIGHT_ON)) != 0) { 1835 metaState |= META_META_ON; 1836 } 1837 if ((metaState & MetaKeyKeyListener.META_CAP_LOCKED) != 0) { 1838 metaState |= META_CAPS_LOCK_ON; 1839 } 1840 if ((metaState & MetaKeyKeyListener.META_ALT_LOCKED) != 0) { 1841 metaState |= META_ALT_ON; 1842 } 1843 if ((metaState & MetaKeyKeyListener.META_SYM_LOCKED) != 0) { 1844 metaState |= META_SYM_ON; 1845 } 1846 return metaState & META_ALL_MASK; 1847 } 1848 1849 /** 1850 * Returns true if no modifiers keys are pressed according to the specified meta state. 1851 * <p> 1852 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK}, 1853 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are 1854 * not considered modifier keys. Consequently, this function ignores 1855 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}. 1856 * </p><p> 1857 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}. 1858 * </p> 1859 * 1860 * @param metaState The meta state to consider. 1861 * @return True if no modifier keys are pressed. 1862 * @see #hasNoModifiers() 1863 */ 1864 public static boolean metaStateHasNoModifiers(int metaState) { 1865 return (normalizeMetaState(metaState) & META_MODIFIER_MASK) == 0; 1866 } 1867 1868 /** 1869 * Returns true if only the specified modifier keys are pressed according to 1870 * the specified meta state. Returns false if a different combination of modifier 1871 * keys are pressed. 1872 * <p> 1873 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK}, 1874 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are 1875 * not considered modifier keys. Consequently, this function ignores 1876 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}. 1877 * </p><p> 1878 * If the specified modifier mask includes directional modifiers, such as 1879 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the 1880 * modifier is pressed on that side. 1881 * If the specified modifier mask includes non-directional modifiers, such as 1882 * {@link #META_SHIFT_ON}, then this method ensures that the modifier 1883 * is pressed on either side. 1884 * If the specified modifier mask includes both directional and non-directional modifiers 1885 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON}, 1886 * then this method throws an illegal argument exception. 1887 * </p> 1888 * 1889 * @param metaState The meta state to consider. 1890 * @param modifiers The meta state of the modifier keys to check. May be a combination 1891 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to 1892 * ensure that no modifier keys are pressed. 1893 * @return True if only the specified modifier keys are pressed. 1894 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers 1895 * @see #hasModifiers 1896 */ 1897 public static boolean metaStateHasModifiers(int metaState, int modifiers) { 1898 // Note: For forward compatibility, we allow the parameter to contain meta states 1899 // that we do not recognize but we explicitly disallow meta states that 1900 // are not valid modifiers. 1901 if ((modifiers & META_INVALID_MODIFIER_MASK) != 0) { 1902 throw new IllegalArgumentException("modifiers must not contain " 1903 + "META_CAPS_LOCK_ON, META_NUM_LOCK_ON, META_SCROLL_LOCK_ON, " 1904 + "META_CAP_LOCKED, META_ALT_LOCKED, META_SYM_LOCKED, " 1905 + "or META_SELECTING"); 1906 } 1907 1908 metaState = normalizeMetaState(metaState) & META_MODIFIER_MASK; 1909 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers, 1910 META_SHIFT_ON, META_SHIFT_LEFT_ON, META_SHIFT_RIGHT_ON); 1911 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers, 1912 META_ALT_ON, META_ALT_LEFT_ON, META_ALT_RIGHT_ON); 1913 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers, 1914 META_CTRL_ON, META_CTRL_LEFT_ON, META_CTRL_RIGHT_ON); 1915 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers, 1916 META_META_ON, META_META_LEFT_ON, META_META_RIGHT_ON); 1917 return metaState == modifiers; 1918 } 1919 1920 private static int metaStateFilterDirectionalModifiers(int metaState, 1921 int modifiers, int basic, int left, int right) { 1922 final boolean wantBasic = (modifiers & basic) != 0; 1923 final int directional = left | right; 1924 final boolean wantLeftOrRight = (modifiers & directional) != 0; 1925 1926 if (wantBasic) { 1927 if (wantLeftOrRight) { 1928 throw new IllegalArgumentException("modifiers must not contain " 1929 + metaStateToString(basic) + " combined with " 1930 + metaStateToString(left) + " or " + metaStateToString(right)); 1931 } 1932 return metaState & ~directional; 1933 } else if (wantLeftOrRight) { 1934 return metaState & ~basic; 1935 } else { 1936 return metaState; 1937 } 1938 } 1939 1940 /** 1941 * Returns true if no modifier keys are pressed. 1942 * <p> 1943 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK}, 1944 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are 1945 * not considered modifier keys. Consequently, this function ignores 1946 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}. 1947 * </p><p> 1948 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}. 1949 * </p> 1950 * 1951 * @return True if no modifier keys are pressed. 1952 * @see #metaStateHasNoModifiers 1953 */ 1954 public final boolean hasNoModifiers() { 1955 return metaStateHasNoModifiers(mMetaState); 1956 } 1957 1958 /** 1959 * Returns true if only the specified modifiers keys are pressed. 1960 * Returns false if a different combination of modifier keys are pressed. 1961 * <p> 1962 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK}, 1963 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are 1964 * not considered modifier keys. Consequently, this function ignores 1965 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}. 1966 * </p><p> 1967 * If the specified modifier mask includes directional modifiers, such as 1968 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the 1969 * modifier is pressed on that side. 1970 * If the specified modifier mask includes non-directional modifiers, such as 1971 * {@link #META_SHIFT_ON}, then this method ensures that the modifier 1972 * is pressed on either side. 1973 * If the specified modifier mask includes both directional and non-directional modifiers 1974 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON}, 1975 * then this method throws an illegal argument exception. 1976 * </p> 1977 * 1978 * @param modifiers The meta state of the modifier keys to check. May be a combination 1979 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to 1980 * ensure that no modifier keys are pressed. 1981 * @return True if only the specified modifier keys are pressed. 1982 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers 1983 * @see #metaStateHasModifiers 1984 */ 1985 public final boolean hasModifiers(int modifiers) { 1986 return metaStateHasModifiers(mMetaState, modifiers); 1987 } 1988 1989 /** 1990 * <p>Returns the pressed state of the ALT meta key.</p> 1991 * 1992 * @return true if the ALT key is pressed, false otherwise 1993 * 1994 * @see #KEYCODE_ALT_LEFT 1995 * @see #KEYCODE_ALT_RIGHT 1996 * @see #META_ALT_ON 1997 */ 1998 public final boolean isAltPressed() { 1999 return (mMetaState & META_ALT_ON) != 0; 2000 } 2001 2002 /** 2003 * <p>Returns the pressed state of the SHIFT meta key.</p> 2004 * 2005 * @return true if the SHIFT key is pressed, false otherwise 2006 * 2007 * @see #KEYCODE_SHIFT_LEFT 2008 * @see #KEYCODE_SHIFT_RIGHT 2009 * @see #META_SHIFT_ON 2010 */ 2011 public final boolean isShiftPressed() { 2012 return (mMetaState & META_SHIFT_ON) != 0; 2013 } 2014 2015 /** 2016 * <p>Returns the pressed state of the SYM meta key.</p> 2017 * 2018 * @return true if the SYM key is pressed, false otherwise 2019 * 2020 * @see #KEYCODE_SYM 2021 * @see #META_SYM_ON 2022 */ 2023 public final boolean isSymPressed() { 2024 return (mMetaState & META_SYM_ON) != 0; 2025 } 2026 2027 /** 2028 * <p>Returns the pressed state of the CTRL meta key.</p> 2029 * 2030 * @return true if the CTRL key is pressed, false otherwise 2031 * 2032 * @see #KEYCODE_CTRL_LEFT 2033 * @see #KEYCODE_CTRL_RIGHT 2034 * @see #META_CTRL_ON 2035 */ 2036 public final boolean isCtrlPressed() { 2037 return (mMetaState & META_CTRL_ON) != 0; 2038 } 2039 2040 /** 2041 * <p>Returns the pressed state of the META meta key.</p> 2042 * 2043 * @return true if the META key is pressed, false otherwise 2044 * 2045 * @see #KEYCODE_META_LEFT 2046 * @see #KEYCODE_META_RIGHT 2047 * @see #META_META_ON 2048 */ 2049 public final boolean isMetaPressed() { 2050 return (mMetaState & META_META_ON) != 0; 2051 } 2052 2053 /** 2054 * <p>Returns the pressed state of the FUNCTION meta key.</p> 2055 * 2056 * @return true if the FUNCTION key is pressed, false otherwise 2057 * 2058 * @see #KEYCODE_FUNCTION 2059 * @see #META_FUNCTION_ON 2060 */ 2061 public final boolean isFunctionPressed() { 2062 return (mMetaState & META_FUNCTION_ON) != 0; 2063 } 2064 2065 /** 2066 * <p>Returns the locked state of the CAPS LOCK meta key.</p> 2067 * 2068 * @return true if the CAPS LOCK key is on, false otherwise 2069 * 2070 * @see #KEYCODE_CAPS_LOCK 2071 * @see #META_CAPS_LOCK_ON 2072 */ 2073 public final boolean isCapsLockOn() { 2074 return (mMetaState & META_CAPS_LOCK_ON) != 0; 2075 } 2076 2077 /** 2078 * <p>Returns the locked state of the NUM LOCK meta key.</p> 2079 * 2080 * @return true if the NUM LOCK key is on, false otherwise 2081 * 2082 * @see #KEYCODE_NUM_LOCK 2083 * @see #META_NUM_LOCK_ON 2084 */ 2085 public final boolean isNumLockOn() { 2086 return (mMetaState & META_NUM_LOCK_ON) != 0; 2087 } 2088 2089 /** 2090 * <p>Returns the locked state of the SCROLL LOCK meta key.</p> 2091 * 2092 * @return true if the SCROLL LOCK key is on, false otherwise 2093 * 2094 * @see #KEYCODE_SCROLL_LOCK 2095 * @see #META_SCROLL_LOCK_ON 2096 */ 2097 public final boolean isScrollLockOn() { 2098 return (mMetaState & META_SCROLL_LOCK_ON) != 0; 2099 } 2100 2101 /** 2102 * Retrieve the action of this key event. May be either 2103 * {@link #ACTION_DOWN}, {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 2104 * 2105 * @return The event action: ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE. 2106 */ 2107 public final int getAction() { 2108 return mAction; 2109 } 2110 2111 /** 2112 * For {@link #ACTION_UP} events, indicates that the event has been 2113 * canceled as per {@link #FLAG_CANCELED}. 2114 */ 2115 public final boolean isCanceled() { 2116 return (mFlags&FLAG_CANCELED) != 0; 2117 } 2118 2119 /** 2120 * Call this during {@link Callback#onKeyDown} to have the system track 2121 * the key through its final up (possibly including a long press). Note 2122 * that only one key can be tracked at a time -- if another key down 2123 * event is received while a previous one is being tracked, tracking is 2124 * stopped on the previous event. 2125 */ 2126 public final void startTracking() { 2127 mFlags |= FLAG_START_TRACKING; 2128 } 2129 2130 /** 2131 * For {@link #ACTION_UP} events, indicates that the event is still being 2132 * tracked from its initial down event as per 2133 * {@link #FLAG_TRACKING}. 2134 */ 2135 public final boolean isTracking() { 2136 return (mFlags&FLAG_TRACKING) != 0; 2137 } 2138 2139 /** 2140 * For {@link #ACTION_DOWN} events, indicates that the event has been 2141 * canceled as per {@link #FLAG_LONG_PRESS}. 2142 */ 2143 public final boolean isLongPress() { 2144 return (mFlags&FLAG_LONG_PRESS) != 0; 2145 } 2146 2147 /** 2148 * Retrieve the key code of the key event. This is the physical key that 2149 * was pressed, <em>not</em> the Unicode character. 2150 * 2151 * @return The key code of the event. 2152 */ 2153 public final int getKeyCode() { 2154 return mKeyCode; 2155 } 2156 2157 /** 2158 * For the special case of a {@link #ACTION_MULTIPLE} event with key 2159 * code of {@link #KEYCODE_UNKNOWN}, this is a raw string of characters 2160 * associated with the event. In all other cases it is null. 2161 * 2162 * @return Returns a String of 1 or more characters associated with 2163 * the event. 2164 */ 2165 public final String getCharacters() { 2166 return mCharacters; 2167 } 2168 2169 /** 2170 * Retrieve the hardware key id of this key event. These values are not 2171 * reliable and vary from device to device. 2172 * 2173 * {@more} 2174 * Mostly this is here for debugging purposes. 2175 */ 2176 public final int getScanCode() { 2177 return mScanCode; 2178 } 2179 2180 /** 2181 * Retrieve the repeat count of the event. For both key up and key down 2182 * events, this is the number of times the key has repeated with the first 2183 * down starting at 0 and counting up from there. For multiple key 2184 * events, this is the number of down/up pairs that have occurred. 2185 * 2186 * @return The number of times the key has repeated. 2187 */ 2188 public final int getRepeatCount() { 2189 return mRepeatCount; 2190 } 2191 2192 /** 2193 * Retrieve the time of the most recent key down event, 2194 * in the {@link android.os.SystemClock#uptimeMillis} time base. If this 2195 * is a down event, this will be the same as {@link #getEventTime()}. 2196 * Note that when chording keys, this value is the down time of the 2197 * most recently pressed key, which may <em>not</em> be the same physical 2198 * key of this event. 2199 * 2200 * @return Returns the most recent key down time, in the 2201 * {@link android.os.SystemClock#uptimeMillis} time base 2202 */ 2203 public final long getDownTime() { 2204 return mDownTime; 2205 } 2206 2207 /** 2208 * Retrieve the time this event occurred, 2209 * in the {@link android.os.SystemClock#uptimeMillis} time base. 2210 * 2211 * @return Returns the time this event occurred, 2212 * in the {@link android.os.SystemClock#uptimeMillis} time base. 2213 */ 2214 public final long getEventTime() { 2215 return mEventTime; 2216 } 2217 2218 /** 2219 * Renamed to {@link #getDeviceId}. 2220 * 2221 * @hide 2222 * @deprecated use {@link #getDeviceId()} instead. 2223 */ 2224 @Deprecated 2225 public final int getKeyboardDevice() { 2226 return mDeviceId; 2227 } 2228 2229 /** 2230 * Gets the {@link KeyCharacterMap} associated with the keyboard device. 2231 * 2232 * @return The associated key character map. 2233 * @throws {@link UnavailableException} if the key character map 2234 * could not be loaded because it was malformed or the default key character map 2235 * is missing from the system. 2236 * 2237 * @see {@link KeyCharacterMap#load} 2238 */ 2239 public final KeyCharacterMap getKeyCharacterMap() { 2240 return KeyCharacterMap.load(mDeviceId); 2241 } 2242 2243 /** 2244 * Gets the primary character for this key. 2245 * In other words, the label that is physically printed on it. 2246 * 2247 * @return The display label character, or 0 if none (eg. for non-printing keys). 2248 */ 2249 public char getDisplayLabel() { 2250 return getKeyCharacterMap().getDisplayLabel(mKeyCode); 2251 } 2252 2253 /** 2254 * Gets the Unicode character generated by the specified key and meta 2255 * key state combination. 2256 * <p> 2257 * Returns the Unicode character that the specified key would produce 2258 * when the specified meta bits (see {@link MetaKeyKeyListener}) 2259 * were active. 2260 * </p><p> 2261 * Returns 0 if the key is not one that is used to type Unicode 2262 * characters. 2263 * </p><p> 2264 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the 2265 * key is a "dead key" that should be combined with another to 2266 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} -- 2267 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}. 2268 * </p> 2269 * 2270 * @return The associated character or combining accent, or 0 if none. 2271 */ 2272 public int getUnicodeChar() { 2273 return getUnicodeChar(mMetaState); 2274 } 2275 2276 /** 2277 * Gets the Unicode character generated by the specified key and meta 2278 * key state combination. 2279 * <p> 2280 * Returns the Unicode character that the specified key would produce 2281 * when the specified meta bits (see {@link MetaKeyKeyListener}) 2282 * were active. 2283 * </p><p> 2284 * Returns 0 if the key is not one that is used to type Unicode 2285 * characters. 2286 * </p><p> 2287 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the 2288 * key is a "dead key" that should be combined with another to 2289 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} -- 2290 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}. 2291 * </p> 2292 * 2293 * @param metaState The meta key modifier state. 2294 * @return The associated character or combining accent, or 0 if none. 2295 */ 2296 public int getUnicodeChar(int metaState) { 2297 return getKeyCharacterMap().get(mKeyCode, metaState); 2298 } 2299 2300 /** 2301 * Get the character conversion data for a given key code. 2302 * 2303 * @param results A {@link KeyCharacterMap.KeyData} instance that will be 2304 * filled with the results. 2305 * @return True if the key was mapped. If the key was not mapped, results is not modified. 2306 * 2307 * @deprecated instead use {@link #getDisplayLabel()}, 2308 * {@link #getNumber()} or {@link #getUnicodeChar(int)}. 2309 */ 2310 @Deprecated 2311 public boolean getKeyData(KeyData results) { 2312 return getKeyCharacterMap().getKeyData(mKeyCode, results); 2313 } 2314 2315 /** 2316 * Gets the first character in the character array that can be generated 2317 * by the specified key code. 2318 * <p> 2319 * This is a convenience function that returns the same value as 2320 * {@link #getMatch(char[],int) getMatch(chars, 0)}. 2321 * </p> 2322 * 2323 * @param chars The array of matching characters to consider. 2324 * @return The matching associated character, or 0 if none. 2325 */ 2326 public char getMatch(char[] chars) { 2327 return getMatch(chars, 0); 2328 } 2329 2330 /** 2331 * Gets the first character in the character array that can be generated 2332 * by the specified key code. If there are multiple choices, prefers 2333 * the one that would be generated with the specified meta key modifier state. 2334 * 2335 * @param chars The array of matching characters to consider. 2336 * @param metaState The preferred meta key modifier state. 2337 * @return The matching associated character, or 0 if none. 2338 */ 2339 public char getMatch(char[] chars, int metaState) { 2340 return getKeyCharacterMap().getMatch(mKeyCode, chars, metaState); 2341 } 2342 2343 /** 2344 * Gets the number or symbol associated with the key. 2345 * <p> 2346 * The character value is returned, not the numeric value. 2347 * If the key is not a number, but is a symbol, the symbol is retuned. 2348 * </p><p> 2349 * This method is intended to to support dial pads and other numeric or 2350 * symbolic entry on keyboards where certain keys serve dual function 2351 * as alphabetic and symbolic keys. This method returns the number 2352 * or symbol associated with the key independent of whether the user 2353 * has pressed the required modifier. 2354 * </p><p> 2355 * For example, on one particular keyboard the keys on the top QWERTY row generate 2356 * numbers when ALT is pressed such that ALT-Q maps to '1'. So for that keyboard 2357 * when {@link #getNumber} is called with {@link KeyEvent#KEYCODE_Q} it returns '1' 2358 * so that the user can type numbers without pressing ALT when it makes sense. 2359 * </p> 2360 * 2361 * @return The associated numeric or symbolic character, or 0 if none. 2362 */ 2363 public char getNumber() { 2364 return getKeyCharacterMap().getNumber(mKeyCode); 2365 } 2366 2367 /** 2368 * Returns true if this key produces a glyph. 2369 * 2370 * @return True if the key is a printing key. 2371 */ 2372 public boolean isPrintingKey() { 2373 return getKeyCharacterMap().isPrintingKey(mKeyCode); 2374 } 2375 2376 /** 2377 * @deprecated Use {@link #dispatch(Callback, DispatcherState, Object)} instead. 2378 */ 2379 @Deprecated 2380 public final boolean dispatch(Callback receiver) { 2381 return dispatch(receiver, null, null); 2382 } 2383 2384 /** 2385 * Deliver this key event to a {@link Callback} interface. If this is 2386 * an ACTION_MULTIPLE event and it is not handled, then an attempt will 2387 * be made to deliver a single normal event. 2388 * 2389 * @param receiver The Callback that will be given the event. 2390 * @param state State information retained across events. 2391 * @param target The target of the dispatch, for use in tracking. 2392 * 2393 * @return The return value from the Callback method that was called. 2394 */ 2395 public final boolean dispatch(Callback receiver, DispatcherState state, 2396 Object target) { 2397 switch (mAction) { 2398 case ACTION_DOWN: { 2399 mFlags &= ~FLAG_START_TRACKING; 2400 if (DEBUG) Log.v(TAG, "Key down to " + target + " in " + state 2401 + ": " + this); 2402 boolean res = receiver.onKeyDown(mKeyCode, this); 2403 if (state != null) { 2404 if (res && mRepeatCount == 0 && (mFlags&FLAG_START_TRACKING) != 0) { 2405 if (DEBUG) Log.v(TAG, " Start tracking!"); 2406 state.startTracking(this, target); 2407 } else if (isLongPress() && state.isTracking(this)) { 2408 try { 2409 if (receiver.onKeyLongPress(mKeyCode, this)) { 2410 if (DEBUG) Log.v(TAG, " Clear from long press!"); 2411 state.performedLongPress(this); 2412 res = true; 2413 } 2414 } catch (AbstractMethodError e) { 2415 } 2416 } 2417 } 2418 return res; 2419 } 2420 case ACTION_UP: 2421 if (DEBUG) Log.v(TAG, "Key up to " + target + " in " + state 2422 + ": " + this); 2423 if (state != null) { 2424 state.handleUpEvent(this); 2425 } 2426 return receiver.onKeyUp(mKeyCode, this); 2427 case ACTION_MULTIPLE: 2428 final int count = mRepeatCount; 2429 final int code = mKeyCode; 2430 if (receiver.onKeyMultiple(code, count, this)) { 2431 return true; 2432 } 2433 if (code != KeyEvent.KEYCODE_UNKNOWN) { 2434 mAction = ACTION_DOWN; 2435 mRepeatCount = 0; 2436 boolean handled = receiver.onKeyDown(code, this); 2437 if (handled) { 2438 mAction = ACTION_UP; 2439 receiver.onKeyUp(code, this); 2440 } 2441 mAction = ACTION_MULTIPLE; 2442 mRepeatCount = count; 2443 return handled; 2444 } 2445 return false; 2446 } 2447 return false; 2448 } 2449 2450 /** 2451 * Use with {@link KeyEvent#dispatch(Callback, DispatcherState, Object)} 2452 * for more advanced key dispatching, such as long presses. 2453 */ 2454 public static class DispatcherState { 2455 int mDownKeyCode; 2456 Object mDownTarget; 2457 SparseIntArray mActiveLongPresses = new SparseIntArray(); 2458 2459 /** 2460 * Reset back to initial state. 2461 */ 2462 public void reset() { 2463 if (DEBUG) Log.v(TAG, "Reset: " + this); 2464 mDownKeyCode = 0; 2465 mDownTarget = null; 2466 mActiveLongPresses.clear(); 2467 } 2468 2469 /** 2470 * Stop any tracking associated with this target. 2471 */ 2472 public void reset(Object target) { 2473 if (mDownTarget == target) { 2474 if (DEBUG) Log.v(TAG, "Reset in " + target + ": " + this); 2475 mDownKeyCode = 0; 2476 mDownTarget = null; 2477 } 2478 } 2479 2480 /** 2481 * Start tracking the key code associated with the given event. This 2482 * can only be called on a key down. It will allow you to see any 2483 * long press associated with the key, and will result in 2484 * {@link KeyEvent#isTracking} return true on the long press and up 2485 * events. 2486 * 2487 * <p>This is only needed if you are directly dispatching events, rather 2488 * than handling them in {@link Callback#onKeyDown}. 2489 */ 2490 public void startTracking(KeyEvent event, Object target) { 2491 if (event.getAction() != ACTION_DOWN) { 2492 throw new IllegalArgumentException( 2493 "Can only start tracking on a down event"); 2494 } 2495 if (DEBUG) Log.v(TAG, "Start trackingt in " + target + ": " + this); 2496 mDownKeyCode = event.getKeyCode(); 2497 mDownTarget = target; 2498 } 2499 2500 /** 2501 * Return true if the key event is for a key code that is currently 2502 * being tracked by the dispatcher. 2503 */ 2504 public boolean isTracking(KeyEvent event) { 2505 return mDownKeyCode == event.getKeyCode(); 2506 } 2507 2508 /** 2509 * Keep track of the given event's key code as having performed an 2510 * action with a long press, so no action should occur on the up. 2511 * <p>This is only needed if you are directly dispatching events, rather 2512 * than handling them in {@link Callback#onKeyLongPress}. 2513 */ 2514 public void performedLongPress(KeyEvent event) { 2515 mActiveLongPresses.put(event.getKeyCode(), 1); 2516 } 2517 2518 /** 2519 * Handle key up event to stop tracking. This resets the dispatcher state, 2520 * and updates the key event state based on it. 2521 * <p>This is only needed if you are directly dispatching events, rather 2522 * than handling them in {@link Callback#onKeyUp}. 2523 */ 2524 public void handleUpEvent(KeyEvent event) { 2525 final int keyCode = event.getKeyCode(); 2526 if (DEBUG) Log.v(TAG, "Handle key up " + event + ": " + this); 2527 int index = mActiveLongPresses.indexOfKey(keyCode); 2528 if (index >= 0) { 2529 if (DEBUG) Log.v(TAG, " Index: " + index); 2530 event.mFlags |= FLAG_CANCELED | FLAG_CANCELED_LONG_PRESS; 2531 mActiveLongPresses.removeAt(index); 2532 } 2533 if (mDownKeyCode == keyCode) { 2534 if (DEBUG) Log.v(TAG, " Tracking!"); 2535 event.mFlags |= FLAG_TRACKING; 2536 mDownKeyCode = 0; 2537 mDownTarget = null; 2538 } 2539 } 2540 } 2541 2542 @Override 2543 public String toString() { 2544 return "KeyEvent{action=" + actionToString(mAction) 2545 + " keycode=" + keyCodeToString(mKeyCode) 2546 + " scancode=" + mScanCode 2547 + " metaState=" + metaStateToString(mMetaState) 2548 + " flags=0x" + Integer.toHexString(mFlags) 2549 + " repeat=" + mRepeatCount 2550 + " device=" + mDeviceId 2551 + " source=0x" + Integer.toHexString(mSource) 2552 + "}"; 2553 } 2554 2555 /** 2556 * Returns a string that represents the symbolic name of the specified action 2557 * such as "ACTION_DOWN", or "35" (if unknown). 2558 * 2559 * @param action The action. 2560 * @return The symbolic name of the specified action. 2561 * @hide 2562 */ 2563 public static String actionToString(int action) { 2564 switch (action) { 2565 case ACTION_DOWN: 2566 return "ACTION_DOWN"; 2567 case ACTION_UP: 2568 return "ACTION_UP"; 2569 case ACTION_MULTIPLE: 2570 return "ACTION_MULTIPLE"; 2571 default: 2572 return Integer.toString(action); 2573 } 2574 } 2575 2576 /** 2577 * Returns a string that represents the symbolic name of the specified keycode 2578 * such as "KEYCODE_A", "KEYCODE_DPAD_UP", or "1001" (if unknown). 2579 * 2580 * @param keyCode The key code. 2581 * @return The symbolic name of the specified keycode. 2582 * 2583 * @see KeyCharacterMap#getDisplayLabel 2584 * @hide 2585 */ 2586 public static String keyCodeToString(int keyCode) { 2587 if (keyCode >= 0 && keyCode < KEYCODE_SYMBOLIC_NAMES.length) { 2588 return KEYCODE_SYMBOLIC_NAMES[keyCode]; 2589 } 2590 return Integer.toString(keyCode); 2591 } 2592 2593 /** 2594 * Gets a keycode by its symbolic name such as "KEYCODE_A" or "1001" (if unknown). 2595 * 2596 * @param symbolicName The symbolic name of the keycode. 2597 * @return The keycode or -1 if not found. 2598 * @see #keycodeToString 2599 * @hide 2600 */ 2601 public static int keyCodeFromString(String symbolicName) { 2602 if (symbolicName == null) { 2603 throw new IllegalArgumentException("symbolicName must not be null"); 2604 } 2605 2606 final int count = KEYCODE_SYMBOLIC_NAMES.length; 2607 for (int i = 0; i < count; i++) { 2608 if (symbolicName.equals(KEYCODE_SYMBOLIC_NAMES[i])) { 2609 return i; 2610 } 2611 } 2612 2613 try { 2614 return Integer.parseInt(symbolicName,10); 2615 } catch (NumberFormatException ex) { 2616 return -1; 2617 } 2618 } 2619 2620 /** 2621 * Returns a string that represents the symbolic name of the specified combined meta 2622 * key modifier state flags such as "0", "META_SHIFT_ON", 2623 * "META_ALT_ON|META_SHIFT_ON" or "0x10000000" (if unknown). 2624 * 2625 * @param metaState The meta state. 2626 * @return The symbolic name of the specified combined meta state flags. 2627 * @hide 2628 */ 2629 public static String metaStateToString(int metaState) { 2630 if (metaState == 0) { 2631 return "0"; 2632 } 2633 StringBuilder result = null; 2634 int i = 0; 2635 while (metaState != 0) { 2636 final boolean isSet = (metaState & 1) != 0; 2637 metaState >>>= 1; // unsigned shift! 2638 if (isSet) { 2639 final String name = META_SYMBOLIC_NAMES[i]; 2640 if (result == null) { 2641 if (metaState == 0) { 2642 return name; 2643 } 2644 result = new StringBuilder(name); 2645 } else { 2646 result.append('|'); 2647 result.append(name); 2648 } 2649 } 2650 i += 1; 2651 } 2652 return result.toString(); 2653 } 2654 2655 public static final Parcelable.Creator<KeyEvent> CREATOR 2656 = new Parcelable.Creator<KeyEvent>() { 2657 public KeyEvent createFromParcel(Parcel in) { 2658 in.readInt(); // skip token, we already know this is a KeyEvent 2659 return KeyEvent.createFromParcelBody(in); 2660 } 2661 2662 public KeyEvent[] newArray(int size) { 2663 return new KeyEvent[size]; 2664 } 2665 }; 2666 2667 /** @hide */ 2668 public static KeyEvent createFromParcelBody(Parcel in) { 2669 return new KeyEvent(in); 2670 } 2671 2672 private KeyEvent(Parcel in) { 2673 mDeviceId = in.readInt(); 2674 mSource = in.readInt(); 2675 mAction = in.readInt(); 2676 mKeyCode = in.readInt(); 2677 mRepeatCount = in.readInt(); 2678 mMetaState = in.readInt(); 2679 mScanCode = in.readInt(); 2680 mFlags = in.readInt(); 2681 mDownTime = in.readLong(); 2682 mEventTime = in.readLong(); 2683 } 2684 2685 public void writeToParcel(Parcel out, int flags) { 2686 out.writeInt(PARCEL_TOKEN_KEY_EVENT); 2687 2688 out.writeInt(mDeviceId); 2689 out.writeInt(mSource); 2690 out.writeInt(mAction); 2691 out.writeInt(mKeyCode); 2692 out.writeInt(mRepeatCount); 2693 out.writeInt(mMetaState); 2694 out.writeInt(mScanCode); 2695 out.writeInt(mFlags); 2696 out.writeLong(mDownTime); 2697 out.writeLong(mEventTime); 2698 } 2699 2700 private native boolean native_isSystemKey(int keyCode); 2701 private native boolean native_hasDefaultAction(int keyCode); 2702} 2703