blob: 5dbda90308689c96f5b7fd37606702c85b9828d4 [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 Brown497a92c2010-09-12 17:55:08 -0700582
Jeff Browncb1404e2011-01-15 18:14:15 -0800583 private static final int LAST_KEYCODE = KEYCODE_BUTTON_16;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584
585 // NOTE: If you add a new keycode here you must also add it to:
586 // isSystem()
Jeff Brown46b9ac02010-04-22 18:58:52 -0700587 // native/include/android/keycodes.h
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 // frameworks/base/include/ui/KeycodeLabels.h
Jeff Brownfd035822010-06-30 16:10:35 -0700589 // external/webkit/WebKit/android/plugins/ANPKeyCodes.h
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 // frameworks/base/core/res/res/values/attrs.xml
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 // emulator?
Dianne Hackborn935ae462009-04-13 16:11:55 -0700592 //
593 // Also Android currently does not reserve code ranges for vendor-
594 // specific key codes. If you have new key codes to have, you
595 // MUST contribute a patch to the open source project to define
596 // those new codes. This is intended to maintain a consistent
597 // set of key code definitions across all Android devices.
Jeff Brown497a92c2010-09-12 17:55:08 -0700598
Jeff Brown6f2fba42011-02-19 01:08:02 -0800599 // Symbolic names of all key codes.
600 private static final SparseArray<String> KEYCODE_SYMBOLIC_NAMES = new SparseArray<String>();
601 private static void populateKeycodeSymbolicNames() {
602 SparseArray<String> names = KEYCODE_SYMBOLIC_NAMES;
603 names.append(KEYCODE_UNKNOWN, "KEYCODE_UNKNOWN");
604 names.append(KEYCODE_SOFT_LEFT, "KEYCODE_SOFT_LEFT");
605 names.append(KEYCODE_SOFT_RIGHT, "KEYCODE_SOFT_RIGHT");
606 names.append(KEYCODE_HOME, "KEYCODE_HOME");
607 names.append(KEYCODE_BACK, "KEYCODE_BACK");
608 names.append(KEYCODE_CALL, "KEYCODE_CALL");
609 names.append(KEYCODE_ENDCALL, "KEYCODE_ENDCALL");
610 names.append(KEYCODE_0, "KEYCODE_0");
611 names.append(KEYCODE_1, "KEYCODE_1");
612 names.append(KEYCODE_2, "KEYCODE_2");
613 names.append(KEYCODE_3, "KEYCODE_3");
614 names.append(KEYCODE_4, "KEYCODE_4");
615 names.append(KEYCODE_5, "KEYCODE_5");
616 names.append(KEYCODE_6, "KEYCODE_6");
617 names.append(KEYCODE_7, "KEYCODE_7");
618 names.append(KEYCODE_8, "KEYCODE_8");
619 names.append(KEYCODE_9, "KEYCODE_9");
620 names.append(KEYCODE_STAR, "KEYCODE_STAR");
621 names.append(KEYCODE_POUND, "KEYCODE_POUND");
622 names.append(KEYCODE_DPAD_UP, "KEYCODE_DPAD_UP");
623 names.append(KEYCODE_DPAD_DOWN, "KEYCODE_DPAD_DOWN");
624 names.append(KEYCODE_DPAD_LEFT, "KEYCODE_DPAD_LEFT");
625 names.append(KEYCODE_DPAD_RIGHT, "KEYCODE_DPAD_RIGHT");
626 names.append(KEYCODE_DPAD_CENTER, "KEYCODE_DPAD_CENTER");
627 names.append(KEYCODE_VOLUME_UP, "KEYCODE_VOLUME_UP");
628 names.append(KEYCODE_VOLUME_DOWN, "KEYCODE_VOLUME_DOWN");
629 names.append(KEYCODE_POWER, "KEYCODE_POWER");
630 names.append(KEYCODE_CAMERA, "KEYCODE_CAMERA");
631 names.append(KEYCODE_CLEAR, "KEYCODE_CLEAR");
632 names.append(KEYCODE_A, "KEYCODE_A");
633 names.append(KEYCODE_B, "KEYCODE_B");
634 names.append(KEYCODE_C, "KEYCODE_C");
635 names.append(KEYCODE_D, "KEYCODE_D");
636 names.append(KEYCODE_E, "KEYCODE_E");
637 names.append(KEYCODE_F, "KEYCODE_F");
638 names.append(KEYCODE_G, "KEYCODE_G");
639 names.append(KEYCODE_H, "KEYCODE_H");
640 names.append(KEYCODE_I, "KEYCODE_I");
641 names.append(KEYCODE_J, "KEYCODE_J");
642 names.append(KEYCODE_K, "KEYCODE_K");
643 names.append(KEYCODE_L, "KEYCODE_L");
644 names.append(KEYCODE_M, "KEYCODE_M");
645 names.append(KEYCODE_N, "KEYCODE_N");
646 names.append(KEYCODE_O, "KEYCODE_O");
647 names.append(KEYCODE_P, "KEYCODE_P");
648 names.append(KEYCODE_Q, "KEYCODE_Q");
649 names.append(KEYCODE_R, "KEYCODE_R");
650 names.append(KEYCODE_S, "KEYCODE_S");
651 names.append(KEYCODE_T, "KEYCODE_T");
652 names.append(KEYCODE_U, "KEYCODE_U");
653 names.append(KEYCODE_V, "KEYCODE_V");
654 names.append(KEYCODE_W, "KEYCODE_W");
655 names.append(KEYCODE_X, "KEYCODE_X");
656 names.append(KEYCODE_Y, "KEYCODE_Y");
657 names.append(KEYCODE_Z, "KEYCODE_Z");
658 names.append(KEYCODE_COMMA, "KEYCODE_COMMA");
659 names.append(KEYCODE_PERIOD, "KEYCODE_PERIOD");
660 names.append(KEYCODE_ALT_LEFT, "KEYCODE_ALT_LEFT");
661 names.append(KEYCODE_ALT_RIGHT, "KEYCODE_ALT_RIGHT");
662 names.append(KEYCODE_SHIFT_LEFT, "KEYCODE_SHIFT_LEFT");
663 names.append(KEYCODE_SHIFT_RIGHT, "KEYCODE_SHIFT_RIGHT");
664 names.append(KEYCODE_TAB, "KEYCODE_TAB");
665 names.append(KEYCODE_SPACE, "KEYCODE_SPACE");
666 names.append(KEYCODE_SYM, "KEYCODE_SYM");
667 names.append(KEYCODE_EXPLORER, "KEYCODE_EXPLORER");
668 names.append(KEYCODE_ENVELOPE, "KEYCODE_ENVELOPE");
669 names.append(KEYCODE_ENTER, "KEYCODE_ENTER");
670 names.append(KEYCODE_DEL, "KEYCODE_DEL");
671 names.append(KEYCODE_GRAVE, "KEYCODE_GRAVE");
672 names.append(KEYCODE_MINUS, "KEYCODE_MINUS");
673 names.append(KEYCODE_EQUALS, "KEYCODE_EQUALS");
674 names.append(KEYCODE_LEFT_BRACKET, "KEYCODE_LEFT_BRACKET");
675 names.append(KEYCODE_RIGHT_BRACKET, "KEYCODE_RIGHT_BRACKET");
676 names.append(KEYCODE_BACKSLASH, "KEYCODE_BACKSLASH");
677 names.append(KEYCODE_SEMICOLON, "KEYCODE_SEMICOLON");
678 names.append(KEYCODE_APOSTROPHE, "KEYCODE_APOSTROPHE");
679 names.append(KEYCODE_SLASH, "KEYCODE_SLASH");
680 names.append(KEYCODE_AT, "KEYCODE_AT");
681 names.append(KEYCODE_NUM, "KEYCODE_NUM");
682 names.append(KEYCODE_HEADSETHOOK, "KEYCODE_HEADSETHOOK");
683 names.append(KEYCODE_FOCUS, "KEYCODE_FOCUS");
684 names.append(KEYCODE_PLUS, "KEYCODE_PLUS");
685 names.append(KEYCODE_MENU, "KEYCODE_MENU");
686 names.append(KEYCODE_NOTIFICATION, "KEYCODE_NOTIFICATION");
687 names.append(KEYCODE_SEARCH, "KEYCODE_SEARCH");
688 names.append(KEYCODE_MEDIA_PLAY_PAUSE, "KEYCODE_MEDIA_PLAY_PAUSE");
689 names.append(KEYCODE_MEDIA_STOP, "KEYCODE_MEDIA_STOP");
690 names.append(KEYCODE_MEDIA_NEXT, "KEYCODE_MEDIA_NEXT");
691 names.append(KEYCODE_MEDIA_PREVIOUS, "KEYCODE_MEDIA_PREVIOUS");
692 names.append(KEYCODE_MEDIA_REWIND, "KEYCODE_MEDIA_REWIND");
693 names.append(KEYCODE_MEDIA_FAST_FORWARD, "KEYCODE_MEDIA_FAST_FORWARD");
694 names.append(KEYCODE_MUTE, "KEYCODE_MUTE");
695 names.append(KEYCODE_PAGE_UP, "KEYCODE_PAGE_UP");
696 names.append(KEYCODE_PAGE_DOWN, "KEYCODE_PAGE_DOWN");
697 names.append(KEYCODE_PICTSYMBOLS, "KEYCODE_PICTSYMBOLS");
698 names.append(KEYCODE_SWITCH_CHARSET, "KEYCODE_SWITCH_CHARSET");
699 names.append(KEYCODE_BUTTON_A, "KEYCODE_BUTTON_A");
700 names.append(KEYCODE_BUTTON_B, "KEYCODE_BUTTON_B");
701 names.append(KEYCODE_BUTTON_C, "KEYCODE_BUTTON_C");
702 names.append(KEYCODE_BUTTON_X, "KEYCODE_BUTTON_X");
703 names.append(KEYCODE_BUTTON_Y, "KEYCODE_BUTTON_Y");
704 names.append(KEYCODE_BUTTON_Z, "KEYCODE_BUTTON_Z");
705 names.append(KEYCODE_BUTTON_L1, "KEYCODE_BUTTON_L1");
706 names.append(KEYCODE_BUTTON_R1, "KEYCODE_BUTTON_R1");
707 names.append(KEYCODE_BUTTON_L2, "KEYCODE_BUTTON_L2");
708 names.append(KEYCODE_BUTTON_R2, "KEYCODE_BUTTON_R2");
709 names.append(KEYCODE_BUTTON_THUMBL, "KEYCODE_BUTTON_THUMBL");
710 names.append(KEYCODE_BUTTON_THUMBR, "KEYCODE_BUTTON_THUMBR");
711 names.append(KEYCODE_BUTTON_START, "KEYCODE_BUTTON_START");
712 names.append(KEYCODE_BUTTON_SELECT, "KEYCODE_BUTTON_SELECT");
713 names.append(KEYCODE_BUTTON_MODE, "KEYCODE_BUTTON_MODE");
714 names.append(KEYCODE_ESCAPE, "KEYCODE_ESCAPE");
715 names.append(KEYCODE_FORWARD_DEL, "KEYCODE_FORWARD_DEL");
716 names.append(KEYCODE_CTRL_LEFT, "KEYCODE_CTRL_LEFT");
717 names.append(KEYCODE_CTRL_RIGHT, "KEYCODE_CTRL_RIGHT");
718 names.append(KEYCODE_CAPS_LOCK, "KEYCODE_CAPS_LOCK");
719 names.append(KEYCODE_SCROLL_LOCK, "KEYCODE_SCROLL_LOCK");
720 names.append(KEYCODE_META_LEFT, "KEYCODE_META_LEFT");
721 names.append(KEYCODE_META_RIGHT, "KEYCODE_META_RIGHT");
722 names.append(KEYCODE_FUNCTION, "KEYCODE_FUNCTION");
723 names.append(KEYCODE_SYSRQ, "KEYCODE_SYSRQ");
724 names.append(KEYCODE_BREAK, "KEYCODE_BREAK");
725 names.append(KEYCODE_MOVE_HOME, "KEYCODE_MOVE_HOME");
726 names.append(KEYCODE_MOVE_END, "KEYCODE_MOVE_END");
727 names.append(KEYCODE_INSERT, "KEYCODE_INSERT");
728 names.append(KEYCODE_FORWARD, "KEYCODE_FORWARD");
729 names.append(KEYCODE_MEDIA_PLAY, "KEYCODE_MEDIA_PLAY");
730 names.append(KEYCODE_MEDIA_PAUSE, "KEYCODE_MEDIA_PAUSE");
731 names.append(KEYCODE_MEDIA_CLOSE, "KEYCODE_MEDIA_CLOSE");
732 names.append(KEYCODE_MEDIA_EJECT, "KEYCODE_MEDIA_EJECT");
733 names.append(KEYCODE_MEDIA_RECORD, "KEYCODE_MEDIA_RECORD");
734 names.append(KEYCODE_F1, "KEYCODE_F1");
735 names.append(KEYCODE_F2, "KEYCODE_F2");
736 names.append(KEYCODE_F3, "KEYCODE_F3");
737 names.append(KEYCODE_F4, "KEYCODE_F4");
738 names.append(KEYCODE_F5, "KEYCODE_F5");
739 names.append(KEYCODE_F6, "KEYCODE_F6");
740 names.append(KEYCODE_F7, "KEYCODE_F7");
741 names.append(KEYCODE_F8, "KEYCODE_F8");
742 names.append(KEYCODE_F9, "KEYCODE_F9");
743 names.append(KEYCODE_F10, "KEYCODE_F10");
744 names.append(KEYCODE_F11, "KEYCODE_F11");
745 names.append(KEYCODE_F12, "KEYCODE_F12");
746 names.append(KEYCODE_NUM_LOCK, "KEYCODE_NUM_LOCK");
747 names.append(KEYCODE_NUMPAD_0, "KEYCODE_NUMPAD_0");
748 names.append(KEYCODE_NUMPAD_1, "KEYCODE_NUMPAD_1");
749 names.append(KEYCODE_NUMPAD_2, "KEYCODE_NUMPAD_2");
750 names.append(KEYCODE_NUMPAD_3, "KEYCODE_NUMPAD_3");
751 names.append(KEYCODE_NUMPAD_4, "KEYCODE_NUMPAD_4");
752 names.append(KEYCODE_NUMPAD_5, "KEYCODE_NUMPAD_5");
753 names.append(KEYCODE_NUMPAD_6, "KEYCODE_NUMPAD_6");
754 names.append(KEYCODE_NUMPAD_7, "KEYCODE_NUMPAD_7");
755 names.append(KEYCODE_NUMPAD_8, "KEYCODE_NUMPAD_8");
756 names.append(KEYCODE_NUMPAD_9, "KEYCODE_NUMPAD_9");
757 names.append(KEYCODE_NUMPAD_DIVIDE, "KEYCODE_NUMPAD_DIVIDE");
758 names.append(KEYCODE_NUMPAD_MULTIPLY, "KEYCODE_NUMPAD_MULTIPLY");
759 names.append(KEYCODE_NUMPAD_SUBTRACT, "KEYCODE_NUMPAD_SUBTRACT");
760 names.append(KEYCODE_NUMPAD_ADD, "KEYCODE_NUMPAD_ADD");
761 names.append(KEYCODE_NUMPAD_DOT, "KEYCODE_NUMPAD_DOT");
762 names.append(KEYCODE_NUMPAD_COMMA, "KEYCODE_NUMPAD_COMMA");
763 names.append(KEYCODE_NUMPAD_ENTER, "KEYCODE_NUMPAD_ENTER");
764 names.append(KEYCODE_NUMPAD_EQUALS, "KEYCODE_NUMPAD_EQUALS");
765 names.append(KEYCODE_NUMPAD_LEFT_PAREN, "KEYCODE_NUMPAD_LEFT_PAREN");
766 names.append(KEYCODE_NUMPAD_RIGHT_PAREN, "KEYCODE_NUMPAD_RIGHT_PAREN");
767 names.append(KEYCODE_VOLUME_MUTE, "KEYCODE_VOLUME_MUTE");
768 names.append(KEYCODE_INFO, "KEYCODE_INFO");
769 names.append(KEYCODE_CHANNEL_UP, "KEYCODE_CHANNEL_UP");
770 names.append(KEYCODE_CHANNEL_DOWN, "KEYCODE_CHANNEL_DOWN");
771 names.append(KEYCODE_ZOOM_IN, "KEYCODE_ZOOM_IN");
772 names.append(KEYCODE_ZOOM_OUT, "KEYCODE_ZOOM_OUT");
773 names.append(KEYCODE_TV, "KEYCODE_TV");
774 names.append(KEYCODE_WINDOW, "KEYCODE_WINDOW");
775 names.append(KEYCODE_GUIDE, "KEYCODE_GUIDE");
776 names.append(KEYCODE_DVR, "KEYCODE_DVR");
777 names.append(KEYCODE_BOOKMARK, "KEYCODE_BOOKMARK");
778 names.append(KEYCODE_CAPTIONS, "KEYCODE_CAPTIONS");
779 names.append(KEYCODE_SETTINGS, "KEYCODE_SETTINGS");
780 names.append(KEYCODE_TV_POWER, "KEYCODE_TV_POWER");
781 names.append(KEYCODE_TV_INPUT, "KEYCODE_TV_INPUT");
782 names.append(KEYCODE_STB_INPUT, "KEYCODE_STB_INPUT");
783 names.append(KEYCODE_STB_POWER, "KEYCODE_STB_POWER");
784 names.append(KEYCODE_AVR_POWER, "KEYCODE_AVR_POWER");
785 names.append(KEYCODE_AVR_INPUT, "KEYCODE_AVR_INPUT");
786 names.append(KEYCODE_PROG_RED, "KEYCODE_PROG_RED");
787 names.append(KEYCODE_PROG_GREEN, "KEYCODE_PROG_GREEN");
788 names.append(KEYCODE_PROG_YELLOW, "KEYCODE_PROG_YELLOW");
789 names.append(KEYCODE_PROG_BLUE, "KEYCODE_PROG_BLUE");
790 names.append(KEYCODE_APP_SWITCH, "KEYCODE_APP_SWITCH");
791 names.append(KEYCODE_BUTTON_1, "KEYCODE_BUTTON_1");
792 names.append(KEYCODE_BUTTON_2, "KEYCODE_BUTTON_2");
793 names.append(KEYCODE_BUTTON_3, "KEYCODE_BUTTON_3");
794 names.append(KEYCODE_BUTTON_4, "KEYCODE_BUTTON_4");
795 names.append(KEYCODE_BUTTON_5, "KEYCODE_BUTTON_5");
796 names.append(KEYCODE_BUTTON_6, "KEYCODE_BUTTON_6");
797 names.append(KEYCODE_BUTTON_7, "KEYCODE_BUTTON_7");
798 names.append(KEYCODE_BUTTON_8, "KEYCODE_BUTTON_8");
799 names.append(KEYCODE_BUTTON_9, "KEYCODE_BUTTON_9");
800 names.append(KEYCODE_BUTTON_10, "KEYCODE_BUTTON_10");
801 names.append(KEYCODE_BUTTON_11, "KEYCODE_BUTTON_11");
802 names.append(KEYCODE_BUTTON_12, "KEYCODE_BUTTON_12");
803 names.append(KEYCODE_BUTTON_13, "KEYCODE_BUTTON_13");
804 names.append(KEYCODE_BUTTON_14, "KEYCODE_BUTTON_14");
805 names.append(KEYCODE_BUTTON_15, "KEYCODE_BUTTON_15");
806 names.append(KEYCODE_BUTTON_16, "KEYCODE_BUTTON_16");
Jeff Brown9812aed2011-03-07 17:09:51 -0800807 names.append(KEYCODE_LANGUAGE_SWITCH, "KEYCODE_LANGUAGE_SWITCH");
808 names.append(KEYCODE_MANNER_MODE, "KEYCODE_MANNER_MODE");
809 names.append(KEYCODE_3D_MODE, "KEYCODE_3D_MODE");
Jeff Brown497a92c2010-09-12 17:55:08 -0700810 };
811
812 // Symbolic names of all metakeys in bit order from least significant to most significant.
813 // Accordingly there are exactly 32 values in this table.
814 private static final String[] META_SYMBOLIC_NAMES = new String[] {
815 "META_SHIFT_ON",
816 "META_ALT_ON",
817 "META_SYM_ON",
818 "META_FUNCTION_ON",
819 "META_ALT_LEFT_ON",
820 "META_ALT_RIGHT_ON",
821 "META_SHIFT_LEFT_ON",
822 "META_SHIFT_RIGHT_ON",
823 "META_CAP_LOCKED",
824 "META_ALT_LOCKED",
825 "META_SYM_LOCKED",
826 "0x00000800",
827 "META_CTRL_ON",
828 "META_CTRL_LEFT_ON",
829 "META_CTRL_RIGHT_ON",
830 "0x00008000",
831 "META_META_ON",
832 "META_META_LEFT_ON",
833 "META_META_RIGHT_ON",
834 "0x00080000",
Jeff Brown51e7fe72010-10-29 22:19:53 -0700835 "META_CAPS_LOCK_ON",
836 "META_NUM_LOCK_ON",
837 "META_SCROLL_LOCK_ON",
Jeff Brown497a92c2010-09-12 17:55:08 -0700838 "0x00800000",
839 "0x01000000",
840 "0x02000000",
841 "0x04000000",
842 "0x08000000",
843 "0x10000000",
844 "0x20000000",
845 "0x40000000",
846 "0x80000000",
847 };
848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 /**
850 * @deprecated There are now more than MAX_KEYCODE keycodes.
851 * Use {@link #getMaxKeyCode()} instead.
852 */
853 @Deprecated
854 public static final int MAX_KEYCODE = 84;
855
856 /**
857 * {@link #getAction} value: the key has been pressed down.
858 */
859 public static final int ACTION_DOWN = 0;
860 /**
861 * {@link #getAction} value: the key has been released.
862 */
863 public static final int ACTION_UP = 1;
864 /**
865 * {@link #getAction} value: multiple duplicate key events have
866 * occurred in a row, or a complex string is being delivered. If the
867 * key code is not {#link {@link #KEYCODE_UNKNOWN} then the
868 * {#link {@link #getRepeatCount()} method returns the number of times
869 * the given key code should be executed.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700870 * Otherwise, if the key code is {@link #KEYCODE_UNKNOWN}, then
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 * this is a sequence of characters as returned by {@link #getCharacters}.
872 */
873 public static final int ACTION_MULTIPLE = 2;
874
875 /**
Jeff Brown497a92c2010-09-12 17:55:08 -0700876 * SHIFT key locked in CAPS mode.
877 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
878 * @hide
879 */
880 public static final int META_CAP_LOCKED = 0x100;
881
882 /**
883 * ALT key locked.
884 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
885 * @hide
886 */
887 public static final int META_ALT_LOCKED = 0x200;
888
889 /**
890 * SYM key locked.
891 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
892 * @hide
893 */
894 public static final int META_SYM_LOCKED = 0x400;
895
896 /**
897 * Text is in selection mode.
898 * Reserved for use by {@link MetaKeyKeyListener} for a private unpublished constant
899 * in its API that is currently being retained for legacy reasons.
900 * @hide
901 */
902 public static final int META_SELECTING = 0x800;
903
904 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 * <p>This mask is used to check whether one of the ALT meta keys is pressed.</p>
906 *
907 * @see #isAltPressed()
908 * @see #getMetaState()
909 * @see #KEYCODE_ALT_LEFT
910 * @see #KEYCODE_ALT_RIGHT
911 */
912 public static final int META_ALT_ON = 0x02;
913
914 /**
915 * <p>This mask is used to check whether the left ALT meta key is pressed.</p>
916 *
917 * @see #isAltPressed()
918 * @see #getMetaState()
919 * @see #KEYCODE_ALT_LEFT
920 */
921 public static final int META_ALT_LEFT_ON = 0x10;
922
923 /**
924 * <p>This mask is used to check whether the right the ALT meta key is pressed.</p>
925 *
926 * @see #isAltPressed()
927 * @see #getMetaState()
928 * @see #KEYCODE_ALT_RIGHT
929 */
930 public static final int META_ALT_RIGHT_ON = 0x20;
931
932 /**
933 * <p>This mask is used to check whether one of the SHIFT meta keys is pressed.</p>
934 *
935 * @see #isShiftPressed()
936 * @see #getMetaState()
937 * @see #KEYCODE_SHIFT_LEFT
938 * @see #KEYCODE_SHIFT_RIGHT
939 */
940 public static final int META_SHIFT_ON = 0x1;
941
942 /**
943 * <p>This mask is used to check whether the left SHIFT meta key is pressed.</p>
944 *
945 * @see #isShiftPressed()
946 * @see #getMetaState()
947 * @see #KEYCODE_SHIFT_LEFT
948 */
949 public static final int META_SHIFT_LEFT_ON = 0x40;
950
951 /**
952 * <p>This mask is used to check whether the right SHIFT meta key is pressed.</p>
953 *
954 * @see #isShiftPressed()
955 * @see #getMetaState()
956 * @see #KEYCODE_SHIFT_RIGHT
957 */
958 public static final int META_SHIFT_RIGHT_ON = 0x80;
959
960 /**
961 * <p>This mask is used to check whether the SYM meta key is pressed.</p>
962 *
963 * @see #isSymPressed()
964 * @see #getMetaState()
965 */
966 public static final int META_SYM_ON = 0x4;
967
968 /**
Jeff Brown497a92c2010-09-12 17:55:08 -0700969 * <p>This mask is used to check whether the FUNCTION meta key is pressed.</p>
970 *
971 * @see #isFunctionPressed()
972 * @see #getMetaState()
973 */
974 public static final int META_FUNCTION_ON = 0x8;
975
976 /**
977 * <p>This mask is used to check whether one of the CTRL meta keys is pressed.</p>
978 *
979 * @see #isCtrlPressed()
980 * @see #getMetaState()
981 * @see #KEYCODE_CTRL_LEFT
982 * @see #KEYCODE_CTRL_RIGHT
983 */
984 public static final int META_CTRL_ON = 0x1000;
985
986 /**
987 * <p>This mask is used to check whether the left CTRL meta key is pressed.</p>
988 *
989 * @see #isCtrlPressed()
990 * @see #getMetaState()
991 * @see #KEYCODE_CTRL_LEFT
992 */
993 public static final int META_CTRL_LEFT_ON = 0x2000;
994
995 /**
996 * <p>This mask is used to check whether the right CTRL meta key is pressed.</p>
997 *
998 * @see #isCtrlPressed()
999 * @see #getMetaState()
1000 * @see #KEYCODE_CTRL_RIGHT
1001 */
1002 public static final int META_CTRL_RIGHT_ON = 0x4000;
1003
1004 /**
1005 * <p>This mask is used to check whether one of the META meta keys is pressed.</p>
1006 *
1007 * @see #isMetaPressed()
1008 * @see #getMetaState()
1009 * @see #KEYCODE_META_LEFT
1010 * @see #KEYCODE_META_RIGHT
1011 */
1012 public static final int META_META_ON = 0x10000;
1013
1014 /**
1015 * <p>This mask is used to check whether the left META meta key is pressed.</p>
1016 *
1017 * @see #isMetaPressed()
1018 * @see #getMetaState()
1019 * @see #KEYCODE_META_LEFT
1020 */
1021 public static final int META_META_LEFT_ON = 0x20000;
1022
1023 /**
1024 * <p>This mask is used to check whether the right META meta key is pressed.</p>
1025 *
1026 * @see #isMetaPressed()
1027 * @see #getMetaState()
1028 * @see #KEYCODE_META_RIGHT
1029 */
1030 public static final int META_META_RIGHT_ON = 0x40000;
1031
1032 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07001033 * <p>This mask is used to check whether the CAPS LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07001034 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07001035 * @see #isCapsLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -07001036 * @see #getMetaState()
1037 * @see #KEYCODE_CAPS_LOCK
1038 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07001039 public static final int META_CAPS_LOCK_ON = 0x100000;
Jeff Brown497a92c2010-09-12 17:55:08 -07001040
1041 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07001042 * <p>This mask is used to check whether the NUM LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07001043 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07001044 * @see #isNumLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -07001045 * @see #getMetaState()
1046 * @see #KEYCODE_NUM_LOCK
1047 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07001048 public static final int META_NUM_LOCK_ON = 0x200000;
Jeff Brown497a92c2010-09-12 17:55:08 -07001049
1050 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07001051 * <p>This mask is used to check whether the SCROLL LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07001052 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07001053 * @see #isScrollLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -07001054 * @see #getMetaState()
1055 * @see #KEYCODE_SCROLL_LOCK
1056 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07001057 public static final int META_SCROLL_LOCK_ON = 0x400000;
Jeff Brown497a92c2010-09-12 17:55:08 -07001058
Jeff Brown64da12a2011-01-04 19:57:47 -08001059 /**
1060 * This mask is a combination of {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON}
1061 * and {@link #META_SHIFT_RIGHT_ON}.
1062 */
Jeff Brownc1df9072010-12-21 16:38:50 -08001063 public static final int META_SHIFT_MASK = META_SHIFT_ON
1064 | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON;
1065
Jeff Brown64da12a2011-01-04 19:57:47 -08001066 /**
1067 * This mask is a combination of {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON}
1068 * and {@link #META_ALT_RIGHT_ON}.
1069 */
Jeff Brownc1df9072010-12-21 16:38:50 -08001070 public static final int META_ALT_MASK = META_ALT_ON
1071 | META_ALT_LEFT_ON | META_ALT_RIGHT_ON;
1072
Jeff Brown64da12a2011-01-04 19:57:47 -08001073 /**
1074 * This mask is a combination of {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON}
1075 * and {@link #META_CTRL_RIGHT_ON}.
1076 */
Jeff Brownc1df9072010-12-21 16:38:50 -08001077 public static final int META_CTRL_MASK = META_CTRL_ON
1078 | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON;
1079
Jeff Brown64da12a2011-01-04 19:57:47 -08001080 /**
1081 * This mask is a combination of {@link #META_META_ON}, {@link #META_META_LEFT_ON}
1082 * and {@link #META_META_RIGHT_ON}.
1083 */
1084 public static final int META_META_MASK = META_META_ON
Jeff Brownc1df9072010-12-21 16:38:50 -08001085 | META_META_LEFT_ON | META_META_RIGHT_ON;
1086
Jeff Brown497a92c2010-09-12 17:55:08 -07001087 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 * This mask is set if the device woke because of this key event.
1089 */
1090 public static final int FLAG_WOKE_HERE = 0x1;
1091
1092 /**
1093 * This mask is set if the key event was generated by a software keyboard.
1094 */
1095 public static final int FLAG_SOFT_KEYBOARD = 0x2;
1096
1097 /**
1098 * This mask is set if we don't want the key event to cause us to leave
1099 * touch mode.
1100 */
1101 public static final int FLAG_KEEP_TOUCH_MODE = 0x4;
1102
1103 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001104 * This mask is set if an event was known to come from a trusted part
1105 * of the system. That is, the event is known to come from the user,
1106 * and could not have been spoofed by a third party component.
1107 */
1108 public static final int FLAG_FROM_SYSTEM = 0x8;
1109
1110 /**
1111 * This mask is used for compatibility, to identify enter keys that are
1112 * coming from an IME whose enter key has been auto-labelled "next" or
1113 * "done". This allows TextView to dispatch these as normal enter keys
1114 * for old applications, but still do the appropriate action when
1115 * receiving them.
1116 */
1117 public static final int FLAG_EDITOR_ACTION = 0x10;
1118
1119 /**
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07001120 * When associated with up key events, this indicates that the key press
1121 * has been canceled. Typically this is used with virtual touch screen
1122 * keys, where the user can slide from the virtual key area on to the
1123 * display: in that case, the application will receive a canceled up
1124 * event and should not perform the action normally associated with the
1125 * key. Note that for this to work, the application can not perform an
1126 * action for a key until it receives an up or the long press timeout has
1127 * expired.
1128 */
1129 public static final int FLAG_CANCELED = 0x20;
1130
1131 /**
1132 * This key event was generated by a virtual (on-screen) hard key area.
1133 * Typically this is an area of the touchscreen, outside of the regular
1134 * display, dedicated to "hardware" buttons.
1135 */
1136 public static final int FLAG_VIRTUAL_HARD_KEY = 0x40;
1137
1138 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001139 * This flag is set for the first key repeat that occurs after the
1140 * long press timeout.
1141 */
1142 public static final int FLAG_LONG_PRESS = 0x80;
1143
1144 /**
1145 * Set when a key event has {@link #FLAG_CANCELED} set because a long
1146 * press action was executed while it was down.
1147 */
1148 public static final int FLAG_CANCELED_LONG_PRESS = 0x100;
1149
1150 /**
1151 * Set for {@link #ACTION_UP} when this event's key code is still being
1152 * tracked from its initial down. That is, somebody requested that tracking
1153 * started on the key down and a long press has not caused
1154 * the tracking to be canceled.
1155 */
1156 public static final int FLAG_TRACKING = 0x200;
Jeff Brown49ed71d2010-12-06 17:13:33 -08001157
1158 /**
1159 * Set when a key event has been synthesized to implement default behavior
1160 * for an event that the application did not handle.
1161 * Fallback key events are generated by unhandled trackball motions
1162 * (to emulate a directional keypad) and by certain unhandled key presses
1163 * that are declared in the key map (such as special function numeric keypad
1164 * keys when numlock is off).
1165 */
1166 public static final int FLAG_FALLBACK = 0x400;
1167
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001168 /**
1169 * Private control to determine when an app is tracking a key sequence.
1170 * @hide
1171 */
1172 public static final int FLAG_START_TRACKING = 0x40000000;
Jeff Brown21bc5c92011-02-28 18:27:14 -08001173
1174 /**
1175 * Private flag that indicates when the system has detected that this key event
1176 * may be inconsistent with respect to the sequence of previously delivered key events,
1177 * such as when a key up event is sent but the key was not down.
1178 *
1179 * @hide
1180 * @see #isTainted
1181 * @see #setTainted
1182 */
1183 public static final int FLAG_TAINTED = 0x80000000;
1184
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001185 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 * Returns the maximum keycode.
1187 */
1188 public static int getMaxKeyCode() {
1189 return LAST_KEYCODE;
1190 }
1191
1192 /**
1193 * Get the character that is produced by putting accent on the character
1194 * c.
1195 * For example, getDeadChar('`', 'e') returns &egrave;.
1196 */
1197 public static int getDeadChar(int accent, int c) {
1198 return KeyCharacterMap.getDeadChar(accent, c);
1199 }
1200
Dianne Hackborn8d374262009-09-14 21:21:52 -07001201 static final boolean DEBUG = false;
1202 static final String TAG = "KeyEvent";
Jeff Brown1f245102010-11-18 20:53:46 -08001203
1204 private static final int MAX_RECYCLED = 10;
1205 private static final Object gRecyclerLock = new Object();
1206 private static int gRecyclerUsed;
1207 private static KeyEvent gRecyclerTop;
1208
1209 private KeyEvent mNext;
1210 private boolean mRecycled;
1211
Jeff Brown91c69ab2011-02-14 17:03:18 -08001212 private int mDeviceId;
1213 private int mSource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 private int mMetaState;
1215 private int mAction;
1216 private int mKeyCode;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001217 private int mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 private int mRepeatCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 private int mFlags;
1220 private long mDownTime;
1221 private long mEventTime;
1222 private String mCharacters;
1223
1224 public interface Callback {
1225 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001226 * Called when a key down event has occurred. If you return true,
1227 * you can first call {@link KeyEvent#startTracking()
1228 * KeyEvent.startTracking()} to have the framework track the event
1229 * through its {@link #onKeyUp(int, KeyEvent)} and also call your
1230 * {@link #onKeyLongPress(int, KeyEvent)} if it occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 *
1232 * @param keyCode The value in event.getKeyCode().
1233 * @param event Description of the key event.
1234 *
1235 * @return If you handled the event, return true. If you want to allow
1236 * the event to be handled by the next receiver, return false.
1237 */
1238 boolean onKeyDown(int keyCode, KeyEvent event);
1239
1240 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001241 * Called when a long press has occurred. If you return true,
1242 * the final key up will have {@link KeyEvent#FLAG_CANCELED} and
1243 * {@link KeyEvent#FLAG_CANCELED_LONG_PRESS} set. Note that in
1244 * order to receive this callback, someone in the event change
1245 * <em>must</em> return true from {@link #onKeyDown} <em>and</em>
1246 * call {@link KeyEvent#startTracking()} on the event.
1247 *
1248 * @param keyCode The value in event.getKeyCode().
1249 * @param event Description of the key event.
1250 *
1251 * @return If you handled the event, return true. If you want to allow
1252 * the event to be handled by the next receiver, return false.
1253 */
1254 boolean onKeyLongPress(int keyCode, KeyEvent event);
1255
1256 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 * Called when a key up event has occurred.
1258 *
1259 * @param keyCode The value in event.getKeyCode().
1260 * @param event Description of the key event.
1261 *
1262 * @return If you handled the event, return true. If you want to allow
1263 * the event to be handled by the next receiver, return false.
1264 */
1265 boolean onKeyUp(int keyCode, KeyEvent event);
1266
1267 /**
1268 * Called when multiple down/up pairs of the same key have occurred
1269 * in a row.
1270 *
1271 * @param keyCode The value in event.getKeyCode().
1272 * @param count Number of pairs as returned by event.getRepeatCount().
1273 * @param event Description of the key event.
1274 *
1275 * @return If you handled the event, return true. If you want to allow
1276 * the event to be handled by the next receiver, return false.
1277 */
1278 boolean onKeyMultiple(int keyCode, int count, KeyEvent event);
1279 }
1280
Jeff Brown497a92c2010-09-12 17:55:08 -07001281 static {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001282 populateKeycodeSymbolicNames();
Jeff Brown497a92c2010-09-12 17:55:08 -07001283 }
1284
Jeff Brown1f245102010-11-18 20:53:46 -08001285 private KeyEvent() {
1286 }
1287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 /**
1289 * Create a new key event.
1290 *
1291 * @param action Action code: either {@link #ACTION_DOWN},
1292 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1293 * @param code The key code.
1294 */
1295 public KeyEvent(int action, int code) {
1296 mAction = action;
1297 mKeyCode = code;
1298 mRepeatCount = 0;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001299 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 }
1301
1302 /**
1303 * Create a new key event.
1304 *
1305 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1306 * at which this key code originally went down.
1307 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1308 * at which this event happened.
1309 * @param action Action code: either {@link #ACTION_DOWN},
1310 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1311 * @param code The key code.
1312 * @param repeat A repeat count for down events (> 0 if this is after the
1313 * initial down) or event count for multiple events.
1314 */
1315 public KeyEvent(long downTime, long eventTime, int action,
1316 int code, int repeat) {
1317 mDownTime = downTime;
1318 mEventTime = eventTime;
1319 mAction = action;
1320 mKeyCode = code;
1321 mRepeatCount = repeat;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001322 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 }
1324
1325 /**
1326 * Create a new key event.
1327 *
1328 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1329 * at which this key code originally went down.
1330 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1331 * at which this event happened.
1332 * @param action Action code: either {@link #ACTION_DOWN},
1333 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1334 * @param code The key code.
1335 * @param repeat A repeat count for down events (> 0 if this is after the
1336 * initial down) or event count for multiple events.
1337 * @param metaState Flags indicating which meta keys are currently pressed.
1338 */
1339 public KeyEvent(long downTime, long eventTime, int action,
1340 int code, int repeat, int metaState) {
1341 mDownTime = downTime;
1342 mEventTime = eventTime;
1343 mAction = action;
1344 mKeyCode = code;
1345 mRepeatCount = repeat;
1346 mMetaState = metaState;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001347 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348 }
1349
1350 /**
1351 * Create a new key event.
1352 *
1353 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1354 * at which this key code originally went down.
1355 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1356 * at which this event happened.
1357 * @param action Action code: either {@link #ACTION_DOWN},
1358 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1359 * @param code The key code.
1360 * @param repeat A repeat count for down events (> 0 if this is after the
1361 * initial down) or event count for multiple events.
1362 * @param metaState Flags indicating which meta keys are currently pressed.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001363 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 * @param scancode Raw device scan code of the event.
1365 */
1366 public KeyEvent(long downTime, long eventTime, int action,
1367 int code, int repeat, int metaState,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001368 int deviceId, int scancode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 mDownTime = downTime;
1370 mEventTime = eventTime;
1371 mAction = action;
1372 mKeyCode = code;
1373 mRepeatCount = repeat;
1374 mMetaState = metaState;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001375 mDeviceId = deviceId;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001376 mScanCode = scancode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 }
1378
1379 /**
1380 * Create a new key event.
1381 *
1382 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1383 * at which this key code originally went down.
1384 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1385 * at which this event happened.
1386 * @param action Action code: either {@link #ACTION_DOWN},
1387 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1388 * @param code The key code.
1389 * @param repeat A repeat count for down events (> 0 if this is after the
1390 * initial down) or event count for multiple events.
1391 * @param metaState Flags indicating which meta keys are currently pressed.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001392 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 * @param scancode Raw device scan code of the event.
1394 * @param flags The flags for this key event
1395 */
1396 public KeyEvent(long downTime, long eventTime, int action,
1397 int code, int repeat, int metaState,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001398 int deviceId, int scancode, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 mDownTime = downTime;
1400 mEventTime = eventTime;
1401 mAction = action;
1402 mKeyCode = code;
1403 mRepeatCount = repeat;
1404 mMetaState = metaState;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001405 mDeviceId = deviceId;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001406 mScanCode = scancode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407 mFlags = flags;
1408 }
1409
1410 /**
Jeff Brownc5ed5912010-07-14 18:48:53 -07001411 * Create a new key event.
1412 *
1413 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1414 * at which this key code originally went down.
1415 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1416 * at which this event happened.
1417 * @param action Action code: either {@link #ACTION_DOWN},
1418 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1419 * @param code The key code.
1420 * @param repeat A repeat count for down events (> 0 if this is after the
1421 * initial down) or event count for multiple events.
1422 * @param metaState Flags indicating which meta keys are currently pressed.
1423 * @param deviceId The device ID that generated the key event.
1424 * @param scancode Raw device scan code of the event.
1425 * @param flags The flags for this key event
1426 * @param source The input source such as {@link InputDevice#SOURCE_KEYBOARD}.
1427 */
1428 public KeyEvent(long downTime, long eventTime, int action,
1429 int code, int repeat, int metaState,
1430 int deviceId, int scancode, int flags, int source) {
1431 mDownTime = downTime;
1432 mEventTime = eventTime;
1433 mAction = action;
1434 mKeyCode = code;
1435 mRepeatCount = repeat;
1436 mMetaState = metaState;
1437 mDeviceId = deviceId;
1438 mScanCode = scancode;
1439 mFlags = flags;
1440 mSource = source;
1441 }
1442
1443 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 * Create a new key event for a string of characters. The key code,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001445 * action, repeat count and source will automatically be set to
1446 * {@link #KEYCODE_UNKNOWN}, {@link #ACTION_MULTIPLE}, 0, and
1447 * {@link InputDevice#SOURCE_KEYBOARD} for you.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 *
1449 * @param time The time (in {@link android.os.SystemClock#uptimeMillis})
1450 * at which this event occured.
1451 * @param characters The string of characters.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001452 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 * @param flags The flags for this key event
1454 */
Jeff Brownc5ed5912010-07-14 18:48:53 -07001455 public KeyEvent(long time, String characters, int deviceId, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 mDownTime = time;
1457 mEventTime = time;
1458 mCharacters = characters;
1459 mAction = ACTION_MULTIPLE;
1460 mKeyCode = KEYCODE_UNKNOWN;
1461 mRepeatCount = 0;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001462 mDeviceId = deviceId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 mFlags = flags;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001464 mSource = InputDevice.SOURCE_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 }
1466
1467 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001468 * Make an exact copy of an existing key event.
1469 */
1470 public KeyEvent(KeyEvent origEvent) {
1471 mDownTime = origEvent.mDownTime;
1472 mEventTime = origEvent.mEventTime;
1473 mAction = origEvent.mAction;
1474 mKeyCode = origEvent.mKeyCode;
1475 mRepeatCount = origEvent.mRepeatCount;
1476 mMetaState = origEvent.mMetaState;
1477 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001478 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001479 mScanCode = origEvent.mScanCode;
The Android Open Source Project10592532009-03-18 17:39:46 -07001480 mFlags = origEvent.mFlags;
1481 mCharacters = origEvent.mCharacters;
1482 }
1483
1484 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 * Copy an existing key event, modifying its time and repeat count.
1486 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001487 * @deprecated Use {@link #changeTimeRepeat(KeyEvent, long, int)}
1488 * instead.
1489 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 * @param origEvent The existing event to be copied.
1491 * @param eventTime The new event time
1492 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1493 * @param newRepeat The new repeat count of the event.
1494 */
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001495 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496 public KeyEvent(KeyEvent origEvent, long eventTime, int newRepeat) {
1497 mDownTime = origEvent.mDownTime;
1498 mEventTime = eventTime;
1499 mAction = origEvent.mAction;
1500 mKeyCode = origEvent.mKeyCode;
1501 mRepeatCount = newRepeat;
1502 mMetaState = origEvent.mMetaState;
1503 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001504 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001505 mScanCode = origEvent.mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 mFlags = origEvent.mFlags;
1507 mCharacters = origEvent.mCharacters;
1508 }
1509
Jeff Brown1f245102010-11-18 20:53:46 -08001510 private static KeyEvent obtain() {
1511 final KeyEvent ev;
1512 synchronized (gRecyclerLock) {
1513 ev = gRecyclerTop;
1514 if (ev == null) {
1515 return new KeyEvent();
1516 }
1517 gRecyclerTop = ev.mNext;
1518 gRecyclerUsed -= 1;
1519 }
1520 ev.mRecycled = false;
1521 ev.mNext = null;
1522 return ev;
1523 }
1524
1525 /**
1526 * Obtains a (potentially recycled) key event.
1527 *
1528 * @hide
1529 */
1530 public static KeyEvent obtain(long downTime, long eventTime, int action,
1531 int code, int repeat, int metaState,
1532 int deviceId, int scancode, int flags, int source, String characters) {
1533 KeyEvent ev = obtain();
1534 ev.mDownTime = downTime;
1535 ev.mEventTime = eventTime;
1536 ev.mAction = action;
1537 ev.mKeyCode = code;
1538 ev.mRepeatCount = repeat;
1539 ev.mMetaState = metaState;
1540 ev.mDeviceId = deviceId;
1541 ev.mScanCode = scancode;
1542 ev.mFlags = flags;
1543 ev.mSource = source;
1544 ev.mCharacters = characters;
1545 return ev;
1546 }
1547
1548 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08001549 * Obtains a (potentially recycled) copy of another key event.
1550 *
1551 * @hide
1552 */
1553 public static KeyEvent obtain(KeyEvent other) {
1554 KeyEvent ev = obtain();
1555 ev.mDownTime = other.mDownTime;
1556 ev.mEventTime = other.mEventTime;
1557 ev.mAction = other.mAction;
1558 ev.mKeyCode = other.mKeyCode;
1559 ev.mRepeatCount = other.mRepeatCount;
1560 ev.mMetaState = other.mMetaState;
1561 ev.mDeviceId = other.mDeviceId;
1562 ev.mScanCode = other.mScanCode;
1563 ev.mFlags = other.mFlags;
1564 ev.mSource = other.mSource;
1565 ev.mCharacters = other.mCharacters;
1566 return ev;
1567 }
1568
1569 /** @hide */
1570 @Override
1571 public KeyEvent copy() {
1572 return obtain(this);
1573 }
1574
1575 /**
Jeff Brown1f245102010-11-18 20:53:46 -08001576 * Recycles a key event.
1577 * Key events should only be recycled if they are owned by the system since user
1578 * code expects them to be essentially immutable, "tracking" notwithstanding.
1579 *
1580 * @hide
1581 */
1582 public final void recycle() {
1583 if (mRecycled) {
1584 throw new RuntimeException(toString() + " recycled twice!");
1585 }
1586 mRecycled = true;
1587 mCharacters = null;
1588
1589 synchronized (gRecyclerLock) {
1590 if (gRecyclerUsed < MAX_RECYCLED) {
1591 gRecyclerUsed++;
1592 mNext = gRecyclerTop;
1593 gRecyclerTop = this;
1594 }
1595 }
1596 }
1597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001599 * Create a new key event that is the same as the given one, but whose
1600 * event time and repeat count are replaced with the given value.
1601 *
1602 * @param event The existing event to be copied. This is not modified.
1603 * @param eventTime The new event time
1604 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1605 * @param newRepeat The new repeat count of the event.
1606 */
1607 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime,
1608 int newRepeat) {
1609 return new KeyEvent(event, eventTime, newRepeat);
1610 }
1611
1612 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001613 * Create a new key event that is the same as the given one, but whose
1614 * event time and repeat count are replaced with the given value.
1615 *
1616 * @param event The existing event to be copied. This is not modified.
1617 * @param eventTime The new event time
1618 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1619 * @param newRepeat The new repeat count of the event.
1620 * @param newFlags New flags for the event, replacing the entire value
1621 * in the original event.
1622 */
1623 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime,
1624 int newRepeat, int newFlags) {
1625 KeyEvent ret = new KeyEvent(event);
1626 ret.mEventTime = eventTime;
1627 ret.mRepeatCount = newRepeat;
1628 ret.mFlags = newFlags;
1629 return ret;
1630 }
1631
1632 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 * Copy an existing key event, modifying its action.
1634 *
1635 * @param origEvent The existing event to be copied.
1636 * @param action The new action code of the event.
1637 */
The Android Open Source Project10592532009-03-18 17:39:46 -07001638 private KeyEvent(KeyEvent origEvent, int action) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 mDownTime = origEvent.mDownTime;
1640 mEventTime = origEvent.mEventTime;
1641 mAction = action;
1642 mKeyCode = origEvent.mKeyCode;
1643 mRepeatCount = origEvent.mRepeatCount;
1644 mMetaState = origEvent.mMetaState;
1645 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001646 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001647 mScanCode = origEvent.mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 mFlags = origEvent.mFlags;
1649 // Don't copy mCharacters, since one way or the other we'll lose it
1650 // when changing the action.
1651 }
1652
1653 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001654 * Create a new key event that is the same as the given one, but whose
1655 * action is replaced with the given value.
1656 *
1657 * @param event The existing event to be copied. This is not modified.
1658 * @param action The new action code of the event.
1659 */
1660 public static KeyEvent changeAction(KeyEvent event, int action) {
1661 return new KeyEvent(event, action);
1662 }
1663
1664 /**
1665 * Create a new key event that is the same as the given one, but whose
1666 * flags are replaced with the given value.
1667 *
1668 * @param event The existing event to be copied. This is not modified.
1669 * @param flags The new flags constant.
1670 */
1671 public static KeyEvent changeFlags(KeyEvent event, int flags) {
1672 event = new KeyEvent(event);
1673 event.mFlags = flags;
1674 return event;
1675 }
Jeff Brown21bc5c92011-02-28 18:27:14 -08001676
1677 /** @hide */
1678 @Override
1679 public final boolean isTainted() {
1680 return (mFlags & FLAG_TAINTED) != 0;
1681 }
1682
1683 /** @hide */
1684 @Override
1685 public final void setTainted(boolean tainted) {
1686 mFlags = tainted ? mFlags | FLAG_TAINTED : mFlags & ~FLAG_TAINTED;
1687 }
1688
The Android Open Source Project10592532009-03-18 17:39:46 -07001689 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690 * Don't use in new code, instead explicitly check
1691 * {@link #getAction()}.
1692 *
1693 * @return If the action is ACTION_DOWN, returns true; else false.
1694 *
1695 * @deprecated
1696 * @hide
1697 */
1698 @Deprecated public final boolean isDown() {
1699 return mAction == ACTION_DOWN;
1700 }
1701
1702 /**
1703 * Is this a system key? System keys can not be used for menu shortcuts.
1704 *
1705 * TODO: this information should come from a table somewhere.
1706 * TODO: should the dpad keys be here? arguably, because they also shouldn't be menu shortcuts
1707 */
1708 public final boolean isSystem() {
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -07001709 return native_isSystemKey(mKeyCode);
1710 }
1711
1712 /** @hide */
1713 public final boolean hasDefaultAction() {
1714 return native_hasDefaultAction(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 }
1716
Jeff Brown6f2fba42011-02-19 01:08:02 -08001717 /**
1718 * Returns true if the specified keycode is a gamepad button.
1719 * @return True if the keycode is a gamepad button, such as {@link #KEYCODE_BUTTON_A}.
1720 */
1721 public static final boolean isGamepadButton(int keyCode) {
1722 switch (keyCode) {
1723 case KeyEvent.KEYCODE_BUTTON_A:
1724 case KeyEvent.KEYCODE_BUTTON_B:
1725 case KeyEvent.KEYCODE_BUTTON_C:
1726 case KeyEvent.KEYCODE_BUTTON_X:
1727 case KeyEvent.KEYCODE_BUTTON_Y:
1728 case KeyEvent.KEYCODE_BUTTON_Z:
1729 case KeyEvent.KEYCODE_BUTTON_L1:
1730 case KeyEvent.KEYCODE_BUTTON_R1:
1731 case KeyEvent.KEYCODE_BUTTON_L2:
1732 case KeyEvent.KEYCODE_BUTTON_R2:
1733 case KeyEvent.KEYCODE_BUTTON_THUMBL:
1734 case KeyEvent.KEYCODE_BUTTON_THUMBR:
1735 case KeyEvent.KEYCODE_BUTTON_START:
1736 case KeyEvent.KEYCODE_BUTTON_SELECT:
1737 case KeyEvent.KEYCODE_BUTTON_MODE:
1738 case KeyEvent.KEYCODE_BUTTON_1:
1739 case KeyEvent.KEYCODE_BUTTON_2:
1740 case KeyEvent.KEYCODE_BUTTON_3:
1741 case KeyEvent.KEYCODE_BUTTON_4:
1742 case KeyEvent.KEYCODE_BUTTON_5:
1743 case KeyEvent.KEYCODE_BUTTON_6:
1744 case KeyEvent.KEYCODE_BUTTON_7:
1745 case KeyEvent.KEYCODE_BUTTON_8:
1746 case KeyEvent.KEYCODE_BUTTON_9:
1747 case KeyEvent.KEYCODE_BUTTON_10:
1748 case KeyEvent.KEYCODE_BUTTON_11:
1749 case KeyEvent.KEYCODE_BUTTON_12:
1750 case KeyEvent.KEYCODE_BUTTON_13:
1751 case KeyEvent.KEYCODE_BUTTON_14:
1752 case KeyEvent.KEYCODE_BUTTON_15:
1753 case KeyEvent.KEYCODE_BUTTON_16:
1754 return true;
1755 default:
1756 return false;
1757 }
1758 }
1759
Jeff Brown91c69ab2011-02-14 17:03:18 -08001760 /** {@inheritDoc} */
1761 @Override
1762 public final int getDeviceId() {
1763 return mDeviceId;
1764 }
1765
1766 /** {@inheritDoc} */
1767 @Override
1768 public final int getSource() {
1769 return mSource;
1770 }
1771
1772 /** {@inheritDoc} */
1773 @Override
1774 public final void setSource(int source) {
1775 mSource = source;
1776 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777
1778 /**
1779 * <p>Returns the state of the meta keys.</p>
1780 *
1781 * @return an integer in which each bit set to 1 represents a pressed
1782 * meta key
1783 *
1784 * @see #isAltPressed()
1785 * @see #isShiftPressed()
1786 * @see #isSymPressed()
Jeff Brown497a92c2010-09-12 17:55:08 -07001787 * @see #isCtrlPressed()
1788 * @see #isMetaPressed()
1789 * @see #isFunctionPressed()
Jeff Brown51e7fe72010-10-29 22:19:53 -07001790 * @see #isCapsLockOn()
1791 * @see #isNumLockOn()
1792 * @see #isScrollLockOn()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 * @see #META_ALT_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001794 * @see #META_ALT_LEFT_ON
1795 * @see #META_ALT_RIGHT_ON
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796 * @see #META_SHIFT_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001797 * @see #META_SHIFT_LEFT_ON
1798 * @see #META_SHIFT_RIGHT_ON
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 * @see #META_SYM_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001800 * @see #META_FUNCTION_ON
1801 * @see #META_CTRL_ON
1802 * @see #META_CTRL_LEFT_ON
1803 * @see #META_CTRL_RIGHT_ON
1804 * @see #META_META_ON
1805 * @see #META_META_LEFT_ON
1806 * @see #META_META_RIGHT_ON
Jeff Brown51e7fe72010-10-29 22:19:53 -07001807 * @see #META_CAPS_LOCK_ON
1808 * @see #META_NUM_LOCK_ON
1809 * @see #META_SCROLL_LOCK_ON
Jeff Brown54875002011-04-06 15:33:01 -07001810 * @see #getModifiers
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 */
1812 public final int getMetaState() {
1813 return mMetaState;
1814 }
1815
1816 /**
Jeff Brown54875002011-04-06 15:33:01 -07001817 * Returns the state of the modifier keys.
1818 * <p>
1819 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1820 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1821 * not considered modifier keys. Consequently, this function specifically masks out
1822 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1823 * </p><p>
1824 * The value returned consists of the meta state (from {@link #getMetaState})
1825 * normalized using {@link #normalizeMetaState(int)} and then masked with
1826 * {@link #getModifierMetaStateMask} so that only valid modifier bits are retained.
1827 * </p>
1828 *
1829 * @return An integer in which each bit set to 1 represents a pressed modifier key.
1830 * @see #getMetaState
1831 */
1832 public final int getModifiers() {
1833 return normalizeMetaState(mMetaState) & META_MODIFIER_MASK;
1834 }
1835
1836 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 * Returns the flags for this key event.
1838 *
1839 * @see #FLAG_WOKE_HERE
1840 */
1841 public final int getFlags() {
1842 return mFlags;
1843 }
1844
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001845 // Mask of all modifier key meta states. Specifically excludes locked keys like caps lock.
1846 private static final int META_MODIFIER_MASK =
1847 META_SHIFT_ON | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON
1848 | META_ALT_ON | META_ALT_LEFT_ON | META_ALT_RIGHT_ON
1849 | META_CTRL_ON | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON
1850 | META_META_ON | META_META_LEFT_ON | META_META_RIGHT_ON
1851 | META_SYM_ON | META_FUNCTION_ON;
1852
1853 // Mask of all lock key meta states.
1854 private static final int META_LOCK_MASK =
1855 META_CAPS_LOCK_ON | META_NUM_LOCK_ON | META_SCROLL_LOCK_ON;
1856
1857 // Mask of all valid meta states.
1858 private static final int META_ALL_MASK = META_MODIFIER_MASK | META_LOCK_MASK;
1859
1860 // Mask of all synthetic meta states that are reserved for API compatibility with
1861 // historical uses in MetaKeyKeyListener.
1862 private static final int META_SYNTHETIC_MASK =
1863 META_CAP_LOCKED | META_ALT_LOCKED | META_SYM_LOCKED | META_SELECTING;
1864
1865 // Mask of all meta states that are not valid use in specifying a modifier key.
1866 // These bits are known to be used for purposes other than specifying modifiers.
1867 private static final int META_INVALID_MODIFIER_MASK =
1868 META_LOCK_MASK | META_SYNTHETIC_MASK;
1869
1870 /**
1871 * Gets a mask that includes all valid modifier key meta state bits.
1872 * <p>
1873 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1874 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1875 * not considered modifier keys. Consequently, the mask specifically excludes
1876 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1877 * </p>
1878 *
1879 * @return The modifier meta state mask which is a combination of
1880 * {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON}, {@link #META_SHIFT_RIGHT_ON},
1881 * {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON}, {@link #META_ALT_RIGHT_ON},
1882 * {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON}, {@link #META_CTRL_RIGHT_ON},
1883 * {@link #META_META_ON}, {@link #META_META_LEFT_ON}, {@link #META_META_RIGHT_ON},
1884 * {@link #META_SYM_ON}, {@link #META_FUNCTION_ON}.
1885 */
1886 public static int getModifierMetaStateMask() {
1887 return META_MODIFIER_MASK;
1888 }
1889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 /**
1891 * Returns true if this key code is a modifier key.
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001892 * <p>
1893 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1894 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1895 * not considered modifier keys. Consequently, this function return false
1896 * for those keys.
1897 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 *
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001899 * @return True if the key code is one of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 * {@link #KEYCODE_SHIFT_LEFT} {@link #KEYCODE_SHIFT_RIGHT},
Jeff Brown497a92c2010-09-12 17:55:08 -07001901 * {@link #KEYCODE_ALT_LEFT}, {@link #KEYCODE_ALT_RIGHT},
Jeff Brown497a92c2010-09-12 17:55:08 -07001902 * {@link #KEYCODE_CTRL_LEFT}, {@link #KEYCODE_CTRL_RIGHT},
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001903 * {@link #KEYCODE_META_LEFT}, or {@link #KEYCODE_META_RIGHT},
1904 * {@link #KEYCODE_SYM}, {@link #KEYCODE_NUM}, {@link #KEYCODE_FUNCTION}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 */
1906 public static boolean isModifierKey(int keyCode) {
Jeff Brown497a92c2010-09-12 17:55:08 -07001907 switch (keyCode) {
1908 case KEYCODE_SHIFT_LEFT:
1909 case KEYCODE_SHIFT_RIGHT:
1910 case KEYCODE_ALT_LEFT:
1911 case KEYCODE_ALT_RIGHT:
Jeff Brown497a92c2010-09-12 17:55:08 -07001912 case KEYCODE_CTRL_LEFT:
1913 case KEYCODE_CTRL_RIGHT:
1914 case KEYCODE_META_LEFT:
1915 case KEYCODE_META_RIGHT:
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001916 case KEYCODE_SYM:
1917 case KEYCODE_NUM:
1918 case KEYCODE_FUNCTION:
Jeff Brown497a92c2010-09-12 17:55:08 -07001919 return true;
1920 default:
1921 return false;
1922 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 }
1924
1925 /**
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001926 * Normalizes the specified meta state.
1927 * <p>
1928 * The meta state is normalized such that if either the left or right modifier meta state
1929 * bits are set then the result will also include the universal bit for that modifier.
1930 * </p><p>
1931 * If the specified meta state contains {@link #META_ALT_LEFT_ON} then
1932 * the result will also contain {@link #META_ALT_ON} in addition to {@link #META_ALT_LEFT_ON}
1933 * and the other bits that were specified in the input. The same is process is
1934 * performed for shift, control and meta.
1935 * </p><p>
1936 * If the specified meta state contains synthetic meta states defined by
1937 * {@link MetaKeyKeyListener}, then those states are translated here and the original
1938 * synthetic meta states are removed from the result.
1939 * {@link MetaKeyKeyListener#META_CAP_LOCKED} is translated to {@link #META_CAPS_LOCK_ON}.
1940 * {@link MetaKeyKeyListener#META_ALT_LOCKED} is translated to {@link #META_ALT_ON}.
1941 * {@link MetaKeyKeyListener#META_SYM_LOCKED} is translated to {@link #META_SYM_ON}.
1942 * </p><p>
1943 * Undefined meta state bits are removed.
1944 * </p>
1945 *
1946 * @param metaState The meta state.
1947 * @return The normalized meta state.
1948 */
1949 public static int normalizeMetaState(int metaState) {
1950 if ((metaState & (META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON)) != 0) {
1951 metaState |= META_SHIFT_ON;
1952 }
1953 if ((metaState & (META_ALT_LEFT_ON | META_ALT_RIGHT_ON)) != 0) {
1954 metaState |= META_ALT_ON;
1955 }
1956 if ((metaState & (META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON)) != 0) {
1957 metaState |= META_CTRL_ON;
1958 }
1959 if ((metaState & (META_META_LEFT_ON | META_META_RIGHT_ON)) != 0) {
1960 metaState |= META_META_ON;
1961 }
1962 if ((metaState & MetaKeyKeyListener.META_CAP_LOCKED) != 0) {
1963 metaState |= META_CAPS_LOCK_ON;
1964 }
1965 if ((metaState & MetaKeyKeyListener.META_ALT_LOCKED) != 0) {
1966 metaState |= META_ALT_ON;
1967 }
1968 if ((metaState & MetaKeyKeyListener.META_SYM_LOCKED) != 0) {
1969 metaState |= META_SYM_ON;
1970 }
1971 return metaState & META_ALL_MASK;
1972 }
1973
1974 /**
1975 * Returns true if no modifiers keys are pressed according to the specified meta state.
1976 * <p>
1977 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1978 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1979 * not considered modifier keys. Consequently, this function ignores
1980 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1981 * </p><p>
1982 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}.
1983 * </p>
1984 *
1985 * @param metaState The meta state to consider.
1986 * @return True if no modifier keys are pressed.
1987 * @see #hasNoModifiers()
1988 */
1989 public static boolean metaStateHasNoModifiers(int metaState) {
1990 return (normalizeMetaState(metaState) & META_MODIFIER_MASK) == 0;
1991 }
1992
1993 /**
1994 * Returns true if only the specified modifier keys are pressed according to
1995 * the specified meta state. Returns false if a different combination of modifier
1996 * keys are pressed.
1997 * <p>
1998 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1999 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2000 * not considered modifier keys. Consequently, this function ignores
2001 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2002 * </p><p>
2003 * If the specified modifier mask includes directional modifiers, such as
2004 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the
2005 * modifier is pressed on that side.
2006 * If the specified modifier mask includes non-directional modifiers, such as
2007 * {@link #META_SHIFT_ON}, then this method ensures that the modifier
2008 * is pressed on either side.
2009 * If the specified modifier mask includes both directional and non-directional modifiers
2010 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON},
2011 * then this method throws an illegal argument exception.
2012 * </p>
2013 *
2014 * @param metaState The meta state to consider.
2015 * @param modifiers The meta state of the modifier keys to check. May be a combination
2016 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to
2017 * ensure that no modifier keys are pressed.
2018 * @return True if only the specified modifier keys are pressed.
2019 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers
2020 * @see #hasModifiers
2021 */
2022 public static boolean metaStateHasModifiers(int metaState, int modifiers) {
2023 // Note: For forward compatibility, we allow the parameter to contain meta states
2024 // that we do not recognize but we explicitly disallow meta states that
2025 // are not valid modifiers.
2026 if ((modifiers & META_INVALID_MODIFIER_MASK) != 0) {
2027 throw new IllegalArgumentException("modifiers must not contain "
2028 + "META_CAPS_LOCK_ON, META_NUM_LOCK_ON, META_SCROLL_LOCK_ON, "
2029 + "META_CAP_LOCKED, META_ALT_LOCKED, META_SYM_LOCKED, "
2030 + "or META_SELECTING");
2031 }
2032
2033 metaState = normalizeMetaState(metaState) & META_MODIFIER_MASK;
2034 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2035 META_SHIFT_ON, META_SHIFT_LEFT_ON, META_SHIFT_RIGHT_ON);
2036 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2037 META_ALT_ON, META_ALT_LEFT_ON, META_ALT_RIGHT_ON);
2038 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2039 META_CTRL_ON, META_CTRL_LEFT_ON, META_CTRL_RIGHT_ON);
2040 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2041 META_META_ON, META_META_LEFT_ON, META_META_RIGHT_ON);
2042 return metaState == modifiers;
2043 }
2044
2045 private static int metaStateFilterDirectionalModifiers(int metaState,
2046 int modifiers, int basic, int left, int right) {
2047 final boolean wantBasic = (modifiers & basic) != 0;
2048 final int directional = left | right;
2049 final boolean wantLeftOrRight = (modifiers & directional) != 0;
2050
2051 if (wantBasic) {
2052 if (wantLeftOrRight) {
2053 throw new IllegalArgumentException("modifiers must not contain "
2054 + metaStateToString(basic) + " combined with "
2055 + metaStateToString(left) + " or " + metaStateToString(right));
2056 }
2057 return metaState & ~directional;
2058 } else if (wantLeftOrRight) {
2059 return metaState & ~basic;
2060 } else {
2061 return metaState;
2062 }
2063 }
2064
2065 /**
2066 * Returns true if no modifier keys are pressed.
2067 * <p>
2068 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2069 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2070 * not considered modifier keys. Consequently, this function ignores
2071 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2072 * </p><p>
2073 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}.
2074 * </p>
2075 *
2076 * @return True if no modifier keys are pressed.
2077 * @see #metaStateHasNoModifiers
2078 */
2079 public final boolean hasNoModifiers() {
2080 return metaStateHasNoModifiers(mMetaState);
2081 }
2082
2083 /**
2084 * Returns true if only the specified modifiers keys are pressed.
2085 * Returns false if a different combination of modifier keys are pressed.
2086 * <p>
2087 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2088 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2089 * not considered modifier keys. Consequently, this function ignores
2090 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2091 * </p><p>
2092 * If the specified modifier mask includes directional modifiers, such as
2093 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the
2094 * modifier is pressed on that side.
2095 * If the specified modifier mask includes non-directional modifiers, such as
2096 * {@link #META_SHIFT_ON}, then this method ensures that the modifier
2097 * is pressed on either side.
2098 * If the specified modifier mask includes both directional and non-directional modifiers
2099 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON},
2100 * then this method throws an illegal argument exception.
2101 * </p>
2102 *
2103 * @param modifiers The meta state of the modifier keys to check. May be a combination
2104 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to
2105 * ensure that no modifier keys are pressed.
2106 * @return True if only the specified modifier keys are pressed.
2107 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers
2108 * @see #metaStateHasModifiers
2109 */
2110 public final boolean hasModifiers(int modifiers) {
2111 return metaStateHasModifiers(mMetaState, modifiers);
2112 }
2113
2114 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115 * <p>Returns the pressed state of the ALT meta key.</p>
2116 *
2117 * @return true if the ALT key is pressed, false otherwise
2118 *
2119 * @see #KEYCODE_ALT_LEFT
2120 * @see #KEYCODE_ALT_RIGHT
2121 * @see #META_ALT_ON
2122 */
2123 public final boolean isAltPressed() {
2124 return (mMetaState & META_ALT_ON) != 0;
2125 }
2126
2127 /**
2128 * <p>Returns the pressed state of the SHIFT meta key.</p>
2129 *
2130 * @return true if the SHIFT key is pressed, false otherwise
2131 *
2132 * @see #KEYCODE_SHIFT_LEFT
2133 * @see #KEYCODE_SHIFT_RIGHT
2134 * @see #META_SHIFT_ON
2135 */
2136 public final boolean isShiftPressed() {
2137 return (mMetaState & META_SHIFT_ON) != 0;
2138 }
2139
2140 /**
2141 * <p>Returns the pressed state of the SYM meta key.</p>
2142 *
2143 * @return true if the SYM key is pressed, false otherwise
2144 *
2145 * @see #KEYCODE_SYM
2146 * @see #META_SYM_ON
2147 */
2148 public final boolean isSymPressed() {
2149 return (mMetaState & META_SYM_ON) != 0;
2150 }
2151
2152 /**
Jeff Brown497a92c2010-09-12 17:55:08 -07002153 * <p>Returns the pressed state of the CTRL meta key.</p>
2154 *
2155 * @return true if the CTRL key is pressed, false otherwise
2156 *
2157 * @see #KEYCODE_CTRL_LEFT
2158 * @see #KEYCODE_CTRL_RIGHT
2159 * @see #META_CTRL_ON
2160 */
2161 public final boolean isCtrlPressed() {
2162 return (mMetaState & META_CTRL_ON) != 0;
2163 }
2164
2165 /**
2166 * <p>Returns the pressed state of the META meta key.</p>
2167 *
2168 * @return true if the META key is pressed, false otherwise
2169 *
2170 * @see #KEYCODE_META_LEFT
2171 * @see #KEYCODE_META_RIGHT
2172 * @see #META_META_ON
2173 */
2174 public final boolean isMetaPressed() {
2175 return (mMetaState & META_META_ON) != 0;
2176 }
2177
2178 /**
2179 * <p>Returns the pressed state of the FUNCTION meta key.</p>
2180 *
2181 * @return true if the FUNCTION key is pressed, false otherwise
2182 *
2183 * @see #KEYCODE_FUNCTION
2184 * @see #META_FUNCTION_ON
2185 */
2186 public final boolean isFunctionPressed() {
2187 return (mMetaState & META_FUNCTION_ON) != 0;
2188 }
2189
2190 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07002191 * <p>Returns the locked state of the CAPS LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002192 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002193 * @return true if the CAPS LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002194 *
2195 * @see #KEYCODE_CAPS_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002196 * @see #META_CAPS_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002197 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002198 public final boolean isCapsLockOn() {
2199 return (mMetaState & META_CAPS_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002200 }
2201
2202 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07002203 * <p>Returns the locked state of the NUM LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002204 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002205 * @return true if the NUM LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002206 *
2207 * @see #KEYCODE_NUM_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002208 * @see #META_NUM_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002209 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002210 public final boolean isNumLockOn() {
2211 return (mMetaState & META_NUM_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002212 }
2213
2214 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07002215 * <p>Returns the locked state of the SCROLL LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002216 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002217 * @return true if the SCROLL LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002218 *
2219 * @see #KEYCODE_SCROLL_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002220 * @see #META_SCROLL_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002221 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002222 public final boolean isScrollLockOn() {
2223 return (mMetaState & META_SCROLL_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002224 }
2225
2226 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 * Retrieve the action of this key event. May be either
2228 * {@link #ACTION_DOWN}, {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
2229 *
2230 * @return The event action: ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.
2231 */
2232 public final int getAction() {
2233 return mAction;
2234 }
2235
2236 /**
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07002237 * For {@link #ACTION_UP} events, indicates that the event has been
2238 * canceled as per {@link #FLAG_CANCELED}.
2239 */
2240 public final boolean isCanceled() {
2241 return (mFlags&FLAG_CANCELED) != 0;
2242 }
2243
2244 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002245 * Call this during {@link Callback#onKeyDown} to have the system track
2246 * the key through its final up (possibly including a long press). Note
2247 * that only one key can be tracked at a time -- if another key down
2248 * event is received while a previous one is being tracked, tracking is
2249 * stopped on the previous event.
2250 */
2251 public final void startTracking() {
2252 mFlags |= FLAG_START_TRACKING;
2253 }
2254
2255 /**
2256 * For {@link #ACTION_UP} events, indicates that the event is still being
2257 * tracked from its initial down event as per
2258 * {@link #FLAG_TRACKING}.
2259 */
2260 public final boolean isTracking() {
2261 return (mFlags&FLAG_TRACKING) != 0;
2262 }
2263
2264 /**
2265 * For {@link #ACTION_DOWN} events, indicates that the event has been
2266 * canceled as per {@link #FLAG_LONG_PRESS}.
2267 */
2268 public final boolean isLongPress() {
2269 return (mFlags&FLAG_LONG_PRESS) != 0;
2270 }
2271
2272 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273 * Retrieve the key code of the key event. This is the physical key that
2274 * was pressed, <em>not</em> the Unicode character.
2275 *
2276 * @return The key code of the event.
2277 */
2278 public final int getKeyCode() {
2279 return mKeyCode;
2280 }
2281
2282 /**
2283 * For the special case of a {@link #ACTION_MULTIPLE} event with key
2284 * code of {@link #KEYCODE_UNKNOWN}, this is a raw string of characters
2285 * associated with the event. In all other cases it is null.
2286 *
2287 * @return Returns a String of 1 or more characters associated with
2288 * the event.
2289 */
2290 public final String getCharacters() {
2291 return mCharacters;
2292 }
2293
2294 /**
2295 * Retrieve the hardware key id of this key event. These values are not
2296 * reliable and vary from device to device.
2297 *
2298 * {@more}
2299 * Mostly this is here for debugging purposes.
2300 */
2301 public final int getScanCode() {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002302 return mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002303 }
2304
2305 /**
2306 * Retrieve the repeat count of the event. For both key up and key down
2307 * events, this is the number of times the key has repeated with the first
2308 * down starting at 0 and counting up from there. For multiple key
2309 * events, this is the number of down/up pairs that have occurred.
2310 *
2311 * @return The number of times the key has repeated.
2312 */
2313 public final int getRepeatCount() {
2314 return mRepeatCount;
2315 }
2316
2317 /**
2318 * Retrieve the time of the most recent key down event,
2319 * in the {@link android.os.SystemClock#uptimeMillis} time base. If this
2320 * is a down event, this will be the same as {@link #getEventTime()}.
2321 * Note that when chording keys, this value is the down time of the
2322 * most recently pressed key, which may <em>not</em> be the same physical
2323 * key of this event.
2324 *
2325 * @return Returns the most recent key down time, in the
2326 * {@link android.os.SystemClock#uptimeMillis} time base
2327 */
2328 public final long getDownTime() {
2329 return mDownTime;
2330 }
2331
2332 /**
2333 * Retrieve the time this event occurred,
2334 * in the {@link android.os.SystemClock#uptimeMillis} time base.
2335 *
2336 * @return Returns the time this event occurred,
2337 * in the {@link android.os.SystemClock#uptimeMillis} time base.
2338 */
2339 public final long getEventTime() {
2340 return mEventTime;
2341 }
2342
Jeff Brown4e91a182011-04-07 11:38:09 -07002343 /** @hide */
2344 @Override
2345 public final long getEventTimeNano() {
2346 return mEventTime * 1000000L;
2347 }
2348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002350 * Renamed to {@link #getDeviceId}.
2351 *
2352 * @hide
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002353 * @deprecated use {@link #getDeviceId()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002354 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002355 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 public final int getKeyboardDevice() {
2357 return mDeviceId;
2358 }
2359
2360 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002361 * Gets the {@link KeyCharacterMap} associated with the keyboard device.
2362 *
2363 * @return The associated key character map.
Jeff Brown7e1e21f2011-01-19 17:05:01 -08002364 * @throws {@link UnavailableException} if the key character map
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002365 * could not be loaded because it was malformed or the default key character map
2366 * is missing from the system.
2367 *
2368 * @see {@link KeyCharacterMap#load}
2369 */
2370 public final KeyCharacterMap getKeyCharacterMap() {
2371 return KeyCharacterMap.load(mDeviceId);
2372 }
2373
2374 /**
2375 * Gets the primary character for this key.
2376 * In other words, the label that is physically printed on it.
2377 *
2378 * @return The display label character, or 0 if none (eg. for non-printing keys).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379 */
2380 public char getDisplayLabel() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002381 return getKeyCharacterMap().getDisplayLabel(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382 }
2383
2384 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002385 * Gets the Unicode character generated by the specified key and meta
2386 * key state combination.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002387 * <p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002388 * Returns the Unicode character that the specified key would produce
2389 * when the specified meta bits (see {@link MetaKeyKeyListener})
2390 * were active.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002391 * </p><p>
2392 * Returns 0 if the key is not one that is used to type Unicode
2393 * characters.
2394 * </p><p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002395 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the
2396 * key is a "dead key" that should be combined with another to
2397 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} --
2398 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399 * </p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002400 *
2401 * @return The associated character or combining accent, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 */
2403 public int getUnicodeChar() {
2404 return getUnicodeChar(mMetaState);
2405 }
2406
2407 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002408 * Gets the Unicode character generated by the specified key and meta
2409 * key state combination.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 * <p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002411 * Returns the Unicode character that the specified key would produce
2412 * when the specified meta bits (see {@link MetaKeyKeyListener})
2413 * were active.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002414 * </p><p>
2415 * Returns 0 if the key is not one that is used to type Unicode
2416 * characters.
2417 * </p><p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002418 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the
2419 * key is a "dead key" that should be combined with another to
2420 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} --
2421 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002422 * </p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002423 *
2424 * @param metaState The meta key modifier state.
2425 * @return The associated character or combining accent, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002426 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002427 public int getUnicodeChar(int metaState) {
2428 return getKeyCharacterMap().get(mKeyCode, metaState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 }
2430
2431 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002432 * Get the character conversion data for a given key code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002433 *
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002434 * @param results A {@link KeyCharacterMap.KeyData} instance that will be
2435 * filled with the results.
2436 * @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 -08002437 *
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002438 * @deprecated instead use {@link #getDisplayLabel()},
2439 * {@link #getNumber()} or {@link #getUnicodeChar(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002440 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002441 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442 public boolean getKeyData(KeyData results) {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002443 return getKeyCharacterMap().getKeyData(mKeyCode, results);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 }
2445
2446 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002447 * Gets the first character in the character array that can be generated
2448 * by the specified key code.
2449 * <p>
2450 * This is a convenience function that returns the same value as
2451 * {@link #getMatch(char[],int) getMatch(chars, 0)}.
2452 * </p>
2453 *
2454 * @param chars The array of matching characters to consider.
2455 * @return The matching associated character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456 */
2457 public char getMatch(char[] chars) {
2458 return getMatch(chars, 0);
2459 }
2460
2461 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002462 * Gets the first character in the character array that can be generated
2463 * by the specified key code. If there are multiple choices, prefers
2464 * the one that would be generated with the specified meta key modifier state.
2465 *
2466 * @param chars The array of matching characters to consider.
2467 * @param metaState The preferred meta key modifier state.
2468 * @return The matching associated character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002469 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002470 public char getMatch(char[] chars, int metaState) {
2471 return getKeyCharacterMap().getMatch(mKeyCode, chars, metaState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002472 }
2473
2474 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002475 * Gets the number or symbol associated with the key.
2476 * <p>
2477 * The character value is returned, not the numeric value.
2478 * If the key is not a number, but is a symbol, the symbol is retuned.
2479 * </p><p>
2480 * This method is intended to to support dial pads and other numeric or
2481 * symbolic entry on keyboards where certain keys serve dual function
2482 * as alphabetic and symbolic keys. This method returns the number
2483 * or symbol associated with the key independent of whether the user
2484 * has pressed the required modifier.
2485 * </p><p>
2486 * For example, on one particular keyboard the keys on the top QWERTY row generate
2487 * numbers when ALT is pressed such that ALT-Q maps to '1'. So for that keyboard
2488 * when {@link #getNumber} is called with {@link KeyEvent#KEYCODE_Q} it returns '1'
2489 * so that the user can type numbers without pressing ALT when it makes sense.
2490 * </p>
2491 *
2492 * @return The associated numeric or symbolic character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 */
2494 public char getNumber() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002495 return getKeyCharacterMap().getNumber(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002496 }
2497
2498 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002499 * Returns true if this key produces a glyph.
2500 *
2501 * @return True if the key is a printing key.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002502 */
2503 public boolean isPrintingKey() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002504 return getKeyCharacterMap().isPrintingKey(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505 }
2506
2507 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002508 * @deprecated Use {@link #dispatch(Callback, DispatcherState, Object)} instead.
2509 */
2510 @Deprecated
2511 public final boolean dispatch(Callback receiver) {
2512 return dispatch(receiver, null, null);
2513 }
2514
2515 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516 * Deliver this key event to a {@link Callback} interface. If this is
2517 * an ACTION_MULTIPLE event and it is not handled, then an attempt will
2518 * be made to deliver a single normal event.
2519 *
2520 * @param receiver The Callback that will be given the event.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002521 * @param state State information retained across events.
2522 * @param target The target of the dispatch, for use in tracking.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002523 *
2524 * @return The return value from the Callback method that was called.
2525 */
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002526 public final boolean dispatch(Callback receiver, DispatcherState state,
2527 Object target) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 switch (mAction) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002529 case ACTION_DOWN: {
2530 mFlags &= ~FLAG_START_TRACKING;
Dianne Hackborn8d374262009-09-14 21:21:52 -07002531 if (DEBUG) Log.v(TAG, "Key down to " + target + " in " + state
2532 + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002533 boolean res = receiver.onKeyDown(mKeyCode, this);
2534 if (state != null) {
2535 if (res && mRepeatCount == 0 && (mFlags&FLAG_START_TRACKING) != 0) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002536 if (DEBUG) Log.v(TAG, " Start tracking!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002537 state.startTracking(this, target);
2538 } else if (isLongPress() && state.isTracking(this)) {
2539 try {
2540 if (receiver.onKeyLongPress(mKeyCode, this)) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002541 if (DEBUG) Log.v(TAG, " Clear from long press!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002542 state.performedLongPress(this);
2543 res = true;
2544 }
2545 } catch (AbstractMethodError e) {
2546 }
2547 }
2548 }
2549 return res;
2550 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551 case ACTION_UP:
Dianne Hackborn8d374262009-09-14 21:21:52 -07002552 if (DEBUG) Log.v(TAG, "Key up to " + target + " in " + state
2553 + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002554 if (state != null) {
2555 state.handleUpEvent(this);
2556 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002557 return receiver.onKeyUp(mKeyCode, this);
2558 case ACTION_MULTIPLE:
2559 final int count = mRepeatCount;
2560 final int code = mKeyCode;
2561 if (receiver.onKeyMultiple(code, count, this)) {
2562 return true;
2563 }
2564 if (code != KeyEvent.KEYCODE_UNKNOWN) {
2565 mAction = ACTION_DOWN;
2566 mRepeatCount = 0;
2567 boolean handled = receiver.onKeyDown(code, this);
2568 if (handled) {
2569 mAction = ACTION_UP;
2570 receiver.onKeyUp(code, this);
2571 }
2572 mAction = ACTION_MULTIPLE;
2573 mRepeatCount = count;
2574 return handled;
2575 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002576 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002577 }
2578 return false;
2579 }
2580
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002581 /**
2582 * Use with {@link KeyEvent#dispatch(Callback, DispatcherState, Object)}
2583 * for more advanced key dispatching, such as long presses.
2584 */
2585 public static class DispatcherState {
2586 int mDownKeyCode;
2587 Object mDownTarget;
2588 SparseIntArray mActiveLongPresses = new SparseIntArray();
2589
2590 /**
2591 * Reset back to initial state.
2592 */
2593 public void reset() {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002594 if (DEBUG) Log.v(TAG, "Reset: " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002595 mDownKeyCode = 0;
2596 mDownTarget = null;
2597 mActiveLongPresses.clear();
2598 }
2599
2600 /**
2601 * Stop any tracking associated with this target.
2602 */
2603 public void reset(Object target) {
2604 if (mDownTarget == target) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002605 if (DEBUG) Log.v(TAG, "Reset in " + target + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002606 mDownKeyCode = 0;
2607 mDownTarget = null;
2608 }
2609 }
2610
2611 /**
2612 * Start tracking the key code associated with the given event. This
2613 * can only be called on a key down. It will allow you to see any
2614 * long press associated with the key, and will result in
2615 * {@link KeyEvent#isTracking} return true on the long press and up
2616 * events.
2617 *
2618 * <p>This is only needed if you are directly dispatching events, rather
2619 * than handling them in {@link Callback#onKeyDown}.
2620 */
2621 public void startTracking(KeyEvent event, Object target) {
2622 if (event.getAction() != ACTION_DOWN) {
2623 throw new IllegalArgumentException(
2624 "Can only start tracking on a down event");
2625 }
Dianne Hackborn8d374262009-09-14 21:21:52 -07002626 if (DEBUG) Log.v(TAG, "Start trackingt in " + target + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002627 mDownKeyCode = event.getKeyCode();
2628 mDownTarget = target;
2629 }
2630
2631 /**
2632 * Return true if the key event is for a key code that is currently
2633 * being tracked by the dispatcher.
2634 */
2635 public boolean isTracking(KeyEvent event) {
2636 return mDownKeyCode == event.getKeyCode();
2637 }
2638
2639 /**
2640 * Keep track of the given event's key code as having performed an
2641 * action with a long press, so no action should occur on the up.
2642 * <p>This is only needed if you are directly dispatching events, rather
2643 * than handling them in {@link Callback#onKeyLongPress}.
2644 */
2645 public void performedLongPress(KeyEvent event) {
2646 mActiveLongPresses.put(event.getKeyCode(), 1);
2647 }
2648
2649 /**
2650 * Handle key up event to stop tracking. This resets the dispatcher state,
2651 * and updates the key event state based on it.
2652 * <p>This is only needed if you are directly dispatching events, rather
2653 * than handling them in {@link Callback#onKeyUp}.
2654 */
2655 public void handleUpEvent(KeyEvent event) {
2656 final int keyCode = event.getKeyCode();
Dianne Hackborn8d374262009-09-14 21:21:52 -07002657 if (DEBUG) Log.v(TAG, "Handle key up " + event + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002658 int index = mActiveLongPresses.indexOfKey(keyCode);
2659 if (index >= 0) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002660 if (DEBUG) Log.v(TAG, " Index: " + index);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002661 event.mFlags |= FLAG_CANCELED | FLAG_CANCELED_LONG_PRESS;
2662 mActiveLongPresses.removeAt(index);
2663 }
2664 if (mDownKeyCode == keyCode) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002665 if (DEBUG) Log.v(TAG, " Tracking!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002666 event.mFlags |= FLAG_TRACKING;
2667 mDownKeyCode = 0;
2668 mDownTarget = null;
2669 }
2670 }
2671 }
Jeff Brown497a92c2010-09-12 17:55:08 -07002672
2673 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 public String toString() {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002675 StringBuilder msg = new StringBuilder();
2676 msg.append("KeyEvent { action=").append(actionToString(mAction));
2677 msg.append(", keyCode=").append(keyCodeToString(mKeyCode));
2678 msg.append(", scanCode=").append(mScanCode);
2679 if (mCharacters != null) {
2680 msg.append(", characters=\"").append(mCharacters).append("\"");
2681 }
2682 msg.append(", metaState=").append(metaStateToString(mMetaState));
2683 msg.append(", flags=0x").append(Integer.toHexString(mFlags));
2684 msg.append(", repeatCount=").append(mRepeatCount);
2685 msg.append(", eventTime=").append(mEventTime);
2686 msg.append(", downTime=").append(mDownTime);
2687 msg.append(", deviceId=").append(mDeviceId);
2688 msg.append(", source=0x").append(Integer.toHexString(mSource));
2689 msg.append(" }");
2690 return msg.toString();
Jeff Brown497a92c2010-09-12 17:55:08 -07002691 }
2692
2693 /**
2694 * Returns a string that represents the symbolic name of the specified action
Jeff Brown6f2fba42011-02-19 01:08:02 -08002695 * such as "ACTION_DOWN", or an equivalent numeric constant such as "35" if unknown.
Jeff Brown497a92c2010-09-12 17:55:08 -07002696 *
2697 * @param action The action.
2698 * @return The symbolic name of the specified action.
2699 * @hide
2700 */
2701 public static String actionToString(int action) {
2702 switch (action) {
2703 case ACTION_DOWN:
2704 return "ACTION_DOWN";
2705 case ACTION_UP:
2706 return "ACTION_UP";
2707 case ACTION_MULTIPLE:
2708 return "ACTION_MULTIPLE";
2709 default:
2710 return Integer.toString(action);
2711 }
2712 }
2713
2714 /**
2715 * Returns a string that represents the symbolic name of the specified keycode
Jeff Brown6f2fba42011-02-19 01:08:02 -08002716 * such as "KEYCODE_A", "KEYCODE_DPAD_UP", or an equivalent numeric constant
2717 * such as "1001" if unknown.
Jeff Brown497a92c2010-09-12 17:55:08 -07002718 *
2719 * @param keyCode The key code.
2720 * @return The symbolic name of the specified keycode.
2721 *
2722 * @see KeyCharacterMap#getDisplayLabel
Jeff Brown497a92c2010-09-12 17:55:08 -07002723 */
2724 public static String keyCodeToString(int keyCode) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08002725 String symbolicName = KEYCODE_SYMBOLIC_NAMES.get(keyCode);
2726 return symbolicName != null ? symbolicName : Integer.toString(keyCode);
Jeff Brown497a92c2010-09-12 17:55:08 -07002727 }
2728
2729 /**
Jeff Brown6f2fba42011-02-19 01:08:02 -08002730 * Gets a keycode by its symbolic name such as "KEYCODE_A" or an equivalent
2731 * numeric constant such as "1001".
Jeff Brown497a92c2010-09-12 17:55:08 -07002732 *
2733 * @param symbolicName The symbolic name of the keycode.
Jeff Brown6f2fba42011-02-19 01:08:02 -08002734 * @return The keycode or {@link #KEYCODE_UNKNOWN} if not found.
Jeff Brown497a92c2010-09-12 17:55:08 -07002735 * @see #keycodeToString
Jeff Brown497a92c2010-09-12 17:55:08 -07002736 */
2737 public static int keyCodeFromString(String symbolicName) {
2738 if (symbolicName == null) {
2739 throw new IllegalArgumentException("symbolicName must not be null");
2740 }
2741
Jeff Brown6f2fba42011-02-19 01:08:02 -08002742 final int count = KEYCODE_SYMBOLIC_NAMES.size();
Jeff Brown497a92c2010-09-12 17:55:08 -07002743 for (int i = 0; i < count; i++) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08002744 if (symbolicName.equals(KEYCODE_SYMBOLIC_NAMES.valueAt(i))) {
Jeff Brown497a92c2010-09-12 17:55:08 -07002745 return i;
2746 }
2747 }
2748
2749 try {
Jeff Brown6f2fba42011-02-19 01:08:02 -08002750 return Integer.parseInt(symbolicName, 10);
Jeff Brown497a92c2010-09-12 17:55:08 -07002751 } catch (NumberFormatException ex) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08002752 return KEYCODE_UNKNOWN;
Jeff Brown497a92c2010-09-12 17:55:08 -07002753 }
2754 }
2755
2756 /**
2757 * Returns a string that represents the symbolic name of the specified combined meta
2758 * key modifier state flags such as "0", "META_SHIFT_ON",
Jeff Brown6f2fba42011-02-19 01:08:02 -08002759 * "META_ALT_ON|META_SHIFT_ON" or an equivalent numeric constant such as "0x10000000"
2760 * if unknown.
Jeff Brown497a92c2010-09-12 17:55:08 -07002761 *
2762 * @param metaState The meta state.
2763 * @return The symbolic name of the specified combined meta state flags.
2764 * @hide
2765 */
2766 public static String metaStateToString(int metaState) {
2767 if (metaState == 0) {
2768 return "0";
2769 }
2770 StringBuilder result = null;
2771 int i = 0;
2772 while (metaState != 0) {
2773 final boolean isSet = (metaState & 1) != 0;
2774 metaState >>>= 1; // unsigned shift!
2775 if (isSet) {
2776 final String name = META_SYMBOLIC_NAMES[i];
2777 if (result == null) {
2778 if (metaState == 0) {
2779 return name;
2780 }
2781 result = new StringBuilder(name);
2782 } else {
2783 result.append('|');
2784 result.append(name);
2785 }
2786 }
2787 i += 1;
2788 }
2789 return result.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 }
2791
2792 public static final Parcelable.Creator<KeyEvent> CREATOR
2793 = new Parcelable.Creator<KeyEvent>() {
2794 public KeyEvent createFromParcel(Parcel in) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07002795 in.readInt(); // skip token, we already know this is a KeyEvent
2796 return KeyEvent.createFromParcelBody(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797 }
2798
2799 public KeyEvent[] newArray(int size) {
2800 return new KeyEvent[size];
2801 }
2802 };
Jeff Brown6ec402b2010-07-28 15:48:59 -07002803
2804 /** @hide */
2805 public static KeyEvent createFromParcelBody(Parcel in) {
2806 return new KeyEvent(in);
2807 }
2808
2809 private KeyEvent(Parcel in) {
Jeff Brown91c69ab2011-02-14 17:03:18 -08002810 mDeviceId = in.readInt();
2811 mSource = in.readInt();
Jeff Brown6ec402b2010-07-28 15:48:59 -07002812 mAction = in.readInt();
2813 mKeyCode = in.readInt();
2814 mRepeatCount = in.readInt();
2815 mMetaState = in.readInt();
2816 mScanCode = in.readInt();
2817 mFlags = in.readInt();
2818 mDownTime = in.readLong();
2819 mEventTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 }
2821
2822 public void writeToParcel(Parcel out, int flags) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07002823 out.writeInt(PARCEL_TOKEN_KEY_EVENT);
Jeff Brown91c69ab2011-02-14 17:03:18 -08002824
2825 out.writeInt(mDeviceId);
2826 out.writeInt(mSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 out.writeInt(mAction);
2828 out.writeInt(mKeyCode);
2829 out.writeInt(mRepeatCount);
2830 out.writeInt(mMetaState);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002831 out.writeInt(mScanCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002832 out.writeInt(mFlags);
2833 out.writeLong(mDownTime);
2834 out.writeLong(mEventTime);
2835 }
2836
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -07002837 private native boolean native_isSystemKey(int keyCode);
2838 private native boolean native_hasDefaultAction(int keyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839}