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