blob: 3a3d1d8f4ba8f86957c3db2e13e53fe81c4bf8fe [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;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070024import android.util.SparseIntArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.view.KeyCharacterMap;
26import android.view.KeyCharacterMap.KeyData;
27
28/**
Jeff Browndc1ab4b2010-09-14 18:03:38 -070029 * Object used to report key and button events.
30 * <p>
31 * Each key press is described by a sequence of key events. A key press
32 * starts with a key event with {@link #ACTION_DOWN}. If the key is held
33 * sufficiently long that it repeats, then the initial down is followed
34 * additional key events with {@link #ACTION_DOWN} and a non-zero value for
35 * {@link #getRepeatCount()}. The last key event is a {@link #ACTION_UP}
36 * for the key up. If the key press is canceled, the key up event will have the
37 * {@link #FLAG_CANCELED} flag set.
38 * </p><p>
39 * Key events are generally accompanied by a key code ({@link #getKeyCode()}),
40 * scan code ({@link #getScanCode()}) and meta state ({@link #getMetaState()}).
41 * Key code constants are defined in this class. Scan code constants are raw
42 * device-specific codes obtained from the OS and so are not generally meaningful
43 * to applications unless interpreted using the {@link KeyCharacterMap}.
44 * Meta states describe the pressed state of key modifiers
45 * such as {@link #META_SHIFT_ON} or {@link #META_ALT_ON}.
46 * </p><p>
Jeff Brown497a92c2010-09-12 17:55:08 -070047 * Key codes typically correspond one-to-one with individual keys on an input device.
48 * Many keys and key combinations serve quite different functions on different
49 * input devices so care must be taken when interpreting them. Always use the
50 * {@link KeyCharacterMap} associated with the input device when mapping keys
51 * to characters. Be aware that there may be multiple key input devices active
52 * at the same time and each will have its own key character map.
53 * </p><p>
Jeff Browndc1ab4b2010-09-14 18:03:38 -070054 * When interacting with an IME, the framework may deliver key events
55 * with the special action {@link #ACTION_MULTIPLE} that either specifies
56 * that single repeated key code or a sequence of characters to insert.
57 * </p><p>
Jeff Brownb6997262010-10-08 22:31:17 -070058 * In general, the framework cannot guarantee that the key events it delivers
59 * to a view always constitute complete key sequences since some events may be dropped
60 * or modified by containing views before they are delivered. The view implementation
61 * should be prepared to handle {@link #FLAG_CANCELED} and should tolerate anomalous
62 * situations such as receiving a new {@link #ACTION_DOWN} without first having
63 * received an {@link #ACTION_UP} for the prior key press.
Jeff Browndc1ab4b2010-09-14 18:03:38 -070064 * </p><p>
65 * Refer to {@link InputDevice} for more information about how different kinds of
66 * input devices and sources represent keys and buttons.
67 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 */
Jeff Brownc5ed5912010-07-14 18:48:53 -070069public class KeyEvent extends InputEvent implements Parcelable {
Jeff Browndc1ab4b2010-09-14 18:03:38 -070070 /** Key code constant: Unknown key code. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 public static final int KEYCODE_UNKNOWN = 0;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070072 /** Key code constant: Soft Left key.
73 * Usually situated below the display on phones and used as a multi-function
74 * feature key for selecting a software defined function shown on the bottom left
75 * of the display. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 public static final int KEYCODE_SOFT_LEFT = 1;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070077 /** Key code constant: Soft Right key.
78 * Usually situated below the display on phones and used as a multi-function
79 * feature key for selecting a software defined function shown on the bottom right
80 * of the display. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 public static final int KEYCODE_SOFT_RIGHT = 2;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070082 /** Key code constant: Home key.
83 * This key is handled by the framework and is never delivered to applications. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 public static final int KEYCODE_HOME = 3;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070085 /** Key code constant: Back key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 public static final int KEYCODE_BACK = 4;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070087 /** Key code constant: Call key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 public static final int KEYCODE_CALL = 5;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070089 /** Key code constant: End Call key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 public static final int KEYCODE_ENDCALL = 6;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070091 /** Key code constant: '0' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 public static final int KEYCODE_0 = 7;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070093 /** Key code constant: '1' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 public static final int KEYCODE_1 = 8;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070095 /** Key code constant: '2' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 public static final int KEYCODE_2 = 9;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070097 /** Key code constant: '3' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 public static final int KEYCODE_3 = 10;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070099 /** Key code constant: '4' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 public static final int KEYCODE_4 = 11;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700101 /** Key code constant: '5' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 public static final int KEYCODE_5 = 12;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700103 /** Key code constant: '6' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 public static final int KEYCODE_6 = 13;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700105 /** Key code constant: '7' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 public static final int KEYCODE_7 = 14;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700107 /** Key code constant: '8' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 public static final int KEYCODE_8 = 15;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700109 /** Key code constant: '9' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 public static final int KEYCODE_9 = 16;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700111 /** Key code constant: '*' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 public static final int KEYCODE_STAR = 17;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700113 /** Key code constant: '#' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 public static final int KEYCODE_POUND = 18;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700115 /** Key code constant: Directional Pad Up key.
116 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 public static final int KEYCODE_DPAD_UP = 19;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700118 /** Key code constant: Directional Pad Down key.
119 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 public static final int KEYCODE_DPAD_DOWN = 20;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700121 /** Key code constant: Directional Pad Left key.
122 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 public static final int KEYCODE_DPAD_LEFT = 21;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700124 /** Key code constant: Directional Pad Right key.
125 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 public static final int KEYCODE_DPAD_RIGHT = 22;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700127 /** Key code constant: Directional Pad Center key.
128 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 public static final int KEYCODE_DPAD_CENTER = 23;
Jeff Brownb0418da2010-11-01 15:24:01 -0700130 /** Key code constant: Volume Up key.
131 * Adjusts the speaker volume up. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 public static final int KEYCODE_VOLUME_UP = 24;
Jeff Brownb0418da2010-11-01 15:24:01 -0700133 /** Key code constant: Volume Down key.
134 * Adjusts the speaker volume down. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 public static final int KEYCODE_VOLUME_DOWN = 25;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700136 /** Key code constant: Power key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 public static final int KEYCODE_POWER = 26;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700138 /** Key code constant: Camera key.
139 * Used to launch a camera application or take pictures. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 public static final int KEYCODE_CAMERA = 27;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700141 /** Key code constant: Clear key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 public static final int KEYCODE_CLEAR = 28;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700143 /** Key code constant: 'A' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 public static final int KEYCODE_A = 29;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700145 /** Key code constant: 'B' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 public static final int KEYCODE_B = 30;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700147 /** Key code constant: 'C' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 public static final int KEYCODE_C = 31;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700149 /** Key code constant: 'D' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 public static final int KEYCODE_D = 32;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700151 /** Key code constant: 'E' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 public static final int KEYCODE_E = 33;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700153 /** Key code constant: 'F' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 public static final int KEYCODE_F = 34;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700155 /** Key code constant: 'G' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 public static final int KEYCODE_G = 35;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700157 /** Key code constant: 'H' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 public static final int KEYCODE_H = 36;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700159 /** Key code constant: 'I' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 public static final int KEYCODE_I = 37;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700161 /** Key code constant: 'J' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 public static final int KEYCODE_J = 38;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700163 /** Key code constant: 'K' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 public static final int KEYCODE_K = 39;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700165 /** Key code constant: 'L' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 public static final int KEYCODE_L = 40;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700167 /** Key code constant: 'M' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 public static final int KEYCODE_M = 41;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700169 /** Key code constant: 'N' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 public static final int KEYCODE_N = 42;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700171 /** Key code constant: 'O' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 public static final int KEYCODE_O = 43;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700173 /** Key code constant: 'P' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 public static final int KEYCODE_P = 44;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700175 /** Key code constant: 'Q' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 public static final int KEYCODE_Q = 45;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700177 /** Key code constant: 'R' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 public static final int KEYCODE_R = 46;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700179 /** Key code constant: 'S' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 public static final int KEYCODE_S = 47;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700181 /** Key code constant: 'T' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 public static final int KEYCODE_T = 48;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700183 /** Key code constant: 'U' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 public static final int KEYCODE_U = 49;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700185 /** Key code constant: 'V' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 public static final int KEYCODE_V = 50;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700187 /** Key code constant: 'W' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 public static final int KEYCODE_W = 51;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700189 /** Key code constant: 'X' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 public static final int KEYCODE_X = 52;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700191 /** Key code constant: 'Y' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 public static final int KEYCODE_Y = 53;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700193 /** Key code constant: 'Z' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 public static final int KEYCODE_Z = 54;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700195 /** Key code constant: ',' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 public static final int KEYCODE_COMMA = 55;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700197 /** Key code constant: '.' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 public static final int KEYCODE_PERIOD = 56;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700199 /** Key code constant: Left Alt modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 public static final int KEYCODE_ALT_LEFT = 57;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700201 /** Key code constant: Right Alt modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 public static final int KEYCODE_ALT_RIGHT = 58;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700203 /** Key code constant: Left Shift modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 public static final int KEYCODE_SHIFT_LEFT = 59;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700205 /** Key code constant: Right Shift modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 public static final int KEYCODE_SHIFT_RIGHT = 60;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700207 /** Key code constant: Tab key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 public static final int KEYCODE_TAB = 61;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700209 /** Key code constant: Space key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 public static final int KEYCODE_SPACE = 62;
Jeff Brown224d4a12010-10-07 20:28:53 -0700211 /** Key code constant: Symbol modifier key.
212 * Used to enter alternate symbols. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 public static final int KEYCODE_SYM = 63;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700214 /** Key code constant: Explorer special function key.
215 * Used to launch a browser application. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 public static final int KEYCODE_EXPLORER = 64;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700217 /** Key code constant: Envelope special function key.
218 * Used to launch a mail application. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 public static final int KEYCODE_ENVELOPE = 65;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700220 /** Key code constant: Enter key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 public static final int KEYCODE_ENTER = 66;
Jeff Brown224d4a12010-10-07 20:28:53 -0700222 /** Key code constant: Backspace key.
Jeff Brown497a92c2010-09-12 17:55:08 -0700223 * Deletes characters before the insertion point, unlike {@link #KEYCODE_FORWARD_DEL}. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 public static final int KEYCODE_DEL = 67;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700225 /** Key code constant: '`' (backtick) key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 public static final int KEYCODE_GRAVE = 68;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700227 /** Key code constant: '-'. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 public static final int KEYCODE_MINUS = 69;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700229 /** Key code constant: '=' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 public static final int KEYCODE_EQUALS = 70;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700231 /** Key code constant: '[' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 public static final int KEYCODE_LEFT_BRACKET = 71;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700233 /** Key code constant: ']' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 public static final int KEYCODE_RIGHT_BRACKET = 72;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700235 /** Key code constant: '\' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 public static final int KEYCODE_BACKSLASH = 73;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700237 /** Key code constant: ';' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 public static final int KEYCODE_SEMICOLON = 74;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700239 /** Key code constant: ''' (apostrophe) key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 public static final int KEYCODE_APOSTROPHE = 75;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700241 /** Key code constant: '/' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 public static final int KEYCODE_SLASH = 76;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700243 /** Key code constant: '@' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 public static final int KEYCODE_AT = 77;
Jeff Brown224d4a12010-10-07 20:28:53 -0700245 /** Key code constant: Number modifier key.
246 * Used to enter numeric symbols.
247 * This key is not Num Lock; it is more like {@link #KEYCODE_ALT_LEFT} and is
248 * interpreted as an ALT key by {@link android.text.method.MetaKeyKeyListener}. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 public static final int KEYCODE_NUM = 78;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700250 /** Key code constant: Headset Hook key.
251 * Used to hang up calls and stop media. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 public static final int KEYCODE_HEADSETHOOK = 79;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700253 /** Key code constant: Camera Focus key.
254 * Used to focus the camera. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 public static final int KEYCODE_FOCUS = 80; // *Camera* focus
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700256 /** Key code constant: '+' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 public static final int KEYCODE_PLUS = 81;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700258 /** Key code constant: Menu key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 public static final int KEYCODE_MENU = 82;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700260 /** Key code constant: Notification key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 public static final int KEYCODE_NOTIFICATION = 83;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700262 /** Key code constant: Search key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 public static final int KEYCODE_SEARCH = 84;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700264 /** Key code constant: Play/Pause media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700265 public static final int KEYCODE_MEDIA_PLAY_PAUSE= 85;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700266 /** Key code constant: Stop media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700267 public static final int KEYCODE_MEDIA_STOP = 86;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700268 /** Key code constant: Play Next media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700269 public static final int KEYCODE_MEDIA_NEXT = 87;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700270 /** Key code constant: Play Previous media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700271 public static final int KEYCODE_MEDIA_PREVIOUS = 88;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700272 /** Key code constant: Rewind media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700273 public static final int KEYCODE_MEDIA_REWIND = 89;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700274 /** Key code constant: Fast Forward media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700275 public static final int KEYCODE_MEDIA_FAST_FORWARD = 90;
Jeff Brownb0418da2010-11-01 15:24:01 -0700276 /** Key code constant: Mute key.
277 * Mutes the microphone, unlike {@link #KEYCODE_VOLUME_MUTE}. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 public static final int KEYCODE_MUTE = 91;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700279 /** Key code constant: Page Up key. */
Chih-Wei Huang4fedd802009-05-27 15:52:50 +0800280 public static final int KEYCODE_PAGE_UP = 92;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700281 /** Key code constant: Page Down key. */
Chih-Wei Huang4fedd802009-05-27 15:52:50 +0800282 public static final int KEYCODE_PAGE_DOWN = 93;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700283 /** Key code constant: Picture Symbols modifier key.
284 * Used to switch symbol sets (Emoji, Kao-moji). */
mogimob032bc02009-10-03 03:13:56 +0900285 public static final int KEYCODE_PICTSYMBOLS = 94; // switch symbol-sets (Emoji,Kao-moji)
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700286 /** Key code constant: Switch Charset modifier key.
287 * Used to switch character sets (Kanji, Katakana). */
mogimob032bc02009-10-03 03:13:56 +0900288 public static final int KEYCODE_SWITCH_CHARSET = 95; // switch char-sets (Kanji,Katakana)
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700289 /** Key code constant: A Button key.
290 * On a game controller, the A button should be either the button labeled A
291 * or the first button on the upper row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700292 public static final int KEYCODE_BUTTON_A = 96;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700293 /** Key code constant: B Button key.
294 * On a game controller, the B button should be either the button labeled B
295 * or the second button on the upper row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700296 public static final int KEYCODE_BUTTON_B = 97;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700297 /** Key code constant: C Button key.
298 * On a game controller, the C button should be either the button labeled C
299 * or the third button on the upper row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700300 public static final int KEYCODE_BUTTON_C = 98;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700301 /** Key code constant: X Button key.
302 * On a game controller, the X button should be either the button labeled X
303 * or the first button on the lower row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700304 public static final int KEYCODE_BUTTON_X = 99;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700305 /** Key code constant: Y Button key.
306 * On a game controller, the Y button should be either the button labeled Y
307 * or the second button on the lower row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700308 public static final int KEYCODE_BUTTON_Y = 100;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700309 /** Key code constant: Z Button key.
310 * On a game controller, the Z button should be either the button labeled Z
311 * or the third button on the lower row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700312 public static final int KEYCODE_BUTTON_Z = 101;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700313 /** Key code constant: L1 Button key.
314 * On a game controller, the L1 button should be either the button labeled L1 (or L)
315 * or the top left trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700316 public static final int KEYCODE_BUTTON_L1 = 102;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700317 /** Key code constant: R1 Button key.
318 * On a game controller, the R1 button should be either the button labeled R1 (or R)
319 * or the top right trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700320 public static final int KEYCODE_BUTTON_R1 = 103;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700321 /** Key code constant: L2 Button key.
322 * On a game controller, the L2 button should be either the button labeled L2
323 * or the bottom left trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700324 public static final int KEYCODE_BUTTON_L2 = 104;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700325 /** Key code constant: R2 Button key.
326 * On a game controller, the R2 button should be either the button labeled R2
327 * or the bottom right trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700328 public static final int KEYCODE_BUTTON_R2 = 105;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700329 /** Key code constant: Left Thumb Button key.
330 * On a game controller, the left thumb button indicates that the left (or only)
331 * joystick is pressed. */
Jeff Brownfd035822010-06-30 16:10:35 -0700332 public static final int KEYCODE_BUTTON_THUMBL = 106;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700333 /** Key code constant: Right Thumb Button key.
334 * On a game controller, the right thumb button indicates that the right
335 * joystick is pressed. */
Jeff Brownfd035822010-06-30 16:10:35 -0700336 public static final int KEYCODE_BUTTON_THUMBR = 107;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700337 /** Key code constant: Start Button key.
338 * On a game controller, the button labeled Start. */
Jeff Brownfd035822010-06-30 16:10:35 -0700339 public static final int KEYCODE_BUTTON_START = 108;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700340 /** Key code constant: Select Button key.
341 * On a game controller, the button labeled Select. */
Jeff Brownfd035822010-06-30 16:10:35 -0700342 public static final int KEYCODE_BUTTON_SELECT = 109;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700343 /** Key code constant: Mode Button key.
344 * On a game controller, the button labeled Mode. */
Jeff Brownfd035822010-06-30 16:10:35 -0700345 public static final int KEYCODE_BUTTON_MODE = 110;
Jeff Brown497a92c2010-09-12 17:55:08 -0700346 /** Key code constant: Escape key. */
347 public static final int KEYCODE_ESCAPE = 111;
348 /** Key code constant: Forward Delete key.
349 * Deletes characters ahead of the insertion point, unlike {@link #KEYCODE_DEL}. */
350 public static final int KEYCODE_FORWARD_DEL = 112;
351 /** Key code constant: Left Control modifier key. */
352 public static final int KEYCODE_CTRL_LEFT = 113;
353 /** Key code constant: Right Control modifier key. */
354 public static final int KEYCODE_CTRL_RIGHT = 114;
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800355 /** Key code constant: Caps Lock key. */
Jeff Brown497a92c2010-09-12 17:55:08 -0700356 public static final int KEYCODE_CAPS_LOCK = 115;
357 /** Key code constant: Scroll Lock key. */
358 public static final int KEYCODE_SCROLL_LOCK = 116;
359 /** Key code constant: Left Meta modifier key. */
360 public static final int KEYCODE_META_LEFT = 117;
361 /** Key code constant: Right Meta modifier key. */
362 public static final int KEYCODE_META_RIGHT = 118;
363 /** Key code constant: Function modifier key. */
364 public static final int KEYCODE_FUNCTION = 119;
365 /** Key code constant: System Request / Print Screen key. */
366 public static final int KEYCODE_SYSRQ = 120;
367 /** Key code constant: Break / Pause key. */
368 public static final int KEYCODE_BREAK = 121;
369 /** Key code constant: Home Movement key.
370 * Used for scrolling or moving the cursor around to the start of a line
371 * or to the top of a list. */
372 public static final int KEYCODE_MOVE_HOME = 122;
373 /** Key code constant: End Movement key.
374 * Used for scrolling or moving the cursor around to the end of a line
375 * or to the bottom of a list. */
376 public static final int KEYCODE_MOVE_END = 123;
377 /** Key code constant: Insert key.
378 * Toggles insert / overwrite edit mode. */
379 public static final int KEYCODE_INSERT = 124;
380 /** Key code constant: Forward key.
381 * Navigates forward in the history stack. Complement of {@link #KEYCODE_BACK}. */
382 public static final int KEYCODE_FORWARD = 125;
383 /** Key code constant: Play media key. */
384 public static final int KEYCODE_MEDIA_PLAY = 126;
385 /** Key code constant: Pause media key. */
386 public static final int KEYCODE_MEDIA_PAUSE = 127;
387 /** Key code constant: Close media key.
388 * May be used to close a CD tray, for example. */
389 public static final int KEYCODE_MEDIA_CLOSE = 128;
390 /** Key code constant: Eject media key.
391 * May be used to eject a CD tray, for example. */
392 public static final int KEYCODE_MEDIA_EJECT = 129;
393 /** Key code constant: Record media key. */
394 public static final int KEYCODE_MEDIA_RECORD = 130;
395 /** Key code constant: F1 key. */
396 public static final int KEYCODE_F1 = 131;
397 /** Key code constant: F2 key. */
398 public static final int KEYCODE_F2 = 132;
399 /** Key code constant: F3 key. */
400 public static final int KEYCODE_F3 = 133;
401 /** Key code constant: F4 key. */
402 public static final int KEYCODE_F4 = 134;
403 /** Key code constant: F5 key. */
404 public static final int KEYCODE_F5 = 135;
405 /** Key code constant: F6 key. */
406 public static final int KEYCODE_F6 = 136;
407 /** Key code constant: F7 key. */
408 public static final int KEYCODE_F7 = 137;
409 /** Key code constant: F8 key. */
410 public static final int KEYCODE_F8 = 138;
411 /** Key code constant: F9 key. */
412 public static final int KEYCODE_F9 = 139;
413 /** Key code constant: F10 key. */
414 public static final int KEYCODE_F10 = 140;
415 /** Key code constant: F11 key. */
416 public static final int KEYCODE_F11 = 141;
417 /** Key code constant: F12 key. */
418 public static final int KEYCODE_F12 = 142;
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800419 /** Key code constant: Num Lock key.
Jeff Brown497a92c2010-09-12 17:55:08 -0700420 * This is the Num Lock key; it is different from {@link #KEYCODE_NUM}.
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800421 * This key alters the behavior of other keys on the numeric keypad. */
Jeff Brown497a92c2010-09-12 17:55:08 -0700422 public static final int KEYCODE_NUM_LOCK = 143;
423 /** Key code constant: Numeric keypad '0' key. */
424 public static final int KEYCODE_NUMPAD_0 = 144;
425 /** Key code constant: Numeric keypad '1' key. */
426 public static final int KEYCODE_NUMPAD_1 = 145;
427 /** Key code constant: Numeric keypad '2' key. */
428 public static final int KEYCODE_NUMPAD_2 = 146;
429 /** Key code constant: Numeric keypad '3' key. */
430 public static final int KEYCODE_NUMPAD_3 = 147;
431 /** Key code constant: Numeric keypad '4' key. */
432 public static final int KEYCODE_NUMPAD_4 = 148;
433 /** Key code constant: Numeric keypad '5' key. */
434 public static final int KEYCODE_NUMPAD_5 = 149;
435 /** Key code constant: Numeric keypad '6' key. */
436 public static final int KEYCODE_NUMPAD_6 = 150;
437 /** Key code constant: Numeric keypad '7' key. */
438 public static final int KEYCODE_NUMPAD_7 = 151;
439 /** Key code constant: Numeric keypad '8' key. */
440 public static final int KEYCODE_NUMPAD_8 = 152;
441 /** Key code constant: Numeric keypad '9' key. */
442 public static final int KEYCODE_NUMPAD_9 = 153;
443 /** Key code constant: Numeric keypad '/' key (for division). */
444 public static final int KEYCODE_NUMPAD_DIVIDE = 154;
445 /** Key code constant: Numeric keypad '*' key (for multiplication). */
446 public static final int KEYCODE_NUMPAD_MULTIPLY = 155;
447 /** Key code constant: Numeric keypad '-' key (for subtraction). */
448 public static final int KEYCODE_NUMPAD_SUBTRACT = 156;
449 /** Key code constant: Numeric keypad '+' key (for addition). */
450 public static final int KEYCODE_NUMPAD_ADD = 157;
451 /** Key code constant: Numeric keypad '.' key (for decimals or digit grouping). */
452 public static final int KEYCODE_NUMPAD_DOT = 158;
453 /** Key code constant: Numeric keypad ',' key (for decimals or digit grouping). */
454 public static final int KEYCODE_NUMPAD_COMMA = 159;
455 /** Key code constant: Numeric keypad Enter key. */
456 public static final int KEYCODE_NUMPAD_ENTER = 160;
457 /** Key code constant: Numeric keypad '=' key. */
458 public static final int KEYCODE_NUMPAD_EQUALS = 161;
459 /** Key code constant: Numeric keypad '(' key. */
460 public static final int KEYCODE_NUMPAD_LEFT_PAREN = 162;
461 /** Key code constant: Numeric keypad ')' key. */
462 public static final int KEYCODE_NUMPAD_RIGHT_PAREN = 163;
Jeff Brownb0418da2010-11-01 15:24:01 -0700463 /** Key code constant: Volume Mute key.
464 * Mutes the speaker, unlike {@link #KEYCODE_MUTE}.
465 * This key should normally be implemented as a toggle such that the first press
466 * mutes the speaker and the second press restores the original volume. */
467 public static final int KEYCODE_VOLUME_MUTE = 164;
Jason Bayer3adf4902010-11-09 14:54:55 -0800468 /** Key code constant: Info key.
469 * Common on TV remotes to show additional information related to what is
470 * currently being viewed. */
471 public static final int KEYCODE_INFO = 165;
472 /** Key code constant: Channel up key.
473 * On TV remotes, increments the television channel. */
474 public static final int KEYCODE_CHANNEL_UP = 166;
475 /** Key code constant: Channel down key.
476 * On TV remotes, decrements the television channel. */
477 public static final int KEYCODE_CHANNEL_DOWN = 167;
478 /** Key code constant: Zoom in key. */
479 public static final int KEYCODE_ZOOM_IN = 168;
480 /** Key code constant: Zoom out key. */
481 public static final int KEYCODE_ZOOM_OUT = 169;
482 /** Key code constant: TV key.
483 * On TV remotes, switches to viewing live TV. */
484 public static final int KEYCODE_TV = 170;
485 /** Key code constant: Window key.
486 * On TV remotes, toggles picture-in-picture mode or other windowing functions. */
487 public static final int KEYCODE_WINDOW = 171;
488 /** Key code constant: Guide key.
489 * On TV remotes, shows a programming guide. */
490 public static final int KEYCODE_GUIDE = 172;
491 /** Key code constant: DVR key.
492 * On some TV remotes, switches to a DVR mode for recorded shows. */
493 public static final int KEYCODE_DVR = 173;
494 /** Key code constant: Bookmark key.
495 * On some TV remotes, bookmarks content or web pages. */
496 public static final int KEYCODE_BOOKMARK = 174;
497 /** Key code constant: Toggle captions key.
498 * Switches the mode for closed-captioning text, for example during television shows. */
499 public static final int KEYCODE_CAPTIONS = 175;
500 /** Key code constant: Settings key.
501 * Starts the system settings activity. */
502 public static final int KEYCODE_SETTINGS = 176;
503 /** Key code constant: TV power key.
504 * On TV remotes, toggles the power on a television screen. */
505 public static final int KEYCODE_TV_POWER = 177;
506 /** Key code constant: TV input key.
507 * On TV remotes, switches the input on a television screen. */
508 public static final int KEYCODE_TV_INPUT = 178;
509 /** Key code constant: Set-top-box power key.
510 * On TV remotes, toggles the power on an external Set-top-box. */
511 public static final int KEYCODE_STB_POWER = 179;
512 /** Key code constant: Set-top-box input key.
513 * On TV remotes, switches the input mode on an external Set-top-box. */
514 public static final int KEYCODE_STB_INPUT = 180;
515 /** Key code constant: A/V Receiver power key.
516 * On TV remotes, toggles the power on an external A/V Receiver. */
517 public static final int KEYCODE_AVR_POWER = 181;
518 /** Key code constant: A/V Receiver input key.
519 * On TV remotes, switches the input mode on an external A/V Receiver. */
520 public static final int KEYCODE_AVR_INPUT = 182;
521 /** Key code constant: Red "programmable" key.
522 * On TV remotes, acts as a contextual/programmable key. */
523 public static final int KEYCODE_PROG_RED = 183;
524 /** Key code constant: Green "programmable" key.
525 * On TV remotes, actsas a contextual/programmable key. */
526 public static final int KEYCODE_PROG_GREEN = 184;
527 /** Key code constant: Yellow "programmable" key.
528 * On TV remotes, acts as a contextual/programmable key. */
529 public static final int KEYCODE_PROG_YELLOW = 185;
530 /** Key code constant: Blue "programmable" key.
531 * On TV remotes, acts as a contextual/programmable key. */
532 public static final int KEYCODE_PROG_BLUE = 186;
Jeff Brown49ed71d2010-12-06 17:13:33 -0800533 /** Key code constant: App switch key.
534 * Should bring up the application switcher dialog. */
535 public static final int KEYCODE_APP_SWITCH = 187;
Jeff Brown497a92c2010-09-12 17:55:08 -0700536
Jeff Brown49ed71d2010-12-06 17:13:33 -0800537 private static final int LAST_KEYCODE = KEYCODE_APP_SWITCH;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538
539 // NOTE: If you add a new keycode here you must also add it to:
540 // isSystem()
Jeff Brown46b9ac02010-04-22 18:58:52 -0700541 // native/include/android/keycodes.h
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 // frameworks/base/include/ui/KeycodeLabels.h
Jeff Brownfd035822010-06-30 16:10:35 -0700543 // external/webkit/WebKit/android/plugins/ANPKeyCodes.h
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 // frameworks/base/core/res/res/values/attrs.xml
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 // emulator?
Dianne Hackborn935ae462009-04-13 16:11:55 -0700546 //
547 // Also Android currently does not reserve code ranges for vendor-
548 // specific key codes. If you have new key codes to have, you
549 // MUST contribute a patch to the open source project to define
550 // those new codes. This is intended to maintain a consistent
551 // set of key code definitions across all Android devices.
Jeff Brown497a92c2010-09-12 17:55:08 -0700552
553 // Symbolic names of all keys indexed by keycode.
554 // There should be exactly LAST_KEYCODE + 1 entries in this table.
555 private static final String[] KEYCODE_SYMBOLIC_NAMES = new String[] {
556 "KEYCODE_UNKNOWN",
557 "KEYCODE_SOFT_LEFT",
558 "KEYCODE_SOFT_RIGHT",
559 "KEYCODE_HOME",
560 "KEYCODE_BACK",
561 "KEYCODE_CALL",
562 "KEYCODE_ENDCALL",
563 "KEYCODE_0",
564 "KEYCODE_1",
565 "KEYCODE_2",
566 "KEYCODE_3",
567 "KEYCODE_4",
568 "KEYCODE_5",
569 "KEYCODE_6",
570 "KEYCODE_7",
571 "KEYCODE_8",
572 "KEYCODE_9",
573 "KEYCODE_STAR",
574 "KEYCODE_POUND",
575 "KEYCODE_DPAD_UP",
576 "KEYCODE_DPAD_DOWN",
577 "KEYCODE_DPAD_LEFT",
578 "KEYCODE_DPAD_RIGHT",
579 "KEYCODE_DPAD_CENTER",
580 "KEYCODE_VOLUME_UP",
581 "KEYCODE_VOLUME_DOWN",
582 "KEYCODE_POWER",
583 "KEYCODE_CAMERA",
584 "KEYCODE_CLEAR",
585 "KEYCODE_A",
586 "KEYCODE_B",
587 "KEYCODE_C",
588 "KEYCODE_D",
589 "KEYCODE_E",
590 "KEYCODE_F",
591 "KEYCODE_G",
592 "KEYCODE_H",
593 "KEYCODE_I",
594 "KEYCODE_J",
595 "KEYCODE_K",
596 "KEYCODE_L",
597 "KEYCODE_M",
598 "KEYCODE_N",
599 "KEYCODE_O",
600 "KEYCODE_P",
601 "KEYCODE_Q",
602 "KEYCODE_R",
603 "KEYCODE_S",
604 "KEYCODE_T",
605 "KEYCODE_U",
606 "KEYCODE_V",
607 "KEYCODE_W",
608 "KEYCODE_X",
609 "KEYCODE_Y",
610 "KEYCODE_Z",
611 "KEYCODE_COMMA",
612 "KEYCODE_PERIOD",
613 "KEYCODE_ALT_LEFT",
614 "KEYCODE_ALT_RIGHT",
615 "KEYCODE_SHIFT_LEFT",
616 "KEYCODE_SHIFT_RIGHT",
617 "KEYCODE_TAB",
618 "KEYCODE_SPACE",
619 "KEYCODE_SYM",
620 "KEYCODE_EXPLORER",
621 "KEYCODE_ENVELOPE",
622 "KEYCODE_ENTER",
623 "KEYCODE_DEL",
624 "KEYCODE_GRAVE",
625 "KEYCODE_MINUS",
626 "KEYCODE_EQUALS",
627 "KEYCODE_LEFT_BRACKET",
628 "KEYCODE_RIGHT_BRACKET",
629 "KEYCODE_BACKSLASH",
630 "KEYCODE_SEMICOLON",
631 "KEYCODE_APOSTROPHE",
632 "KEYCODE_SLASH",
633 "KEYCODE_AT",
634 "KEYCODE_NUM",
635 "KEYCODE_HEADSETHOOK",
636 "KEYCODE_FOCUS",
637 "KEYCODE_PLUS",
638 "KEYCODE_MENU",
639 "KEYCODE_NOTIFICATION",
640 "KEYCODE_SEARCH",
641 "KEYCODE_MEDIA_PLAY_PAUSE",
642 "KEYCODE_MEDIA_STOP",
643 "KEYCODE_MEDIA_NEXT",
644 "KEYCODE_MEDIA_PREVIOUS",
645 "KEYCODE_MEDIA_REWIND",
646 "KEYCODE_MEDIA_FAST_FORWARD",
647 "KEYCODE_MUTE",
648 "KEYCODE_PAGE_UP",
649 "KEYCODE_PAGE_DOWN",
650 "KEYCODE_PICTSYMBOLS",
651 "KEYCODE_SWITCH_CHARSET",
652 "KEYCODE_BUTTON_A",
653 "KEYCODE_BUTTON_B",
654 "KEYCODE_BUTTON_C",
655 "KEYCODE_BUTTON_X",
656 "KEYCODE_BUTTON_Y",
657 "KEYCODE_BUTTON_Z",
658 "KEYCODE_BUTTON_L1",
659 "KEYCODE_BUTTON_R1",
660 "KEYCODE_BUTTON_L2",
661 "KEYCODE_BUTTON_R2",
662 "KEYCODE_BUTTON_THUMBL",
663 "KEYCODE_BUTTON_THUMBR",
664 "KEYCODE_BUTTON_START",
665 "KEYCODE_BUTTON_SELECT",
666 "KEYCODE_BUTTON_MODE",
667 "KEYCODE_ESCAPE",
668 "KEYCODE_FORWARD_DEL",
669 "KEYCODE_CTRL_LEFT",
670 "KEYCODE_CTRL_RIGHT",
671 "KEYCODE_CAPS_LOCK",
672 "KEYCODE_SCROLL_LOCK",
673 "KEYCODE_META_LEFT",
674 "KEYCODE_META_RIGHT",
675 "KEYCODE_FUNCTION",
676 "KEYCODE_SYSRQ",
677 "KEYCODE_BREAK",
678 "KEYCODE_MOVE_HOME",
679 "KEYCODE_MOVE_END",
680 "KEYCODE_INSERT",
681 "KEYCODE_FORWARD",
682 "KEYCODE_MEDIA_PLAY",
683 "KEYCODE_MEDIA_PAUSE",
684 "KEYCODE_MEDIA_CLOSE",
685 "KEYCODE_MEDIA_EJECT",
686 "KEYCODE_MEDIA_RECORD",
687 "KEYCODE_F1",
688 "KEYCODE_F2",
689 "KEYCODE_F3",
690 "KEYCODE_F4",
691 "KEYCODE_F5",
692 "KEYCODE_F6",
693 "KEYCODE_F7",
694 "KEYCODE_F8",
695 "KEYCODE_F9",
696 "KEYCODE_F10",
697 "KEYCODE_F11",
698 "KEYCODE_F12",
699 "KEYCODE_NUM_LOCK",
700 "KEYCODE_NUMPAD_0",
701 "KEYCODE_NUMPAD_1",
702 "KEYCODE_NUMPAD_2",
703 "KEYCODE_NUMPAD_3",
704 "KEYCODE_NUMPAD_4",
705 "KEYCODE_NUMPAD_5",
706 "KEYCODE_NUMPAD_6",
707 "KEYCODE_NUMPAD_7",
708 "KEYCODE_NUMPAD_8",
709 "KEYCODE_NUMPAD_9",
710 "KEYCODE_NUMPAD_DIVIDE",
711 "KEYCODE_NUMPAD_MULTIPLY",
712 "KEYCODE_MUMPAD_SUBTRACT",
713 "KEYCODE_NUMPAD_ADD",
714 "KEYCODE_NUMPAD_DOT",
715 "KEYCODE_NUMPAD_COMMA",
716 "KEYCODE_NUMPAD_ENTER",
717 "KEYCODE_NUMPAD_EQUALS",
718 "KEYCODE_NUMPAD_LEFT_PAREN",
719 "KEYCODE_NUMPAD_RIGHT_PAREN",
Jeff Brownb0418da2010-11-01 15:24:01 -0700720 "KEYCODE_VOLUME_MUTE",
Jason Bayer3adf4902010-11-09 14:54:55 -0800721 "KEYCODE_INFO",
722 "KEYCODE_CHANNEL_UP",
723 "KEYCODE_CHANNEL_DOWN",
724 "KEYCODE_ZOOM_IN",
725 "KEYCODE_ZOOM_OUT",
726 "KEYCODE_TV",
727 "KEYCODE_WINDOW",
728 "KEYCODE_GUIDE",
729 "KEYCODE_DVR",
730 "KEYCODE_BOOKMARK",
731 "KEYCODE_CAPTIONS",
732 "KEYCODE_SETTINGS",
733 "KEYCODE_TV_POWER",
734 "KEYCODE_TV_INPUT",
735 "KEYCODE_STB_INPUT",
736 "KEYCODE_STB_POWER",
737 "KEYCODE_AVR_POWER",
738 "KEYCODE_AVR_INPUT",
739 "KEYCODE_PROG_RED",
740 "KEYCODE_PROG_GREEN",
741 "KEYCODE_PROG_YELLOW",
742 "KEYCODE_PROG_BLUE",
Jeff Brown49ed71d2010-12-06 17:13:33 -0800743 "KEYCODE_APP_SWITCH",
Jeff Brown497a92c2010-09-12 17:55:08 -0700744 };
745
746 // Symbolic names of all metakeys in bit order from least significant to most significant.
747 // Accordingly there are exactly 32 values in this table.
748 private static final String[] META_SYMBOLIC_NAMES = new String[] {
749 "META_SHIFT_ON",
750 "META_ALT_ON",
751 "META_SYM_ON",
752 "META_FUNCTION_ON",
753 "META_ALT_LEFT_ON",
754 "META_ALT_RIGHT_ON",
755 "META_SHIFT_LEFT_ON",
756 "META_SHIFT_RIGHT_ON",
757 "META_CAP_LOCKED",
758 "META_ALT_LOCKED",
759 "META_SYM_LOCKED",
760 "0x00000800",
761 "META_CTRL_ON",
762 "META_CTRL_LEFT_ON",
763 "META_CTRL_RIGHT_ON",
764 "0x00008000",
765 "META_META_ON",
766 "META_META_LEFT_ON",
767 "META_META_RIGHT_ON",
768 "0x00080000",
Jeff Brown51e7fe72010-10-29 22:19:53 -0700769 "META_CAPS_LOCK_ON",
770 "META_NUM_LOCK_ON",
771 "META_SCROLL_LOCK_ON",
Jeff Brown497a92c2010-09-12 17:55:08 -0700772 "0x00800000",
773 "0x01000000",
774 "0x02000000",
775 "0x04000000",
776 "0x08000000",
777 "0x10000000",
778 "0x20000000",
779 "0x40000000",
780 "0x80000000",
781 };
782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 /**
784 * @deprecated There are now more than MAX_KEYCODE keycodes.
785 * Use {@link #getMaxKeyCode()} instead.
786 */
787 @Deprecated
788 public static final int MAX_KEYCODE = 84;
789
790 /**
791 * {@link #getAction} value: the key has been pressed down.
792 */
793 public static final int ACTION_DOWN = 0;
794 /**
795 * {@link #getAction} value: the key has been released.
796 */
797 public static final int ACTION_UP = 1;
798 /**
799 * {@link #getAction} value: multiple duplicate key events have
800 * occurred in a row, or a complex string is being delivered. If the
801 * key code is not {#link {@link #KEYCODE_UNKNOWN} then the
802 * {#link {@link #getRepeatCount()} method returns the number of times
803 * the given key code should be executed.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700804 * Otherwise, if the key code is {@link #KEYCODE_UNKNOWN}, then
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 * this is a sequence of characters as returned by {@link #getCharacters}.
806 */
807 public static final int ACTION_MULTIPLE = 2;
808
809 /**
Jeff Brown497a92c2010-09-12 17:55:08 -0700810 * SHIFT key locked in CAPS mode.
811 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
812 * @hide
813 */
814 public static final int META_CAP_LOCKED = 0x100;
815
816 /**
817 * ALT key locked.
818 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
819 * @hide
820 */
821 public static final int META_ALT_LOCKED = 0x200;
822
823 /**
824 * SYM key locked.
825 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
826 * @hide
827 */
828 public static final int META_SYM_LOCKED = 0x400;
829
830 /**
831 * Text is in selection mode.
832 * Reserved for use by {@link MetaKeyKeyListener} for a private unpublished constant
833 * in its API that is currently being retained for legacy reasons.
834 * @hide
835 */
836 public static final int META_SELECTING = 0x800;
837
838 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 * <p>This mask is used to check whether one of the ALT meta keys is pressed.</p>
840 *
841 * @see #isAltPressed()
842 * @see #getMetaState()
843 * @see #KEYCODE_ALT_LEFT
844 * @see #KEYCODE_ALT_RIGHT
845 */
846 public static final int META_ALT_ON = 0x02;
847
848 /**
849 * <p>This mask is used to check whether the left ALT meta key is pressed.</p>
850 *
851 * @see #isAltPressed()
852 * @see #getMetaState()
853 * @see #KEYCODE_ALT_LEFT
854 */
855 public static final int META_ALT_LEFT_ON = 0x10;
856
857 /**
858 * <p>This mask is used to check whether the right the ALT meta key is pressed.</p>
859 *
860 * @see #isAltPressed()
861 * @see #getMetaState()
862 * @see #KEYCODE_ALT_RIGHT
863 */
864 public static final int META_ALT_RIGHT_ON = 0x20;
865
866 /**
867 * <p>This mask is used to check whether one of the SHIFT meta keys is pressed.</p>
868 *
869 * @see #isShiftPressed()
870 * @see #getMetaState()
871 * @see #KEYCODE_SHIFT_LEFT
872 * @see #KEYCODE_SHIFT_RIGHT
873 */
874 public static final int META_SHIFT_ON = 0x1;
875
876 /**
877 * <p>This mask is used to check whether the left SHIFT meta key is pressed.</p>
878 *
879 * @see #isShiftPressed()
880 * @see #getMetaState()
881 * @see #KEYCODE_SHIFT_LEFT
882 */
883 public static final int META_SHIFT_LEFT_ON = 0x40;
884
885 /**
886 * <p>This mask is used to check whether the right SHIFT meta key is pressed.</p>
887 *
888 * @see #isShiftPressed()
889 * @see #getMetaState()
890 * @see #KEYCODE_SHIFT_RIGHT
891 */
892 public static final int META_SHIFT_RIGHT_ON = 0x80;
893
894 /**
895 * <p>This mask is used to check whether the SYM meta key is pressed.</p>
896 *
897 * @see #isSymPressed()
898 * @see #getMetaState()
899 */
900 public static final int META_SYM_ON = 0x4;
901
902 /**
Jeff Brown497a92c2010-09-12 17:55:08 -0700903 * <p>This mask is used to check whether the FUNCTION meta key is pressed.</p>
904 *
905 * @see #isFunctionPressed()
906 * @see #getMetaState()
907 */
908 public static final int META_FUNCTION_ON = 0x8;
909
910 /**
911 * <p>This mask is used to check whether one of the CTRL meta keys is pressed.</p>
912 *
913 * @see #isCtrlPressed()
914 * @see #getMetaState()
915 * @see #KEYCODE_CTRL_LEFT
916 * @see #KEYCODE_CTRL_RIGHT
917 */
918 public static final int META_CTRL_ON = 0x1000;
919
920 /**
921 * <p>This mask is used to check whether the left CTRL meta key is pressed.</p>
922 *
923 * @see #isCtrlPressed()
924 * @see #getMetaState()
925 * @see #KEYCODE_CTRL_LEFT
926 */
927 public static final int META_CTRL_LEFT_ON = 0x2000;
928
929 /**
930 * <p>This mask is used to check whether the right CTRL meta key is pressed.</p>
931 *
932 * @see #isCtrlPressed()
933 * @see #getMetaState()
934 * @see #KEYCODE_CTRL_RIGHT
935 */
936 public static final int META_CTRL_RIGHT_ON = 0x4000;
937
938 /**
939 * <p>This mask is used to check whether one of the META meta keys is pressed.</p>
940 *
941 * @see #isMetaPressed()
942 * @see #getMetaState()
943 * @see #KEYCODE_META_LEFT
944 * @see #KEYCODE_META_RIGHT
945 */
946 public static final int META_META_ON = 0x10000;
947
948 /**
949 * <p>This mask is used to check whether the left META meta key is pressed.</p>
950 *
951 * @see #isMetaPressed()
952 * @see #getMetaState()
953 * @see #KEYCODE_META_LEFT
954 */
955 public static final int META_META_LEFT_ON = 0x20000;
956
957 /**
958 * <p>This mask is used to check whether the right META meta key is pressed.</p>
959 *
960 * @see #isMetaPressed()
961 * @see #getMetaState()
962 * @see #KEYCODE_META_RIGHT
963 */
964 public static final int META_META_RIGHT_ON = 0x40000;
965
966 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -0700967 * <p>This mask is used to check whether the CAPS LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -0700968 *
Jeff Brown51e7fe72010-10-29 22:19:53 -0700969 * @see #isCapsLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -0700970 * @see #getMetaState()
971 * @see #KEYCODE_CAPS_LOCK
972 */
Jeff Brown51e7fe72010-10-29 22:19:53 -0700973 public static final int META_CAPS_LOCK_ON = 0x100000;
Jeff Brown497a92c2010-09-12 17:55:08 -0700974
975 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -0700976 * <p>This mask is used to check whether the NUM LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -0700977 *
Jeff Brown51e7fe72010-10-29 22:19:53 -0700978 * @see #isNumLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -0700979 * @see #getMetaState()
980 * @see #KEYCODE_NUM_LOCK
981 */
Jeff Brown51e7fe72010-10-29 22:19:53 -0700982 public static final int META_NUM_LOCK_ON = 0x200000;
Jeff Brown497a92c2010-09-12 17:55:08 -0700983
984 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -0700985 * <p>This mask is used to check whether the SCROLL LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -0700986 *
Jeff Brown51e7fe72010-10-29 22:19:53 -0700987 * @see #isScrollLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -0700988 * @see #getMetaState()
989 * @see #KEYCODE_SCROLL_LOCK
990 */
Jeff Brown51e7fe72010-10-29 22:19:53 -0700991 public static final int META_SCROLL_LOCK_ON = 0x400000;
Jeff Brown497a92c2010-09-12 17:55:08 -0700992
Jeff Brownc1df9072010-12-21 16:38:50 -0800993 /** {@hide} */
994 public static final int META_SHIFT_MASK = META_SHIFT_ON
995 | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON;
996
997 /** {@hide} */
998 public static final int META_ALT_MASK = META_ALT_ON
999 | META_ALT_LEFT_ON | META_ALT_RIGHT_ON;
1000
1001 /** {@hide} */
1002 public static final int META_CTRL_MASK = META_CTRL_ON
1003 | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON;
1004
1005 /** {@hide} */
1006 public static final int META_META_MASK = META_ALT_ON
1007 | META_META_LEFT_ON | META_META_RIGHT_ON;
1008
Jeff Brown497a92c2010-09-12 17:55:08 -07001009 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 * This mask is set if the device woke because of this key event.
1011 */
1012 public static final int FLAG_WOKE_HERE = 0x1;
1013
1014 /**
1015 * This mask is set if the key event was generated by a software keyboard.
1016 */
1017 public static final int FLAG_SOFT_KEYBOARD = 0x2;
1018
1019 /**
1020 * This mask is set if we don't want the key event to cause us to leave
1021 * touch mode.
1022 */
1023 public static final int FLAG_KEEP_TOUCH_MODE = 0x4;
1024
1025 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001026 * This mask is set if an event was known to come from a trusted part
1027 * of the system. That is, the event is known to come from the user,
1028 * and could not have been spoofed by a third party component.
1029 */
1030 public static final int FLAG_FROM_SYSTEM = 0x8;
1031
1032 /**
1033 * This mask is used for compatibility, to identify enter keys that are
1034 * coming from an IME whose enter key has been auto-labelled "next" or
1035 * "done". This allows TextView to dispatch these as normal enter keys
1036 * for old applications, but still do the appropriate action when
1037 * receiving them.
1038 */
1039 public static final int FLAG_EDITOR_ACTION = 0x10;
1040
1041 /**
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07001042 * When associated with up key events, this indicates that the key press
1043 * has been canceled. Typically this is used with virtual touch screen
1044 * keys, where the user can slide from the virtual key area on to the
1045 * display: in that case, the application will receive a canceled up
1046 * event and should not perform the action normally associated with the
1047 * key. Note that for this to work, the application can not perform an
1048 * action for a key until it receives an up or the long press timeout has
1049 * expired.
1050 */
1051 public static final int FLAG_CANCELED = 0x20;
1052
1053 /**
1054 * This key event was generated by a virtual (on-screen) hard key area.
1055 * Typically this is an area of the touchscreen, outside of the regular
1056 * display, dedicated to "hardware" buttons.
1057 */
1058 public static final int FLAG_VIRTUAL_HARD_KEY = 0x40;
1059
1060 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001061 * This flag is set for the first key repeat that occurs after the
1062 * long press timeout.
1063 */
1064 public static final int FLAG_LONG_PRESS = 0x80;
1065
1066 /**
1067 * Set when a key event has {@link #FLAG_CANCELED} set because a long
1068 * press action was executed while it was down.
1069 */
1070 public static final int FLAG_CANCELED_LONG_PRESS = 0x100;
1071
1072 /**
1073 * Set for {@link #ACTION_UP} when this event's key code is still being
1074 * tracked from its initial down. That is, somebody requested that tracking
1075 * started on the key down and a long press has not caused
1076 * the tracking to be canceled.
1077 */
1078 public static final int FLAG_TRACKING = 0x200;
Jeff Brown49ed71d2010-12-06 17:13:33 -08001079
1080 /**
1081 * Set when a key event has been synthesized to implement default behavior
1082 * for an event that the application did not handle.
1083 * Fallback key events are generated by unhandled trackball motions
1084 * (to emulate a directional keypad) and by certain unhandled key presses
1085 * that are declared in the key map (such as special function numeric keypad
1086 * keys when numlock is off).
1087 */
1088 public static final int FLAG_FALLBACK = 0x400;
1089
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001090 /**
1091 * Private control to determine when an app is tracking a key sequence.
1092 * @hide
1093 */
1094 public static final int FLAG_START_TRACKING = 0x40000000;
1095
1096 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 * Returns the maximum keycode.
1098 */
1099 public static int getMaxKeyCode() {
1100 return LAST_KEYCODE;
1101 }
1102
1103 /**
1104 * Get the character that is produced by putting accent on the character
1105 * c.
1106 * For example, getDeadChar('`', 'e') returns &egrave;.
1107 */
1108 public static int getDeadChar(int accent, int c) {
1109 return KeyCharacterMap.getDeadChar(accent, c);
1110 }
1111
Dianne Hackborn8d374262009-09-14 21:21:52 -07001112 static final boolean DEBUG = false;
1113 static final String TAG = "KeyEvent";
Jeff Brown1f245102010-11-18 20:53:46 -08001114
1115 private static final int MAX_RECYCLED = 10;
1116 private static final Object gRecyclerLock = new Object();
1117 private static int gRecyclerUsed;
1118 private static KeyEvent gRecyclerTop;
1119
1120 private KeyEvent mNext;
1121 private boolean mRecycled;
1122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 private int mMetaState;
1124 private int mAction;
1125 private int mKeyCode;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001126 private int mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 private int mRepeatCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 private int mFlags;
1129 private long mDownTime;
1130 private long mEventTime;
1131 private String mCharacters;
1132
1133 public interface Callback {
1134 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001135 * Called when a key down event has occurred. If you return true,
1136 * you can first call {@link KeyEvent#startTracking()
1137 * KeyEvent.startTracking()} to have the framework track the event
1138 * through its {@link #onKeyUp(int, KeyEvent)} and also call your
1139 * {@link #onKeyLongPress(int, KeyEvent)} if it occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 *
1141 * @param keyCode The value in event.getKeyCode().
1142 * @param event Description of the key event.
1143 *
1144 * @return If you handled the event, return true. If you want to allow
1145 * the event to be handled by the next receiver, return false.
1146 */
1147 boolean onKeyDown(int keyCode, KeyEvent event);
1148
1149 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001150 * Called when a long press has occurred. If you return true,
1151 * the final key up will have {@link KeyEvent#FLAG_CANCELED} and
1152 * {@link KeyEvent#FLAG_CANCELED_LONG_PRESS} set. Note that in
1153 * order to receive this callback, someone in the event change
1154 * <em>must</em> return true from {@link #onKeyDown} <em>and</em>
1155 * call {@link KeyEvent#startTracking()} on the event.
1156 *
1157 * @param keyCode The value in event.getKeyCode().
1158 * @param event Description of the key event.
1159 *
1160 * @return If you handled the event, return true. If you want to allow
1161 * the event to be handled by the next receiver, return false.
1162 */
1163 boolean onKeyLongPress(int keyCode, KeyEvent event);
1164
1165 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 * Called when a key up event has occurred.
1167 *
1168 * @param keyCode The value in event.getKeyCode().
1169 * @param event Description of the key event.
1170 *
1171 * @return If you handled the event, return true. If you want to allow
1172 * the event to be handled by the next receiver, return false.
1173 */
1174 boolean onKeyUp(int keyCode, KeyEvent event);
1175
1176 /**
1177 * Called when multiple down/up pairs of the same key have occurred
1178 * in a row.
1179 *
1180 * @param keyCode The value in event.getKeyCode().
1181 * @param count Number of pairs as returned by event.getRepeatCount().
1182 * @param event Description of the key event.
1183 *
1184 * @return If you handled the event, return true. If you want to allow
1185 * the event to be handled by the next receiver, return false.
1186 */
1187 boolean onKeyMultiple(int keyCode, int count, KeyEvent event);
1188 }
1189
Jeff Brown497a92c2010-09-12 17:55:08 -07001190 static {
1191 if (META_SYMBOLIC_NAMES.length != 32) {
1192 throw new IllegalStateException(
1193 "META_SYMBOLIC_NAMES array should contain exactly 32 entries.");
1194 }
1195 if (KEYCODE_SYMBOLIC_NAMES.length != LAST_KEYCODE + 1) {
1196 throw new IllegalStateException(
1197 "KEYCODE_SYMBOLIC_NAMES array is out of sync with the keycode constants.");
1198 }
1199 }
1200
Jeff Brown1f245102010-11-18 20:53:46 -08001201 private KeyEvent() {
1202 }
1203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 /**
1205 * Create a new key event.
1206 *
1207 * @param action Action code: either {@link #ACTION_DOWN},
1208 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1209 * @param code The key code.
1210 */
1211 public KeyEvent(int action, int code) {
1212 mAction = action;
1213 mKeyCode = code;
1214 mRepeatCount = 0;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001215 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 }
1217
1218 /**
1219 * Create a new key event.
1220 *
1221 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1222 * at which this key code originally went down.
1223 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1224 * at which this event happened.
1225 * @param action Action code: either {@link #ACTION_DOWN},
1226 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1227 * @param code The key code.
1228 * @param repeat A repeat count for down events (> 0 if this is after the
1229 * initial down) or event count for multiple events.
1230 */
1231 public KeyEvent(long downTime, long eventTime, int action,
1232 int code, int repeat) {
1233 mDownTime = downTime;
1234 mEventTime = eventTime;
1235 mAction = action;
1236 mKeyCode = code;
1237 mRepeatCount = repeat;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001238 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 }
1240
1241 /**
1242 * Create a new key event.
1243 *
1244 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1245 * at which this key code originally went down.
1246 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1247 * at which this event happened.
1248 * @param action Action code: either {@link #ACTION_DOWN},
1249 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1250 * @param code The key code.
1251 * @param repeat A repeat count for down events (> 0 if this is after the
1252 * initial down) or event count for multiple events.
1253 * @param metaState Flags indicating which meta keys are currently pressed.
1254 */
1255 public KeyEvent(long downTime, long eventTime, int action,
1256 int code, int repeat, int metaState) {
1257 mDownTime = downTime;
1258 mEventTime = eventTime;
1259 mAction = action;
1260 mKeyCode = code;
1261 mRepeatCount = repeat;
1262 mMetaState = metaState;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001263 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 }
1265
1266 /**
1267 * Create a new key event.
1268 *
1269 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1270 * at which this key code originally went down.
1271 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1272 * at which this event happened.
1273 * @param action Action code: either {@link #ACTION_DOWN},
1274 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1275 * @param code The key code.
1276 * @param repeat A repeat count for down events (> 0 if this is after the
1277 * initial down) or event count for multiple events.
1278 * @param metaState Flags indicating which meta keys are currently pressed.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001279 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 * @param scancode Raw device scan code of the event.
1281 */
1282 public KeyEvent(long downTime, long eventTime, int action,
1283 int code, int repeat, int metaState,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001284 int deviceId, int scancode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 mDownTime = downTime;
1286 mEventTime = eventTime;
1287 mAction = action;
1288 mKeyCode = code;
1289 mRepeatCount = repeat;
1290 mMetaState = metaState;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001291 mDeviceId = deviceId;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001292 mScanCode = scancode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 }
1294
1295 /**
1296 * Create a new key event.
1297 *
1298 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1299 * at which this key code originally went down.
1300 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1301 * at which this event happened.
1302 * @param action Action code: either {@link #ACTION_DOWN},
1303 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1304 * @param code The key code.
1305 * @param repeat A repeat count for down events (> 0 if this is after the
1306 * initial down) or event count for multiple events.
1307 * @param metaState Flags indicating which meta keys are currently pressed.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001308 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 * @param scancode Raw device scan code of the event.
1310 * @param flags The flags for this key event
1311 */
1312 public KeyEvent(long downTime, long eventTime, int action,
1313 int code, int repeat, int metaState,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001314 int deviceId, int scancode, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 mDownTime = downTime;
1316 mEventTime = eventTime;
1317 mAction = action;
1318 mKeyCode = code;
1319 mRepeatCount = repeat;
1320 mMetaState = metaState;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001321 mDeviceId = deviceId;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001322 mScanCode = scancode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 mFlags = flags;
1324 }
1325
1326 /**
Jeff Brownc5ed5912010-07-14 18:48:53 -07001327 * Create a new key event.
1328 *
1329 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1330 * at which this key code originally went down.
1331 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1332 * at which this event happened.
1333 * @param action Action code: either {@link #ACTION_DOWN},
1334 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1335 * @param code The key code.
1336 * @param repeat A repeat count for down events (> 0 if this is after the
1337 * initial down) or event count for multiple events.
1338 * @param metaState Flags indicating which meta keys are currently pressed.
1339 * @param deviceId The device ID that generated the key event.
1340 * @param scancode Raw device scan code of the event.
1341 * @param flags The flags for this key event
1342 * @param source The input source such as {@link InputDevice#SOURCE_KEYBOARD}.
1343 */
1344 public KeyEvent(long downTime, long eventTime, int action,
1345 int code, int repeat, int metaState,
1346 int deviceId, int scancode, int flags, int source) {
1347 mDownTime = downTime;
1348 mEventTime = eventTime;
1349 mAction = action;
1350 mKeyCode = code;
1351 mRepeatCount = repeat;
1352 mMetaState = metaState;
1353 mDeviceId = deviceId;
1354 mScanCode = scancode;
1355 mFlags = flags;
1356 mSource = source;
1357 }
1358
1359 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 * Create a new key event for a string of characters. The key code,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001361 * action, repeat count and source will automatically be set to
1362 * {@link #KEYCODE_UNKNOWN}, {@link #ACTION_MULTIPLE}, 0, and
1363 * {@link InputDevice#SOURCE_KEYBOARD} for you.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 *
1365 * @param time The time (in {@link android.os.SystemClock#uptimeMillis})
1366 * at which this event occured.
1367 * @param characters The string of characters.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001368 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 * @param flags The flags for this key event
1370 */
Jeff Brownc5ed5912010-07-14 18:48:53 -07001371 public KeyEvent(long time, String characters, int deviceId, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 mDownTime = time;
1373 mEventTime = time;
1374 mCharacters = characters;
1375 mAction = ACTION_MULTIPLE;
1376 mKeyCode = KEYCODE_UNKNOWN;
1377 mRepeatCount = 0;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001378 mDeviceId = deviceId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 mFlags = flags;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001380 mSource = InputDevice.SOURCE_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 }
1382
1383 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001384 * Make an exact copy of an existing key event.
1385 */
1386 public KeyEvent(KeyEvent origEvent) {
1387 mDownTime = origEvent.mDownTime;
1388 mEventTime = origEvent.mEventTime;
1389 mAction = origEvent.mAction;
1390 mKeyCode = origEvent.mKeyCode;
1391 mRepeatCount = origEvent.mRepeatCount;
1392 mMetaState = origEvent.mMetaState;
1393 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001394 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001395 mScanCode = origEvent.mScanCode;
The Android Open Source Project10592532009-03-18 17:39:46 -07001396 mFlags = origEvent.mFlags;
1397 mCharacters = origEvent.mCharacters;
1398 }
1399
1400 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 * Copy an existing key event, modifying its time and repeat count.
1402 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001403 * @deprecated Use {@link #changeTimeRepeat(KeyEvent, long, int)}
1404 * instead.
1405 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 * @param origEvent The existing event to be copied.
1407 * @param eventTime The new event time
1408 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1409 * @param newRepeat The new repeat count of the event.
1410 */
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001411 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 public KeyEvent(KeyEvent origEvent, long eventTime, int newRepeat) {
1413 mDownTime = origEvent.mDownTime;
1414 mEventTime = eventTime;
1415 mAction = origEvent.mAction;
1416 mKeyCode = origEvent.mKeyCode;
1417 mRepeatCount = newRepeat;
1418 mMetaState = origEvent.mMetaState;
1419 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001420 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001421 mScanCode = origEvent.mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 mFlags = origEvent.mFlags;
1423 mCharacters = origEvent.mCharacters;
1424 }
1425
Jeff Brown1f245102010-11-18 20:53:46 -08001426 private static KeyEvent obtain() {
1427 final KeyEvent ev;
1428 synchronized (gRecyclerLock) {
1429 ev = gRecyclerTop;
1430 if (ev == null) {
1431 return new KeyEvent();
1432 }
1433 gRecyclerTop = ev.mNext;
1434 gRecyclerUsed -= 1;
1435 }
1436 ev.mRecycled = false;
1437 ev.mNext = null;
1438 return ev;
1439 }
1440
1441 /**
1442 * Obtains a (potentially recycled) key event.
1443 *
1444 * @hide
1445 */
1446 public static KeyEvent obtain(long downTime, long eventTime, int action,
1447 int code, int repeat, int metaState,
1448 int deviceId, int scancode, int flags, int source, String characters) {
1449 KeyEvent ev = obtain();
1450 ev.mDownTime = downTime;
1451 ev.mEventTime = eventTime;
1452 ev.mAction = action;
1453 ev.mKeyCode = code;
1454 ev.mRepeatCount = repeat;
1455 ev.mMetaState = metaState;
1456 ev.mDeviceId = deviceId;
1457 ev.mScanCode = scancode;
1458 ev.mFlags = flags;
1459 ev.mSource = source;
1460 ev.mCharacters = characters;
1461 return ev;
1462 }
1463
1464 /**
1465 * Recycles a key event.
1466 * Key events should only be recycled if they are owned by the system since user
1467 * code expects them to be essentially immutable, "tracking" notwithstanding.
1468 *
1469 * @hide
1470 */
1471 public final void recycle() {
1472 if (mRecycled) {
1473 throw new RuntimeException(toString() + " recycled twice!");
1474 }
1475 mRecycled = true;
1476 mCharacters = null;
1477
1478 synchronized (gRecyclerLock) {
1479 if (gRecyclerUsed < MAX_RECYCLED) {
1480 gRecyclerUsed++;
1481 mNext = gRecyclerTop;
1482 gRecyclerTop = this;
1483 }
1484 }
1485 }
1486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001488 * Create a new key event that is the same as the given one, but whose
1489 * event time and repeat count are replaced with the given value.
1490 *
1491 * @param event The existing event to be copied. This is not modified.
1492 * @param eventTime The new event time
1493 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1494 * @param newRepeat The new repeat count of the event.
1495 */
1496 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime,
1497 int newRepeat) {
1498 return new KeyEvent(event, eventTime, newRepeat);
1499 }
1500
1501 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001502 * Create a new key event that is the same as the given one, but whose
1503 * event time and repeat count are replaced with the given value.
1504 *
1505 * @param event The existing event to be copied. This is not modified.
1506 * @param eventTime The new event time
1507 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1508 * @param newRepeat The new repeat count of the event.
1509 * @param newFlags New flags for the event, replacing the entire value
1510 * in the original event.
1511 */
1512 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime,
1513 int newRepeat, int newFlags) {
1514 KeyEvent ret = new KeyEvent(event);
1515 ret.mEventTime = eventTime;
1516 ret.mRepeatCount = newRepeat;
1517 ret.mFlags = newFlags;
1518 return ret;
1519 }
1520
1521 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 * Copy an existing key event, modifying its action.
1523 *
1524 * @param origEvent The existing event to be copied.
1525 * @param action The new action code of the event.
1526 */
The Android Open Source Project10592532009-03-18 17:39:46 -07001527 private KeyEvent(KeyEvent origEvent, int action) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 mDownTime = origEvent.mDownTime;
1529 mEventTime = origEvent.mEventTime;
1530 mAction = action;
1531 mKeyCode = origEvent.mKeyCode;
1532 mRepeatCount = origEvent.mRepeatCount;
1533 mMetaState = origEvent.mMetaState;
1534 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001535 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001536 mScanCode = origEvent.mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 mFlags = origEvent.mFlags;
1538 // Don't copy mCharacters, since one way or the other we'll lose it
1539 // when changing the action.
1540 }
1541
1542 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001543 * Create a new key event that is the same as the given one, but whose
1544 * action is replaced with the given value.
1545 *
1546 * @param event The existing event to be copied. This is not modified.
1547 * @param action The new action code of the event.
1548 */
1549 public static KeyEvent changeAction(KeyEvent event, int action) {
1550 return new KeyEvent(event, action);
1551 }
1552
1553 /**
1554 * Create a new key event that is the same as the given one, but whose
1555 * flags are replaced with the given value.
1556 *
1557 * @param event The existing event to be copied. This is not modified.
1558 * @param flags The new flags constant.
1559 */
1560 public static KeyEvent changeFlags(KeyEvent event, int flags) {
1561 event = new KeyEvent(event);
1562 event.mFlags = flags;
1563 return event;
1564 }
1565
1566 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 * Don't use in new code, instead explicitly check
1568 * {@link #getAction()}.
1569 *
1570 * @return If the action is ACTION_DOWN, returns true; else false.
1571 *
1572 * @deprecated
1573 * @hide
1574 */
1575 @Deprecated public final boolean isDown() {
1576 return mAction == ACTION_DOWN;
1577 }
1578
1579 /**
1580 * Is this a system key? System keys can not be used for menu shortcuts.
1581 *
1582 * TODO: this information should come from a table somewhere.
1583 * TODO: should the dpad keys be here? arguably, because they also shouldn't be menu shortcuts
1584 */
1585 public final boolean isSystem() {
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -07001586 return native_isSystemKey(mKeyCode);
1587 }
1588
1589 /** @hide */
1590 public final boolean hasDefaultAction() {
1591 return native_hasDefaultAction(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 }
1593
1594
1595 /**
1596 * <p>Returns the state of the meta keys.</p>
1597 *
1598 * @return an integer in which each bit set to 1 represents a pressed
1599 * meta key
1600 *
1601 * @see #isAltPressed()
1602 * @see #isShiftPressed()
1603 * @see #isSymPressed()
Jeff Brown497a92c2010-09-12 17:55:08 -07001604 * @see #isCtrlPressed()
1605 * @see #isMetaPressed()
1606 * @see #isFunctionPressed()
Jeff Brown51e7fe72010-10-29 22:19:53 -07001607 * @see #isCapsLockOn()
1608 * @see #isNumLockOn()
1609 * @see #isScrollLockOn()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 * @see #META_ALT_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001611 * @see #META_ALT_LEFT_ON
1612 * @see #META_ALT_RIGHT_ON
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 * @see #META_SHIFT_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001614 * @see #META_SHIFT_LEFT_ON
1615 * @see #META_SHIFT_RIGHT_ON
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 * @see #META_SYM_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001617 * @see #META_FUNCTION_ON
1618 * @see #META_CTRL_ON
1619 * @see #META_CTRL_LEFT_ON
1620 * @see #META_CTRL_RIGHT_ON
1621 * @see #META_META_ON
1622 * @see #META_META_LEFT_ON
1623 * @see #META_META_RIGHT_ON
Jeff Brown51e7fe72010-10-29 22:19:53 -07001624 * @see #META_CAPS_LOCK_ON
1625 * @see #META_NUM_LOCK_ON
1626 * @see #META_SCROLL_LOCK_ON
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627 */
1628 public final int getMetaState() {
1629 return mMetaState;
1630 }
1631
1632 /**
1633 * Returns the flags for this key event.
1634 *
1635 * @see #FLAG_WOKE_HERE
1636 */
1637 public final int getFlags() {
1638 return mFlags;
1639 }
1640
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001641 // Mask of all modifier key meta states. Specifically excludes locked keys like caps lock.
1642 private static final int META_MODIFIER_MASK =
1643 META_SHIFT_ON | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON
1644 | META_ALT_ON | META_ALT_LEFT_ON | META_ALT_RIGHT_ON
1645 | META_CTRL_ON | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON
1646 | META_META_ON | META_META_LEFT_ON | META_META_RIGHT_ON
1647 | META_SYM_ON | META_FUNCTION_ON;
1648
1649 // Mask of all lock key meta states.
1650 private static final int META_LOCK_MASK =
1651 META_CAPS_LOCK_ON | META_NUM_LOCK_ON | META_SCROLL_LOCK_ON;
1652
1653 // Mask of all valid meta states.
1654 private static final int META_ALL_MASK = META_MODIFIER_MASK | META_LOCK_MASK;
1655
1656 // Mask of all synthetic meta states that are reserved for API compatibility with
1657 // historical uses in MetaKeyKeyListener.
1658 private static final int META_SYNTHETIC_MASK =
1659 META_CAP_LOCKED | META_ALT_LOCKED | META_SYM_LOCKED | META_SELECTING;
1660
1661 // Mask of all meta states that are not valid use in specifying a modifier key.
1662 // These bits are known to be used for purposes other than specifying modifiers.
1663 private static final int META_INVALID_MODIFIER_MASK =
1664 META_LOCK_MASK | META_SYNTHETIC_MASK;
1665
1666 /**
1667 * Gets a mask that includes all valid modifier key meta state bits.
1668 * <p>
1669 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1670 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1671 * not considered modifier keys. Consequently, the mask specifically excludes
1672 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1673 * </p>
1674 *
1675 * @return The modifier meta state mask which is a combination of
1676 * {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON}, {@link #META_SHIFT_RIGHT_ON},
1677 * {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON}, {@link #META_ALT_RIGHT_ON},
1678 * {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON}, {@link #META_CTRL_RIGHT_ON},
1679 * {@link #META_META_ON}, {@link #META_META_LEFT_ON}, {@link #META_META_RIGHT_ON},
1680 * {@link #META_SYM_ON}, {@link #META_FUNCTION_ON}.
1681 */
1682 public static int getModifierMetaStateMask() {
1683 return META_MODIFIER_MASK;
1684 }
1685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 /**
1687 * Returns true if this key code is a modifier key.
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001688 * <p>
1689 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1690 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1691 * not considered modifier keys. Consequently, this function return false
1692 * for those keys.
1693 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 *
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001695 * @return True if the key code is one of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 * {@link #KEYCODE_SHIFT_LEFT} {@link #KEYCODE_SHIFT_RIGHT},
Jeff Brown497a92c2010-09-12 17:55:08 -07001697 * {@link #KEYCODE_ALT_LEFT}, {@link #KEYCODE_ALT_RIGHT},
Jeff Brown497a92c2010-09-12 17:55:08 -07001698 * {@link #KEYCODE_CTRL_LEFT}, {@link #KEYCODE_CTRL_RIGHT},
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001699 * {@link #KEYCODE_META_LEFT}, or {@link #KEYCODE_META_RIGHT},
1700 * {@link #KEYCODE_SYM}, {@link #KEYCODE_NUM}, {@link #KEYCODE_FUNCTION}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 */
1702 public static boolean isModifierKey(int keyCode) {
Jeff Brown497a92c2010-09-12 17:55:08 -07001703 switch (keyCode) {
1704 case KEYCODE_SHIFT_LEFT:
1705 case KEYCODE_SHIFT_RIGHT:
1706 case KEYCODE_ALT_LEFT:
1707 case KEYCODE_ALT_RIGHT:
Jeff Brown497a92c2010-09-12 17:55:08 -07001708 case KEYCODE_CTRL_LEFT:
1709 case KEYCODE_CTRL_RIGHT:
1710 case KEYCODE_META_LEFT:
1711 case KEYCODE_META_RIGHT:
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001712 case KEYCODE_SYM:
1713 case KEYCODE_NUM:
1714 case KEYCODE_FUNCTION:
Jeff Brown497a92c2010-09-12 17:55:08 -07001715 return true;
1716 default:
1717 return false;
1718 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001719 }
1720
1721 /**
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001722 * Normalizes the specified meta state.
1723 * <p>
1724 * The meta state is normalized such that if either the left or right modifier meta state
1725 * bits are set then the result will also include the universal bit for that modifier.
1726 * </p><p>
1727 * If the specified meta state contains {@link #META_ALT_LEFT_ON} then
1728 * the result will also contain {@link #META_ALT_ON} in addition to {@link #META_ALT_LEFT_ON}
1729 * and the other bits that were specified in the input. The same is process is
1730 * performed for shift, control and meta.
1731 * </p><p>
1732 * If the specified meta state contains synthetic meta states defined by
1733 * {@link MetaKeyKeyListener}, then those states are translated here and the original
1734 * synthetic meta states are removed from the result.
1735 * {@link MetaKeyKeyListener#META_CAP_LOCKED} is translated to {@link #META_CAPS_LOCK_ON}.
1736 * {@link MetaKeyKeyListener#META_ALT_LOCKED} is translated to {@link #META_ALT_ON}.
1737 * {@link MetaKeyKeyListener#META_SYM_LOCKED} is translated to {@link #META_SYM_ON}.
1738 * </p><p>
1739 * Undefined meta state bits are removed.
1740 * </p>
1741 *
1742 * @param metaState The meta state.
1743 * @return The normalized meta state.
1744 */
1745 public static int normalizeMetaState(int metaState) {
1746 if ((metaState & (META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON)) != 0) {
1747 metaState |= META_SHIFT_ON;
1748 }
1749 if ((metaState & (META_ALT_LEFT_ON | META_ALT_RIGHT_ON)) != 0) {
1750 metaState |= META_ALT_ON;
1751 }
1752 if ((metaState & (META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON)) != 0) {
1753 metaState |= META_CTRL_ON;
1754 }
1755 if ((metaState & (META_META_LEFT_ON | META_META_RIGHT_ON)) != 0) {
1756 metaState |= META_META_ON;
1757 }
1758 if ((metaState & MetaKeyKeyListener.META_CAP_LOCKED) != 0) {
1759 metaState |= META_CAPS_LOCK_ON;
1760 }
1761 if ((metaState & MetaKeyKeyListener.META_ALT_LOCKED) != 0) {
1762 metaState |= META_ALT_ON;
1763 }
1764 if ((metaState & MetaKeyKeyListener.META_SYM_LOCKED) != 0) {
1765 metaState |= META_SYM_ON;
1766 }
1767 return metaState & META_ALL_MASK;
1768 }
1769
1770 /**
1771 * Returns true if no modifiers keys are pressed according to the specified meta state.
1772 * <p>
1773 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1774 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1775 * not considered modifier keys. Consequently, this function ignores
1776 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1777 * </p><p>
1778 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}.
1779 * </p>
1780 *
1781 * @param metaState The meta state to consider.
1782 * @return True if no modifier keys are pressed.
1783 * @see #hasNoModifiers()
1784 */
1785 public static boolean metaStateHasNoModifiers(int metaState) {
1786 return (normalizeMetaState(metaState) & META_MODIFIER_MASK) == 0;
1787 }
1788
1789 /**
1790 * Returns true if only the specified modifier keys are pressed according to
1791 * the specified meta state. Returns false if a different combination of modifier
1792 * keys are pressed.
1793 * <p>
1794 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1795 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1796 * not considered modifier keys. Consequently, this function ignores
1797 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1798 * </p><p>
1799 * If the specified modifier mask includes directional modifiers, such as
1800 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the
1801 * modifier is pressed on that side.
1802 * If the specified modifier mask includes non-directional modifiers, such as
1803 * {@link #META_SHIFT_ON}, then this method ensures that the modifier
1804 * is pressed on either side.
1805 * If the specified modifier mask includes both directional and non-directional modifiers
1806 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON},
1807 * then this method throws an illegal argument exception.
1808 * </p>
1809 *
1810 * @param metaState The meta state to consider.
1811 * @param modifiers The meta state of the modifier keys to check. May be a combination
1812 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to
1813 * ensure that no modifier keys are pressed.
1814 * @return True if only the specified modifier keys are pressed.
1815 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers
1816 * @see #hasModifiers
1817 */
1818 public static boolean metaStateHasModifiers(int metaState, int modifiers) {
1819 // Note: For forward compatibility, we allow the parameter to contain meta states
1820 // that we do not recognize but we explicitly disallow meta states that
1821 // are not valid modifiers.
1822 if ((modifiers & META_INVALID_MODIFIER_MASK) != 0) {
1823 throw new IllegalArgumentException("modifiers must not contain "
1824 + "META_CAPS_LOCK_ON, META_NUM_LOCK_ON, META_SCROLL_LOCK_ON, "
1825 + "META_CAP_LOCKED, META_ALT_LOCKED, META_SYM_LOCKED, "
1826 + "or META_SELECTING");
1827 }
1828
1829 metaState = normalizeMetaState(metaState) & META_MODIFIER_MASK;
1830 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
1831 META_SHIFT_ON, META_SHIFT_LEFT_ON, META_SHIFT_RIGHT_ON);
1832 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
1833 META_ALT_ON, META_ALT_LEFT_ON, META_ALT_RIGHT_ON);
1834 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
1835 META_CTRL_ON, META_CTRL_LEFT_ON, META_CTRL_RIGHT_ON);
1836 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
1837 META_META_ON, META_META_LEFT_ON, META_META_RIGHT_ON);
1838 return metaState == modifiers;
1839 }
1840
1841 private static int metaStateFilterDirectionalModifiers(int metaState,
1842 int modifiers, int basic, int left, int right) {
1843 final boolean wantBasic = (modifiers & basic) != 0;
1844 final int directional = left | right;
1845 final boolean wantLeftOrRight = (modifiers & directional) != 0;
1846
1847 if (wantBasic) {
1848 if (wantLeftOrRight) {
1849 throw new IllegalArgumentException("modifiers must not contain "
1850 + metaStateToString(basic) + " combined with "
1851 + metaStateToString(left) + " or " + metaStateToString(right));
1852 }
1853 return metaState & ~directional;
1854 } else if (wantLeftOrRight) {
1855 return metaState & ~basic;
1856 } else {
1857 return metaState;
1858 }
1859 }
1860
1861 /**
1862 * Returns true if no modifier keys are pressed.
1863 * <p>
1864 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1865 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1866 * not considered modifier keys. Consequently, this function ignores
1867 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1868 * </p><p>
1869 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}.
1870 * </p>
1871 *
1872 * @return True if no modifier keys are pressed.
1873 * @see #metaStateHasNoModifiers
1874 */
1875 public final boolean hasNoModifiers() {
1876 return metaStateHasNoModifiers(mMetaState);
1877 }
1878
1879 /**
1880 * Returns true if only the specified modifiers keys are pressed.
1881 * Returns false if a different combination of modifier keys are pressed.
1882 * <p>
1883 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1884 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1885 * not considered modifier keys. Consequently, this function ignores
1886 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1887 * </p><p>
1888 * If the specified modifier mask includes directional modifiers, such as
1889 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the
1890 * modifier is pressed on that side.
1891 * If the specified modifier mask includes non-directional modifiers, such as
1892 * {@link #META_SHIFT_ON}, then this method ensures that the modifier
1893 * is pressed on either side.
1894 * If the specified modifier mask includes both directional and non-directional modifiers
1895 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON},
1896 * then this method throws an illegal argument exception.
1897 * </p>
1898 *
1899 * @param modifiers The meta state of the modifier keys to check. May be a combination
1900 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to
1901 * ensure that no modifier keys are pressed.
1902 * @return True if only the specified modifier keys are pressed.
1903 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers
1904 * @see #metaStateHasModifiers
1905 */
1906 public final boolean hasModifiers(int modifiers) {
1907 return metaStateHasModifiers(mMetaState, modifiers);
1908 }
1909
1910 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 * <p>Returns the pressed state of the ALT meta key.</p>
1912 *
1913 * @return true if the ALT key is pressed, false otherwise
1914 *
1915 * @see #KEYCODE_ALT_LEFT
1916 * @see #KEYCODE_ALT_RIGHT
1917 * @see #META_ALT_ON
1918 */
1919 public final boolean isAltPressed() {
1920 return (mMetaState & META_ALT_ON) != 0;
1921 }
1922
1923 /**
1924 * <p>Returns the pressed state of the SHIFT meta key.</p>
1925 *
1926 * @return true if the SHIFT key is pressed, false otherwise
1927 *
1928 * @see #KEYCODE_SHIFT_LEFT
1929 * @see #KEYCODE_SHIFT_RIGHT
1930 * @see #META_SHIFT_ON
1931 */
1932 public final boolean isShiftPressed() {
1933 return (mMetaState & META_SHIFT_ON) != 0;
1934 }
1935
1936 /**
1937 * <p>Returns the pressed state of the SYM meta key.</p>
1938 *
1939 * @return true if the SYM key is pressed, false otherwise
1940 *
1941 * @see #KEYCODE_SYM
1942 * @see #META_SYM_ON
1943 */
1944 public final boolean isSymPressed() {
1945 return (mMetaState & META_SYM_ON) != 0;
1946 }
1947
1948 /**
Jeff Brown497a92c2010-09-12 17:55:08 -07001949 * <p>Returns the pressed state of the CTRL meta key.</p>
1950 *
1951 * @return true if the CTRL key is pressed, false otherwise
1952 *
1953 * @see #KEYCODE_CTRL_LEFT
1954 * @see #KEYCODE_CTRL_RIGHT
1955 * @see #META_CTRL_ON
1956 */
1957 public final boolean isCtrlPressed() {
1958 return (mMetaState & META_CTRL_ON) != 0;
1959 }
1960
1961 /**
1962 * <p>Returns the pressed state of the META meta key.</p>
1963 *
1964 * @return true if the META key is pressed, false otherwise
1965 *
1966 * @see #KEYCODE_META_LEFT
1967 * @see #KEYCODE_META_RIGHT
1968 * @see #META_META_ON
1969 */
1970 public final boolean isMetaPressed() {
1971 return (mMetaState & META_META_ON) != 0;
1972 }
1973
1974 /**
1975 * <p>Returns the pressed state of the FUNCTION meta key.</p>
1976 *
1977 * @return true if the FUNCTION key is pressed, false otherwise
1978 *
1979 * @see #KEYCODE_FUNCTION
1980 * @see #META_FUNCTION_ON
1981 */
1982 public final boolean isFunctionPressed() {
1983 return (mMetaState & META_FUNCTION_ON) != 0;
1984 }
1985
1986 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07001987 * <p>Returns the locked state of the CAPS LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07001988 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07001989 * @return true if the CAPS LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07001990 *
1991 * @see #KEYCODE_CAPS_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07001992 * @see #META_CAPS_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001993 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07001994 public final boolean isCapsLockOn() {
1995 return (mMetaState & META_CAPS_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07001996 }
1997
1998 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07001999 * <p>Returns the locked state of the NUM LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002000 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002001 * @return true if the NUM LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002002 *
2003 * @see #KEYCODE_NUM_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002004 * @see #META_NUM_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002005 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002006 public final boolean isNumLockOn() {
2007 return (mMetaState & META_NUM_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002008 }
2009
2010 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07002011 * <p>Returns the locked state of the SCROLL LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002012 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002013 * @return true if the SCROLL LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002014 *
2015 * @see #KEYCODE_SCROLL_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002016 * @see #META_SCROLL_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002017 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002018 public final boolean isScrollLockOn() {
2019 return (mMetaState & META_SCROLL_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002020 }
2021
2022 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 * Retrieve the action of this key event. May be either
2024 * {@link #ACTION_DOWN}, {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
2025 *
2026 * @return The event action: ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.
2027 */
2028 public final int getAction() {
2029 return mAction;
2030 }
2031
2032 /**
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07002033 * For {@link #ACTION_UP} events, indicates that the event has been
2034 * canceled as per {@link #FLAG_CANCELED}.
2035 */
2036 public final boolean isCanceled() {
2037 return (mFlags&FLAG_CANCELED) != 0;
2038 }
2039
2040 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002041 * Call this during {@link Callback#onKeyDown} to have the system track
2042 * the key through its final up (possibly including a long press). Note
2043 * that only one key can be tracked at a time -- if another key down
2044 * event is received while a previous one is being tracked, tracking is
2045 * stopped on the previous event.
2046 */
2047 public final void startTracking() {
2048 mFlags |= FLAG_START_TRACKING;
2049 }
2050
2051 /**
2052 * For {@link #ACTION_UP} events, indicates that the event is still being
2053 * tracked from its initial down event as per
2054 * {@link #FLAG_TRACKING}.
2055 */
2056 public final boolean isTracking() {
2057 return (mFlags&FLAG_TRACKING) != 0;
2058 }
2059
2060 /**
2061 * For {@link #ACTION_DOWN} events, indicates that the event has been
2062 * canceled as per {@link #FLAG_LONG_PRESS}.
2063 */
2064 public final boolean isLongPress() {
2065 return (mFlags&FLAG_LONG_PRESS) != 0;
2066 }
2067
2068 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 * Retrieve the key code of the key event. This is the physical key that
2070 * was pressed, <em>not</em> the Unicode character.
2071 *
2072 * @return The key code of the event.
2073 */
2074 public final int getKeyCode() {
2075 return mKeyCode;
2076 }
2077
2078 /**
2079 * For the special case of a {@link #ACTION_MULTIPLE} event with key
2080 * code of {@link #KEYCODE_UNKNOWN}, this is a raw string of characters
2081 * associated with the event. In all other cases it is null.
2082 *
2083 * @return Returns a String of 1 or more characters associated with
2084 * the event.
2085 */
2086 public final String getCharacters() {
2087 return mCharacters;
2088 }
2089
2090 /**
2091 * Retrieve the hardware key id of this key event. These values are not
2092 * reliable and vary from device to device.
2093 *
2094 * {@more}
2095 * Mostly this is here for debugging purposes.
2096 */
2097 public final int getScanCode() {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002098 return mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 }
2100
2101 /**
2102 * Retrieve the repeat count of the event. For both key up and key down
2103 * events, this is the number of times the key has repeated with the first
2104 * down starting at 0 and counting up from there. For multiple key
2105 * events, this is the number of down/up pairs that have occurred.
2106 *
2107 * @return The number of times the key has repeated.
2108 */
2109 public final int getRepeatCount() {
2110 return mRepeatCount;
2111 }
2112
2113 /**
2114 * Retrieve the time of the most recent key down event,
2115 * in the {@link android.os.SystemClock#uptimeMillis} time base. If this
2116 * is a down event, this will be the same as {@link #getEventTime()}.
2117 * Note that when chording keys, this value is the down time of the
2118 * most recently pressed key, which may <em>not</em> be the same physical
2119 * key of this event.
2120 *
2121 * @return Returns the most recent key down time, in the
2122 * {@link android.os.SystemClock#uptimeMillis} time base
2123 */
2124 public final long getDownTime() {
2125 return mDownTime;
2126 }
2127
2128 /**
2129 * Retrieve the time this event occurred,
2130 * in the {@link android.os.SystemClock#uptimeMillis} time base.
2131 *
2132 * @return Returns the time this event occurred,
2133 * in the {@link android.os.SystemClock#uptimeMillis} time base.
2134 */
2135 public final long getEventTime() {
2136 return mEventTime;
2137 }
2138
2139 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 * Renamed to {@link #getDeviceId}.
2141 *
2142 * @hide
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002143 * @deprecated use {@link #getDeviceId()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002145 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 public final int getKeyboardDevice() {
2147 return mDeviceId;
2148 }
2149
2150 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002151 * Gets the {@link KeyCharacterMap} associated with the keyboard device.
2152 *
2153 * @return The associated key character map.
2154 * @throws {@link KeyCharacterMapUnavailableException} if the key character map
2155 * could not be loaded because it was malformed or the default key character map
2156 * is missing from the system.
2157 *
2158 * @see {@link KeyCharacterMap#load}
2159 */
2160 public final KeyCharacterMap getKeyCharacterMap() {
2161 return KeyCharacterMap.load(mDeviceId);
2162 }
2163
2164 /**
2165 * Gets the primary character for this key.
2166 * In other words, the label that is physically printed on it.
2167 *
2168 * @return The display label character, or 0 if none (eg. for non-printing keys).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 */
2170 public char getDisplayLabel() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002171 return getKeyCharacterMap().getDisplayLabel(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 }
2173
2174 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002175 * Gets the Unicode character generated by the specified key and meta
2176 * key state combination.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002177 * <p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002178 * Returns the Unicode character that the specified key would produce
2179 * when the specified meta bits (see {@link MetaKeyKeyListener})
2180 * were active.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181 * </p><p>
2182 * Returns 0 if the key is not one that is used to type Unicode
2183 * characters.
2184 * </p><p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002185 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the
2186 * key is a "dead key" that should be combined with another to
2187 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} --
2188 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 * </p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002190 *
2191 * @return The associated character or combining accent, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 */
2193 public int getUnicodeChar() {
2194 return getUnicodeChar(mMetaState);
2195 }
2196
2197 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002198 * Gets the Unicode character generated by the specified key and meta
2199 * key state combination.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 * <p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002201 * Returns the Unicode character that the specified key would produce
2202 * when the specified meta bits (see {@link MetaKeyKeyListener})
2203 * were active.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 * </p><p>
2205 * Returns 0 if the key is not one that is used to type Unicode
2206 * characters.
2207 * </p><p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002208 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the
2209 * key is a "dead key" that should be combined with another to
2210 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} --
2211 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 * </p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002213 *
2214 * @param metaState The meta key modifier state.
2215 * @return The associated character or combining accent, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002217 public int getUnicodeChar(int metaState) {
2218 return getKeyCharacterMap().get(mKeyCode, metaState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219 }
2220
2221 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002222 * Get the character conversion data for a given key code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 *
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002224 * @param results A {@link KeyCharacterMap.KeyData} instance that will be
2225 * filled with the results.
2226 * @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 -08002227 *
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002228 * @deprecated instead use {@link #getDisplayLabel()},
2229 * {@link #getNumber()} or {@link #getUnicodeChar(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002230 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002231 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 public boolean getKeyData(KeyData results) {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002233 return getKeyCharacterMap().getKeyData(mKeyCode, results);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002234 }
2235
2236 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002237 * Gets the first character in the character array that can be generated
2238 * by the specified key code.
2239 * <p>
2240 * This is a convenience function that returns the same value as
2241 * {@link #getMatch(char[],int) getMatch(chars, 0)}.
2242 * </p>
2243 *
2244 * @param chars The array of matching characters to consider.
2245 * @return The matching associated character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 */
2247 public char getMatch(char[] chars) {
2248 return getMatch(chars, 0);
2249 }
2250
2251 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002252 * Gets the first character in the character array that can be generated
2253 * by the specified key code. If there are multiple choices, prefers
2254 * the one that would be generated with the specified meta key modifier state.
2255 *
2256 * @param chars The array of matching characters to consider.
2257 * @param metaState The preferred meta key modifier state.
2258 * @return The matching associated character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002259 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002260 public char getMatch(char[] chars, int metaState) {
2261 return getKeyCharacterMap().getMatch(mKeyCode, chars, metaState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 }
2263
2264 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002265 * Gets the number or symbol associated with the key.
2266 * <p>
2267 * The character value is returned, not the numeric value.
2268 * If the key is not a number, but is a symbol, the symbol is retuned.
2269 * </p><p>
2270 * This method is intended to to support dial pads and other numeric or
2271 * symbolic entry on keyboards where certain keys serve dual function
2272 * as alphabetic and symbolic keys. This method returns the number
2273 * or symbol associated with the key independent of whether the user
2274 * has pressed the required modifier.
2275 * </p><p>
2276 * For example, on one particular keyboard the keys on the top QWERTY row generate
2277 * numbers when ALT is pressed such that ALT-Q maps to '1'. So for that keyboard
2278 * when {@link #getNumber} is called with {@link KeyEvent#KEYCODE_Q} it returns '1'
2279 * so that the user can type numbers without pressing ALT when it makes sense.
2280 * </p>
2281 *
2282 * @return The associated numeric or symbolic character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 */
2284 public char getNumber() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002285 return getKeyCharacterMap().getNumber(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002286 }
2287
2288 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002289 * Returns true if this key produces a glyph.
2290 *
2291 * @return True if the key is a printing key.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002292 */
2293 public boolean isPrintingKey() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002294 return getKeyCharacterMap().isPrintingKey(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002295 }
2296
2297 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002298 * @deprecated Use {@link #dispatch(Callback, DispatcherState, Object)} instead.
2299 */
2300 @Deprecated
2301 public final boolean dispatch(Callback receiver) {
2302 return dispatch(receiver, null, null);
2303 }
2304
2305 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002306 * Deliver this key event to a {@link Callback} interface. If this is
2307 * an ACTION_MULTIPLE event and it is not handled, then an attempt will
2308 * be made to deliver a single normal event.
2309 *
2310 * @param receiver The Callback that will be given the event.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002311 * @param state State information retained across events.
2312 * @param target The target of the dispatch, for use in tracking.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002313 *
2314 * @return The return value from the Callback method that was called.
2315 */
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002316 public final boolean dispatch(Callback receiver, DispatcherState state,
2317 Object target) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002318 switch (mAction) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002319 case ACTION_DOWN: {
2320 mFlags &= ~FLAG_START_TRACKING;
Dianne Hackborn8d374262009-09-14 21:21:52 -07002321 if (DEBUG) Log.v(TAG, "Key down to " + target + " in " + state
2322 + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002323 boolean res = receiver.onKeyDown(mKeyCode, this);
2324 if (state != null) {
2325 if (res && mRepeatCount == 0 && (mFlags&FLAG_START_TRACKING) != 0) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002326 if (DEBUG) Log.v(TAG, " Start tracking!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002327 state.startTracking(this, target);
2328 } else if (isLongPress() && state.isTracking(this)) {
2329 try {
2330 if (receiver.onKeyLongPress(mKeyCode, this)) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002331 if (DEBUG) Log.v(TAG, " Clear from long press!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002332 state.performedLongPress(this);
2333 res = true;
2334 }
2335 } catch (AbstractMethodError e) {
2336 }
2337 }
2338 }
2339 return res;
2340 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 case ACTION_UP:
Dianne Hackborn8d374262009-09-14 21:21:52 -07002342 if (DEBUG) Log.v(TAG, "Key up to " + target + " in " + state
2343 + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002344 if (state != null) {
2345 state.handleUpEvent(this);
2346 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347 return receiver.onKeyUp(mKeyCode, this);
2348 case ACTION_MULTIPLE:
2349 final int count = mRepeatCount;
2350 final int code = mKeyCode;
2351 if (receiver.onKeyMultiple(code, count, this)) {
2352 return true;
2353 }
2354 if (code != KeyEvent.KEYCODE_UNKNOWN) {
2355 mAction = ACTION_DOWN;
2356 mRepeatCount = 0;
2357 boolean handled = receiver.onKeyDown(code, this);
2358 if (handled) {
2359 mAction = ACTION_UP;
2360 receiver.onKeyUp(code, this);
2361 }
2362 mAction = ACTION_MULTIPLE;
2363 mRepeatCount = count;
2364 return handled;
2365 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002366 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 }
2368 return false;
2369 }
2370
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002371 /**
2372 * Use with {@link KeyEvent#dispatch(Callback, DispatcherState, Object)}
2373 * for more advanced key dispatching, such as long presses.
2374 */
2375 public static class DispatcherState {
2376 int mDownKeyCode;
2377 Object mDownTarget;
2378 SparseIntArray mActiveLongPresses = new SparseIntArray();
2379
2380 /**
2381 * Reset back to initial state.
2382 */
2383 public void reset() {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002384 if (DEBUG) Log.v(TAG, "Reset: " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002385 mDownKeyCode = 0;
2386 mDownTarget = null;
2387 mActiveLongPresses.clear();
2388 }
2389
2390 /**
2391 * Stop any tracking associated with this target.
2392 */
2393 public void reset(Object target) {
2394 if (mDownTarget == target) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002395 if (DEBUG) Log.v(TAG, "Reset in " + target + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002396 mDownKeyCode = 0;
2397 mDownTarget = null;
2398 }
2399 }
2400
2401 /**
2402 * Start tracking the key code associated with the given event. This
2403 * can only be called on a key down. It will allow you to see any
2404 * long press associated with the key, and will result in
2405 * {@link KeyEvent#isTracking} return true on the long press and up
2406 * events.
2407 *
2408 * <p>This is only needed if you are directly dispatching events, rather
2409 * than handling them in {@link Callback#onKeyDown}.
2410 */
2411 public void startTracking(KeyEvent event, Object target) {
2412 if (event.getAction() != ACTION_DOWN) {
2413 throw new IllegalArgumentException(
2414 "Can only start tracking on a down event");
2415 }
Dianne Hackborn8d374262009-09-14 21:21:52 -07002416 if (DEBUG) Log.v(TAG, "Start trackingt in " + target + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002417 mDownKeyCode = event.getKeyCode();
2418 mDownTarget = target;
2419 }
2420
2421 /**
2422 * Return true if the key event is for a key code that is currently
2423 * being tracked by the dispatcher.
2424 */
2425 public boolean isTracking(KeyEvent event) {
2426 return mDownKeyCode == event.getKeyCode();
2427 }
2428
2429 /**
2430 * Keep track of the given event's key code as having performed an
2431 * action with a long press, so no action should occur on the up.
2432 * <p>This is only needed if you are directly dispatching events, rather
2433 * than handling them in {@link Callback#onKeyLongPress}.
2434 */
2435 public void performedLongPress(KeyEvent event) {
2436 mActiveLongPresses.put(event.getKeyCode(), 1);
2437 }
2438
2439 /**
2440 * Handle key up event to stop tracking. This resets the dispatcher state,
2441 * and updates the key event state based on it.
2442 * <p>This is only needed if you are directly dispatching events, rather
2443 * than handling them in {@link Callback#onKeyUp}.
2444 */
2445 public void handleUpEvent(KeyEvent event) {
2446 final int keyCode = event.getKeyCode();
Dianne Hackborn8d374262009-09-14 21:21:52 -07002447 if (DEBUG) Log.v(TAG, "Handle key up " + event + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002448 int index = mActiveLongPresses.indexOfKey(keyCode);
2449 if (index >= 0) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002450 if (DEBUG) Log.v(TAG, " Index: " + index);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002451 event.mFlags |= FLAG_CANCELED | FLAG_CANCELED_LONG_PRESS;
2452 mActiveLongPresses.removeAt(index);
2453 }
2454 if (mDownKeyCode == keyCode) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002455 if (DEBUG) Log.v(TAG, " Tracking!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002456 event.mFlags |= FLAG_TRACKING;
2457 mDownKeyCode = 0;
2458 mDownTarget = null;
2459 }
2460 }
2461 }
Jeff Brown497a92c2010-09-12 17:55:08 -07002462
2463 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002464 public String toString() {
Jeff Brown497a92c2010-09-12 17:55:08 -07002465 return "KeyEvent{action=" + actionToString(mAction)
2466 + " keycode=" + keyCodeToString(mKeyCode)
2467 + " scancode=" + mScanCode
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002468 + " metaState=" + metaStateToString(mMetaState)
Jeff Brown497a92c2010-09-12 17:55:08 -07002469 + " flags=0x" + Integer.toHexString(mFlags)
2470 + " repeat=" + mRepeatCount
2471 + " device=" + mDeviceId
2472 + " source=0x" + Integer.toHexString(mSource)
2473 + "}";
2474 }
2475
2476 /**
2477 * Returns a string that represents the symbolic name of the specified action
2478 * such as "ACTION_DOWN", or "35" (if unknown).
2479 *
2480 * @param action The action.
2481 * @return The symbolic name of the specified action.
2482 * @hide
2483 */
2484 public static String actionToString(int action) {
2485 switch (action) {
2486 case ACTION_DOWN:
2487 return "ACTION_DOWN";
2488 case ACTION_UP:
2489 return "ACTION_UP";
2490 case ACTION_MULTIPLE:
2491 return "ACTION_MULTIPLE";
2492 default:
2493 return Integer.toString(action);
2494 }
2495 }
2496
2497 /**
2498 * Returns a string that represents the symbolic name of the specified keycode
2499 * such as "KEYCODE_A", "KEYCODE_DPAD_UP", or "1001" (if unknown).
2500 *
2501 * @param keyCode The key code.
2502 * @return The symbolic name of the specified keycode.
2503 *
2504 * @see KeyCharacterMap#getDisplayLabel
2505 * @hide
2506 */
2507 public static String keyCodeToString(int keyCode) {
2508 if (keyCode >= 0 && keyCode < KEYCODE_SYMBOLIC_NAMES.length) {
2509 return KEYCODE_SYMBOLIC_NAMES[keyCode];
2510 }
2511 return Integer.toString(keyCode);
2512 }
2513
2514 /**
2515 * Gets a keycode by its symbolic name such as "KEYCODE_A" or "1001" (if unknown).
2516 *
2517 * @param symbolicName The symbolic name of the keycode.
2518 * @return The keycode or -1 if not found.
2519 * @see #keycodeToString
2520 * @hide
2521 */
2522 public static int keyCodeFromString(String symbolicName) {
2523 if (symbolicName == null) {
2524 throw new IllegalArgumentException("symbolicName must not be null");
2525 }
2526
2527 final int count = KEYCODE_SYMBOLIC_NAMES.length;
2528 for (int i = 0; i < count; i++) {
2529 if (symbolicName.equals(KEYCODE_SYMBOLIC_NAMES[i])) {
2530 return i;
2531 }
2532 }
2533
2534 try {
2535 return Integer.parseInt(symbolicName,10);
2536 } catch (NumberFormatException ex) {
2537 return -1;
2538 }
2539 }
2540
2541 /**
2542 * Returns a string that represents the symbolic name of the specified combined meta
2543 * key modifier state flags such as "0", "META_SHIFT_ON",
2544 * "META_ALT_ON|META_SHIFT_ON" or "0x10000000" (if unknown).
2545 *
2546 * @param metaState The meta state.
2547 * @return The symbolic name of the specified combined meta state flags.
2548 * @hide
2549 */
2550 public static String metaStateToString(int metaState) {
2551 if (metaState == 0) {
2552 return "0";
2553 }
2554 StringBuilder result = null;
2555 int i = 0;
2556 while (metaState != 0) {
2557 final boolean isSet = (metaState & 1) != 0;
2558 metaState >>>= 1; // unsigned shift!
2559 if (isSet) {
2560 final String name = META_SYMBOLIC_NAMES[i];
2561 if (result == null) {
2562 if (metaState == 0) {
2563 return name;
2564 }
2565 result = new StringBuilder(name);
2566 } else {
2567 result.append('|');
2568 result.append(name);
2569 }
2570 }
2571 i += 1;
2572 }
2573 return result.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002574 }
2575
2576 public static final Parcelable.Creator<KeyEvent> CREATOR
2577 = new Parcelable.Creator<KeyEvent>() {
2578 public KeyEvent createFromParcel(Parcel in) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07002579 in.readInt(); // skip token, we already know this is a KeyEvent
2580 return KeyEvent.createFromParcelBody(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 }
2582
2583 public KeyEvent[] newArray(int size) {
2584 return new KeyEvent[size];
2585 }
2586 };
Jeff Brown6ec402b2010-07-28 15:48:59 -07002587
2588 /** @hide */
2589 public static KeyEvent createFromParcelBody(Parcel in) {
2590 return new KeyEvent(in);
2591 }
2592
2593 private KeyEvent(Parcel in) {
2594 readBaseFromParcel(in);
2595
2596 mAction = in.readInt();
2597 mKeyCode = in.readInt();
2598 mRepeatCount = in.readInt();
2599 mMetaState = in.readInt();
2600 mScanCode = in.readInt();
2601 mFlags = in.readInt();
2602 mDownTime = in.readLong();
2603 mEventTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002604 }
2605
2606 public void writeToParcel(Parcel out, int flags) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07002607 out.writeInt(PARCEL_TOKEN_KEY_EVENT);
2608
2609 writeBaseToParcel(out);
2610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002611 out.writeInt(mAction);
2612 out.writeInt(mKeyCode);
2613 out.writeInt(mRepeatCount);
2614 out.writeInt(mMetaState);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002615 out.writeInt(mScanCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 out.writeInt(mFlags);
2617 out.writeLong(mDownTime);
2618 out.writeLong(mEventTime);
2619 }
2620
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -07002621 private native boolean native_isSystemKey(int keyCode);
2622 private native boolean native_hasDefaultAction(int keyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623}