KeyEvent.java revision c0d4062965b392dffef267ad4835930d504ca39f
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 mMetaState; 1184 private int mAction; 1185 private int mKeyCode; 1186 private int mScanCode; 1187 private int mRepeatCount; 1188 private int mFlags; 1189 private long mDownTime; 1190 private long mEventTime; 1191 private String mCharacters; 1192 1193 public interface Callback { 1194 /** 1195 * Called when a key down event has occurred. If you return true, 1196 * you can first call {@link KeyEvent#startTracking() 1197 * KeyEvent.startTracking()} to have the framework track the event 1198 * through its {@link #onKeyUp(int, KeyEvent)} and also call your 1199 * {@link #onKeyLongPress(int, KeyEvent)} if it occurs. 1200 * 1201 * @param keyCode The value in event.getKeyCode(). 1202 * @param event Description of the key event. 1203 * 1204 * @return If you handled the event, return true. If you want to allow 1205 * the event to be handled by the next receiver, return false. 1206 */ 1207 boolean onKeyDown(int keyCode, KeyEvent event); 1208 1209 /** 1210 * Called when a long press has occurred. If you return true, 1211 * the final key up will have {@link KeyEvent#FLAG_CANCELED} and 1212 * {@link KeyEvent#FLAG_CANCELED_LONG_PRESS} set. Note that in 1213 * order to receive this callback, someone in the event change 1214 * <em>must</em> return true from {@link #onKeyDown} <em>and</em> 1215 * call {@link KeyEvent#startTracking()} on the event. 1216 * 1217 * @param keyCode The value in event.getKeyCode(). 1218 * @param event Description of the key event. 1219 * 1220 * @return If you handled the event, return true. If you want to allow 1221 * the event to be handled by the next receiver, return false. 1222 */ 1223 boolean onKeyLongPress(int keyCode, KeyEvent event); 1224 1225 /** 1226 * Called when a key up event has occurred. 1227 * 1228 * @param keyCode The value in event.getKeyCode(). 1229 * @param event Description of the key event. 1230 * 1231 * @return If you handled the event, return true. If you want to allow 1232 * the event to be handled by the next receiver, return false. 1233 */ 1234 boolean onKeyUp(int keyCode, KeyEvent event); 1235 1236 /** 1237 * Called when multiple down/up pairs of the same key have occurred 1238 * in a row. 1239 * 1240 * @param keyCode The value in event.getKeyCode(). 1241 * @param count Number of pairs as returned by event.getRepeatCount(). 1242 * @param event Description of the key event. 1243 * 1244 * @return If you handled the event, return true. If you want to allow 1245 * the event to be handled by the next receiver, return false. 1246 */ 1247 boolean onKeyMultiple(int keyCode, int count, KeyEvent event); 1248 } 1249 1250 static { 1251 if (META_SYMBOLIC_NAMES.length != 32) { 1252 throw new IllegalStateException( 1253 "META_SYMBOLIC_NAMES array should contain exactly 32 entries."); 1254 } 1255 if (KEYCODE_SYMBOLIC_NAMES.length != LAST_KEYCODE + 1) { 1256 throw new IllegalStateException( 1257 "KEYCODE_SYMBOLIC_NAMES array is out of sync with the keycode constants."); 1258 } 1259 } 1260 1261 private KeyEvent() { 1262 } 1263 1264 /** 1265 * Create a new key event. 1266 * 1267 * @param action Action code: either {@link #ACTION_DOWN}, 1268 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 1269 * @param code The key code. 1270 */ 1271 public KeyEvent(int action, int code) { 1272 mAction = action; 1273 mKeyCode = code; 1274 mRepeatCount = 0; 1275 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD; 1276 } 1277 1278 /** 1279 * Create a new key event. 1280 * 1281 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1282 * at which this key code originally went down. 1283 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1284 * at which this event happened. 1285 * @param action Action code: either {@link #ACTION_DOWN}, 1286 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 1287 * @param code The key code. 1288 * @param repeat A repeat count for down events (> 0 if this is after the 1289 * initial down) or event count for multiple events. 1290 */ 1291 public KeyEvent(long downTime, long eventTime, int action, 1292 int code, int repeat) { 1293 mDownTime = downTime; 1294 mEventTime = eventTime; 1295 mAction = action; 1296 mKeyCode = code; 1297 mRepeatCount = repeat; 1298 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD; 1299 } 1300 1301 /** 1302 * Create a new key event. 1303 * 1304 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1305 * at which this key code originally went down. 1306 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1307 * at which this event happened. 1308 * @param action Action code: either {@link #ACTION_DOWN}, 1309 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 1310 * @param code The key code. 1311 * @param repeat A repeat count for down events (> 0 if this is after the 1312 * initial down) or event count for multiple events. 1313 * @param metaState Flags indicating which meta keys are currently pressed. 1314 */ 1315 public KeyEvent(long downTime, long eventTime, int action, 1316 int code, int repeat, int metaState) { 1317 mDownTime = downTime; 1318 mEventTime = eventTime; 1319 mAction = action; 1320 mKeyCode = code; 1321 mRepeatCount = repeat; 1322 mMetaState = metaState; 1323 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD; 1324 } 1325 1326 /** 1327 * Create a new key event. 1328 * 1329 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1330 * at which this key code originally went down. 1331 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1332 * at which this event happened. 1333 * @param action Action code: either {@link #ACTION_DOWN}, 1334 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 1335 * @param code The key code. 1336 * @param repeat A repeat count for down events (> 0 if this is after the 1337 * initial down) or event count for multiple events. 1338 * @param metaState Flags indicating which meta keys are currently pressed. 1339 * @param deviceId The device ID that generated the key event. 1340 * @param scancode Raw device scan code of the event. 1341 */ 1342 public KeyEvent(long downTime, long eventTime, int action, 1343 int code, int repeat, int metaState, 1344 int deviceId, int scancode) { 1345 mDownTime = downTime; 1346 mEventTime = eventTime; 1347 mAction = action; 1348 mKeyCode = code; 1349 mRepeatCount = repeat; 1350 mMetaState = metaState; 1351 mDeviceId = deviceId; 1352 mScanCode = scancode; 1353 } 1354 1355 /** 1356 * Create a new key event. 1357 * 1358 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1359 * at which this key code originally went down. 1360 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1361 * at which this event happened. 1362 * @param action Action code: either {@link #ACTION_DOWN}, 1363 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 1364 * @param code The key code. 1365 * @param repeat A repeat count for down events (> 0 if this is after the 1366 * initial down) or event count for multiple events. 1367 * @param metaState Flags indicating which meta keys are currently pressed. 1368 * @param deviceId The device ID that generated the key event. 1369 * @param scancode Raw device scan code of the event. 1370 * @param flags The flags for this key event 1371 */ 1372 public KeyEvent(long downTime, long eventTime, int action, 1373 int code, int repeat, int metaState, 1374 int deviceId, int scancode, int flags) { 1375 mDownTime = downTime; 1376 mEventTime = eventTime; 1377 mAction = action; 1378 mKeyCode = code; 1379 mRepeatCount = repeat; 1380 mMetaState = metaState; 1381 mDeviceId = deviceId; 1382 mScanCode = scancode; 1383 mFlags = flags; 1384 } 1385 1386 /** 1387 * Create a new key event. 1388 * 1389 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1390 * at which this key code originally went down. 1391 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis}) 1392 * at which this event happened. 1393 * @param action Action code: either {@link #ACTION_DOWN}, 1394 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 1395 * @param code The key code. 1396 * @param repeat A repeat count for down events (> 0 if this is after the 1397 * initial down) or event count for multiple events. 1398 * @param metaState Flags indicating which meta keys are currently pressed. 1399 * @param deviceId The device ID that generated the key event. 1400 * @param scancode Raw device scan code of the event. 1401 * @param flags The flags for this key event 1402 * @param source The input source such as {@link InputDevice#SOURCE_KEYBOARD}. 1403 */ 1404 public KeyEvent(long downTime, long eventTime, int action, 1405 int code, int repeat, int metaState, 1406 int deviceId, int scancode, int flags, int source) { 1407 mDownTime = downTime; 1408 mEventTime = eventTime; 1409 mAction = action; 1410 mKeyCode = code; 1411 mRepeatCount = repeat; 1412 mMetaState = metaState; 1413 mDeviceId = deviceId; 1414 mScanCode = scancode; 1415 mFlags = flags; 1416 mSource = source; 1417 } 1418 1419 /** 1420 * Create a new key event for a string of characters. The key code, 1421 * action, repeat count and source will automatically be set to 1422 * {@link #KEYCODE_UNKNOWN}, {@link #ACTION_MULTIPLE}, 0, and 1423 * {@link InputDevice#SOURCE_KEYBOARD} for you. 1424 * 1425 * @param time The time (in {@link android.os.SystemClock#uptimeMillis}) 1426 * at which this event occured. 1427 * @param characters The string of characters. 1428 * @param deviceId The device ID that generated the key event. 1429 * @param flags The flags for this key event 1430 */ 1431 public KeyEvent(long time, String characters, int deviceId, int flags) { 1432 mDownTime = time; 1433 mEventTime = time; 1434 mCharacters = characters; 1435 mAction = ACTION_MULTIPLE; 1436 mKeyCode = KEYCODE_UNKNOWN; 1437 mRepeatCount = 0; 1438 mDeviceId = deviceId; 1439 mFlags = flags; 1440 mSource = InputDevice.SOURCE_KEYBOARD; 1441 } 1442 1443 /** 1444 * Make an exact copy of an existing key event. 1445 */ 1446 public KeyEvent(KeyEvent origEvent) { 1447 mDownTime = origEvent.mDownTime; 1448 mEventTime = origEvent.mEventTime; 1449 mAction = origEvent.mAction; 1450 mKeyCode = origEvent.mKeyCode; 1451 mRepeatCount = origEvent.mRepeatCount; 1452 mMetaState = origEvent.mMetaState; 1453 mDeviceId = origEvent.mDeviceId; 1454 mSource = origEvent.mSource; 1455 mScanCode = origEvent.mScanCode; 1456 mFlags = origEvent.mFlags; 1457 mCharacters = origEvent.mCharacters; 1458 } 1459 1460 /** 1461 * Copy an existing key event, modifying its time and repeat count. 1462 * 1463 * @deprecated Use {@link #changeTimeRepeat(KeyEvent, long, int)} 1464 * instead. 1465 * 1466 * @param origEvent The existing event to be copied. 1467 * @param eventTime The new event time 1468 * (in {@link android.os.SystemClock#uptimeMillis}) of the event. 1469 * @param newRepeat The new repeat count of the event. 1470 */ 1471 @Deprecated 1472 public KeyEvent(KeyEvent origEvent, long eventTime, int newRepeat) { 1473 mDownTime = origEvent.mDownTime; 1474 mEventTime = eventTime; 1475 mAction = origEvent.mAction; 1476 mKeyCode = origEvent.mKeyCode; 1477 mRepeatCount = newRepeat; 1478 mMetaState = origEvent.mMetaState; 1479 mDeviceId = origEvent.mDeviceId; 1480 mSource = origEvent.mSource; 1481 mScanCode = origEvent.mScanCode; 1482 mFlags = origEvent.mFlags; 1483 mCharacters = origEvent.mCharacters; 1484 } 1485 1486 private static KeyEvent obtain() { 1487 final KeyEvent ev; 1488 synchronized (gRecyclerLock) { 1489 ev = gRecyclerTop; 1490 if (ev == null) { 1491 return new KeyEvent(); 1492 } 1493 gRecyclerTop = ev.mNext; 1494 gRecyclerUsed -= 1; 1495 } 1496 ev.mRecycled = false; 1497 ev.mNext = null; 1498 return ev; 1499 } 1500 1501 /** 1502 * Obtains a (potentially recycled) key event. 1503 * 1504 * @hide 1505 */ 1506 public static KeyEvent obtain(long downTime, long eventTime, int action, 1507 int code, int repeat, int metaState, 1508 int deviceId, int scancode, int flags, int source, String characters) { 1509 KeyEvent ev = obtain(); 1510 ev.mDownTime = downTime; 1511 ev.mEventTime = eventTime; 1512 ev.mAction = action; 1513 ev.mKeyCode = code; 1514 ev.mRepeatCount = repeat; 1515 ev.mMetaState = metaState; 1516 ev.mDeviceId = deviceId; 1517 ev.mScanCode = scancode; 1518 ev.mFlags = flags; 1519 ev.mSource = source; 1520 ev.mCharacters = characters; 1521 return ev; 1522 } 1523 1524 /** 1525 * Recycles a key event. 1526 * Key events should only be recycled if they are owned by the system since user 1527 * code expects them to be essentially immutable, "tracking" notwithstanding. 1528 * 1529 * @hide 1530 */ 1531 public final void recycle() { 1532 if (mRecycled) { 1533 throw new RuntimeException(toString() + " recycled twice!"); 1534 } 1535 mRecycled = true; 1536 mCharacters = null; 1537 1538 synchronized (gRecyclerLock) { 1539 if (gRecyclerUsed < MAX_RECYCLED) { 1540 gRecyclerUsed++; 1541 mNext = gRecyclerTop; 1542 gRecyclerTop = this; 1543 } 1544 } 1545 } 1546 1547 /** 1548 * Create a new key event that is the same as the given one, but whose 1549 * event time and repeat count are replaced with the given value. 1550 * 1551 * @param event The existing event to be copied. This is not modified. 1552 * @param eventTime The new event time 1553 * (in {@link android.os.SystemClock#uptimeMillis}) of the event. 1554 * @param newRepeat The new repeat count of the event. 1555 */ 1556 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime, 1557 int newRepeat) { 1558 return new KeyEvent(event, eventTime, newRepeat); 1559 } 1560 1561 /** 1562 * Create a new key event that is the same as the given one, but whose 1563 * event time and repeat count are replaced with the given value. 1564 * 1565 * @param event The existing event to be copied. This is not modified. 1566 * @param eventTime The new event time 1567 * (in {@link android.os.SystemClock#uptimeMillis}) of the event. 1568 * @param newRepeat The new repeat count of the event. 1569 * @param newFlags New flags for the event, replacing the entire value 1570 * in the original event. 1571 */ 1572 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime, 1573 int newRepeat, int newFlags) { 1574 KeyEvent ret = new KeyEvent(event); 1575 ret.mEventTime = eventTime; 1576 ret.mRepeatCount = newRepeat; 1577 ret.mFlags = newFlags; 1578 return ret; 1579 } 1580 1581 /** 1582 * Copy an existing key event, modifying its action. 1583 * 1584 * @param origEvent The existing event to be copied. 1585 * @param action The new action code of the event. 1586 */ 1587 private KeyEvent(KeyEvent origEvent, int action) { 1588 mDownTime = origEvent.mDownTime; 1589 mEventTime = origEvent.mEventTime; 1590 mAction = action; 1591 mKeyCode = origEvent.mKeyCode; 1592 mRepeatCount = origEvent.mRepeatCount; 1593 mMetaState = origEvent.mMetaState; 1594 mDeviceId = origEvent.mDeviceId; 1595 mSource = origEvent.mSource; 1596 mScanCode = origEvent.mScanCode; 1597 mFlags = origEvent.mFlags; 1598 // Don't copy mCharacters, since one way or the other we'll lose it 1599 // when changing the action. 1600 } 1601 1602 /** 1603 * Create a new key event that is the same as the given one, but whose 1604 * action is replaced with the given value. 1605 * 1606 * @param event The existing event to be copied. This is not modified. 1607 * @param action The new action code of the event. 1608 */ 1609 public static KeyEvent changeAction(KeyEvent event, int action) { 1610 return new KeyEvent(event, action); 1611 } 1612 1613 /** 1614 * Create a new key event that is the same as the given one, but whose 1615 * flags are replaced with the given value. 1616 * 1617 * @param event The existing event to be copied. This is not modified. 1618 * @param flags The new flags constant. 1619 */ 1620 public static KeyEvent changeFlags(KeyEvent event, int flags) { 1621 event = new KeyEvent(event); 1622 event.mFlags = flags; 1623 return event; 1624 } 1625 1626 /** 1627 * Don't use in new code, instead explicitly check 1628 * {@link #getAction()}. 1629 * 1630 * @return If the action is ACTION_DOWN, returns true; else false. 1631 * 1632 * @deprecated 1633 * @hide 1634 */ 1635 @Deprecated public final boolean isDown() { 1636 return mAction == ACTION_DOWN; 1637 } 1638 1639 /** 1640 * Is this a system key? System keys can not be used for menu shortcuts. 1641 * 1642 * TODO: this information should come from a table somewhere. 1643 * TODO: should the dpad keys be here? arguably, because they also shouldn't be menu shortcuts 1644 */ 1645 public final boolean isSystem() { 1646 return native_isSystemKey(mKeyCode); 1647 } 1648 1649 /** @hide */ 1650 public final boolean hasDefaultAction() { 1651 return native_hasDefaultAction(mKeyCode); 1652 } 1653 1654 1655 /** 1656 * <p>Returns the state of the meta keys.</p> 1657 * 1658 * @return an integer in which each bit set to 1 represents a pressed 1659 * meta key 1660 * 1661 * @see #isAltPressed() 1662 * @see #isShiftPressed() 1663 * @see #isSymPressed() 1664 * @see #isCtrlPressed() 1665 * @see #isMetaPressed() 1666 * @see #isFunctionPressed() 1667 * @see #isCapsLockOn() 1668 * @see #isNumLockOn() 1669 * @see #isScrollLockOn() 1670 * @see #META_ALT_ON 1671 * @see #META_ALT_LEFT_ON 1672 * @see #META_ALT_RIGHT_ON 1673 * @see #META_SHIFT_ON 1674 * @see #META_SHIFT_LEFT_ON 1675 * @see #META_SHIFT_RIGHT_ON 1676 * @see #META_SYM_ON 1677 * @see #META_FUNCTION_ON 1678 * @see #META_CTRL_ON 1679 * @see #META_CTRL_LEFT_ON 1680 * @see #META_CTRL_RIGHT_ON 1681 * @see #META_META_ON 1682 * @see #META_META_LEFT_ON 1683 * @see #META_META_RIGHT_ON 1684 * @see #META_CAPS_LOCK_ON 1685 * @see #META_NUM_LOCK_ON 1686 * @see #META_SCROLL_LOCK_ON 1687 */ 1688 public final int getMetaState() { 1689 return mMetaState; 1690 } 1691 1692 /** 1693 * Returns the flags for this key event. 1694 * 1695 * @see #FLAG_WOKE_HERE 1696 */ 1697 public final int getFlags() { 1698 return mFlags; 1699 } 1700 1701 // Mask of all modifier key meta states. Specifically excludes locked keys like caps lock. 1702 private static final int META_MODIFIER_MASK = 1703 META_SHIFT_ON | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON 1704 | META_ALT_ON | META_ALT_LEFT_ON | META_ALT_RIGHT_ON 1705 | META_CTRL_ON | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON 1706 | META_META_ON | META_META_LEFT_ON | META_META_RIGHT_ON 1707 | META_SYM_ON | META_FUNCTION_ON; 1708 1709 // Mask of all lock key meta states. 1710 private static final int META_LOCK_MASK = 1711 META_CAPS_LOCK_ON | META_NUM_LOCK_ON | META_SCROLL_LOCK_ON; 1712 1713 // Mask of all valid meta states. 1714 private static final int META_ALL_MASK = META_MODIFIER_MASK | META_LOCK_MASK; 1715 1716 // Mask of all synthetic meta states that are reserved for API compatibility with 1717 // historical uses in MetaKeyKeyListener. 1718 private static final int META_SYNTHETIC_MASK = 1719 META_CAP_LOCKED | META_ALT_LOCKED | META_SYM_LOCKED | META_SELECTING; 1720 1721 // Mask of all meta states that are not valid use in specifying a modifier key. 1722 // These bits are known to be used for purposes other than specifying modifiers. 1723 private static final int META_INVALID_MODIFIER_MASK = 1724 META_LOCK_MASK | META_SYNTHETIC_MASK; 1725 1726 /** 1727 * Gets a mask that includes all valid modifier key meta state bits. 1728 * <p> 1729 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK}, 1730 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are 1731 * not considered modifier keys. Consequently, the mask specifically excludes 1732 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}. 1733 * </p> 1734 * 1735 * @return The modifier meta state mask which is a combination of 1736 * {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON}, {@link #META_SHIFT_RIGHT_ON}, 1737 * {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON}, {@link #META_ALT_RIGHT_ON}, 1738 * {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON}, {@link #META_CTRL_RIGHT_ON}, 1739 * {@link #META_META_ON}, {@link #META_META_LEFT_ON}, {@link #META_META_RIGHT_ON}, 1740 * {@link #META_SYM_ON}, {@link #META_FUNCTION_ON}. 1741 */ 1742 public static int getModifierMetaStateMask() { 1743 return META_MODIFIER_MASK; 1744 } 1745 1746 /** 1747 * Returns true if this key code is a modifier key. 1748 * <p> 1749 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK}, 1750 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are 1751 * not considered modifier keys. Consequently, this function return false 1752 * for those keys. 1753 * </p> 1754 * 1755 * @return True if the key code is one of 1756 * {@link #KEYCODE_SHIFT_LEFT} {@link #KEYCODE_SHIFT_RIGHT}, 1757 * {@link #KEYCODE_ALT_LEFT}, {@link #KEYCODE_ALT_RIGHT}, 1758 * {@link #KEYCODE_CTRL_LEFT}, {@link #KEYCODE_CTRL_RIGHT}, 1759 * {@link #KEYCODE_META_LEFT}, or {@link #KEYCODE_META_RIGHT}, 1760 * {@link #KEYCODE_SYM}, {@link #KEYCODE_NUM}, {@link #KEYCODE_FUNCTION}. 1761 */ 1762 public static boolean isModifierKey(int keyCode) { 1763 switch (keyCode) { 1764 case KEYCODE_SHIFT_LEFT: 1765 case KEYCODE_SHIFT_RIGHT: 1766 case KEYCODE_ALT_LEFT: 1767 case KEYCODE_ALT_RIGHT: 1768 case KEYCODE_CTRL_LEFT: 1769 case KEYCODE_CTRL_RIGHT: 1770 case KEYCODE_META_LEFT: 1771 case KEYCODE_META_RIGHT: 1772 case KEYCODE_SYM: 1773 case KEYCODE_NUM: 1774 case KEYCODE_FUNCTION: 1775 return true; 1776 default: 1777 return false; 1778 } 1779 } 1780 1781 /** 1782 * Normalizes the specified meta state. 1783 * <p> 1784 * The meta state is normalized such that if either the left or right modifier meta state 1785 * bits are set then the result will also include the universal bit for that modifier. 1786 * </p><p> 1787 * If the specified meta state contains {@link #META_ALT_LEFT_ON} then 1788 * the result will also contain {@link #META_ALT_ON} in addition to {@link #META_ALT_LEFT_ON} 1789 * and the other bits that were specified in the input. The same is process is 1790 * performed for shift, control and meta. 1791 * </p><p> 1792 * If the specified meta state contains synthetic meta states defined by 1793 * {@link MetaKeyKeyListener}, then those states are translated here and the original 1794 * synthetic meta states are removed from the result. 1795 * {@link MetaKeyKeyListener#META_CAP_LOCKED} is translated to {@link #META_CAPS_LOCK_ON}. 1796 * {@link MetaKeyKeyListener#META_ALT_LOCKED} is translated to {@link #META_ALT_ON}. 1797 * {@link MetaKeyKeyListener#META_SYM_LOCKED} is translated to {@link #META_SYM_ON}. 1798 * </p><p> 1799 * Undefined meta state bits are removed. 1800 * </p> 1801 * 1802 * @param metaState The meta state. 1803 * @return The normalized meta state. 1804 */ 1805 public static int normalizeMetaState(int metaState) { 1806 if ((metaState & (META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON)) != 0) { 1807 metaState |= META_SHIFT_ON; 1808 } 1809 if ((metaState & (META_ALT_LEFT_ON | META_ALT_RIGHT_ON)) != 0) { 1810 metaState |= META_ALT_ON; 1811 } 1812 if ((metaState & (META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON)) != 0) { 1813 metaState |= META_CTRL_ON; 1814 } 1815 if ((metaState & (META_META_LEFT_ON | META_META_RIGHT_ON)) != 0) { 1816 metaState |= META_META_ON; 1817 } 1818 if ((metaState & MetaKeyKeyListener.META_CAP_LOCKED) != 0) { 1819 metaState |= META_CAPS_LOCK_ON; 1820 } 1821 if ((metaState & MetaKeyKeyListener.META_ALT_LOCKED) != 0) { 1822 metaState |= META_ALT_ON; 1823 } 1824 if ((metaState & MetaKeyKeyListener.META_SYM_LOCKED) != 0) { 1825 metaState |= META_SYM_ON; 1826 } 1827 return metaState & META_ALL_MASK; 1828 } 1829 1830 /** 1831 * Returns true if no modifiers keys are pressed according to the specified meta state. 1832 * <p> 1833 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK}, 1834 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are 1835 * not considered modifier keys. Consequently, this function ignores 1836 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}. 1837 * </p><p> 1838 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}. 1839 * </p> 1840 * 1841 * @param metaState The meta state to consider. 1842 * @return True if no modifier keys are pressed. 1843 * @see #hasNoModifiers() 1844 */ 1845 public static boolean metaStateHasNoModifiers(int metaState) { 1846 return (normalizeMetaState(metaState) & META_MODIFIER_MASK) == 0; 1847 } 1848 1849 /** 1850 * Returns true if only the specified modifier keys are pressed according to 1851 * the specified meta state. Returns false if a different combination of modifier 1852 * keys are pressed. 1853 * <p> 1854 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK}, 1855 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are 1856 * not considered modifier keys. Consequently, this function ignores 1857 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}. 1858 * </p><p> 1859 * If the specified modifier mask includes directional modifiers, such as 1860 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the 1861 * modifier is pressed on that side. 1862 * If the specified modifier mask includes non-directional modifiers, such as 1863 * {@link #META_SHIFT_ON}, then this method ensures that the modifier 1864 * is pressed on either side. 1865 * If the specified modifier mask includes both directional and non-directional modifiers 1866 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON}, 1867 * then this method throws an illegal argument exception. 1868 * </p> 1869 * 1870 * @param metaState The meta state to consider. 1871 * @param modifiers The meta state of the modifier keys to check. May be a combination 1872 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to 1873 * ensure that no modifier keys are pressed. 1874 * @return True if only the specified modifier keys are pressed. 1875 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers 1876 * @see #hasModifiers 1877 */ 1878 public static boolean metaStateHasModifiers(int metaState, int modifiers) { 1879 // Note: For forward compatibility, we allow the parameter to contain meta states 1880 // that we do not recognize but we explicitly disallow meta states that 1881 // are not valid modifiers. 1882 if ((modifiers & META_INVALID_MODIFIER_MASK) != 0) { 1883 throw new IllegalArgumentException("modifiers must not contain " 1884 + "META_CAPS_LOCK_ON, META_NUM_LOCK_ON, META_SCROLL_LOCK_ON, " 1885 + "META_CAP_LOCKED, META_ALT_LOCKED, META_SYM_LOCKED, " 1886 + "or META_SELECTING"); 1887 } 1888 1889 metaState = normalizeMetaState(metaState) & META_MODIFIER_MASK; 1890 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers, 1891 META_SHIFT_ON, META_SHIFT_LEFT_ON, META_SHIFT_RIGHT_ON); 1892 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers, 1893 META_ALT_ON, META_ALT_LEFT_ON, META_ALT_RIGHT_ON); 1894 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers, 1895 META_CTRL_ON, META_CTRL_LEFT_ON, META_CTRL_RIGHT_ON); 1896 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers, 1897 META_META_ON, META_META_LEFT_ON, META_META_RIGHT_ON); 1898 return metaState == modifiers; 1899 } 1900 1901 private static int metaStateFilterDirectionalModifiers(int metaState, 1902 int modifiers, int basic, int left, int right) { 1903 final boolean wantBasic = (modifiers & basic) != 0; 1904 final int directional = left | right; 1905 final boolean wantLeftOrRight = (modifiers & directional) != 0; 1906 1907 if (wantBasic) { 1908 if (wantLeftOrRight) { 1909 throw new IllegalArgumentException("modifiers must not contain " 1910 + metaStateToString(basic) + " combined with " 1911 + metaStateToString(left) + " or " + metaStateToString(right)); 1912 } 1913 return metaState & ~directional; 1914 } else if (wantLeftOrRight) { 1915 return metaState & ~basic; 1916 } else { 1917 return metaState; 1918 } 1919 } 1920 1921 /** 1922 * Returns true if no modifier keys are pressed. 1923 * <p> 1924 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK}, 1925 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are 1926 * not considered modifier keys. Consequently, this function ignores 1927 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}. 1928 * </p><p> 1929 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}. 1930 * </p> 1931 * 1932 * @return True if no modifier keys are pressed. 1933 * @see #metaStateHasNoModifiers 1934 */ 1935 public final boolean hasNoModifiers() { 1936 return metaStateHasNoModifiers(mMetaState); 1937 } 1938 1939 /** 1940 * Returns true if only the specified modifiers keys are pressed. 1941 * Returns false if a different combination of 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 * If the specified modifier mask includes directional modifiers, such as 1949 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the 1950 * modifier is pressed on that side. 1951 * If the specified modifier mask includes non-directional modifiers, such as 1952 * {@link #META_SHIFT_ON}, then this method ensures that the modifier 1953 * is pressed on either side. 1954 * If the specified modifier mask includes both directional and non-directional modifiers 1955 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON}, 1956 * then this method throws an illegal argument exception. 1957 * </p> 1958 * 1959 * @param modifiers The meta state of the modifier keys to check. May be a combination 1960 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to 1961 * ensure that no modifier keys are pressed. 1962 * @return True if only the specified modifier keys are pressed. 1963 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers 1964 * @see #metaStateHasModifiers 1965 */ 1966 public final boolean hasModifiers(int modifiers) { 1967 return metaStateHasModifiers(mMetaState, modifiers); 1968 } 1969 1970 /** 1971 * <p>Returns the pressed state of the ALT meta key.</p> 1972 * 1973 * @return true if the ALT key is pressed, false otherwise 1974 * 1975 * @see #KEYCODE_ALT_LEFT 1976 * @see #KEYCODE_ALT_RIGHT 1977 * @see #META_ALT_ON 1978 */ 1979 public final boolean isAltPressed() { 1980 return (mMetaState & META_ALT_ON) != 0; 1981 } 1982 1983 /** 1984 * <p>Returns the pressed state of the SHIFT meta key.</p> 1985 * 1986 * @return true if the SHIFT key is pressed, false otherwise 1987 * 1988 * @see #KEYCODE_SHIFT_LEFT 1989 * @see #KEYCODE_SHIFT_RIGHT 1990 * @see #META_SHIFT_ON 1991 */ 1992 public final boolean isShiftPressed() { 1993 return (mMetaState & META_SHIFT_ON) != 0; 1994 } 1995 1996 /** 1997 * <p>Returns the pressed state of the SYM meta key.</p> 1998 * 1999 * @return true if the SYM key is pressed, false otherwise 2000 * 2001 * @see #KEYCODE_SYM 2002 * @see #META_SYM_ON 2003 */ 2004 public final boolean isSymPressed() { 2005 return (mMetaState & META_SYM_ON) != 0; 2006 } 2007 2008 /** 2009 * <p>Returns the pressed state of the CTRL meta key.</p> 2010 * 2011 * @return true if the CTRL key is pressed, false otherwise 2012 * 2013 * @see #KEYCODE_CTRL_LEFT 2014 * @see #KEYCODE_CTRL_RIGHT 2015 * @see #META_CTRL_ON 2016 */ 2017 public final boolean isCtrlPressed() { 2018 return (mMetaState & META_CTRL_ON) != 0; 2019 } 2020 2021 /** 2022 * <p>Returns the pressed state of the META meta key.</p> 2023 * 2024 * @return true if the META key is pressed, false otherwise 2025 * 2026 * @see #KEYCODE_META_LEFT 2027 * @see #KEYCODE_META_RIGHT 2028 * @see #META_META_ON 2029 */ 2030 public final boolean isMetaPressed() { 2031 return (mMetaState & META_META_ON) != 0; 2032 } 2033 2034 /** 2035 * <p>Returns the pressed state of the FUNCTION meta key.</p> 2036 * 2037 * @return true if the FUNCTION key is pressed, false otherwise 2038 * 2039 * @see #KEYCODE_FUNCTION 2040 * @see #META_FUNCTION_ON 2041 */ 2042 public final boolean isFunctionPressed() { 2043 return (mMetaState & META_FUNCTION_ON) != 0; 2044 } 2045 2046 /** 2047 * <p>Returns the locked state of the CAPS LOCK meta key.</p> 2048 * 2049 * @return true if the CAPS LOCK key is on, false otherwise 2050 * 2051 * @see #KEYCODE_CAPS_LOCK 2052 * @see #META_CAPS_LOCK_ON 2053 */ 2054 public final boolean isCapsLockOn() { 2055 return (mMetaState & META_CAPS_LOCK_ON) != 0; 2056 } 2057 2058 /** 2059 * <p>Returns the locked state of the NUM LOCK meta key.</p> 2060 * 2061 * @return true if the NUM LOCK key is on, false otherwise 2062 * 2063 * @see #KEYCODE_NUM_LOCK 2064 * @see #META_NUM_LOCK_ON 2065 */ 2066 public final boolean isNumLockOn() { 2067 return (mMetaState & META_NUM_LOCK_ON) != 0; 2068 } 2069 2070 /** 2071 * <p>Returns the locked state of the SCROLL LOCK meta key.</p> 2072 * 2073 * @return true if the SCROLL LOCK key is on, false otherwise 2074 * 2075 * @see #KEYCODE_SCROLL_LOCK 2076 * @see #META_SCROLL_LOCK_ON 2077 */ 2078 public final boolean isScrollLockOn() { 2079 return (mMetaState & META_SCROLL_LOCK_ON) != 0; 2080 } 2081 2082 /** 2083 * Retrieve the action of this key event. May be either 2084 * {@link #ACTION_DOWN}, {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}. 2085 * 2086 * @return The event action: ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE. 2087 */ 2088 public final int getAction() { 2089 return mAction; 2090 } 2091 2092 /** 2093 * For {@link #ACTION_UP} events, indicates that the event has been 2094 * canceled as per {@link #FLAG_CANCELED}. 2095 */ 2096 public final boolean isCanceled() { 2097 return (mFlags&FLAG_CANCELED) != 0; 2098 } 2099 2100 /** 2101 * Call this during {@link Callback#onKeyDown} to have the system track 2102 * the key through its final up (possibly including a long press). Note 2103 * that only one key can be tracked at a time -- if another key down 2104 * event is received while a previous one is being tracked, tracking is 2105 * stopped on the previous event. 2106 */ 2107 public final void startTracking() { 2108 mFlags |= FLAG_START_TRACKING; 2109 } 2110 2111 /** 2112 * For {@link #ACTION_UP} events, indicates that the event is still being 2113 * tracked from its initial down event as per 2114 * {@link #FLAG_TRACKING}. 2115 */ 2116 public final boolean isTracking() { 2117 return (mFlags&FLAG_TRACKING) != 0; 2118 } 2119 2120 /** 2121 * For {@link #ACTION_DOWN} events, indicates that the event has been 2122 * canceled as per {@link #FLAG_LONG_PRESS}. 2123 */ 2124 public final boolean isLongPress() { 2125 return (mFlags&FLAG_LONG_PRESS) != 0; 2126 } 2127 2128 /** 2129 * Retrieve the key code of the key event. This is the physical key that 2130 * was pressed, <em>not</em> the Unicode character. 2131 * 2132 * @return The key code of the event. 2133 */ 2134 public final int getKeyCode() { 2135 return mKeyCode; 2136 } 2137 2138 /** 2139 * For the special case of a {@link #ACTION_MULTIPLE} event with key 2140 * code of {@link #KEYCODE_UNKNOWN}, this is a raw string of characters 2141 * associated with the event. In all other cases it is null. 2142 * 2143 * @return Returns a String of 1 or more characters associated with 2144 * the event. 2145 */ 2146 public final String getCharacters() { 2147 return mCharacters; 2148 } 2149 2150 /** 2151 * Retrieve the hardware key id of this key event. These values are not 2152 * reliable and vary from device to device. 2153 * 2154 * {@more} 2155 * Mostly this is here for debugging purposes. 2156 */ 2157 public final int getScanCode() { 2158 return mScanCode; 2159 } 2160 2161 /** 2162 * Retrieve the repeat count of the event. For both key up and key down 2163 * events, this is the number of times the key has repeated with the first 2164 * down starting at 0 and counting up from there. For multiple key 2165 * events, this is the number of down/up pairs that have occurred. 2166 * 2167 * @return The number of times the key has repeated. 2168 */ 2169 public final int getRepeatCount() { 2170 return mRepeatCount; 2171 } 2172 2173 /** 2174 * Retrieve the time of the most recent key down event, 2175 * in the {@link android.os.SystemClock#uptimeMillis} time base. If this 2176 * is a down event, this will be the same as {@link #getEventTime()}. 2177 * Note that when chording keys, this value is the down time of the 2178 * most recently pressed key, which may <em>not</em> be the same physical 2179 * key of this event. 2180 * 2181 * @return Returns the most recent key down time, in the 2182 * {@link android.os.SystemClock#uptimeMillis} time base 2183 */ 2184 public final long getDownTime() { 2185 return mDownTime; 2186 } 2187 2188 /** 2189 * Retrieve the time this event occurred, 2190 * in the {@link android.os.SystemClock#uptimeMillis} time base. 2191 * 2192 * @return Returns the time this event occurred, 2193 * in the {@link android.os.SystemClock#uptimeMillis} time base. 2194 */ 2195 public final long getEventTime() { 2196 return mEventTime; 2197 } 2198 2199 /** 2200 * Renamed to {@link #getDeviceId}. 2201 * 2202 * @hide 2203 * @deprecated use {@link #getDeviceId()} instead. 2204 */ 2205 @Deprecated 2206 public final int getKeyboardDevice() { 2207 return mDeviceId; 2208 } 2209 2210 /** 2211 * Gets the {@link KeyCharacterMap} associated with the keyboard device. 2212 * 2213 * @return The associated key character map. 2214 * @throws {@link UnavailableException} if the key character map 2215 * could not be loaded because it was malformed or the default key character map 2216 * is missing from the system. 2217 * 2218 * @see {@link KeyCharacterMap#load} 2219 */ 2220 public final KeyCharacterMap getKeyCharacterMap() { 2221 return KeyCharacterMap.load(mDeviceId); 2222 } 2223 2224 /** 2225 * Gets the primary character for this key. 2226 * In other words, the label that is physically printed on it. 2227 * 2228 * @return The display label character, or 0 if none (eg. for non-printing keys). 2229 */ 2230 public char getDisplayLabel() { 2231 return getKeyCharacterMap().getDisplayLabel(mKeyCode); 2232 } 2233 2234 /** 2235 * Gets the Unicode character generated by the specified key and meta 2236 * key state combination. 2237 * <p> 2238 * Returns the Unicode character that the specified key would produce 2239 * when the specified meta bits (see {@link MetaKeyKeyListener}) 2240 * were active. 2241 * </p><p> 2242 * Returns 0 if the key is not one that is used to type Unicode 2243 * characters. 2244 * </p><p> 2245 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the 2246 * key is a "dead key" that should be combined with another to 2247 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} -- 2248 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}. 2249 * </p> 2250 * 2251 * @return The associated character or combining accent, or 0 if none. 2252 */ 2253 public int getUnicodeChar() { 2254 return getUnicodeChar(mMetaState); 2255 } 2256 2257 /** 2258 * Gets the Unicode character generated by the specified key and meta 2259 * key state combination. 2260 * <p> 2261 * Returns the Unicode character that the specified key would produce 2262 * when the specified meta bits (see {@link MetaKeyKeyListener}) 2263 * were active. 2264 * </p><p> 2265 * Returns 0 if the key is not one that is used to type Unicode 2266 * characters. 2267 * </p><p> 2268 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the 2269 * key is a "dead key" that should be combined with another to 2270 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} -- 2271 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}. 2272 * </p> 2273 * 2274 * @param metaState The meta key modifier state. 2275 * @return The associated character or combining accent, or 0 if none. 2276 */ 2277 public int getUnicodeChar(int metaState) { 2278 return getKeyCharacterMap().get(mKeyCode, metaState); 2279 } 2280 2281 /** 2282 * Get the character conversion data for a given key code. 2283 * 2284 * @param results A {@link KeyCharacterMap.KeyData} instance that will be 2285 * filled with the results. 2286 * @return True if the key was mapped. If the key was not mapped, results is not modified. 2287 * 2288 * @deprecated instead use {@link #getDisplayLabel()}, 2289 * {@link #getNumber()} or {@link #getUnicodeChar(int)}. 2290 */ 2291 @Deprecated 2292 public boolean getKeyData(KeyData results) { 2293 return getKeyCharacterMap().getKeyData(mKeyCode, results); 2294 } 2295 2296 /** 2297 * Gets the first character in the character array that can be generated 2298 * by the specified key code. 2299 * <p> 2300 * This is a convenience function that returns the same value as 2301 * {@link #getMatch(char[],int) getMatch(chars, 0)}. 2302 * </p> 2303 * 2304 * @param chars The array of matching characters to consider. 2305 * @return The matching associated character, or 0 if none. 2306 */ 2307 public char getMatch(char[] chars) { 2308 return getMatch(chars, 0); 2309 } 2310 2311 /** 2312 * Gets the first character in the character array that can be generated 2313 * by the specified key code. If there are multiple choices, prefers 2314 * the one that would be generated with the specified meta key modifier state. 2315 * 2316 * @param chars The array of matching characters to consider. 2317 * @param metaState The preferred meta key modifier state. 2318 * @return The matching associated character, or 0 if none. 2319 */ 2320 public char getMatch(char[] chars, int metaState) { 2321 return getKeyCharacterMap().getMatch(mKeyCode, chars, metaState); 2322 } 2323 2324 /** 2325 * Gets the number or symbol associated with the key. 2326 * <p> 2327 * The character value is returned, not the numeric value. 2328 * If the key is not a number, but is a symbol, the symbol is retuned. 2329 * </p><p> 2330 * This method is intended to to support dial pads and other numeric or 2331 * symbolic entry on keyboards where certain keys serve dual function 2332 * as alphabetic and symbolic keys. This method returns the number 2333 * or symbol associated with the key independent of whether the user 2334 * has pressed the required modifier. 2335 * </p><p> 2336 * For example, on one particular keyboard the keys on the top QWERTY row generate 2337 * numbers when ALT is pressed such that ALT-Q maps to '1'. So for that keyboard 2338 * when {@link #getNumber} is called with {@link KeyEvent#KEYCODE_Q} it returns '1' 2339 * so that the user can type numbers without pressing ALT when it makes sense. 2340 * </p> 2341 * 2342 * @return The associated numeric or symbolic character, or 0 if none. 2343 */ 2344 public char getNumber() { 2345 return getKeyCharacterMap().getNumber(mKeyCode); 2346 } 2347 2348 /** 2349 * Returns true if this key produces a glyph. 2350 * 2351 * @return True if the key is a printing key. 2352 */ 2353 public boolean isPrintingKey() { 2354 return getKeyCharacterMap().isPrintingKey(mKeyCode); 2355 } 2356 2357 /** 2358 * @deprecated Use {@link #dispatch(Callback, DispatcherState, Object)} instead. 2359 */ 2360 @Deprecated 2361 public final boolean dispatch(Callback receiver) { 2362 return dispatch(receiver, null, null); 2363 } 2364 2365 /** 2366 * Deliver this key event to a {@link Callback} interface. If this is 2367 * an ACTION_MULTIPLE event and it is not handled, then an attempt will 2368 * be made to deliver a single normal event. 2369 * 2370 * @param receiver The Callback that will be given the event. 2371 * @param state State information retained across events. 2372 * @param target The target of the dispatch, for use in tracking. 2373 * 2374 * @return The return value from the Callback method that was called. 2375 */ 2376 public final boolean dispatch(Callback receiver, DispatcherState state, 2377 Object target) { 2378 switch (mAction) { 2379 case ACTION_DOWN: { 2380 mFlags &= ~FLAG_START_TRACKING; 2381 if (DEBUG) Log.v(TAG, "Key down to " + target + " in " + state 2382 + ": " + this); 2383 boolean res = receiver.onKeyDown(mKeyCode, this); 2384 if (state != null) { 2385 if (res && mRepeatCount == 0 && (mFlags&FLAG_START_TRACKING) != 0) { 2386 if (DEBUG) Log.v(TAG, " Start tracking!"); 2387 state.startTracking(this, target); 2388 } else if (isLongPress() && state.isTracking(this)) { 2389 try { 2390 if (receiver.onKeyLongPress(mKeyCode, this)) { 2391 if (DEBUG) Log.v(TAG, " Clear from long press!"); 2392 state.performedLongPress(this); 2393 res = true; 2394 } 2395 } catch (AbstractMethodError e) { 2396 } 2397 } 2398 } 2399 return res; 2400 } 2401 case ACTION_UP: 2402 if (DEBUG) Log.v(TAG, "Key up to " + target + " in " + state 2403 + ": " + this); 2404 if (state != null) { 2405 state.handleUpEvent(this); 2406 } 2407 return receiver.onKeyUp(mKeyCode, this); 2408 case ACTION_MULTIPLE: 2409 final int count = mRepeatCount; 2410 final int code = mKeyCode; 2411 if (receiver.onKeyMultiple(code, count, this)) { 2412 return true; 2413 } 2414 if (code != KeyEvent.KEYCODE_UNKNOWN) { 2415 mAction = ACTION_DOWN; 2416 mRepeatCount = 0; 2417 boolean handled = receiver.onKeyDown(code, this); 2418 if (handled) { 2419 mAction = ACTION_UP; 2420 receiver.onKeyUp(code, this); 2421 } 2422 mAction = ACTION_MULTIPLE; 2423 mRepeatCount = count; 2424 return handled; 2425 } 2426 return false; 2427 } 2428 return false; 2429 } 2430 2431 /** 2432 * Use with {@link KeyEvent#dispatch(Callback, DispatcherState, Object)} 2433 * for more advanced key dispatching, such as long presses. 2434 */ 2435 public static class DispatcherState { 2436 int mDownKeyCode; 2437 Object mDownTarget; 2438 SparseIntArray mActiveLongPresses = new SparseIntArray(); 2439 2440 /** 2441 * Reset back to initial state. 2442 */ 2443 public void reset() { 2444 if (DEBUG) Log.v(TAG, "Reset: " + this); 2445 mDownKeyCode = 0; 2446 mDownTarget = null; 2447 mActiveLongPresses.clear(); 2448 } 2449 2450 /** 2451 * Stop any tracking associated with this target. 2452 */ 2453 public void reset(Object target) { 2454 if (mDownTarget == target) { 2455 if (DEBUG) Log.v(TAG, "Reset in " + target + ": " + this); 2456 mDownKeyCode = 0; 2457 mDownTarget = null; 2458 } 2459 } 2460 2461 /** 2462 * Start tracking the key code associated with the given event. This 2463 * can only be called on a key down. It will allow you to see any 2464 * long press associated with the key, and will result in 2465 * {@link KeyEvent#isTracking} return true on the long press and up 2466 * events. 2467 * 2468 * <p>This is only needed if you are directly dispatching events, rather 2469 * than handling them in {@link Callback#onKeyDown}. 2470 */ 2471 public void startTracking(KeyEvent event, Object target) { 2472 if (event.getAction() != ACTION_DOWN) { 2473 throw new IllegalArgumentException( 2474 "Can only start tracking on a down event"); 2475 } 2476 if (DEBUG) Log.v(TAG, "Start trackingt in " + target + ": " + this); 2477 mDownKeyCode = event.getKeyCode(); 2478 mDownTarget = target; 2479 } 2480 2481 /** 2482 * Return true if the key event is for a key code that is currently 2483 * being tracked by the dispatcher. 2484 */ 2485 public boolean isTracking(KeyEvent event) { 2486 return mDownKeyCode == event.getKeyCode(); 2487 } 2488 2489 /** 2490 * Keep track of the given event's key code as having performed an 2491 * action with a long press, so no action should occur on the up. 2492 * <p>This is only needed if you are directly dispatching events, rather 2493 * than handling them in {@link Callback#onKeyLongPress}. 2494 */ 2495 public void performedLongPress(KeyEvent event) { 2496 mActiveLongPresses.put(event.getKeyCode(), 1); 2497 } 2498 2499 /** 2500 * Handle key up event to stop tracking. This resets the dispatcher state, 2501 * and updates the key event state based on it. 2502 * <p>This is only needed if you are directly dispatching events, rather 2503 * than handling them in {@link Callback#onKeyUp}. 2504 */ 2505 public void handleUpEvent(KeyEvent event) { 2506 final int keyCode = event.getKeyCode(); 2507 if (DEBUG) Log.v(TAG, "Handle key up " + event + ": " + this); 2508 int index = mActiveLongPresses.indexOfKey(keyCode); 2509 if (index >= 0) { 2510 if (DEBUG) Log.v(TAG, " Index: " + index); 2511 event.mFlags |= FLAG_CANCELED | FLAG_CANCELED_LONG_PRESS; 2512 mActiveLongPresses.removeAt(index); 2513 } 2514 if (mDownKeyCode == keyCode) { 2515 if (DEBUG) Log.v(TAG, " Tracking!"); 2516 event.mFlags |= FLAG_TRACKING; 2517 mDownKeyCode = 0; 2518 mDownTarget = null; 2519 } 2520 } 2521 } 2522 2523 @Override 2524 public String toString() { 2525 return "KeyEvent{action=" + actionToString(mAction) 2526 + " keycode=" + keyCodeToString(mKeyCode) 2527 + " scancode=" + mScanCode 2528 + " metaState=" + metaStateToString(mMetaState) 2529 + " flags=0x" + Integer.toHexString(mFlags) 2530 + " repeat=" + mRepeatCount 2531 + " device=" + mDeviceId 2532 + " source=0x" + Integer.toHexString(mSource) 2533 + "}"; 2534 } 2535 2536 /** 2537 * Returns a string that represents the symbolic name of the specified action 2538 * such as "ACTION_DOWN", or "35" (if unknown). 2539 * 2540 * @param action The action. 2541 * @return The symbolic name of the specified action. 2542 * @hide 2543 */ 2544 public static String actionToString(int action) { 2545 switch (action) { 2546 case ACTION_DOWN: 2547 return "ACTION_DOWN"; 2548 case ACTION_UP: 2549 return "ACTION_UP"; 2550 case ACTION_MULTIPLE: 2551 return "ACTION_MULTIPLE"; 2552 default: 2553 return Integer.toString(action); 2554 } 2555 } 2556 2557 /** 2558 * Returns a string that represents the symbolic name of the specified keycode 2559 * such as "KEYCODE_A", "KEYCODE_DPAD_UP", or "1001" (if unknown). 2560 * 2561 * @param keyCode The key code. 2562 * @return The symbolic name of the specified keycode. 2563 * 2564 * @see KeyCharacterMap#getDisplayLabel 2565 * @hide 2566 */ 2567 public static String keyCodeToString(int keyCode) { 2568 if (keyCode >= 0 && keyCode < KEYCODE_SYMBOLIC_NAMES.length) { 2569 return KEYCODE_SYMBOLIC_NAMES[keyCode]; 2570 } 2571 return Integer.toString(keyCode); 2572 } 2573 2574 /** 2575 * Gets a keycode by its symbolic name such as "KEYCODE_A" or "1001" (if unknown). 2576 * 2577 * @param symbolicName The symbolic name of the keycode. 2578 * @return The keycode or -1 if not found. 2579 * @see #keycodeToString 2580 * @hide 2581 */ 2582 public static int keyCodeFromString(String symbolicName) { 2583 if (symbolicName == null) { 2584 throw new IllegalArgumentException("symbolicName must not be null"); 2585 } 2586 2587 final int count = KEYCODE_SYMBOLIC_NAMES.length; 2588 for (int i = 0; i < count; i++) { 2589 if (symbolicName.equals(KEYCODE_SYMBOLIC_NAMES[i])) { 2590 return i; 2591 } 2592 } 2593 2594 try { 2595 return Integer.parseInt(symbolicName,10); 2596 } catch (NumberFormatException ex) { 2597 return -1; 2598 } 2599 } 2600 2601 /** 2602 * Returns a string that represents the symbolic name of the specified combined meta 2603 * key modifier state flags such as "0", "META_SHIFT_ON", 2604 * "META_ALT_ON|META_SHIFT_ON" or "0x10000000" (if unknown). 2605 * 2606 * @param metaState The meta state. 2607 * @return The symbolic name of the specified combined meta state flags. 2608 * @hide 2609 */ 2610 public static String metaStateToString(int metaState) { 2611 if (metaState == 0) { 2612 return "0"; 2613 } 2614 StringBuilder result = null; 2615 int i = 0; 2616 while (metaState != 0) { 2617 final boolean isSet = (metaState & 1) != 0; 2618 metaState >>>= 1; // unsigned shift! 2619 if (isSet) { 2620 final String name = META_SYMBOLIC_NAMES[i]; 2621 if (result == null) { 2622 if (metaState == 0) { 2623 return name; 2624 } 2625 result = new StringBuilder(name); 2626 } else { 2627 result.append('|'); 2628 result.append(name); 2629 } 2630 } 2631 i += 1; 2632 } 2633 return result.toString(); 2634 } 2635 2636 public static final Parcelable.Creator<KeyEvent> CREATOR 2637 = new Parcelable.Creator<KeyEvent>() { 2638 public KeyEvent createFromParcel(Parcel in) { 2639 in.readInt(); // skip token, we already know this is a KeyEvent 2640 return KeyEvent.createFromParcelBody(in); 2641 } 2642 2643 public KeyEvent[] newArray(int size) { 2644 return new KeyEvent[size]; 2645 } 2646 }; 2647 2648 /** @hide */ 2649 public static KeyEvent createFromParcelBody(Parcel in) { 2650 return new KeyEvent(in); 2651 } 2652 2653 private KeyEvent(Parcel in) { 2654 readBaseFromParcel(in); 2655 2656 mAction = in.readInt(); 2657 mKeyCode = in.readInt(); 2658 mRepeatCount = in.readInt(); 2659 mMetaState = in.readInt(); 2660 mScanCode = in.readInt(); 2661 mFlags = in.readInt(); 2662 mDownTime = in.readLong(); 2663 mEventTime = in.readLong(); 2664 } 2665 2666 public void writeToParcel(Parcel out, int flags) { 2667 out.writeInt(PARCEL_TOKEN_KEY_EVENT); 2668 2669 writeBaseToParcel(out); 2670 2671 out.writeInt(mAction); 2672 out.writeInt(mKeyCode); 2673 out.writeInt(mRepeatCount); 2674 out.writeInt(mMetaState); 2675 out.writeInt(mScanCode); 2676 out.writeInt(mFlags); 2677 out.writeLong(mDownTime); 2678 out.writeLong(mEventTime); 2679 } 2680 2681 private native boolean native_isSystemKey(int keyCode); 2682 private native boolean native_hasDefaultAction(int keyCode); 2683} 2684