blob: e4a4a7569dc8583fb0e7ab5bf4d761da309710e8 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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;
Jeff Brown6b53e8d2010-11-10 16:03:06 -080021import android.text.method.MetaKeyKeyListener;
Dianne Hackborn8d374262009-09-14 21:21:52 -070022import android.util.Log;
Jeff Brown28cbf4b2010-12-13 10:33:20 -080023import android.util.Slog;
Jeff Brown6f2fba42011-02-19 01:08:02 -080024import android.util.SparseArray;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070025import android.util.SparseIntArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.view.KeyCharacterMap;
27import android.view.KeyCharacterMap.KeyData;
28
29/**
Jeff Browndc1ab4b2010-09-14 18:03:38 -070030 * Object used to report key and button events.
31 * <p>
32 * Each key press is described by a sequence of key events. A key press
33 * starts with a key event with {@link #ACTION_DOWN}. If the key is held
34 * sufficiently long that it repeats, then the initial down is followed
35 * additional key events with {@link #ACTION_DOWN} and a non-zero value for
36 * {@link #getRepeatCount()}. The last key event is a {@link #ACTION_UP}
37 * for the key up. If the key press is canceled, the key up event will have the
38 * {@link #FLAG_CANCELED} flag set.
39 * </p><p>
40 * Key events are generally accompanied by a key code ({@link #getKeyCode()}),
41 * scan code ({@link #getScanCode()}) and meta state ({@link #getMetaState()}).
42 * Key code constants are defined in this class. Scan code constants are raw
43 * device-specific codes obtained from the OS and so are not generally meaningful
44 * to applications unless interpreted using the {@link KeyCharacterMap}.
45 * Meta states describe the pressed state of key modifiers
46 * such as {@link #META_SHIFT_ON} or {@link #META_ALT_ON}.
47 * </p><p>
Jeff Brown497a92c2010-09-12 17:55:08 -070048 * Key codes typically correspond one-to-one with individual keys on an input device.
49 * Many keys and key combinations serve quite different functions on different
50 * input devices so care must be taken when interpreting them. Always use the
51 * {@link KeyCharacterMap} associated with the input device when mapping keys
52 * to characters. Be aware that there may be multiple key input devices active
53 * at the same time and each will have its own key character map.
54 * </p><p>
Jeff Browndc1ab4b2010-09-14 18:03:38 -070055 * When interacting with an IME, the framework may deliver key events
56 * with the special action {@link #ACTION_MULTIPLE} that either specifies
57 * that single repeated key code or a sequence of characters to insert.
58 * </p><p>
Jeff Brownb6997262010-10-08 22:31:17 -070059 * In general, the framework cannot guarantee that the key events it delivers
60 * to a view always constitute complete key sequences since some events may be dropped
61 * or modified by containing views before they are delivered. The view implementation
62 * should be prepared to handle {@link #FLAG_CANCELED} and should tolerate anomalous
63 * situations such as receiving a new {@link #ACTION_DOWN} without first having
64 * received an {@link #ACTION_UP} for the prior key press.
Jeff Browndc1ab4b2010-09-14 18:03:38 -070065 * </p><p>
66 * Refer to {@link InputDevice} for more information about how different kinds of
67 * input devices and sources represent keys and buttons.
68 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 */
Jeff Brownc5ed5912010-07-14 18:48:53 -070070public class KeyEvent extends InputEvent implements Parcelable {
Jeff Browndc1ab4b2010-09-14 18:03:38 -070071 /** Key code constant: Unknown key code. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 public static final int KEYCODE_UNKNOWN = 0;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070073 /** Key code constant: Soft Left key.
74 * Usually situated below the display on phones and used as a multi-function
75 * feature key for selecting a software defined function shown on the bottom left
76 * of the display. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 public static final int KEYCODE_SOFT_LEFT = 1;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070078 /** Key code constant: Soft Right key.
79 * Usually situated below the display on phones and used as a multi-function
80 * feature key for selecting a software defined function shown on the bottom right
81 * of the display. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 public static final int KEYCODE_SOFT_RIGHT = 2;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070083 /** Key code constant: Home key.
84 * This key is handled by the framework and is never delivered to applications. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 public static final int KEYCODE_HOME = 3;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070086 /** Key code constant: Back key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 public static final int KEYCODE_BACK = 4;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070088 /** Key code constant: Call key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 public static final int KEYCODE_CALL = 5;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070090 /** Key code constant: End Call key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 public static final int KEYCODE_ENDCALL = 6;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070092 /** Key code constant: '0' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 public static final int KEYCODE_0 = 7;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070094 /** Key code constant: '1' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 public static final int KEYCODE_1 = 8;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070096 /** Key code constant: '2' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 public static final int KEYCODE_2 = 9;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070098 /** Key code constant: '3' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 public static final int KEYCODE_3 = 10;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700100 /** Key code constant: '4' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 public static final int KEYCODE_4 = 11;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700102 /** Key code constant: '5' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 public static final int KEYCODE_5 = 12;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700104 /** Key code constant: '6' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 public static final int KEYCODE_6 = 13;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700106 /** Key code constant: '7' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 public static final int KEYCODE_7 = 14;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700108 /** Key code constant: '8' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 public static final int KEYCODE_8 = 15;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700110 /** Key code constant: '9' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 public static final int KEYCODE_9 = 16;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700112 /** Key code constant: '*' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 public static final int KEYCODE_STAR = 17;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700114 /** Key code constant: '#' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 public static final int KEYCODE_POUND = 18;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700116 /** Key code constant: Directional Pad Up key.
117 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 public static final int KEYCODE_DPAD_UP = 19;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700119 /** Key code constant: Directional Pad Down key.
120 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 public static final int KEYCODE_DPAD_DOWN = 20;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700122 /** Key code constant: Directional Pad Left key.
123 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 public static final int KEYCODE_DPAD_LEFT = 21;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700125 /** Key code constant: Directional Pad Right key.
126 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 public static final int KEYCODE_DPAD_RIGHT = 22;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700128 /** Key code constant: Directional Pad Center key.
129 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 public static final int KEYCODE_DPAD_CENTER = 23;
Jeff Brownb0418da2010-11-01 15:24:01 -0700131 /** Key code constant: Volume Up key.
132 * Adjusts the speaker volume up. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 public static final int KEYCODE_VOLUME_UP = 24;
Jeff Brownb0418da2010-11-01 15:24:01 -0700134 /** Key code constant: Volume Down key.
135 * Adjusts the speaker volume down. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 public static final int KEYCODE_VOLUME_DOWN = 25;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700137 /** Key code constant: Power key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 public static final int KEYCODE_POWER = 26;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700139 /** Key code constant: Camera key.
140 * Used to launch a camera application or take pictures. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 public static final int KEYCODE_CAMERA = 27;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700142 /** Key code constant: Clear key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 public static final int KEYCODE_CLEAR = 28;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700144 /** Key code constant: 'A' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 public static final int KEYCODE_A = 29;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700146 /** Key code constant: 'B' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 public static final int KEYCODE_B = 30;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700148 /** Key code constant: 'C' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 public static final int KEYCODE_C = 31;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700150 /** Key code constant: 'D' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 public static final int KEYCODE_D = 32;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700152 /** Key code constant: 'E' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 public static final int KEYCODE_E = 33;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700154 /** Key code constant: 'F' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 public static final int KEYCODE_F = 34;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700156 /** Key code constant: 'G' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 public static final int KEYCODE_G = 35;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700158 /** Key code constant: 'H' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 public static final int KEYCODE_H = 36;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700160 /** Key code constant: 'I' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 public static final int KEYCODE_I = 37;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700162 /** Key code constant: 'J' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 public static final int KEYCODE_J = 38;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700164 /** Key code constant: 'K' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 public static final int KEYCODE_K = 39;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700166 /** Key code constant: 'L' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 public static final int KEYCODE_L = 40;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700168 /** Key code constant: 'M' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 public static final int KEYCODE_M = 41;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700170 /** Key code constant: 'N' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 public static final int KEYCODE_N = 42;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700172 /** Key code constant: 'O' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 public static final int KEYCODE_O = 43;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700174 /** Key code constant: 'P' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 public static final int KEYCODE_P = 44;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700176 /** Key code constant: 'Q' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 public static final int KEYCODE_Q = 45;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700178 /** Key code constant: 'R' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 public static final int KEYCODE_R = 46;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700180 /** Key code constant: 'S' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 public static final int KEYCODE_S = 47;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700182 /** Key code constant: 'T' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 public static final int KEYCODE_T = 48;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700184 /** Key code constant: 'U' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 public static final int KEYCODE_U = 49;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700186 /** Key code constant: 'V' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 public static final int KEYCODE_V = 50;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700188 /** Key code constant: 'W' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 public static final int KEYCODE_W = 51;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700190 /** Key code constant: 'X' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 public static final int KEYCODE_X = 52;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700192 /** Key code constant: 'Y' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 public static final int KEYCODE_Y = 53;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700194 /** Key code constant: 'Z' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 public static final int KEYCODE_Z = 54;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700196 /** Key code constant: ',' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 public static final int KEYCODE_COMMA = 55;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700198 /** Key code constant: '.' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 public static final int KEYCODE_PERIOD = 56;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700200 /** Key code constant: Left Alt modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 public static final int KEYCODE_ALT_LEFT = 57;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700202 /** Key code constant: Right Alt modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 public static final int KEYCODE_ALT_RIGHT = 58;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700204 /** Key code constant: Left Shift modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 public static final int KEYCODE_SHIFT_LEFT = 59;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700206 /** Key code constant: Right Shift modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 public static final int KEYCODE_SHIFT_RIGHT = 60;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700208 /** Key code constant: Tab key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 public static final int KEYCODE_TAB = 61;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700210 /** Key code constant: Space key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 public static final int KEYCODE_SPACE = 62;
Jeff Brown224d4a12010-10-07 20:28:53 -0700212 /** Key code constant: Symbol modifier key.
213 * Used to enter alternate symbols. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 public static final int KEYCODE_SYM = 63;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700215 /** Key code constant: Explorer special function key.
216 * Used to launch a browser application. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 public static final int KEYCODE_EXPLORER = 64;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700218 /** Key code constant: Envelope special function key.
219 * Used to launch a mail application. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 public static final int KEYCODE_ENVELOPE = 65;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700221 /** Key code constant: Enter key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 public static final int KEYCODE_ENTER = 66;
Jeff Brown224d4a12010-10-07 20:28:53 -0700223 /** Key code constant: Backspace key.
Jeff Brown497a92c2010-09-12 17:55:08 -0700224 * Deletes characters before the insertion point, unlike {@link #KEYCODE_FORWARD_DEL}. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 public static final int KEYCODE_DEL = 67;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700226 /** Key code constant: '`' (backtick) key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 public static final int KEYCODE_GRAVE = 68;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700228 /** Key code constant: '-'. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 public static final int KEYCODE_MINUS = 69;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700230 /** Key code constant: '=' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 public static final int KEYCODE_EQUALS = 70;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700232 /** Key code constant: '[' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 public static final int KEYCODE_LEFT_BRACKET = 71;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700234 /** Key code constant: ']' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 public static final int KEYCODE_RIGHT_BRACKET = 72;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700236 /** Key code constant: '\' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 public static final int KEYCODE_BACKSLASH = 73;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700238 /** Key code constant: ';' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 public static final int KEYCODE_SEMICOLON = 74;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700240 /** Key code constant: ''' (apostrophe) key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 public static final int KEYCODE_APOSTROPHE = 75;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700242 /** Key code constant: '/' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 public static final int KEYCODE_SLASH = 76;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700244 /** Key code constant: '@' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 public static final int KEYCODE_AT = 77;
Jeff Brown224d4a12010-10-07 20:28:53 -0700246 /** Key code constant: Number modifier key.
247 * Used to enter numeric symbols.
248 * This key is not Num Lock; it is more like {@link #KEYCODE_ALT_LEFT} and is
249 * interpreted as an ALT key by {@link android.text.method.MetaKeyKeyListener}. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 public static final int KEYCODE_NUM = 78;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700251 /** Key code constant: Headset Hook key.
252 * Used to hang up calls and stop media. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 public static final int KEYCODE_HEADSETHOOK = 79;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700254 /** Key code constant: Camera Focus key.
255 * Used to focus the camera. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 public static final int KEYCODE_FOCUS = 80; // *Camera* focus
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700257 /** Key code constant: '+' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 public static final int KEYCODE_PLUS = 81;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700259 /** Key code constant: Menu key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 public static final int KEYCODE_MENU = 82;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700261 /** Key code constant: Notification key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 public static final int KEYCODE_NOTIFICATION = 83;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700263 /** Key code constant: Search key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 public static final int KEYCODE_SEARCH = 84;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700265 /** Key code constant: Play/Pause media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700266 public static final int KEYCODE_MEDIA_PLAY_PAUSE= 85;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700267 /** Key code constant: Stop media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700268 public static final int KEYCODE_MEDIA_STOP = 86;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700269 /** Key code constant: Play Next media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700270 public static final int KEYCODE_MEDIA_NEXT = 87;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700271 /** Key code constant: Play Previous media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700272 public static final int KEYCODE_MEDIA_PREVIOUS = 88;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700273 /** Key code constant: Rewind media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700274 public static final int KEYCODE_MEDIA_REWIND = 89;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700275 /** Key code constant: Fast Forward media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700276 public static final int KEYCODE_MEDIA_FAST_FORWARD = 90;
Jeff Brownb0418da2010-11-01 15:24:01 -0700277 /** Key code constant: Mute key.
278 * Mutes the microphone, unlike {@link #KEYCODE_VOLUME_MUTE}. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 public static final int KEYCODE_MUTE = 91;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700280 /** Key code constant: Page Up key. */
Chih-Wei Huang4fedd802009-05-27 15:52:50 +0800281 public static final int KEYCODE_PAGE_UP = 92;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700282 /** Key code constant: Page Down key. */
Chih-Wei Huang4fedd802009-05-27 15:52:50 +0800283 public static final int KEYCODE_PAGE_DOWN = 93;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700284 /** Key code constant: Picture Symbols modifier key.
285 * Used to switch symbol sets (Emoji, Kao-moji). */
mogimob032bc02009-10-03 03:13:56 +0900286 public static final int KEYCODE_PICTSYMBOLS = 94; // switch symbol-sets (Emoji,Kao-moji)
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700287 /** Key code constant: Switch Charset modifier key.
288 * Used to switch character sets (Kanji, Katakana). */
mogimob032bc02009-10-03 03:13:56 +0900289 public static final int KEYCODE_SWITCH_CHARSET = 95; // switch char-sets (Kanji,Katakana)
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700290 /** Key code constant: A Button key.
291 * On a game controller, the A button should be either the button labeled A
292 * or the first button on the upper row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700293 public static final int KEYCODE_BUTTON_A = 96;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700294 /** Key code constant: B Button key.
295 * On a game controller, the B button should be either the button labeled B
296 * or the second button on the upper row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700297 public static final int KEYCODE_BUTTON_B = 97;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700298 /** Key code constant: C Button key.
299 * On a game controller, the C button should be either the button labeled C
300 * or the third button on the upper row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700301 public static final int KEYCODE_BUTTON_C = 98;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700302 /** Key code constant: X Button key.
303 * On a game controller, the X button should be either the button labeled X
304 * or the first button on the lower row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700305 public static final int KEYCODE_BUTTON_X = 99;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700306 /** Key code constant: Y Button key.
307 * On a game controller, the Y button should be either the button labeled Y
308 * or the second button on the lower row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700309 public static final int KEYCODE_BUTTON_Y = 100;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700310 /** Key code constant: Z Button key.
311 * On a game controller, the Z button should be either the button labeled Z
312 * or the third button on the lower row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700313 public static final int KEYCODE_BUTTON_Z = 101;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700314 /** Key code constant: L1 Button key.
315 * On a game controller, the L1 button should be either the button labeled L1 (or L)
316 * or the top left trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700317 public static final int KEYCODE_BUTTON_L1 = 102;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700318 /** Key code constant: R1 Button key.
319 * On a game controller, the R1 button should be either the button labeled R1 (or R)
320 * or the top right trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700321 public static final int KEYCODE_BUTTON_R1 = 103;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700322 /** Key code constant: L2 Button key.
323 * On a game controller, the L2 button should be either the button labeled L2
324 * or the bottom left trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700325 public static final int KEYCODE_BUTTON_L2 = 104;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700326 /** Key code constant: R2 Button key.
327 * On a game controller, the R2 button should be either the button labeled R2
328 * or the bottom right trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700329 public static final int KEYCODE_BUTTON_R2 = 105;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700330 /** Key code constant: Left Thumb Button key.
331 * On a game controller, the left thumb button indicates that the left (or only)
332 * joystick is pressed. */
Jeff Brownfd035822010-06-30 16:10:35 -0700333 public static final int KEYCODE_BUTTON_THUMBL = 106;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700334 /** Key code constant: Right Thumb Button key.
335 * On a game controller, the right thumb button indicates that the right
336 * joystick is pressed. */
Jeff Brownfd035822010-06-30 16:10:35 -0700337 public static final int KEYCODE_BUTTON_THUMBR = 107;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700338 /** Key code constant: Start Button key.
339 * On a game controller, the button labeled Start. */
Jeff Brownfd035822010-06-30 16:10:35 -0700340 public static final int KEYCODE_BUTTON_START = 108;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700341 /** Key code constant: Select Button key.
342 * On a game controller, the button labeled Select. */
Jeff Brownfd035822010-06-30 16:10:35 -0700343 public static final int KEYCODE_BUTTON_SELECT = 109;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700344 /** Key code constant: Mode Button key.
345 * On a game controller, the button labeled Mode. */
Jeff Brownfd035822010-06-30 16:10:35 -0700346 public static final int KEYCODE_BUTTON_MODE = 110;
Jeff Brown497a92c2010-09-12 17:55:08 -0700347 /** Key code constant: Escape key. */
348 public static final int KEYCODE_ESCAPE = 111;
349 /** Key code constant: Forward Delete key.
350 * Deletes characters ahead of the insertion point, unlike {@link #KEYCODE_DEL}. */
351 public static final int KEYCODE_FORWARD_DEL = 112;
352 /** Key code constant: Left Control modifier key. */
353 public static final int KEYCODE_CTRL_LEFT = 113;
354 /** Key code constant: Right Control modifier key. */
355 public static final int KEYCODE_CTRL_RIGHT = 114;
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800356 /** Key code constant: Caps Lock key. */
Jeff Brown497a92c2010-09-12 17:55:08 -0700357 public static final int KEYCODE_CAPS_LOCK = 115;
358 /** Key code constant: Scroll Lock key. */
359 public static final int KEYCODE_SCROLL_LOCK = 116;
360 /** Key code constant: Left Meta modifier key. */
361 public static final int KEYCODE_META_LEFT = 117;
362 /** Key code constant: Right Meta modifier key. */
363 public static final int KEYCODE_META_RIGHT = 118;
364 /** Key code constant: Function modifier key. */
365 public static final int KEYCODE_FUNCTION = 119;
366 /** Key code constant: System Request / Print Screen key. */
367 public static final int KEYCODE_SYSRQ = 120;
368 /** Key code constant: Break / Pause key. */
369 public static final int KEYCODE_BREAK = 121;
370 /** Key code constant: Home Movement key.
371 * Used for scrolling or moving the cursor around to the start of a line
372 * or to the top of a list. */
373 public static final int KEYCODE_MOVE_HOME = 122;
374 /** Key code constant: End Movement key.
375 * Used for scrolling or moving the cursor around to the end of a line
376 * or to the bottom of a list. */
377 public static final int KEYCODE_MOVE_END = 123;
378 /** Key code constant: Insert key.
379 * Toggles insert / overwrite edit mode. */
380 public static final int KEYCODE_INSERT = 124;
381 /** Key code constant: Forward key.
382 * Navigates forward in the history stack. Complement of {@link #KEYCODE_BACK}. */
383 public static final int KEYCODE_FORWARD = 125;
384 /** Key code constant: Play media key. */
385 public static final int KEYCODE_MEDIA_PLAY = 126;
386 /** Key code constant: Pause media key. */
387 public static final int KEYCODE_MEDIA_PAUSE = 127;
388 /** Key code constant: Close media key.
389 * May be used to close a CD tray, for example. */
390 public static final int KEYCODE_MEDIA_CLOSE = 128;
391 /** Key code constant: Eject media key.
392 * May be used to eject a CD tray, for example. */
393 public static final int KEYCODE_MEDIA_EJECT = 129;
394 /** Key code constant: Record media key. */
395 public static final int KEYCODE_MEDIA_RECORD = 130;
396 /** Key code constant: F1 key. */
397 public static final int KEYCODE_F1 = 131;
398 /** Key code constant: F2 key. */
399 public static final int KEYCODE_F2 = 132;
400 /** Key code constant: F3 key. */
401 public static final int KEYCODE_F3 = 133;
402 /** Key code constant: F4 key. */
403 public static final int KEYCODE_F4 = 134;
404 /** Key code constant: F5 key. */
405 public static final int KEYCODE_F5 = 135;
406 /** Key code constant: F6 key. */
407 public static final int KEYCODE_F6 = 136;
408 /** Key code constant: F7 key. */
409 public static final int KEYCODE_F7 = 137;
410 /** Key code constant: F8 key. */
411 public static final int KEYCODE_F8 = 138;
412 /** Key code constant: F9 key. */
413 public static final int KEYCODE_F9 = 139;
414 /** Key code constant: F10 key. */
415 public static final int KEYCODE_F10 = 140;
416 /** Key code constant: F11 key. */
417 public static final int KEYCODE_F11 = 141;
418 /** Key code constant: F12 key. */
419 public static final int KEYCODE_F12 = 142;
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800420 /** Key code constant: Num Lock key.
Jeff Brown497a92c2010-09-12 17:55:08 -0700421 * This is the Num Lock key; it is different from {@link #KEYCODE_NUM}.
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800422 * This key alters the behavior of other keys on the numeric keypad. */
Jeff Brown497a92c2010-09-12 17:55:08 -0700423 public static final int KEYCODE_NUM_LOCK = 143;
424 /** Key code constant: Numeric keypad '0' key. */
425 public static final int KEYCODE_NUMPAD_0 = 144;
426 /** Key code constant: Numeric keypad '1' key. */
427 public static final int KEYCODE_NUMPAD_1 = 145;
428 /** Key code constant: Numeric keypad '2' key. */
429 public static final int KEYCODE_NUMPAD_2 = 146;
430 /** Key code constant: Numeric keypad '3' key. */
431 public static final int KEYCODE_NUMPAD_3 = 147;
432 /** Key code constant: Numeric keypad '4' key. */
433 public static final int KEYCODE_NUMPAD_4 = 148;
434 /** Key code constant: Numeric keypad '5' key. */
435 public static final int KEYCODE_NUMPAD_5 = 149;
436 /** Key code constant: Numeric keypad '6' key. */
437 public static final int KEYCODE_NUMPAD_6 = 150;
438 /** Key code constant: Numeric keypad '7' key. */
439 public static final int KEYCODE_NUMPAD_7 = 151;
440 /** Key code constant: Numeric keypad '8' key. */
441 public static final int KEYCODE_NUMPAD_8 = 152;
442 /** Key code constant: Numeric keypad '9' key. */
443 public static final int KEYCODE_NUMPAD_9 = 153;
444 /** Key code constant: Numeric keypad '/' key (for division). */
445 public static final int KEYCODE_NUMPAD_DIVIDE = 154;
446 /** Key code constant: Numeric keypad '*' key (for multiplication). */
447 public static final int KEYCODE_NUMPAD_MULTIPLY = 155;
448 /** Key code constant: Numeric keypad '-' key (for subtraction). */
449 public static final int KEYCODE_NUMPAD_SUBTRACT = 156;
450 /** Key code constant: Numeric keypad '+' key (for addition). */
451 public static final int KEYCODE_NUMPAD_ADD = 157;
452 /** Key code constant: Numeric keypad '.' key (for decimals or digit grouping). */
453 public static final int KEYCODE_NUMPAD_DOT = 158;
454 /** Key code constant: Numeric keypad ',' key (for decimals or digit grouping). */
455 public static final int KEYCODE_NUMPAD_COMMA = 159;
456 /** Key code constant: Numeric keypad Enter key. */
457 public static final int KEYCODE_NUMPAD_ENTER = 160;
458 /** Key code constant: Numeric keypad '=' key. */
459 public static final int KEYCODE_NUMPAD_EQUALS = 161;
460 /** Key code constant: Numeric keypad '(' key. */
461 public static final int KEYCODE_NUMPAD_LEFT_PAREN = 162;
462 /** Key code constant: Numeric keypad ')' key. */
463 public static final int KEYCODE_NUMPAD_RIGHT_PAREN = 163;
Jeff Brownb0418da2010-11-01 15:24:01 -0700464 /** Key code constant: Volume Mute key.
465 * Mutes the speaker, unlike {@link #KEYCODE_MUTE}.
466 * This key should normally be implemented as a toggle such that the first press
467 * mutes the speaker and the second press restores the original volume. */
468 public static final int KEYCODE_VOLUME_MUTE = 164;
Jason Bayer3adf4902010-11-09 14:54:55 -0800469 /** Key code constant: Info key.
470 * Common on TV remotes to show additional information related to what is
471 * currently being viewed. */
472 public static final int KEYCODE_INFO = 165;
473 /** Key code constant: Channel up key.
474 * On TV remotes, increments the television channel. */
475 public static final int KEYCODE_CHANNEL_UP = 166;
476 /** Key code constant: Channel down key.
477 * On TV remotes, decrements the television channel. */
478 public static final int KEYCODE_CHANNEL_DOWN = 167;
479 /** Key code constant: Zoom in key. */
480 public static final int KEYCODE_ZOOM_IN = 168;
481 /** Key code constant: Zoom out key. */
482 public static final int KEYCODE_ZOOM_OUT = 169;
483 /** Key code constant: TV key.
484 * On TV remotes, switches to viewing live TV. */
485 public static final int KEYCODE_TV = 170;
486 /** Key code constant: Window key.
487 * On TV remotes, toggles picture-in-picture mode or other windowing functions. */
488 public static final int KEYCODE_WINDOW = 171;
489 /** Key code constant: Guide key.
490 * On TV remotes, shows a programming guide. */
491 public static final int KEYCODE_GUIDE = 172;
492 /** Key code constant: DVR key.
493 * On some TV remotes, switches to a DVR mode for recorded shows. */
494 public static final int KEYCODE_DVR = 173;
495 /** Key code constant: Bookmark key.
496 * On some TV remotes, bookmarks content or web pages. */
497 public static final int KEYCODE_BOOKMARK = 174;
498 /** Key code constant: Toggle captions key.
499 * Switches the mode for closed-captioning text, for example during television shows. */
500 public static final int KEYCODE_CAPTIONS = 175;
501 /** Key code constant: Settings key.
502 * Starts the system settings activity. */
503 public static final int KEYCODE_SETTINGS = 176;
504 /** Key code constant: TV power key.
505 * On TV remotes, toggles the power on a television screen. */
506 public static final int KEYCODE_TV_POWER = 177;
507 /** Key code constant: TV input key.
508 * On TV remotes, switches the input on a television screen. */
509 public static final int KEYCODE_TV_INPUT = 178;
510 /** Key code constant: Set-top-box power key.
511 * On TV remotes, toggles the power on an external Set-top-box. */
512 public static final int KEYCODE_STB_POWER = 179;
513 /** Key code constant: Set-top-box input key.
514 * On TV remotes, switches the input mode on an external Set-top-box. */
515 public static final int KEYCODE_STB_INPUT = 180;
516 /** Key code constant: A/V Receiver power key.
517 * On TV remotes, toggles the power on an external A/V Receiver. */
518 public static final int KEYCODE_AVR_POWER = 181;
519 /** Key code constant: A/V Receiver input key.
520 * On TV remotes, switches the input mode on an external A/V Receiver. */
521 public static final int KEYCODE_AVR_INPUT = 182;
522 /** Key code constant: Red "programmable" key.
523 * On TV remotes, acts as a contextual/programmable key. */
524 public static final int KEYCODE_PROG_RED = 183;
525 /** Key code constant: Green "programmable" key.
526 * On TV remotes, actsas a contextual/programmable key. */
527 public static final int KEYCODE_PROG_GREEN = 184;
528 /** Key code constant: Yellow "programmable" key.
529 * On TV remotes, acts as a contextual/programmable key. */
530 public static final int KEYCODE_PROG_YELLOW = 185;
531 /** Key code constant: Blue "programmable" key.
532 * On TV remotes, acts as a contextual/programmable key. */
533 public static final int KEYCODE_PROG_BLUE = 186;
Jeff Brown49ed71d2010-12-06 17:13:33 -0800534 /** Key code constant: App switch key.
535 * Should bring up the application switcher dialog. */
536 public static final int KEYCODE_APP_SWITCH = 187;
Jeff Browncb1404e2011-01-15 18:14:15 -0800537 /** Key code constant: Generic Game Pad Button #1.*/
538 public static final int KEYCODE_BUTTON_1 = 188;
539 /** Key code constant: Generic Game Pad Button #2.*/
540 public static final int KEYCODE_BUTTON_2 = 189;
541 /** Key code constant: Generic Game Pad Button #3.*/
542 public static final int KEYCODE_BUTTON_3 = 190;
543 /** Key code constant: Generic Game Pad Button #4.*/
544 public static final int KEYCODE_BUTTON_4 = 191;
545 /** Key code constant: Generic Game Pad Button #5.*/
546 public static final int KEYCODE_BUTTON_5 = 192;
547 /** Key code constant: Generic Game Pad Button #6.*/
548 public static final int KEYCODE_BUTTON_6 = 193;
549 /** Key code constant: Generic Game Pad Button #7.*/
550 public static final int KEYCODE_BUTTON_7 = 194;
551 /** Key code constant: Generic Game Pad Button #8.*/
552 public static final int KEYCODE_BUTTON_8 = 195;
553 /** Key code constant: Generic Game Pad Button #9.*/
554 public static final int KEYCODE_BUTTON_9 = 196;
555 /** Key code constant: Generic Game Pad Button #10.*/
556 public static final int KEYCODE_BUTTON_10 = 197;
557 /** Key code constant: Generic Game Pad Button #11.*/
558 public static final int KEYCODE_BUTTON_11 = 198;
559 /** Key code constant: Generic Game Pad Button #12.*/
560 public static final int KEYCODE_BUTTON_12 = 199;
561 /** Key code constant: Generic Game Pad Button #13.*/
562 public static final int KEYCODE_BUTTON_13 = 200;
563 /** Key code constant: Generic Game Pad Button #14.*/
564 public static final int KEYCODE_BUTTON_14 = 201;
565 /** Key code constant: Generic Game Pad Button #15.*/
566 public static final int KEYCODE_BUTTON_15 = 202;
567 /** Key code constant: Generic Game Pad Button #16.*/
568 public static final int KEYCODE_BUTTON_16 = 203;
Jeff Brown9812aed2011-03-07 17:09:51 -0800569 /** Key code constant: Language Switch key.
570 * Toggles the current input language such as switching between English and Japanese on
571 * a QWERTY keyboard. On some devices, the same function may be performed by
572 * pressing Shift+Spacebar. */
573 public static final int KEYCODE_LANGUAGE_SWITCH = 204;
574 /** Key code constant: Manner Mode key.
575 * Toggles silent or vibrate mode on and off to make the device behave more politely
576 * in certain settings such as on a crowded train. On some devices, the key may only
577 * operate when long-pressed. */
578 public static final int KEYCODE_MANNER_MODE = 205;
579 /** Key code constant: 3D Mode key.
580 * Toggles the display between 2D and 3D mode. */
581 public static final int KEYCODE_3D_MODE = 206;
Jeff Brown6651a632011-11-28 12:59:11 -0800582 /** Key code constant: Contacts special function key.
583 * Used to launch an address book application. */
584 public static final int KEYCODE_CONTACTS = 207;
585 /** Key code constant: Calendar special function key.
586 * Used to launch a calendar application. */
587 public static final int KEYCODE_CALENDAR = 208;
588 /** Key code constant: Music special function key.
589 * Used to launch a music player application. */
590 public static final int KEYCODE_MUSIC = 209;
591 /** Key code constant: Calculator special function key.
592 * Used to launch a calculator application. */
593 public static final int KEYCODE_CALCULATOR = 210;
Yang Chuang7511f9c2012-02-10 15:18:26 +0800594 /** Key code constant: Japanese full-width / half-width key. */
595 public static final int KEYCODE_ZENKAKU_HANKAKU = 211;
596 /** Key code constant: Japanese alphanumeric key. */
597 public static final int KEYCODE_EISU = 212;
598 /** Key code constant: Japanese non-conversion key. */
599 public static final int KEYCODE_MUHENKAN = 213;
600 /** Key code constant: Japanese conversion key. */
601 public static final int KEYCODE_HENKAN = 214;
602 /** Key code constant: Japanese katakana / hiragana key. */
603 public static final int KEYCODE_KATAKANA_HIRAGANA = 215;
604 /** Key code constant: Japanese Yen key. */
605 public static final int KEYCODE_YEN = 216;
606 /** Key code constant: Japanese Ro key. */
607 public static final int KEYCODE_RO = 217;
608 /** Key code constant: Japanese kana key. */
609 public static final int KEYCODE_KANA = 218;
Jeff Brown497a92c2010-09-12 17:55:08 -0700610
Yang Chuang7511f9c2012-02-10 15:18:26 +0800611 private static final int LAST_KEYCODE = KEYCODE_KANA;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612
613 // NOTE: If you add a new keycode here you must also add it to:
614 // isSystem()
Jeff Brown46b9ac02010-04-22 18:58:52 -0700615 // native/include/android/keycodes.h
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 // frameworks/base/include/ui/KeycodeLabels.h
Jeff Brownfd035822010-06-30 16:10:35 -0700617 // external/webkit/WebKit/android/plugins/ANPKeyCodes.h
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 // frameworks/base/core/res/res/values/attrs.xml
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 // emulator?
Jeff Brown6651a632011-11-28 12:59:11 -0800620 // LAST_KEYCODE
621 // KEYCODE_SYMBOLIC_NAMES
Dianne Hackborn935ae462009-04-13 16:11:55 -0700622 //
623 // Also Android currently does not reserve code ranges for vendor-
624 // specific key codes. If you have new key codes to have, you
625 // MUST contribute a patch to the open source project to define
626 // those new codes. This is intended to maintain a consistent
627 // set of key code definitions across all Android devices.
Jeff Brown497a92c2010-09-12 17:55:08 -0700628
Jeff Brown6f2fba42011-02-19 01:08:02 -0800629 // Symbolic names of all key codes.
630 private static final SparseArray<String> KEYCODE_SYMBOLIC_NAMES = new SparseArray<String>();
631 private static void populateKeycodeSymbolicNames() {
632 SparseArray<String> names = KEYCODE_SYMBOLIC_NAMES;
633 names.append(KEYCODE_UNKNOWN, "KEYCODE_UNKNOWN");
634 names.append(KEYCODE_SOFT_LEFT, "KEYCODE_SOFT_LEFT");
635 names.append(KEYCODE_SOFT_RIGHT, "KEYCODE_SOFT_RIGHT");
636 names.append(KEYCODE_HOME, "KEYCODE_HOME");
637 names.append(KEYCODE_BACK, "KEYCODE_BACK");
638 names.append(KEYCODE_CALL, "KEYCODE_CALL");
639 names.append(KEYCODE_ENDCALL, "KEYCODE_ENDCALL");
640 names.append(KEYCODE_0, "KEYCODE_0");
641 names.append(KEYCODE_1, "KEYCODE_1");
642 names.append(KEYCODE_2, "KEYCODE_2");
643 names.append(KEYCODE_3, "KEYCODE_3");
644 names.append(KEYCODE_4, "KEYCODE_4");
645 names.append(KEYCODE_5, "KEYCODE_5");
646 names.append(KEYCODE_6, "KEYCODE_6");
647 names.append(KEYCODE_7, "KEYCODE_7");
648 names.append(KEYCODE_8, "KEYCODE_8");
649 names.append(KEYCODE_9, "KEYCODE_9");
650 names.append(KEYCODE_STAR, "KEYCODE_STAR");
651 names.append(KEYCODE_POUND, "KEYCODE_POUND");
652 names.append(KEYCODE_DPAD_UP, "KEYCODE_DPAD_UP");
653 names.append(KEYCODE_DPAD_DOWN, "KEYCODE_DPAD_DOWN");
654 names.append(KEYCODE_DPAD_LEFT, "KEYCODE_DPAD_LEFT");
655 names.append(KEYCODE_DPAD_RIGHT, "KEYCODE_DPAD_RIGHT");
656 names.append(KEYCODE_DPAD_CENTER, "KEYCODE_DPAD_CENTER");
657 names.append(KEYCODE_VOLUME_UP, "KEYCODE_VOLUME_UP");
658 names.append(KEYCODE_VOLUME_DOWN, "KEYCODE_VOLUME_DOWN");
659 names.append(KEYCODE_POWER, "KEYCODE_POWER");
660 names.append(KEYCODE_CAMERA, "KEYCODE_CAMERA");
661 names.append(KEYCODE_CLEAR, "KEYCODE_CLEAR");
662 names.append(KEYCODE_A, "KEYCODE_A");
663 names.append(KEYCODE_B, "KEYCODE_B");
664 names.append(KEYCODE_C, "KEYCODE_C");
665 names.append(KEYCODE_D, "KEYCODE_D");
666 names.append(KEYCODE_E, "KEYCODE_E");
667 names.append(KEYCODE_F, "KEYCODE_F");
668 names.append(KEYCODE_G, "KEYCODE_G");
669 names.append(KEYCODE_H, "KEYCODE_H");
670 names.append(KEYCODE_I, "KEYCODE_I");
671 names.append(KEYCODE_J, "KEYCODE_J");
672 names.append(KEYCODE_K, "KEYCODE_K");
673 names.append(KEYCODE_L, "KEYCODE_L");
674 names.append(KEYCODE_M, "KEYCODE_M");
675 names.append(KEYCODE_N, "KEYCODE_N");
676 names.append(KEYCODE_O, "KEYCODE_O");
677 names.append(KEYCODE_P, "KEYCODE_P");
678 names.append(KEYCODE_Q, "KEYCODE_Q");
679 names.append(KEYCODE_R, "KEYCODE_R");
680 names.append(KEYCODE_S, "KEYCODE_S");
681 names.append(KEYCODE_T, "KEYCODE_T");
682 names.append(KEYCODE_U, "KEYCODE_U");
683 names.append(KEYCODE_V, "KEYCODE_V");
684 names.append(KEYCODE_W, "KEYCODE_W");
685 names.append(KEYCODE_X, "KEYCODE_X");
686 names.append(KEYCODE_Y, "KEYCODE_Y");
687 names.append(KEYCODE_Z, "KEYCODE_Z");
688 names.append(KEYCODE_COMMA, "KEYCODE_COMMA");
689 names.append(KEYCODE_PERIOD, "KEYCODE_PERIOD");
690 names.append(KEYCODE_ALT_LEFT, "KEYCODE_ALT_LEFT");
691 names.append(KEYCODE_ALT_RIGHT, "KEYCODE_ALT_RIGHT");
692 names.append(KEYCODE_SHIFT_LEFT, "KEYCODE_SHIFT_LEFT");
693 names.append(KEYCODE_SHIFT_RIGHT, "KEYCODE_SHIFT_RIGHT");
694 names.append(KEYCODE_TAB, "KEYCODE_TAB");
695 names.append(KEYCODE_SPACE, "KEYCODE_SPACE");
696 names.append(KEYCODE_SYM, "KEYCODE_SYM");
697 names.append(KEYCODE_EXPLORER, "KEYCODE_EXPLORER");
698 names.append(KEYCODE_ENVELOPE, "KEYCODE_ENVELOPE");
699 names.append(KEYCODE_ENTER, "KEYCODE_ENTER");
700 names.append(KEYCODE_DEL, "KEYCODE_DEL");
701 names.append(KEYCODE_GRAVE, "KEYCODE_GRAVE");
702 names.append(KEYCODE_MINUS, "KEYCODE_MINUS");
703 names.append(KEYCODE_EQUALS, "KEYCODE_EQUALS");
704 names.append(KEYCODE_LEFT_BRACKET, "KEYCODE_LEFT_BRACKET");
705 names.append(KEYCODE_RIGHT_BRACKET, "KEYCODE_RIGHT_BRACKET");
706 names.append(KEYCODE_BACKSLASH, "KEYCODE_BACKSLASH");
707 names.append(KEYCODE_SEMICOLON, "KEYCODE_SEMICOLON");
708 names.append(KEYCODE_APOSTROPHE, "KEYCODE_APOSTROPHE");
709 names.append(KEYCODE_SLASH, "KEYCODE_SLASH");
710 names.append(KEYCODE_AT, "KEYCODE_AT");
711 names.append(KEYCODE_NUM, "KEYCODE_NUM");
712 names.append(KEYCODE_HEADSETHOOK, "KEYCODE_HEADSETHOOK");
713 names.append(KEYCODE_FOCUS, "KEYCODE_FOCUS");
714 names.append(KEYCODE_PLUS, "KEYCODE_PLUS");
715 names.append(KEYCODE_MENU, "KEYCODE_MENU");
716 names.append(KEYCODE_NOTIFICATION, "KEYCODE_NOTIFICATION");
717 names.append(KEYCODE_SEARCH, "KEYCODE_SEARCH");
718 names.append(KEYCODE_MEDIA_PLAY_PAUSE, "KEYCODE_MEDIA_PLAY_PAUSE");
719 names.append(KEYCODE_MEDIA_STOP, "KEYCODE_MEDIA_STOP");
720 names.append(KEYCODE_MEDIA_NEXT, "KEYCODE_MEDIA_NEXT");
721 names.append(KEYCODE_MEDIA_PREVIOUS, "KEYCODE_MEDIA_PREVIOUS");
722 names.append(KEYCODE_MEDIA_REWIND, "KEYCODE_MEDIA_REWIND");
723 names.append(KEYCODE_MEDIA_FAST_FORWARD, "KEYCODE_MEDIA_FAST_FORWARD");
724 names.append(KEYCODE_MUTE, "KEYCODE_MUTE");
725 names.append(KEYCODE_PAGE_UP, "KEYCODE_PAGE_UP");
726 names.append(KEYCODE_PAGE_DOWN, "KEYCODE_PAGE_DOWN");
727 names.append(KEYCODE_PICTSYMBOLS, "KEYCODE_PICTSYMBOLS");
728 names.append(KEYCODE_SWITCH_CHARSET, "KEYCODE_SWITCH_CHARSET");
729 names.append(KEYCODE_BUTTON_A, "KEYCODE_BUTTON_A");
730 names.append(KEYCODE_BUTTON_B, "KEYCODE_BUTTON_B");
731 names.append(KEYCODE_BUTTON_C, "KEYCODE_BUTTON_C");
732 names.append(KEYCODE_BUTTON_X, "KEYCODE_BUTTON_X");
733 names.append(KEYCODE_BUTTON_Y, "KEYCODE_BUTTON_Y");
734 names.append(KEYCODE_BUTTON_Z, "KEYCODE_BUTTON_Z");
735 names.append(KEYCODE_BUTTON_L1, "KEYCODE_BUTTON_L1");
736 names.append(KEYCODE_BUTTON_R1, "KEYCODE_BUTTON_R1");
737 names.append(KEYCODE_BUTTON_L2, "KEYCODE_BUTTON_L2");
738 names.append(KEYCODE_BUTTON_R2, "KEYCODE_BUTTON_R2");
739 names.append(KEYCODE_BUTTON_THUMBL, "KEYCODE_BUTTON_THUMBL");
740 names.append(KEYCODE_BUTTON_THUMBR, "KEYCODE_BUTTON_THUMBR");
741 names.append(KEYCODE_BUTTON_START, "KEYCODE_BUTTON_START");
742 names.append(KEYCODE_BUTTON_SELECT, "KEYCODE_BUTTON_SELECT");
743 names.append(KEYCODE_BUTTON_MODE, "KEYCODE_BUTTON_MODE");
744 names.append(KEYCODE_ESCAPE, "KEYCODE_ESCAPE");
745 names.append(KEYCODE_FORWARD_DEL, "KEYCODE_FORWARD_DEL");
746 names.append(KEYCODE_CTRL_LEFT, "KEYCODE_CTRL_LEFT");
747 names.append(KEYCODE_CTRL_RIGHT, "KEYCODE_CTRL_RIGHT");
748 names.append(KEYCODE_CAPS_LOCK, "KEYCODE_CAPS_LOCK");
749 names.append(KEYCODE_SCROLL_LOCK, "KEYCODE_SCROLL_LOCK");
750 names.append(KEYCODE_META_LEFT, "KEYCODE_META_LEFT");
751 names.append(KEYCODE_META_RIGHT, "KEYCODE_META_RIGHT");
752 names.append(KEYCODE_FUNCTION, "KEYCODE_FUNCTION");
753 names.append(KEYCODE_SYSRQ, "KEYCODE_SYSRQ");
754 names.append(KEYCODE_BREAK, "KEYCODE_BREAK");
755 names.append(KEYCODE_MOVE_HOME, "KEYCODE_MOVE_HOME");
756 names.append(KEYCODE_MOVE_END, "KEYCODE_MOVE_END");
757 names.append(KEYCODE_INSERT, "KEYCODE_INSERT");
758 names.append(KEYCODE_FORWARD, "KEYCODE_FORWARD");
759 names.append(KEYCODE_MEDIA_PLAY, "KEYCODE_MEDIA_PLAY");
760 names.append(KEYCODE_MEDIA_PAUSE, "KEYCODE_MEDIA_PAUSE");
761 names.append(KEYCODE_MEDIA_CLOSE, "KEYCODE_MEDIA_CLOSE");
762 names.append(KEYCODE_MEDIA_EJECT, "KEYCODE_MEDIA_EJECT");
763 names.append(KEYCODE_MEDIA_RECORD, "KEYCODE_MEDIA_RECORD");
764 names.append(KEYCODE_F1, "KEYCODE_F1");
765 names.append(KEYCODE_F2, "KEYCODE_F2");
766 names.append(KEYCODE_F3, "KEYCODE_F3");
767 names.append(KEYCODE_F4, "KEYCODE_F4");
768 names.append(KEYCODE_F5, "KEYCODE_F5");
769 names.append(KEYCODE_F6, "KEYCODE_F6");
770 names.append(KEYCODE_F7, "KEYCODE_F7");
771 names.append(KEYCODE_F8, "KEYCODE_F8");
772 names.append(KEYCODE_F9, "KEYCODE_F9");
773 names.append(KEYCODE_F10, "KEYCODE_F10");
774 names.append(KEYCODE_F11, "KEYCODE_F11");
775 names.append(KEYCODE_F12, "KEYCODE_F12");
776 names.append(KEYCODE_NUM_LOCK, "KEYCODE_NUM_LOCK");
777 names.append(KEYCODE_NUMPAD_0, "KEYCODE_NUMPAD_0");
778 names.append(KEYCODE_NUMPAD_1, "KEYCODE_NUMPAD_1");
779 names.append(KEYCODE_NUMPAD_2, "KEYCODE_NUMPAD_2");
780 names.append(KEYCODE_NUMPAD_3, "KEYCODE_NUMPAD_3");
781 names.append(KEYCODE_NUMPAD_4, "KEYCODE_NUMPAD_4");
782 names.append(KEYCODE_NUMPAD_5, "KEYCODE_NUMPAD_5");
783 names.append(KEYCODE_NUMPAD_6, "KEYCODE_NUMPAD_6");
784 names.append(KEYCODE_NUMPAD_7, "KEYCODE_NUMPAD_7");
785 names.append(KEYCODE_NUMPAD_8, "KEYCODE_NUMPAD_8");
786 names.append(KEYCODE_NUMPAD_9, "KEYCODE_NUMPAD_9");
787 names.append(KEYCODE_NUMPAD_DIVIDE, "KEYCODE_NUMPAD_DIVIDE");
788 names.append(KEYCODE_NUMPAD_MULTIPLY, "KEYCODE_NUMPAD_MULTIPLY");
789 names.append(KEYCODE_NUMPAD_SUBTRACT, "KEYCODE_NUMPAD_SUBTRACT");
790 names.append(KEYCODE_NUMPAD_ADD, "KEYCODE_NUMPAD_ADD");
791 names.append(KEYCODE_NUMPAD_DOT, "KEYCODE_NUMPAD_DOT");
792 names.append(KEYCODE_NUMPAD_COMMA, "KEYCODE_NUMPAD_COMMA");
793 names.append(KEYCODE_NUMPAD_ENTER, "KEYCODE_NUMPAD_ENTER");
794 names.append(KEYCODE_NUMPAD_EQUALS, "KEYCODE_NUMPAD_EQUALS");
795 names.append(KEYCODE_NUMPAD_LEFT_PAREN, "KEYCODE_NUMPAD_LEFT_PAREN");
796 names.append(KEYCODE_NUMPAD_RIGHT_PAREN, "KEYCODE_NUMPAD_RIGHT_PAREN");
797 names.append(KEYCODE_VOLUME_MUTE, "KEYCODE_VOLUME_MUTE");
798 names.append(KEYCODE_INFO, "KEYCODE_INFO");
799 names.append(KEYCODE_CHANNEL_UP, "KEYCODE_CHANNEL_UP");
800 names.append(KEYCODE_CHANNEL_DOWN, "KEYCODE_CHANNEL_DOWN");
801 names.append(KEYCODE_ZOOM_IN, "KEYCODE_ZOOM_IN");
802 names.append(KEYCODE_ZOOM_OUT, "KEYCODE_ZOOM_OUT");
803 names.append(KEYCODE_TV, "KEYCODE_TV");
804 names.append(KEYCODE_WINDOW, "KEYCODE_WINDOW");
805 names.append(KEYCODE_GUIDE, "KEYCODE_GUIDE");
806 names.append(KEYCODE_DVR, "KEYCODE_DVR");
807 names.append(KEYCODE_BOOKMARK, "KEYCODE_BOOKMARK");
808 names.append(KEYCODE_CAPTIONS, "KEYCODE_CAPTIONS");
809 names.append(KEYCODE_SETTINGS, "KEYCODE_SETTINGS");
810 names.append(KEYCODE_TV_POWER, "KEYCODE_TV_POWER");
811 names.append(KEYCODE_TV_INPUT, "KEYCODE_TV_INPUT");
812 names.append(KEYCODE_STB_INPUT, "KEYCODE_STB_INPUT");
813 names.append(KEYCODE_STB_POWER, "KEYCODE_STB_POWER");
814 names.append(KEYCODE_AVR_POWER, "KEYCODE_AVR_POWER");
815 names.append(KEYCODE_AVR_INPUT, "KEYCODE_AVR_INPUT");
816 names.append(KEYCODE_PROG_RED, "KEYCODE_PROG_RED");
817 names.append(KEYCODE_PROG_GREEN, "KEYCODE_PROG_GREEN");
818 names.append(KEYCODE_PROG_YELLOW, "KEYCODE_PROG_YELLOW");
819 names.append(KEYCODE_PROG_BLUE, "KEYCODE_PROG_BLUE");
820 names.append(KEYCODE_APP_SWITCH, "KEYCODE_APP_SWITCH");
821 names.append(KEYCODE_BUTTON_1, "KEYCODE_BUTTON_1");
822 names.append(KEYCODE_BUTTON_2, "KEYCODE_BUTTON_2");
823 names.append(KEYCODE_BUTTON_3, "KEYCODE_BUTTON_3");
824 names.append(KEYCODE_BUTTON_4, "KEYCODE_BUTTON_4");
825 names.append(KEYCODE_BUTTON_5, "KEYCODE_BUTTON_5");
826 names.append(KEYCODE_BUTTON_6, "KEYCODE_BUTTON_6");
827 names.append(KEYCODE_BUTTON_7, "KEYCODE_BUTTON_7");
828 names.append(KEYCODE_BUTTON_8, "KEYCODE_BUTTON_8");
829 names.append(KEYCODE_BUTTON_9, "KEYCODE_BUTTON_9");
830 names.append(KEYCODE_BUTTON_10, "KEYCODE_BUTTON_10");
831 names.append(KEYCODE_BUTTON_11, "KEYCODE_BUTTON_11");
832 names.append(KEYCODE_BUTTON_12, "KEYCODE_BUTTON_12");
833 names.append(KEYCODE_BUTTON_13, "KEYCODE_BUTTON_13");
834 names.append(KEYCODE_BUTTON_14, "KEYCODE_BUTTON_14");
835 names.append(KEYCODE_BUTTON_15, "KEYCODE_BUTTON_15");
836 names.append(KEYCODE_BUTTON_16, "KEYCODE_BUTTON_16");
Jeff Brown9812aed2011-03-07 17:09:51 -0800837 names.append(KEYCODE_LANGUAGE_SWITCH, "KEYCODE_LANGUAGE_SWITCH");
838 names.append(KEYCODE_MANNER_MODE, "KEYCODE_MANNER_MODE");
839 names.append(KEYCODE_3D_MODE, "KEYCODE_3D_MODE");
Jeff Brown6651a632011-11-28 12:59:11 -0800840 names.append(KEYCODE_CONTACTS, "KEYCODE_CONTACTS");
841 names.append(KEYCODE_CALENDAR, "KEYCODE_CALENDAR");
842 names.append(KEYCODE_MUSIC, "KEYCODE_MUSIC");
843 names.append(KEYCODE_CALCULATOR, "KEYCODE_CALCULATOR");
Yang Chuang7511f9c2012-02-10 15:18:26 +0800844 names.append(KEYCODE_ZENKAKU_HANKAKU, "KEYCODE_ZENKAKU_HANKAKU");
845 names.append(KEYCODE_EISU, "KEYCODE_EISU");
846 names.append(KEYCODE_MUHENKAN, "KEYCODE_MUHENKAN");
847 names.append(KEYCODE_HENKAN, "KEYCODE_HENKAN");
848 names.append(KEYCODE_KATAKANA_HIRAGANA, "KEYCODE_KATAKANA_HIRAGANA");
849 names.append(KEYCODE_YEN, "KEYCODE_YEN");
850 names.append(KEYCODE_RO, "KEYCODE_RO");
851 names.append(KEYCODE_KANA, "KEYCODE_KANA");
Jeff Brown497a92c2010-09-12 17:55:08 -0700852 };
853
854 // Symbolic names of all metakeys in bit order from least significant to most significant.
855 // Accordingly there are exactly 32 values in this table.
856 private static final String[] META_SYMBOLIC_NAMES = new String[] {
857 "META_SHIFT_ON",
858 "META_ALT_ON",
859 "META_SYM_ON",
860 "META_FUNCTION_ON",
861 "META_ALT_LEFT_ON",
862 "META_ALT_RIGHT_ON",
863 "META_SHIFT_LEFT_ON",
864 "META_SHIFT_RIGHT_ON",
865 "META_CAP_LOCKED",
866 "META_ALT_LOCKED",
867 "META_SYM_LOCKED",
868 "0x00000800",
869 "META_CTRL_ON",
870 "META_CTRL_LEFT_ON",
871 "META_CTRL_RIGHT_ON",
872 "0x00008000",
873 "META_META_ON",
874 "META_META_LEFT_ON",
875 "META_META_RIGHT_ON",
876 "0x00080000",
Jeff Brown51e7fe72010-10-29 22:19:53 -0700877 "META_CAPS_LOCK_ON",
878 "META_NUM_LOCK_ON",
879 "META_SCROLL_LOCK_ON",
Jeff Brown497a92c2010-09-12 17:55:08 -0700880 "0x00800000",
881 "0x01000000",
882 "0x02000000",
883 "0x04000000",
884 "0x08000000",
885 "0x10000000",
886 "0x20000000",
887 "0x40000000",
888 "0x80000000",
889 };
890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 /**
892 * @deprecated There are now more than MAX_KEYCODE keycodes.
893 * Use {@link #getMaxKeyCode()} instead.
894 */
895 @Deprecated
896 public static final int MAX_KEYCODE = 84;
897
898 /**
899 * {@link #getAction} value: the key has been pressed down.
900 */
901 public static final int ACTION_DOWN = 0;
902 /**
903 * {@link #getAction} value: the key has been released.
904 */
905 public static final int ACTION_UP = 1;
906 /**
907 * {@link #getAction} value: multiple duplicate key events have
908 * occurred in a row, or a complex string is being delivered. If the
909 * key code is not {#link {@link #KEYCODE_UNKNOWN} then the
910 * {#link {@link #getRepeatCount()} method returns the number of times
911 * the given key code should be executed.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700912 * Otherwise, if the key code is {@link #KEYCODE_UNKNOWN}, then
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 * this is a sequence of characters as returned by {@link #getCharacters}.
914 */
915 public static final int ACTION_MULTIPLE = 2;
916
917 /**
Jeff Brown497a92c2010-09-12 17:55:08 -0700918 * SHIFT key locked in CAPS mode.
919 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
920 * @hide
921 */
922 public static final int META_CAP_LOCKED = 0x100;
923
924 /**
925 * ALT key locked.
926 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
927 * @hide
928 */
929 public static final int META_ALT_LOCKED = 0x200;
930
931 /**
932 * SYM key locked.
933 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
934 * @hide
935 */
936 public static final int META_SYM_LOCKED = 0x400;
937
938 /**
939 * Text is in selection mode.
940 * Reserved for use by {@link MetaKeyKeyListener} for a private unpublished constant
941 * in its API that is currently being retained for legacy reasons.
942 * @hide
943 */
944 public static final int META_SELECTING = 0x800;
945
946 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 * <p>This mask is used to check whether one of the ALT meta keys is pressed.</p>
948 *
949 * @see #isAltPressed()
950 * @see #getMetaState()
951 * @see #KEYCODE_ALT_LEFT
952 * @see #KEYCODE_ALT_RIGHT
953 */
954 public static final int META_ALT_ON = 0x02;
955
956 /**
957 * <p>This mask is used to check whether the left ALT meta key is pressed.</p>
958 *
959 * @see #isAltPressed()
960 * @see #getMetaState()
961 * @see #KEYCODE_ALT_LEFT
962 */
963 public static final int META_ALT_LEFT_ON = 0x10;
964
965 /**
966 * <p>This mask is used to check whether the right the ALT meta key is pressed.</p>
967 *
968 * @see #isAltPressed()
969 * @see #getMetaState()
970 * @see #KEYCODE_ALT_RIGHT
971 */
972 public static final int META_ALT_RIGHT_ON = 0x20;
973
974 /**
975 * <p>This mask is used to check whether one of the SHIFT meta keys is pressed.</p>
976 *
977 * @see #isShiftPressed()
978 * @see #getMetaState()
979 * @see #KEYCODE_SHIFT_LEFT
980 * @see #KEYCODE_SHIFT_RIGHT
981 */
982 public static final int META_SHIFT_ON = 0x1;
983
984 /**
985 * <p>This mask is used to check whether the left SHIFT meta key is pressed.</p>
986 *
987 * @see #isShiftPressed()
988 * @see #getMetaState()
989 * @see #KEYCODE_SHIFT_LEFT
990 */
991 public static final int META_SHIFT_LEFT_ON = 0x40;
992
993 /**
994 * <p>This mask is used to check whether the right SHIFT meta key is pressed.</p>
995 *
996 * @see #isShiftPressed()
997 * @see #getMetaState()
998 * @see #KEYCODE_SHIFT_RIGHT
999 */
1000 public static final int META_SHIFT_RIGHT_ON = 0x80;
1001
1002 /**
1003 * <p>This mask is used to check whether the SYM meta key is pressed.</p>
1004 *
1005 * @see #isSymPressed()
1006 * @see #getMetaState()
1007 */
1008 public static final int META_SYM_ON = 0x4;
1009
1010 /**
Jeff Brown497a92c2010-09-12 17:55:08 -07001011 * <p>This mask is used to check whether the FUNCTION meta key is pressed.</p>
1012 *
1013 * @see #isFunctionPressed()
1014 * @see #getMetaState()
1015 */
1016 public static final int META_FUNCTION_ON = 0x8;
1017
1018 /**
1019 * <p>This mask is used to check whether one of the CTRL meta keys is pressed.</p>
1020 *
1021 * @see #isCtrlPressed()
1022 * @see #getMetaState()
1023 * @see #KEYCODE_CTRL_LEFT
1024 * @see #KEYCODE_CTRL_RIGHT
1025 */
1026 public static final int META_CTRL_ON = 0x1000;
1027
1028 /**
1029 * <p>This mask is used to check whether the left CTRL meta key is pressed.</p>
1030 *
1031 * @see #isCtrlPressed()
1032 * @see #getMetaState()
1033 * @see #KEYCODE_CTRL_LEFT
1034 */
1035 public static final int META_CTRL_LEFT_ON = 0x2000;
1036
1037 /**
1038 * <p>This mask is used to check whether the right CTRL meta key is pressed.</p>
1039 *
1040 * @see #isCtrlPressed()
1041 * @see #getMetaState()
1042 * @see #KEYCODE_CTRL_RIGHT
1043 */
1044 public static final int META_CTRL_RIGHT_ON = 0x4000;
1045
1046 /**
1047 * <p>This mask is used to check whether one of the META meta keys is pressed.</p>
1048 *
1049 * @see #isMetaPressed()
1050 * @see #getMetaState()
1051 * @see #KEYCODE_META_LEFT
1052 * @see #KEYCODE_META_RIGHT
1053 */
1054 public static final int META_META_ON = 0x10000;
1055
1056 /**
1057 * <p>This mask is used to check whether the left META meta key is pressed.</p>
1058 *
1059 * @see #isMetaPressed()
1060 * @see #getMetaState()
1061 * @see #KEYCODE_META_LEFT
1062 */
1063 public static final int META_META_LEFT_ON = 0x20000;
1064
1065 /**
1066 * <p>This mask is used to check whether the right META meta key is pressed.</p>
1067 *
1068 * @see #isMetaPressed()
1069 * @see #getMetaState()
1070 * @see #KEYCODE_META_RIGHT
1071 */
1072 public static final int META_META_RIGHT_ON = 0x40000;
1073
1074 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07001075 * <p>This mask is used to check whether the CAPS LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07001076 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07001077 * @see #isCapsLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -07001078 * @see #getMetaState()
1079 * @see #KEYCODE_CAPS_LOCK
1080 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07001081 public static final int META_CAPS_LOCK_ON = 0x100000;
Jeff Brown497a92c2010-09-12 17:55:08 -07001082
1083 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07001084 * <p>This mask is used to check whether the NUM LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07001085 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07001086 * @see #isNumLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -07001087 * @see #getMetaState()
1088 * @see #KEYCODE_NUM_LOCK
1089 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07001090 public static final int META_NUM_LOCK_ON = 0x200000;
Jeff Brown497a92c2010-09-12 17:55:08 -07001091
1092 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07001093 * <p>This mask is used to check whether the SCROLL LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07001094 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07001095 * @see #isScrollLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -07001096 * @see #getMetaState()
1097 * @see #KEYCODE_SCROLL_LOCK
1098 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07001099 public static final int META_SCROLL_LOCK_ON = 0x400000;
Jeff Brown497a92c2010-09-12 17:55:08 -07001100
Jeff Brown64da12a2011-01-04 19:57:47 -08001101 /**
1102 * This mask is a combination of {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON}
1103 * and {@link #META_SHIFT_RIGHT_ON}.
1104 */
Jeff Brownc1df9072010-12-21 16:38:50 -08001105 public static final int META_SHIFT_MASK = META_SHIFT_ON
1106 | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON;
1107
Jeff Brown64da12a2011-01-04 19:57:47 -08001108 /**
1109 * This mask is a combination of {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON}
1110 * and {@link #META_ALT_RIGHT_ON}.
1111 */
Jeff Brownc1df9072010-12-21 16:38:50 -08001112 public static final int META_ALT_MASK = META_ALT_ON
1113 | META_ALT_LEFT_ON | META_ALT_RIGHT_ON;
1114
Jeff Brown64da12a2011-01-04 19:57:47 -08001115 /**
1116 * This mask is a combination of {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON}
1117 * and {@link #META_CTRL_RIGHT_ON}.
1118 */
Jeff Brownc1df9072010-12-21 16:38:50 -08001119 public static final int META_CTRL_MASK = META_CTRL_ON
1120 | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON;
1121
Jeff Brown64da12a2011-01-04 19:57:47 -08001122 /**
1123 * This mask is a combination of {@link #META_META_ON}, {@link #META_META_LEFT_ON}
1124 * and {@link #META_META_RIGHT_ON}.
1125 */
1126 public static final int META_META_MASK = META_META_ON
Jeff Brownc1df9072010-12-21 16:38:50 -08001127 | META_META_LEFT_ON | META_META_RIGHT_ON;
1128
Jeff Brown497a92c2010-09-12 17:55:08 -07001129 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 * This mask is set if the device woke because of this key event.
1131 */
1132 public static final int FLAG_WOKE_HERE = 0x1;
1133
1134 /**
1135 * This mask is set if the key event was generated by a software keyboard.
1136 */
1137 public static final int FLAG_SOFT_KEYBOARD = 0x2;
1138
1139 /**
1140 * This mask is set if we don't want the key event to cause us to leave
1141 * touch mode.
1142 */
1143 public static final int FLAG_KEEP_TOUCH_MODE = 0x4;
1144
1145 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001146 * This mask is set if an event was known to come from a trusted part
1147 * of the system. That is, the event is known to come from the user,
1148 * and could not have been spoofed by a third party component.
1149 */
1150 public static final int FLAG_FROM_SYSTEM = 0x8;
1151
1152 /**
1153 * This mask is used for compatibility, to identify enter keys that are
1154 * coming from an IME whose enter key has been auto-labelled "next" or
1155 * "done". This allows TextView to dispatch these as normal enter keys
1156 * for old applications, but still do the appropriate action when
1157 * receiving them.
1158 */
1159 public static final int FLAG_EDITOR_ACTION = 0x10;
1160
1161 /**
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07001162 * When associated with up key events, this indicates that the key press
1163 * has been canceled. Typically this is used with virtual touch screen
1164 * keys, where the user can slide from the virtual key area on to the
1165 * display: in that case, the application will receive a canceled up
1166 * event and should not perform the action normally associated with the
1167 * key. Note that for this to work, the application can not perform an
1168 * action for a key until it receives an up or the long press timeout has
1169 * expired.
1170 */
1171 public static final int FLAG_CANCELED = 0x20;
1172
1173 /**
1174 * This key event was generated by a virtual (on-screen) hard key area.
1175 * Typically this is an area of the touchscreen, outside of the regular
1176 * display, dedicated to "hardware" buttons.
1177 */
1178 public static final int FLAG_VIRTUAL_HARD_KEY = 0x40;
1179
1180 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001181 * This flag is set for the first key repeat that occurs after the
1182 * long press timeout.
1183 */
1184 public static final int FLAG_LONG_PRESS = 0x80;
1185
1186 /**
1187 * Set when a key event has {@link #FLAG_CANCELED} set because a long
1188 * press action was executed while it was down.
1189 */
1190 public static final int FLAG_CANCELED_LONG_PRESS = 0x100;
1191
1192 /**
1193 * Set for {@link #ACTION_UP} when this event's key code is still being
1194 * tracked from its initial down. That is, somebody requested that tracking
1195 * started on the key down and a long press has not caused
1196 * the tracking to be canceled.
1197 */
1198 public static final int FLAG_TRACKING = 0x200;
Jeff Brown49ed71d2010-12-06 17:13:33 -08001199
1200 /**
1201 * Set when a key event has been synthesized to implement default behavior
1202 * for an event that the application did not handle.
1203 * Fallback key events are generated by unhandled trackball motions
1204 * (to emulate a directional keypad) and by certain unhandled key presses
1205 * that are declared in the key map (such as special function numeric keypad
1206 * keys when numlock is off).
1207 */
1208 public static final int FLAG_FALLBACK = 0x400;
1209
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001210 /**
1211 * Private control to determine when an app is tracking a key sequence.
1212 * @hide
1213 */
1214 public static final int FLAG_START_TRACKING = 0x40000000;
Jeff Brown21bc5c92011-02-28 18:27:14 -08001215
1216 /**
1217 * Private flag that indicates when the system has detected that this key event
1218 * may be inconsistent with respect to the sequence of previously delivered key events,
1219 * such as when a key up event is sent but the key was not down.
1220 *
1221 * @hide
1222 * @see #isTainted
1223 * @see #setTainted
1224 */
1225 public static final int FLAG_TAINTED = 0x80000000;
1226
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001227 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 * Returns the maximum keycode.
1229 */
1230 public static int getMaxKeyCode() {
1231 return LAST_KEYCODE;
1232 }
1233
1234 /**
1235 * Get the character that is produced by putting accent on the character
1236 * c.
1237 * For example, getDeadChar('`', 'e') returns &egrave;.
1238 */
1239 public static int getDeadChar(int accent, int c) {
1240 return KeyCharacterMap.getDeadChar(accent, c);
1241 }
1242
Dianne Hackborn8d374262009-09-14 21:21:52 -07001243 static final boolean DEBUG = false;
1244 static final String TAG = "KeyEvent";
Jeff Brown1f245102010-11-18 20:53:46 -08001245
1246 private static final int MAX_RECYCLED = 10;
1247 private static final Object gRecyclerLock = new Object();
1248 private static int gRecyclerUsed;
1249 private static KeyEvent gRecyclerTop;
1250
1251 private KeyEvent mNext;
Jeff Brown1f245102010-11-18 20:53:46 -08001252
Jeff Brown91c69ab2011-02-14 17:03:18 -08001253 private int mDeviceId;
1254 private int mSource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 private int mMetaState;
1256 private int mAction;
1257 private int mKeyCode;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001258 private int mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 private int mRepeatCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 private int mFlags;
1261 private long mDownTime;
1262 private long mEventTime;
1263 private String mCharacters;
1264
1265 public interface Callback {
1266 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001267 * Called when a key down event has occurred. If you return true,
1268 * you can first call {@link KeyEvent#startTracking()
1269 * KeyEvent.startTracking()} to have the framework track the event
1270 * through its {@link #onKeyUp(int, KeyEvent)} and also call your
1271 * {@link #onKeyLongPress(int, KeyEvent)} if it occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 *
1273 * @param keyCode The value in event.getKeyCode().
1274 * @param event Description of the key event.
1275 *
1276 * @return If you handled the event, return true. If you want to allow
1277 * the event to be handled by the next receiver, return false.
1278 */
1279 boolean onKeyDown(int keyCode, KeyEvent event);
1280
1281 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001282 * Called when a long press has occurred. If you return true,
1283 * the final key up will have {@link KeyEvent#FLAG_CANCELED} and
1284 * {@link KeyEvent#FLAG_CANCELED_LONG_PRESS} set. Note that in
1285 * order to receive this callback, someone in the event change
1286 * <em>must</em> return true from {@link #onKeyDown} <em>and</em>
1287 * call {@link KeyEvent#startTracking()} on the event.
1288 *
1289 * @param keyCode The value in event.getKeyCode().
1290 * @param event Description of the key event.
1291 *
1292 * @return If you handled the event, return true. If you want to allow
1293 * the event to be handled by the next receiver, return false.
1294 */
1295 boolean onKeyLongPress(int keyCode, KeyEvent event);
1296
1297 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 * Called when a key up event has occurred.
1299 *
1300 * @param keyCode The value in event.getKeyCode().
1301 * @param event Description of the key event.
1302 *
1303 * @return If you handled the event, return true. If you want to allow
1304 * the event to be handled by the next receiver, return false.
1305 */
1306 boolean onKeyUp(int keyCode, KeyEvent event);
1307
1308 /**
1309 * Called when multiple down/up pairs of the same key have occurred
1310 * in a row.
1311 *
1312 * @param keyCode The value in event.getKeyCode().
1313 * @param count Number of pairs as returned by event.getRepeatCount().
1314 * @param event Description of the key event.
1315 *
1316 * @return If you handled the event, return true. If you want to allow
1317 * the event to be handled by the next receiver, return false.
1318 */
1319 boolean onKeyMultiple(int keyCode, int count, KeyEvent event);
1320 }
1321
Jeff Brown497a92c2010-09-12 17:55:08 -07001322 static {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001323 populateKeycodeSymbolicNames();
Jeff Brown497a92c2010-09-12 17:55:08 -07001324 }
1325
Jeff Brown1f245102010-11-18 20:53:46 -08001326 private KeyEvent() {
1327 }
1328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 /**
1330 * Create a new key event.
1331 *
1332 * @param action Action code: either {@link #ACTION_DOWN},
1333 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1334 * @param code The key code.
1335 */
1336 public KeyEvent(int action, int code) {
1337 mAction = action;
1338 mKeyCode = code;
1339 mRepeatCount = 0;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001340 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 }
1342
1343 /**
1344 * Create a new key event.
1345 *
1346 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1347 * at which this key code originally went down.
1348 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1349 * at which this event happened.
1350 * @param action Action code: either {@link #ACTION_DOWN},
1351 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1352 * @param code The key code.
1353 * @param repeat A repeat count for down events (> 0 if this is after the
1354 * initial down) or event count for multiple events.
1355 */
1356 public KeyEvent(long downTime, long eventTime, int action,
1357 int code, int repeat) {
1358 mDownTime = downTime;
1359 mEventTime = eventTime;
1360 mAction = action;
1361 mKeyCode = code;
1362 mRepeatCount = repeat;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001363 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 }
1365
1366 /**
1367 * Create a new key event.
1368 *
1369 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1370 * at which this key code originally went down.
1371 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1372 * at which this event happened.
1373 * @param action Action code: either {@link #ACTION_DOWN},
1374 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1375 * @param code The key code.
1376 * @param repeat A repeat count for down events (> 0 if this is after the
1377 * initial down) or event count for multiple events.
1378 * @param metaState Flags indicating which meta keys are currently pressed.
1379 */
1380 public KeyEvent(long downTime, long eventTime, int action,
1381 int code, int repeat, int metaState) {
1382 mDownTime = downTime;
1383 mEventTime = eventTime;
1384 mAction = action;
1385 mKeyCode = code;
1386 mRepeatCount = repeat;
1387 mMetaState = metaState;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001388 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 }
1390
1391 /**
1392 * Create a new key event.
1393 *
1394 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1395 * at which this key code originally went down.
1396 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1397 * at which this event happened.
1398 * @param action Action code: either {@link #ACTION_DOWN},
1399 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1400 * @param code The key code.
1401 * @param repeat A repeat count for down events (> 0 if this is after the
1402 * initial down) or event count for multiple events.
1403 * @param metaState Flags indicating which meta keys are currently pressed.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001404 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 * @param scancode Raw device scan code of the event.
1406 */
1407 public KeyEvent(long downTime, long eventTime, int action,
1408 int code, int repeat, int metaState,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001409 int deviceId, int scancode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 mDownTime = downTime;
1411 mEventTime = eventTime;
1412 mAction = action;
1413 mKeyCode = code;
1414 mRepeatCount = repeat;
1415 mMetaState = metaState;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001416 mDeviceId = deviceId;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001417 mScanCode = scancode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 }
1419
1420 /**
1421 * Create a new key event.
1422 *
1423 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1424 * at which this key code originally went down.
1425 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1426 * at which this event happened.
1427 * @param action Action code: either {@link #ACTION_DOWN},
1428 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1429 * @param code The key code.
1430 * @param repeat A repeat count for down events (> 0 if this is after the
1431 * initial down) or event count for multiple events.
1432 * @param metaState Flags indicating which meta keys are currently pressed.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001433 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 * @param scancode Raw device scan code of the event.
1435 * @param flags The flags for this key event
1436 */
1437 public KeyEvent(long downTime, long eventTime, int action,
1438 int code, int repeat, int metaState,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001439 int deviceId, int scancode, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 mDownTime = downTime;
1441 mEventTime = eventTime;
1442 mAction = action;
1443 mKeyCode = code;
1444 mRepeatCount = repeat;
1445 mMetaState = metaState;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001446 mDeviceId = deviceId;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001447 mScanCode = scancode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 mFlags = flags;
1449 }
1450
1451 /**
Jeff Brownc5ed5912010-07-14 18:48:53 -07001452 * Create a new key event.
1453 *
1454 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1455 * at which this key code originally went down.
1456 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1457 * at which this event happened.
1458 * @param action Action code: either {@link #ACTION_DOWN},
1459 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1460 * @param code The key code.
1461 * @param repeat A repeat count for down events (> 0 if this is after the
1462 * initial down) or event count for multiple events.
1463 * @param metaState Flags indicating which meta keys are currently pressed.
1464 * @param deviceId The device ID that generated the key event.
1465 * @param scancode Raw device scan code of the event.
1466 * @param flags The flags for this key event
1467 * @param source The input source such as {@link InputDevice#SOURCE_KEYBOARD}.
1468 */
1469 public KeyEvent(long downTime, long eventTime, int action,
1470 int code, int repeat, int metaState,
1471 int deviceId, int scancode, int flags, int source) {
1472 mDownTime = downTime;
1473 mEventTime = eventTime;
1474 mAction = action;
1475 mKeyCode = code;
1476 mRepeatCount = repeat;
1477 mMetaState = metaState;
1478 mDeviceId = deviceId;
1479 mScanCode = scancode;
1480 mFlags = flags;
1481 mSource = source;
1482 }
1483
1484 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 * Create a new key event for a string of characters. The key code,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001486 * action, repeat count and source will automatically be set to
1487 * {@link #KEYCODE_UNKNOWN}, {@link #ACTION_MULTIPLE}, 0, and
1488 * {@link InputDevice#SOURCE_KEYBOARD} for you.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 *
1490 * @param time The time (in {@link android.os.SystemClock#uptimeMillis})
1491 * at which this event occured.
1492 * @param characters The string of characters.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001493 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494 * @param flags The flags for this key event
1495 */
Jeff Brownc5ed5912010-07-14 18:48:53 -07001496 public KeyEvent(long time, String characters, int deviceId, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 mDownTime = time;
1498 mEventTime = time;
1499 mCharacters = characters;
1500 mAction = ACTION_MULTIPLE;
1501 mKeyCode = KEYCODE_UNKNOWN;
1502 mRepeatCount = 0;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001503 mDeviceId = deviceId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 mFlags = flags;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001505 mSource = InputDevice.SOURCE_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 }
1507
1508 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001509 * Make an exact copy of an existing key event.
1510 */
1511 public KeyEvent(KeyEvent origEvent) {
1512 mDownTime = origEvent.mDownTime;
1513 mEventTime = origEvent.mEventTime;
1514 mAction = origEvent.mAction;
1515 mKeyCode = origEvent.mKeyCode;
1516 mRepeatCount = origEvent.mRepeatCount;
1517 mMetaState = origEvent.mMetaState;
1518 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001519 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001520 mScanCode = origEvent.mScanCode;
The Android Open Source Project10592532009-03-18 17:39:46 -07001521 mFlags = origEvent.mFlags;
1522 mCharacters = origEvent.mCharacters;
1523 }
1524
1525 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 * Copy an existing key event, modifying its time and repeat count.
1527 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001528 * @deprecated Use {@link #changeTimeRepeat(KeyEvent, long, int)}
1529 * instead.
1530 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 * @param origEvent The existing event to be copied.
1532 * @param eventTime The new event time
1533 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1534 * @param newRepeat The new repeat count of the event.
1535 */
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001536 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 public KeyEvent(KeyEvent origEvent, long eventTime, int newRepeat) {
1538 mDownTime = origEvent.mDownTime;
1539 mEventTime = eventTime;
1540 mAction = origEvent.mAction;
1541 mKeyCode = origEvent.mKeyCode;
1542 mRepeatCount = newRepeat;
1543 mMetaState = origEvent.mMetaState;
1544 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001545 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001546 mScanCode = origEvent.mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 mFlags = origEvent.mFlags;
1548 mCharacters = origEvent.mCharacters;
1549 }
1550
Jeff Brown1f245102010-11-18 20:53:46 -08001551 private static KeyEvent obtain() {
1552 final KeyEvent ev;
1553 synchronized (gRecyclerLock) {
1554 ev = gRecyclerTop;
1555 if (ev == null) {
1556 return new KeyEvent();
1557 }
1558 gRecyclerTop = ev.mNext;
1559 gRecyclerUsed -= 1;
1560 }
Jeff Brown1f245102010-11-18 20:53:46 -08001561 ev.mNext = null;
Jeff Brown32cbc38552011-12-01 14:01:49 -08001562 ev.prepareForReuse();
Jeff Brown1f245102010-11-18 20:53:46 -08001563 return ev;
1564 }
1565
1566 /**
1567 * Obtains a (potentially recycled) key event.
1568 *
1569 * @hide
1570 */
1571 public static KeyEvent obtain(long downTime, long eventTime, int action,
1572 int code, int repeat, int metaState,
1573 int deviceId, int scancode, int flags, int source, String characters) {
1574 KeyEvent ev = obtain();
1575 ev.mDownTime = downTime;
1576 ev.mEventTime = eventTime;
1577 ev.mAction = action;
1578 ev.mKeyCode = code;
1579 ev.mRepeatCount = repeat;
1580 ev.mMetaState = metaState;
1581 ev.mDeviceId = deviceId;
1582 ev.mScanCode = scancode;
1583 ev.mFlags = flags;
1584 ev.mSource = source;
1585 ev.mCharacters = characters;
1586 return ev;
1587 }
1588
1589 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08001590 * Obtains a (potentially recycled) copy of another key event.
1591 *
1592 * @hide
1593 */
1594 public static KeyEvent obtain(KeyEvent other) {
1595 KeyEvent ev = obtain();
1596 ev.mDownTime = other.mDownTime;
1597 ev.mEventTime = other.mEventTime;
1598 ev.mAction = other.mAction;
1599 ev.mKeyCode = other.mKeyCode;
1600 ev.mRepeatCount = other.mRepeatCount;
1601 ev.mMetaState = other.mMetaState;
1602 ev.mDeviceId = other.mDeviceId;
1603 ev.mScanCode = other.mScanCode;
1604 ev.mFlags = other.mFlags;
1605 ev.mSource = other.mSource;
1606 ev.mCharacters = other.mCharacters;
1607 return ev;
1608 }
1609
1610 /** @hide */
1611 @Override
1612 public KeyEvent copy() {
1613 return obtain(this);
1614 }
1615
1616 /**
Jeff Brown1f245102010-11-18 20:53:46 -08001617 * Recycles a key event.
1618 * Key events should only be recycled if they are owned by the system since user
1619 * code expects them to be essentially immutable, "tracking" notwithstanding.
1620 *
1621 * @hide
1622 */
Jeff Brown92cc2d82011-12-02 01:19:47 -08001623 @Override
Jeff Brown1f245102010-11-18 20:53:46 -08001624 public final void recycle() {
Jeff Brown32cbc38552011-12-01 14:01:49 -08001625 super.recycle();
Jeff Brown1f245102010-11-18 20:53:46 -08001626 mCharacters = null;
1627
1628 synchronized (gRecyclerLock) {
1629 if (gRecyclerUsed < MAX_RECYCLED) {
1630 gRecyclerUsed++;
1631 mNext = gRecyclerTop;
1632 gRecyclerTop = this;
1633 }
1634 }
1635 }
1636
Jeff Brown92cc2d82011-12-02 01:19:47 -08001637 /** @hide */
1638 @Override
1639 public final void recycleIfNeededAfterDispatch() {
1640 // Do nothing.
1641 }
1642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001644 * Create a new key event that is the same as the given one, but whose
1645 * event time and repeat count are replaced with the given value.
1646 *
1647 * @param event The existing event to be copied. This is not modified.
1648 * @param eventTime The new event time
1649 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1650 * @param newRepeat The new repeat count of the event.
1651 */
1652 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime,
1653 int newRepeat) {
1654 return new KeyEvent(event, eventTime, newRepeat);
1655 }
1656
1657 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001658 * Create a new key event that is the same as the given one, but whose
1659 * event time and repeat count are replaced with the given value.
1660 *
1661 * @param event The existing event to be copied. This is not modified.
1662 * @param eventTime The new event time
1663 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1664 * @param newRepeat The new repeat count of the event.
1665 * @param newFlags New flags for the event, replacing the entire value
1666 * in the original event.
1667 */
1668 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime,
1669 int newRepeat, int newFlags) {
1670 KeyEvent ret = new KeyEvent(event);
1671 ret.mEventTime = eventTime;
1672 ret.mRepeatCount = newRepeat;
1673 ret.mFlags = newFlags;
1674 return ret;
1675 }
1676
1677 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 * Copy an existing key event, modifying its action.
1679 *
1680 * @param origEvent The existing event to be copied.
1681 * @param action The new action code of the event.
1682 */
The Android Open Source Project10592532009-03-18 17:39:46 -07001683 private KeyEvent(KeyEvent origEvent, int action) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 mDownTime = origEvent.mDownTime;
1685 mEventTime = origEvent.mEventTime;
1686 mAction = action;
1687 mKeyCode = origEvent.mKeyCode;
1688 mRepeatCount = origEvent.mRepeatCount;
1689 mMetaState = origEvent.mMetaState;
1690 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001691 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001692 mScanCode = origEvent.mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 mFlags = origEvent.mFlags;
1694 // Don't copy mCharacters, since one way or the other we'll lose it
1695 // when changing the action.
1696 }
1697
1698 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001699 * Create a new key event that is the same as the given one, but whose
1700 * action is replaced with the given value.
1701 *
1702 * @param event The existing event to be copied. This is not modified.
1703 * @param action The new action code of the event.
1704 */
1705 public static KeyEvent changeAction(KeyEvent event, int action) {
1706 return new KeyEvent(event, action);
1707 }
1708
1709 /**
1710 * Create a new key event that is the same as the given one, but whose
1711 * flags are replaced with the given value.
1712 *
1713 * @param event The existing event to be copied. This is not modified.
1714 * @param flags The new flags constant.
1715 */
1716 public static KeyEvent changeFlags(KeyEvent event, int flags) {
1717 event = new KeyEvent(event);
1718 event.mFlags = flags;
1719 return event;
1720 }
Jeff Brown21bc5c92011-02-28 18:27:14 -08001721
1722 /** @hide */
1723 @Override
1724 public final boolean isTainted() {
1725 return (mFlags & FLAG_TAINTED) != 0;
1726 }
1727
1728 /** @hide */
1729 @Override
1730 public final void setTainted(boolean tainted) {
1731 mFlags = tainted ? mFlags | FLAG_TAINTED : mFlags & ~FLAG_TAINTED;
1732 }
1733
The Android Open Source Project10592532009-03-18 17:39:46 -07001734 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 * Don't use in new code, instead explicitly check
1736 * {@link #getAction()}.
1737 *
1738 * @return If the action is ACTION_DOWN, returns true; else false.
1739 *
1740 * @deprecated
1741 * @hide
1742 */
1743 @Deprecated public final boolean isDown() {
1744 return mAction == ACTION_DOWN;
1745 }
1746
1747 /**
1748 * Is this a system key? System keys can not be used for menu shortcuts.
1749 *
1750 * TODO: this information should come from a table somewhere.
1751 * TODO: should the dpad keys be here? arguably, because they also shouldn't be menu shortcuts
1752 */
1753 public final boolean isSystem() {
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -07001754 return native_isSystemKey(mKeyCode);
1755 }
1756
1757 /** @hide */
1758 public final boolean hasDefaultAction() {
1759 return native_hasDefaultAction(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 }
1761
Jeff Brown6f2fba42011-02-19 01:08:02 -08001762 /**
1763 * Returns true if the specified keycode is a gamepad button.
1764 * @return True if the keycode is a gamepad button, such as {@link #KEYCODE_BUTTON_A}.
1765 */
1766 public static final boolean isGamepadButton(int keyCode) {
1767 switch (keyCode) {
1768 case KeyEvent.KEYCODE_BUTTON_A:
1769 case KeyEvent.KEYCODE_BUTTON_B:
1770 case KeyEvent.KEYCODE_BUTTON_C:
1771 case KeyEvent.KEYCODE_BUTTON_X:
1772 case KeyEvent.KEYCODE_BUTTON_Y:
1773 case KeyEvent.KEYCODE_BUTTON_Z:
1774 case KeyEvent.KEYCODE_BUTTON_L1:
1775 case KeyEvent.KEYCODE_BUTTON_R1:
1776 case KeyEvent.KEYCODE_BUTTON_L2:
1777 case KeyEvent.KEYCODE_BUTTON_R2:
1778 case KeyEvent.KEYCODE_BUTTON_THUMBL:
1779 case KeyEvent.KEYCODE_BUTTON_THUMBR:
1780 case KeyEvent.KEYCODE_BUTTON_START:
1781 case KeyEvent.KEYCODE_BUTTON_SELECT:
1782 case KeyEvent.KEYCODE_BUTTON_MODE:
1783 case KeyEvent.KEYCODE_BUTTON_1:
1784 case KeyEvent.KEYCODE_BUTTON_2:
1785 case KeyEvent.KEYCODE_BUTTON_3:
1786 case KeyEvent.KEYCODE_BUTTON_4:
1787 case KeyEvent.KEYCODE_BUTTON_5:
1788 case KeyEvent.KEYCODE_BUTTON_6:
1789 case KeyEvent.KEYCODE_BUTTON_7:
1790 case KeyEvent.KEYCODE_BUTTON_8:
1791 case KeyEvent.KEYCODE_BUTTON_9:
1792 case KeyEvent.KEYCODE_BUTTON_10:
1793 case KeyEvent.KEYCODE_BUTTON_11:
1794 case KeyEvent.KEYCODE_BUTTON_12:
1795 case KeyEvent.KEYCODE_BUTTON_13:
1796 case KeyEvent.KEYCODE_BUTTON_14:
1797 case KeyEvent.KEYCODE_BUTTON_15:
1798 case KeyEvent.KEYCODE_BUTTON_16:
1799 return true;
1800 default:
1801 return false;
1802 }
1803 }
1804
Jeff Brown91c69ab2011-02-14 17:03:18 -08001805 /** {@inheritDoc} */
1806 @Override
1807 public final int getDeviceId() {
1808 return mDeviceId;
1809 }
1810
1811 /** {@inheritDoc} */
1812 @Override
1813 public final int getSource() {
1814 return mSource;
1815 }
1816
1817 /** {@inheritDoc} */
1818 @Override
1819 public final void setSource(int source) {
1820 mSource = source;
1821 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822
1823 /**
1824 * <p>Returns the state of the meta keys.</p>
1825 *
1826 * @return an integer in which each bit set to 1 represents a pressed
1827 * meta key
1828 *
1829 * @see #isAltPressed()
1830 * @see #isShiftPressed()
1831 * @see #isSymPressed()
Jeff Brown497a92c2010-09-12 17:55:08 -07001832 * @see #isCtrlPressed()
1833 * @see #isMetaPressed()
1834 * @see #isFunctionPressed()
Jeff Brown51e7fe72010-10-29 22:19:53 -07001835 * @see #isCapsLockOn()
1836 * @see #isNumLockOn()
1837 * @see #isScrollLockOn()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 * @see #META_ALT_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001839 * @see #META_ALT_LEFT_ON
1840 * @see #META_ALT_RIGHT_ON
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 * @see #META_SHIFT_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001842 * @see #META_SHIFT_LEFT_ON
1843 * @see #META_SHIFT_RIGHT_ON
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 * @see #META_SYM_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001845 * @see #META_FUNCTION_ON
1846 * @see #META_CTRL_ON
1847 * @see #META_CTRL_LEFT_ON
1848 * @see #META_CTRL_RIGHT_ON
1849 * @see #META_META_ON
1850 * @see #META_META_LEFT_ON
1851 * @see #META_META_RIGHT_ON
Jeff Brown51e7fe72010-10-29 22:19:53 -07001852 * @see #META_CAPS_LOCK_ON
1853 * @see #META_NUM_LOCK_ON
1854 * @see #META_SCROLL_LOCK_ON
Jeff Brown54875002011-04-06 15:33:01 -07001855 * @see #getModifiers
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856 */
1857 public final int getMetaState() {
1858 return mMetaState;
1859 }
1860
1861 /**
Jeff Brown54875002011-04-06 15:33:01 -07001862 * Returns the state of the modifier keys.
1863 * <p>
1864 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1865 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1866 * not considered modifier keys. Consequently, this function specifically masks out
1867 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1868 * </p><p>
1869 * The value returned consists of the meta state (from {@link #getMetaState})
1870 * normalized using {@link #normalizeMetaState(int)} and then masked with
1871 * {@link #getModifierMetaStateMask} so that only valid modifier bits are retained.
1872 * </p>
1873 *
1874 * @return An integer in which each bit set to 1 represents a pressed modifier key.
1875 * @see #getMetaState
1876 */
1877 public final int getModifiers() {
1878 return normalizeMetaState(mMetaState) & META_MODIFIER_MASK;
1879 }
1880
1881 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001882 * Returns the flags for this key event.
1883 *
1884 * @see #FLAG_WOKE_HERE
1885 */
1886 public final int getFlags() {
1887 return mFlags;
1888 }
1889
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001890 // Mask of all modifier key meta states. Specifically excludes locked keys like caps lock.
1891 private static final int META_MODIFIER_MASK =
1892 META_SHIFT_ON | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON
1893 | META_ALT_ON | META_ALT_LEFT_ON | META_ALT_RIGHT_ON
1894 | META_CTRL_ON | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON
1895 | META_META_ON | META_META_LEFT_ON | META_META_RIGHT_ON
1896 | META_SYM_ON | META_FUNCTION_ON;
1897
1898 // Mask of all lock key meta states.
1899 private static final int META_LOCK_MASK =
1900 META_CAPS_LOCK_ON | META_NUM_LOCK_ON | META_SCROLL_LOCK_ON;
1901
1902 // Mask of all valid meta states.
1903 private static final int META_ALL_MASK = META_MODIFIER_MASK | META_LOCK_MASK;
1904
1905 // Mask of all synthetic meta states that are reserved for API compatibility with
1906 // historical uses in MetaKeyKeyListener.
1907 private static final int META_SYNTHETIC_MASK =
1908 META_CAP_LOCKED | META_ALT_LOCKED | META_SYM_LOCKED | META_SELECTING;
1909
1910 // Mask of all meta states that are not valid use in specifying a modifier key.
1911 // These bits are known to be used for purposes other than specifying modifiers.
1912 private static final int META_INVALID_MODIFIER_MASK =
1913 META_LOCK_MASK | META_SYNTHETIC_MASK;
1914
1915 /**
1916 * Gets a mask that includes all valid modifier key meta state bits.
1917 * <p>
1918 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1919 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1920 * not considered modifier keys. Consequently, the mask specifically excludes
1921 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1922 * </p>
1923 *
1924 * @return The modifier meta state mask which is a combination of
1925 * {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON}, {@link #META_SHIFT_RIGHT_ON},
1926 * {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON}, {@link #META_ALT_RIGHT_ON},
1927 * {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON}, {@link #META_CTRL_RIGHT_ON},
1928 * {@link #META_META_ON}, {@link #META_META_LEFT_ON}, {@link #META_META_RIGHT_ON},
1929 * {@link #META_SYM_ON}, {@link #META_FUNCTION_ON}.
1930 */
1931 public static int getModifierMetaStateMask() {
1932 return META_MODIFIER_MASK;
1933 }
1934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935 /**
1936 * Returns true if this key code is a modifier key.
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001937 * <p>
1938 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1939 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1940 * not considered modifier keys. Consequently, this function return false
1941 * for those keys.
1942 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 *
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001944 * @return True if the key code is one of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 * {@link #KEYCODE_SHIFT_LEFT} {@link #KEYCODE_SHIFT_RIGHT},
Jeff Brown497a92c2010-09-12 17:55:08 -07001946 * {@link #KEYCODE_ALT_LEFT}, {@link #KEYCODE_ALT_RIGHT},
Jeff Brown497a92c2010-09-12 17:55:08 -07001947 * {@link #KEYCODE_CTRL_LEFT}, {@link #KEYCODE_CTRL_RIGHT},
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001948 * {@link #KEYCODE_META_LEFT}, or {@link #KEYCODE_META_RIGHT},
1949 * {@link #KEYCODE_SYM}, {@link #KEYCODE_NUM}, {@link #KEYCODE_FUNCTION}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 */
1951 public static boolean isModifierKey(int keyCode) {
Jeff Brown497a92c2010-09-12 17:55:08 -07001952 switch (keyCode) {
1953 case KEYCODE_SHIFT_LEFT:
1954 case KEYCODE_SHIFT_RIGHT:
1955 case KEYCODE_ALT_LEFT:
1956 case KEYCODE_ALT_RIGHT:
Jeff Brown497a92c2010-09-12 17:55:08 -07001957 case KEYCODE_CTRL_LEFT:
1958 case KEYCODE_CTRL_RIGHT:
1959 case KEYCODE_META_LEFT:
1960 case KEYCODE_META_RIGHT:
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001961 case KEYCODE_SYM:
1962 case KEYCODE_NUM:
1963 case KEYCODE_FUNCTION:
Jeff Brown497a92c2010-09-12 17:55:08 -07001964 return true;
1965 default:
1966 return false;
1967 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 }
1969
1970 /**
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001971 * Normalizes the specified meta state.
1972 * <p>
1973 * The meta state is normalized such that if either the left or right modifier meta state
1974 * bits are set then the result will also include the universal bit for that modifier.
1975 * </p><p>
1976 * If the specified meta state contains {@link #META_ALT_LEFT_ON} then
1977 * the result will also contain {@link #META_ALT_ON} in addition to {@link #META_ALT_LEFT_ON}
1978 * and the other bits that were specified in the input. The same is process is
1979 * performed for shift, control and meta.
1980 * </p><p>
1981 * If the specified meta state contains synthetic meta states defined by
1982 * {@link MetaKeyKeyListener}, then those states are translated here and the original
1983 * synthetic meta states are removed from the result.
1984 * {@link MetaKeyKeyListener#META_CAP_LOCKED} is translated to {@link #META_CAPS_LOCK_ON}.
1985 * {@link MetaKeyKeyListener#META_ALT_LOCKED} is translated to {@link #META_ALT_ON}.
1986 * {@link MetaKeyKeyListener#META_SYM_LOCKED} is translated to {@link #META_SYM_ON}.
1987 * </p><p>
1988 * Undefined meta state bits are removed.
1989 * </p>
1990 *
1991 * @param metaState The meta state.
1992 * @return The normalized meta state.
1993 */
1994 public static int normalizeMetaState(int metaState) {
1995 if ((metaState & (META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON)) != 0) {
1996 metaState |= META_SHIFT_ON;
1997 }
1998 if ((metaState & (META_ALT_LEFT_ON | META_ALT_RIGHT_ON)) != 0) {
1999 metaState |= META_ALT_ON;
2000 }
2001 if ((metaState & (META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON)) != 0) {
2002 metaState |= META_CTRL_ON;
2003 }
2004 if ((metaState & (META_META_LEFT_ON | META_META_RIGHT_ON)) != 0) {
2005 metaState |= META_META_ON;
2006 }
2007 if ((metaState & MetaKeyKeyListener.META_CAP_LOCKED) != 0) {
2008 metaState |= META_CAPS_LOCK_ON;
2009 }
2010 if ((metaState & MetaKeyKeyListener.META_ALT_LOCKED) != 0) {
2011 metaState |= META_ALT_ON;
2012 }
2013 if ((metaState & MetaKeyKeyListener.META_SYM_LOCKED) != 0) {
2014 metaState |= META_SYM_ON;
2015 }
2016 return metaState & META_ALL_MASK;
2017 }
2018
2019 /**
2020 * Returns true if no modifiers keys are pressed according to the specified meta state.
2021 * <p>
2022 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2023 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2024 * not considered modifier keys. Consequently, this function ignores
2025 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2026 * </p><p>
2027 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}.
2028 * </p>
2029 *
2030 * @param metaState The meta state to consider.
2031 * @return True if no modifier keys are pressed.
2032 * @see #hasNoModifiers()
2033 */
2034 public static boolean metaStateHasNoModifiers(int metaState) {
2035 return (normalizeMetaState(metaState) & META_MODIFIER_MASK) == 0;
2036 }
2037
2038 /**
2039 * Returns true if only the specified modifier keys are pressed according to
2040 * the specified meta state. Returns false if a different combination of modifier
2041 * keys are pressed.
2042 * <p>
2043 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2044 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2045 * not considered modifier keys. Consequently, this function ignores
2046 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2047 * </p><p>
2048 * If the specified modifier mask includes directional modifiers, such as
2049 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the
2050 * modifier is pressed on that side.
2051 * If the specified modifier mask includes non-directional modifiers, such as
2052 * {@link #META_SHIFT_ON}, then this method ensures that the modifier
2053 * is pressed on either side.
2054 * If the specified modifier mask includes both directional and non-directional modifiers
2055 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON},
2056 * then this method throws an illegal argument exception.
2057 * </p>
2058 *
2059 * @param metaState The meta state to consider.
2060 * @param modifiers The meta state of the modifier keys to check. May be a combination
2061 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to
2062 * ensure that no modifier keys are pressed.
2063 * @return True if only the specified modifier keys are pressed.
2064 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers
2065 * @see #hasModifiers
2066 */
2067 public static boolean metaStateHasModifiers(int metaState, int modifiers) {
2068 // Note: For forward compatibility, we allow the parameter to contain meta states
2069 // that we do not recognize but we explicitly disallow meta states that
2070 // are not valid modifiers.
2071 if ((modifiers & META_INVALID_MODIFIER_MASK) != 0) {
2072 throw new IllegalArgumentException("modifiers must not contain "
2073 + "META_CAPS_LOCK_ON, META_NUM_LOCK_ON, META_SCROLL_LOCK_ON, "
2074 + "META_CAP_LOCKED, META_ALT_LOCKED, META_SYM_LOCKED, "
2075 + "or META_SELECTING");
2076 }
2077
2078 metaState = normalizeMetaState(metaState) & META_MODIFIER_MASK;
2079 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2080 META_SHIFT_ON, META_SHIFT_LEFT_ON, META_SHIFT_RIGHT_ON);
2081 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2082 META_ALT_ON, META_ALT_LEFT_ON, META_ALT_RIGHT_ON);
2083 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2084 META_CTRL_ON, META_CTRL_LEFT_ON, META_CTRL_RIGHT_ON);
2085 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2086 META_META_ON, META_META_LEFT_ON, META_META_RIGHT_ON);
2087 return metaState == modifiers;
2088 }
2089
2090 private static int metaStateFilterDirectionalModifiers(int metaState,
2091 int modifiers, int basic, int left, int right) {
2092 final boolean wantBasic = (modifiers & basic) != 0;
2093 final int directional = left | right;
2094 final boolean wantLeftOrRight = (modifiers & directional) != 0;
2095
2096 if (wantBasic) {
2097 if (wantLeftOrRight) {
2098 throw new IllegalArgumentException("modifiers must not contain "
2099 + metaStateToString(basic) + " combined with "
2100 + metaStateToString(left) + " or " + metaStateToString(right));
2101 }
2102 return metaState & ~directional;
2103 } else if (wantLeftOrRight) {
2104 return metaState & ~basic;
2105 } else {
2106 return metaState;
2107 }
2108 }
2109
2110 /**
2111 * Returns true if no modifier keys are pressed.
2112 * <p>
2113 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2114 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2115 * not considered modifier keys. Consequently, this function ignores
2116 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2117 * </p><p>
2118 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}.
2119 * </p>
2120 *
2121 * @return True if no modifier keys are pressed.
2122 * @see #metaStateHasNoModifiers
2123 */
2124 public final boolean hasNoModifiers() {
2125 return metaStateHasNoModifiers(mMetaState);
2126 }
2127
2128 /**
2129 * Returns true if only the specified modifiers keys are pressed.
2130 * Returns false if a different combination of modifier keys are pressed.
2131 * <p>
2132 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2133 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2134 * not considered modifier keys. Consequently, this function ignores
2135 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2136 * </p><p>
2137 * If the specified modifier mask includes directional modifiers, such as
2138 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the
2139 * modifier is pressed on that side.
2140 * If the specified modifier mask includes non-directional modifiers, such as
2141 * {@link #META_SHIFT_ON}, then this method ensures that the modifier
2142 * is pressed on either side.
2143 * If the specified modifier mask includes both directional and non-directional modifiers
2144 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON},
2145 * then this method throws an illegal argument exception.
2146 * </p>
2147 *
2148 * @param modifiers The meta state of the modifier keys to check. May be a combination
2149 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to
2150 * ensure that no modifier keys are pressed.
2151 * @return True if only the specified modifier keys are pressed.
2152 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers
2153 * @see #metaStateHasModifiers
2154 */
2155 public final boolean hasModifiers(int modifiers) {
2156 return metaStateHasModifiers(mMetaState, modifiers);
2157 }
2158
2159 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 * <p>Returns the pressed state of the ALT meta key.</p>
2161 *
2162 * @return true if the ALT key is pressed, false otherwise
2163 *
2164 * @see #KEYCODE_ALT_LEFT
2165 * @see #KEYCODE_ALT_RIGHT
2166 * @see #META_ALT_ON
2167 */
2168 public final boolean isAltPressed() {
2169 return (mMetaState & META_ALT_ON) != 0;
2170 }
2171
2172 /**
2173 * <p>Returns the pressed state of the SHIFT meta key.</p>
2174 *
2175 * @return true if the SHIFT key is pressed, false otherwise
2176 *
2177 * @see #KEYCODE_SHIFT_LEFT
2178 * @see #KEYCODE_SHIFT_RIGHT
2179 * @see #META_SHIFT_ON
2180 */
2181 public final boolean isShiftPressed() {
2182 return (mMetaState & META_SHIFT_ON) != 0;
2183 }
2184
2185 /**
2186 * <p>Returns the pressed state of the SYM meta key.</p>
2187 *
2188 * @return true if the SYM key is pressed, false otherwise
2189 *
2190 * @see #KEYCODE_SYM
2191 * @see #META_SYM_ON
2192 */
2193 public final boolean isSymPressed() {
2194 return (mMetaState & META_SYM_ON) != 0;
2195 }
2196
2197 /**
Jeff Brown497a92c2010-09-12 17:55:08 -07002198 * <p>Returns the pressed state of the CTRL meta key.</p>
2199 *
2200 * @return true if the CTRL key is pressed, false otherwise
2201 *
2202 * @see #KEYCODE_CTRL_LEFT
2203 * @see #KEYCODE_CTRL_RIGHT
2204 * @see #META_CTRL_ON
2205 */
2206 public final boolean isCtrlPressed() {
2207 return (mMetaState & META_CTRL_ON) != 0;
2208 }
2209
2210 /**
2211 * <p>Returns the pressed state of the META meta key.</p>
2212 *
2213 * @return true if the META key is pressed, false otherwise
2214 *
2215 * @see #KEYCODE_META_LEFT
2216 * @see #KEYCODE_META_RIGHT
2217 * @see #META_META_ON
2218 */
2219 public final boolean isMetaPressed() {
2220 return (mMetaState & META_META_ON) != 0;
2221 }
2222
2223 /**
2224 * <p>Returns the pressed state of the FUNCTION meta key.</p>
2225 *
2226 * @return true if the FUNCTION key is pressed, false otherwise
2227 *
2228 * @see #KEYCODE_FUNCTION
2229 * @see #META_FUNCTION_ON
2230 */
2231 public final boolean isFunctionPressed() {
2232 return (mMetaState & META_FUNCTION_ON) != 0;
2233 }
2234
2235 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07002236 * <p>Returns the locked state of the CAPS LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002237 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002238 * @return true if the CAPS LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002239 *
2240 * @see #KEYCODE_CAPS_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002241 * @see #META_CAPS_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002242 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002243 public final boolean isCapsLockOn() {
2244 return (mMetaState & META_CAPS_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002245 }
2246
2247 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07002248 * <p>Returns the locked state of the NUM LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002249 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002250 * @return true if the NUM LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002251 *
2252 * @see #KEYCODE_NUM_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002253 * @see #META_NUM_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002254 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002255 public final boolean isNumLockOn() {
2256 return (mMetaState & META_NUM_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002257 }
2258
2259 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07002260 * <p>Returns the locked state of the SCROLL LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002261 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002262 * @return true if the SCROLL LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002263 *
2264 * @see #KEYCODE_SCROLL_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002265 * @see #META_SCROLL_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002266 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002267 public final boolean isScrollLockOn() {
2268 return (mMetaState & META_SCROLL_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002269 }
2270
2271 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002272 * Retrieve the action of this key event. May be either
2273 * {@link #ACTION_DOWN}, {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
2274 *
2275 * @return The event action: ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.
2276 */
2277 public final int getAction() {
2278 return mAction;
2279 }
2280
2281 /**
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07002282 * For {@link #ACTION_UP} events, indicates that the event has been
2283 * canceled as per {@link #FLAG_CANCELED}.
2284 */
2285 public final boolean isCanceled() {
2286 return (mFlags&FLAG_CANCELED) != 0;
2287 }
2288
2289 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002290 * Call this during {@link Callback#onKeyDown} to have the system track
2291 * the key through its final up (possibly including a long press). Note
2292 * that only one key can be tracked at a time -- if another key down
2293 * event is received while a previous one is being tracked, tracking is
2294 * stopped on the previous event.
2295 */
2296 public final void startTracking() {
2297 mFlags |= FLAG_START_TRACKING;
2298 }
2299
2300 /**
2301 * For {@link #ACTION_UP} events, indicates that the event is still being
2302 * tracked from its initial down event as per
2303 * {@link #FLAG_TRACKING}.
2304 */
2305 public final boolean isTracking() {
2306 return (mFlags&FLAG_TRACKING) != 0;
2307 }
2308
2309 /**
2310 * For {@link #ACTION_DOWN} events, indicates that the event has been
2311 * canceled as per {@link #FLAG_LONG_PRESS}.
2312 */
2313 public final boolean isLongPress() {
2314 return (mFlags&FLAG_LONG_PRESS) != 0;
2315 }
2316
2317 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002318 * Retrieve the key code of the key event. This is the physical key that
2319 * was pressed, <em>not</em> the Unicode character.
2320 *
2321 * @return The key code of the event.
2322 */
2323 public final int getKeyCode() {
2324 return mKeyCode;
2325 }
2326
2327 /**
2328 * For the special case of a {@link #ACTION_MULTIPLE} event with key
2329 * code of {@link #KEYCODE_UNKNOWN}, this is a raw string of characters
2330 * associated with the event. In all other cases it is null.
2331 *
2332 * @return Returns a String of 1 or more characters associated with
2333 * the event.
2334 */
2335 public final String getCharacters() {
2336 return mCharacters;
2337 }
2338
2339 /**
2340 * Retrieve the hardware key id of this key event. These values are not
2341 * reliable and vary from device to device.
2342 *
2343 * {@more}
2344 * Mostly this is here for debugging purposes.
2345 */
2346 public final int getScanCode() {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002347 return mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002348 }
2349
2350 /**
2351 * Retrieve the repeat count of the event. For both key up and key down
2352 * events, this is the number of times the key has repeated with the first
2353 * down starting at 0 and counting up from there. For multiple key
2354 * events, this is the number of down/up pairs that have occurred.
2355 *
2356 * @return The number of times the key has repeated.
2357 */
2358 public final int getRepeatCount() {
2359 return mRepeatCount;
2360 }
2361
2362 /**
2363 * Retrieve the time of the most recent key down event,
2364 * in the {@link android.os.SystemClock#uptimeMillis} time base. If this
2365 * is a down event, this will be the same as {@link #getEventTime()}.
2366 * Note that when chording keys, this value is the down time of the
2367 * most recently pressed key, which may <em>not</em> be the same physical
2368 * key of this event.
2369 *
2370 * @return Returns the most recent key down time, in the
2371 * {@link android.os.SystemClock#uptimeMillis} time base
2372 */
2373 public final long getDownTime() {
2374 return mDownTime;
2375 }
2376
2377 /**
2378 * Retrieve the time this event occurred,
2379 * in the {@link android.os.SystemClock#uptimeMillis} time base.
2380 *
2381 * @return Returns the time this event occurred,
2382 * in the {@link android.os.SystemClock#uptimeMillis} time base.
2383 */
2384 public final long getEventTime() {
2385 return mEventTime;
2386 }
2387
Jeff Brown4e91a182011-04-07 11:38:09 -07002388 /** @hide */
2389 @Override
2390 public final long getEventTimeNano() {
2391 return mEventTime * 1000000L;
2392 }
2393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002394 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 * Renamed to {@link #getDeviceId}.
2396 *
2397 * @hide
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002398 * @deprecated use {@link #getDeviceId()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002400 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 public final int getKeyboardDevice() {
2402 return mDeviceId;
2403 }
2404
2405 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002406 * Gets the {@link KeyCharacterMap} associated with the keyboard device.
2407 *
2408 * @return The associated key character map.
Andrew Sapperstein8ab3dc72011-06-23 18:56:44 -07002409 * @throws {@link KeyCharacterMap.UnavailableException} if the key character map
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002410 * could not be loaded because it was malformed or the default key character map
2411 * is missing from the system.
2412 *
Andrew Sapperstein8ab3dc72011-06-23 18:56:44 -07002413 * @see KeyCharacterMap#load
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002414 */
2415 public final KeyCharacterMap getKeyCharacterMap() {
2416 return KeyCharacterMap.load(mDeviceId);
2417 }
2418
2419 /**
2420 * Gets the primary character for this key.
2421 * In other words, the label that is physically printed on it.
2422 *
2423 * @return The display label character, or 0 if none (eg. for non-printing keys).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002424 */
2425 public char getDisplayLabel() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002426 return getKeyCharacterMap().getDisplayLabel(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427 }
2428
2429 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002430 * Gets the Unicode character generated by the specified key and meta
2431 * key state combination.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432 * <p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002433 * Returns the Unicode character that the specified key would produce
2434 * when the specified meta bits (see {@link MetaKeyKeyListener})
2435 * were active.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 * </p><p>
2437 * Returns 0 if the key is not one that is used to type Unicode
2438 * characters.
2439 * </p><p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002440 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the
2441 * key is a "dead key" that should be combined with another to
2442 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} --
2443 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 * </p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002445 *
2446 * @return The associated character or combining accent, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447 */
2448 public int getUnicodeChar() {
2449 return getUnicodeChar(mMetaState);
2450 }
2451
2452 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002453 * Gets the Unicode character generated by the specified key and meta
2454 * key state combination.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455 * <p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002456 * Returns the Unicode character that the specified key would produce
2457 * when the specified meta bits (see {@link MetaKeyKeyListener})
2458 * were active.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002459 * </p><p>
2460 * Returns 0 if the key is not one that is used to type Unicode
2461 * characters.
2462 * </p><p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002463 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the
2464 * key is a "dead key" that should be combined with another to
2465 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} --
2466 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 * </p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002468 *
2469 * @param metaState The meta key modifier state.
2470 * @return The associated character or combining accent, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002472 public int getUnicodeChar(int metaState) {
2473 return getKeyCharacterMap().get(mKeyCode, metaState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474 }
2475
2476 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002477 * Get the character conversion data for a given key code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478 *
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002479 * @param results A {@link KeyCharacterMap.KeyData} instance that will be
2480 * filled with the results.
2481 * @return True if the key was mapped. If the key was not mapped, results is not modified.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002482 *
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002483 * @deprecated instead use {@link #getDisplayLabel()},
2484 * {@link #getNumber()} or {@link #getUnicodeChar(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002485 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002486 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487 public boolean getKeyData(KeyData results) {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002488 return getKeyCharacterMap().getKeyData(mKeyCode, results);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002489 }
2490
2491 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002492 * Gets the first character in the character array that can be generated
2493 * by the specified key code.
2494 * <p>
2495 * This is a convenience function that returns the same value as
2496 * {@link #getMatch(char[],int) getMatch(chars, 0)}.
2497 * </p>
2498 *
2499 * @param chars The array of matching characters to consider.
2500 * @return The matching associated character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002501 */
2502 public char getMatch(char[] chars) {
2503 return getMatch(chars, 0);
2504 }
2505
2506 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002507 * Gets the first character in the character array that can be generated
2508 * by the specified key code. If there are multiple choices, prefers
2509 * the one that would be generated with the specified meta key modifier state.
2510 *
2511 * @param chars The array of matching characters to consider.
2512 * @param metaState The preferred meta key modifier state.
2513 * @return The matching associated character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002515 public char getMatch(char[] chars, int metaState) {
2516 return getKeyCharacterMap().getMatch(mKeyCode, chars, metaState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 }
2518
2519 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002520 * Gets the number or symbol associated with the key.
2521 * <p>
2522 * The character value is returned, not the numeric value.
2523 * If the key is not a number, but is a symbol, the symbol is retuned.
2524 * </p><p>
2525 * This method is intended to to support dial pads and other numeric or
2526 * symbolic entry on keyboards where certain keys serve dual function
2527 * as alphabetic and symbolic keys. This method returns the number
2528 * or symbol associated with the key independent of whether the user
2529 * has pressed the required modifier.
2530 * </p><p>
2531 * For example, on one particular keyboard the keys on the top QWERTY row generate
2532 * numbers when ALT is pressed such that ALT-Q maps to '1'. So for that keyboard
2533 * when {@link #getNumber} is called with {@link KeyEvent#KEYCODE_Q} it returns '1'
2534 * so that the user can type numbers without pressing ALT when it makes sense.
2535 * </p>
2536 *
2537 * @return The associated numeric or symbolic character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538 */
2539 public char getNumber() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002540 return getKeyCharacterMap().getNumber(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541 }
2542
2543 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002544 * Returns true if this key produces a glyph.
2545 *
2546 * @return True if the key is a printing key.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547 */
2548 public boolean isPrintingKey() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002549 return getKeyCharacterMap().isPrintingKey(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 }
2551
2552 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002553 * @deprecated Use {@link #dispatch(Callback, DispatcherState, Object)} instead.
2554 */
2555 @Deprecated
2556 public final boolean dispatch(Callback receiver) {
2557 return dispatch(receiver, null, null);
2558 }
2559
2560 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002561 * Deliver this key event to a {@link Callback} interface. If this is
2562 * an ACTION_MULTIPLE event and it is not handled, then an attempt will
2563 * be made to deliver a single normal event.
2564 *
2565 * @param receiver The Callback that will be given the event.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002566 * @param state State information retained across events.
2567 * @param target The target of the dispatch, for use in tracking.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002568 *
2569 * @return The return value from the Callback method that was called.
2570 */
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002571 public final boolean dispatch(Callback receiver, DispatcherState state,
2572 Object target) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002573 switch (mAction) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002574 case ACTION_DOWN: {
2575 mFlags &= ~FLAG_START_TRACKING;
Dianne Hackborn8d374262009-09-14 21:21:52 -07002576 if (DEBUG) Log.v(TAG, "Key down to " + target + " in " + state
2577 + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002578 boolean res = receiver.onKeyDown(mKeyCode, this);
2579 if (state != null) {
2580 if (res && mRepeatCount == 0 && (mFlags&FLAG_START_TRACKING) != 0) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002581 if (DEBUG) Log.v(TAG, " Start tracking!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002582 state.startTracking(this, target);
2583 } else if (isLongPress() && state.isTracking(this)) {
2584 try {
2585 if (receiver.onKeyLongPress(mKeyCode, this)) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002586 if (DEBUG) Log.v(TAG, " Clear from long press!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002587 state.performedLongPress(this);
2588 res = true;
2589 }
2590 } catch (AbstractMethodError e) {
2591 }
2592 }
2593 }
2594 return res;
2595 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 case ACTION_UP:
Dianne Hackborn8d374262009-09-14 21:21:52 -07002597 if (DEBUG) Log.v(TAG, "Key up to " + target + " in " + state
2598 + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002599 if (state != null) {
2600 state.handleUpEvent(this);
2601 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002602 return receiver.onKeyUp(mKeyCode, this);
2603 case ACTION_MULTIPLE:
2604 final int count = mRepeatCount;
2605 final int code = mKeyCode;
2606 if (receiver.onKeyMultiple(code, count, this)) {
2607 return true;
2608 }
2609 if (code != KeyEvent.KEYCODE_UNKNOWN) {
2610 mAction = ACTION_DOWN;
2611 mRepeatCount = 0;
2612 boolean handled = receiver.onKeyDown(code, this);
2613 if (handled) {
2614 mAction = ACTION_UP;
2615 receiver.onKeyUp(code, this);
2616 }
2617 mAction = ACTION_MULTIPLE;
2618 mRepeatCount = count;
2619 return handled;
2620 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002621 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002622 }
2623 return false;
2624 }
2625
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002626 /**
2627 * Use with {@link KeyEvent#dispatch(Callback, DispatcherState, Object)}
2628 * for more advanced key dispatching, such as long presses.
2629 */
2630 public static class DispatcherState {
2631 int mDownKeyCode;
2632 Object mDownTarget;
2633 SparseIntArray mActiveLongPresses = new SparseIntArray();
2634
2635 /**
2636 * Reset back to initial state.
2637 */
2638 public void reset() {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002639 if (DEBUG) Log.v(TAG, "Reset: " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002640 mDownKeyCode = 0;
2641 mDownTarget = null;
2642 mActiveLongPresses.clear();
2643 }
2644
2645 /**
2646 * Stop any tracking associated with this target.
2647 */
2648 public void reset(Object target) {
2649 if (mDownTarget == target) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002650 if (DEBUG) Log.v(TAG, "Reset in " + target + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002651 mDownKeyCode = 0;
2652 mDownTarget = null;
2653 }
2654 }
2655
2656 /**
2657 * Start tracking the key code associated with the given event. This
2658 * can only be called on a key down. It will allow you to see any
2659 * long press associated with the key, and will result in
2660 * {@link KeyEvent#isTracking} return true on the long press and up
2661 * events.
2662 *
2663 * <p>This is only needed if you are directly dispatching events, rather
2664 * than handling them in {@link Callback#onKeyDown}.
2665 */
2666 public void startTracking(KeyEvent event, Object target) {
2667 if (event.getAction() != ACTION_DOWN) {
2668 throw new IllegalArgumentException(
2669 "Can only start tracking on a down event");
2670 }
Dianne Hackborn8d374262009-09-14 21:21:52 -07002671 if (DEBUG) Log.v(TAG, "Start trackingt in " + target + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002672 mDownKeyCode = event.getKeyCode();
2673 mDownTarget = target;
2674 }
2675
2676 /**
2677 * Return true if the key event is for a key code that is currently
2678 * being tracked by the dispatcher.
2679 */
2680 public boolean isTracking(KeyEvent event) {
2681 return mDownKeyCode == event.getKeyCode();
2682 }
2683
2684 /**
2685 * Keep track of the given event's key code as having performed an
2686 * action with a long press, so no action should occur on the up.
2687 * <p>This is only needed if you are directly dispatching events, rather
2688 * than handling them in {@link Callback#onKeyLongPress}.
2689 */
2690 public void performedLongPress(KeyEvent event) {
2691 mActiveLongPresses.put(event.getKeyCode(), 1);
2692 }
2693
2694 /**
2695 * Handle key up event to stop tracking. This resets the dispatcher state,
2696 * and updates the key event state based on it.
2697 * <p>This is only needed if you are directly dispatching events, rather
2698 * than handling them in {@link Callback#onKeyUp}.
2699 */
2700 public void handleUpEvent(KeyEvent event) {
2701 final int keyCode = event.getKeyCode();
Dianne Hackborn8d374262009-09-14 21:21:52 -07002702 if (DEBUG) Log.v(TAG, "Handle key up " + event + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002703 int index = mActiveLongPresses.indexOfKey(keyCode);
2704 if (index >= 0) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002705 if (DEBUG) Log.v(TAG, " Index: " + index);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002706 event.mFlags |= FLAG_CANCELED | FLAG_CANCELED_LONG_PRESS;
2707 mActiveLongPresses.removeAt(index);
2708 }
2709 if (mDownKeyCode == keyCode) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002710 if (DEBUG) Log.v(TAG, " Tracking!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002711 event.mFlags |= FLAG_TRACKING;
2712 mDownKeyCode = 0;
2713 mDownTarget = null;
2714 }
2715 }
2716 }
Jeff Brown497a92c2010-09-12 17:55:08 -07002717
2718 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 public String toString() {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002720 StringBuilder msg = new StringBuilder();
2721 msg.append("KeyEvent { action=").append(actionToString(mAction));
2722 msg.append(", keyCode=").append(keyCodeToString(mKeyCode));
2723 msg.append(", scanCode=").append(mScanCode);
2724 if (mCharacters != null) {
2725 msg.append(", characters=\"").append(mCharacters).append("\"");
2726 }
2727 msg.append(", metaState=").append(metaStateToString(mMetaState));
2728 msg.append(", flags=0x").append(Integer.toHexString(mFlags));
2729 msg.append(", repeatCount=").append(mRepeatCount);
2730 msg.append(", eventTime=").append(mEventTime);
2731 msg.append(", downTime=").append(mDownTime);
2732 msg.append(", deviceId=").append(mDeviceId);
2733 msg.append(", source=0x").append(Integer.toHexString(mSource));
2734 msg.append(" }");
2735 return msg.toString();
Jeff Brown497a92c2010-09-12 17:55:08 -07002736 }
2737
2738 /**
2739 * Returns a string that represents the symbolic name of the specified action
Jeff Brown6f2fba42011-02-19 01:08:02 -08002740 * such as "ACTION_DOWN", or an equivalent numeric constant such as "35" if unknown.
Jeff Brown497a92c2010-09-12 17:55:08 -07002741 *
2742 * @param action The action.
2743 * @return The symbolic name of the specified action.
2744 * @hide
2745 */
2746 public static String actionToString(int action) {
2747 switch (action) {
2748 case ACTION_DOWN:
2749 return "ACTION_DOWN";
2750 case ACTION_UP:
2751 return "ACTION_UP";
2752 case ACTION_MULTIPLE:
2753 return "ACTION_MULTIPLE";
2754 default:
2755 return Integer.toString(action);
2756 }
2757 }
2758
2759 /**
2760 * Returns a string that represents the symbolic name of the specified keycode
Jeff Brown6f2fba42011-02-19 01:08:02 -08002761 * such as "KEYCODE_A", "KEYCODE_DPAD_UP", or an equivalent numeric constant
2762 * such as "1001" if unknown.
Jeff Brown497a92c2010-09-12 17:55:08 -07002763 *
2764 * @param keyCode The key code.
2765 * @return The symbolic name of the specified keycode.
2766 *
2767 * @see KeyCharacterMap#getDisplayLabel
Jeff Brown497a92c2010-09-12 17:55:08 -07002768 */
2769 public static String keyCodeToString(int keyCode) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08002770 String symbolicName = KEYCODE_SYMBOLIC_NAMES.get(keyCode);
2771 return symbolicName != null ? symbolicName : Integer.toString(keyCode);
Jeff Brown497a92c2010-09-12 17:55:08 -07002772 }
2773
2774 /**
Jeff Brown6f2fba42011-02-19 01:08:02 -08002775 * Gets a keycode by its symbolic name such as "KEYCODE_A" or an equivalent
2776 * numeric constant such as "1001".
Jeff Brown497a92c2010-09-12 17:55:08 -07002777 *
2778 * @param symbolicName The symbolic name of the keycode.
Jeff Brown6f2fba42011-02-19 01:08:02 -08002779 * @return The keycode or {@link #KEYCODE_UNKNOWN} if not found.
Jeff Brown497a92c2010-09-12 17:55:08 -07002780 * @see #keycodeToString
Jeff Brown497a92c2010-09-12 17:55:08 -07002781 */
2782 public static int keyCodeFromString(String symbolicName) {
2783 if (symbolicName == null) {
2784 throw new IllegalArgumentException("symbolicName must not be null");
2785 }
2786
Jeff Brown6f2fba42011-02-19 01:08:02 -08002787 final int count = KEYCODE_SYMBOLIC_NAMES.size();
Jeff Brown497a92c2010-09-12 17:55:08 -07002788 for (int i = 0; i < count; i++) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08002789 if (symbolicName.equals(KEYCODE_SYMBOLIC_NAMES.valueAt(i))) {
Jeff Brown497a92c2010-09-12 17:55:08 -07002790 return i;
2791 }
2792 }
2793
2794 try {
Jeff Brown6f2fba42011-02-19 01:08:02 -08002795 return Integer.parseInt(symbolicName, 10);
Jeff Brown497a92c2010-09-12 17:55:08 -07002796 } catch (NumberFormatException ex) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08002797 return KEYCODE_UNKNOWN;
Jeff Brown497a92c2010-09-12 17:55:08 -07002798 }
2799 }
2800
2801 /**
2802 * Returns a string that represents the symbolic name of the specified combined meta
2803 * key modifier state flags such as "0", "META_SHIFT_ON",
Jeff Brown6f2fba42011-02-19 01:08:02 -08002804 * "META_ALT_ON|META_SHIFT_ON" or an equivalent numeric constant such as "0x10000000"
2805 * if unknown.
Jeff Brown497a92c2010-09-12 17:55:08 -07002806 *
2807 * @param metaState The meta state.
2808 * @return The symbolic name of the specified combined meta state flags.
2809 * @hide
2810 */
2811 public static String metaStateToString(int metaState) {
2812 if (metaState == 0) {
2813 return "0";
2814 }
2815 StringBuilder result = null;
2816 int i = 0;
2817 while (metaState != 0) {
2818 final boolean isSet = (metaState & 1) != 0;
2819 metaState >>>= 1; // unsigned shift!
2820 if (isSet) {
2821 final String name = META_SYMBOLIC_NAMES[i];
2822 if (result == null) {
2823 if (metaState == 0) {
2824 return name;
2825 }
2826 result = new StringBuilder(name);
2827 } else {
2828 result.append('|');
2829 result.append(name);
2830 }
2831 }
2832 i += 1;
2833 }
2834 return result.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 }
2836
2837 public static final Parcelable.Creator<KeyEvent> CREATOR
2838 = new Parcelable.Creator<KeyEvent>() {
2839 public KeyEvent createFromParcel(Parcel in) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07002840 in.readInt(); // skip token, we already know this is a KeyEvent
2841 return KeyEvent.createFromParcelBody(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 }
2843
2844 public KeyEvent[] newArray(int size) {
2845 return new KeyEvent[size];
2846 }
2847 };
Jeff Brown6ec402b2010-07-28 15:48:59 -07002848
2849 /** @hide */
2850 public static KeyEvent createFromParcelBody(Parcel in) {
2851 return new KeyEvent(in);
2852 }
2853
2854 private KeyEvent(Parcel in) {
Jeff Brown91c69ab2011-02-14 17:03:18 -08002855 mDeviceId = in.readInt();
2856 mSource = in.readInt();
Jeff Brown6ec402b2010-07-28 15:48:59 -07002857 mAction = in.readInt();
2858 mKeyCode = in.readInt();
2859 mRepeatCount = in.readInt();
2860 mMetaState = in.readInt();
2861 mScanCode = in.readInt();
2862 mFlags = in.readInt();
2863 mDownTime = in.readLong();
2864 mEventTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 }
2866
2867 public void writeToParcel(Parcel out, int flags) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07002868 out.writeInt(PARCEL_TOKEN_KEY_EVENT);
Jeff Brown91c69ab2011-02-14 17:03:18 -08002869
2870 out.writeInt(mDeviceId);
2871 out.writeInt(mSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 out.writeInt(mAction);
2873 out.writeInt(mKeyCode);
2874 out.writeInt(mRepeatCount);
2875 out.writeInt(mMetaState);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002876 out.writeInt(mScanCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877 out.writeInt(mFlags);
2878 out.writeLong(mDownTime);
2879 out.writeLong(mEventTime);
2880 }
2881
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -07002882 private native boolean native_isSystemKey(int keyCode);
2883 private native boolean native_hasDefaultAction(int keyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002884}