blob: ecf1aefc935e7c8b48d10ef88c8282e55852acac [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 Brown64da12a2011-01-04 19:57:47 -0800993 /**
994 * This mask is a combination of {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON}
995 * and {@link #META_SHIFT_RIGHT_ON}.
996 */
Jeff Brownc1df9072010-12-21 16:38:50 -0800997 public static final int META_SHIFT_MASK = META_SHIFT_ON
998 | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON;
999
Jeff Brown64da12a2011-01-04 19:57:47 -08001000 /**
1001 * This mask is a combination of {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON}
1002 * and {@link #META_ALT_RIGHT_ON}.
1003 */
Jeff Brownc1df9072010-12-21 16:38:50 -08001004 public static final int META_ALT_MASK = META_ALT_ON
1005 | META_ALT_LEFT_ON | META_ALT_RIGHT_ON;
1006
Jeff Brown64da12a2011-01-04 19:57:47 -08001007 /**
1008 * This mask is a combination of {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON}
1009 * and {@link #META_CTRL_RIGHT_ON}.
1010 */
Jeff Brownc1df9072010-12-21 16:38:50 -08001011 public static final int META_CTRL_MASK = META_CTRL_ON
1012 | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON;
1013
Jeff Brown64da12a2011-01-04 19:57:47 -08001014 /**
1015 * This mask is a combination of {@link #META_META_ON}, {@link #META_META_LEFT_ON}
1016 * and {@link #META_META_RIGHT_ON}.
1017 */
1018 public static final int META_META_MASK = META_META_ON
Jeff Brownc1df9072010-12-21 16:38:50 -08001019 | META_META_LEFT_ON | META_META_RIGHT_ON;
1020
Jeff Brown497a92c2010-09-12 17:55:08 -07001021 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 * This mask is set if the device woke because of this key event.
1023 */
1024 public static final int FLAG_WOKE_HERE = 0x1;
1025
1026 /**
1027 * This mask is set if the key event was generated by a software keyboard.
1028 */
1029 public static final int FLAG_SOFT_KEYBOARD = 0x2;
1030
1031 /**
1032 * This mask is set if we don't want the key event to cause us to leave
1033 * touch mode.
1034 */
1035 public static final int FLAG_KEEP_TOUCH_MODE = 0x4;
1036
1037 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001038 * This mask is set if an event was known to come from a trusted part
1039 * of the system. That is, the event is known to come from the user,
1040 * and could not have been spoofed by a third party component.
1041 */
1042 public static final int FLAG_FROM_SYSTEM = 0x8;
1043
1044 /**
1045 * This mask is used for compatibility, to identify enter keys that are
1046 * coming from an IME whose enter key has been auto-labelled "next" or
1047 * "done". This allows TextView to dispatch these as normal enter keys
1048 * for old applications, but still do the appropriate action when
1049 * receiving them.
1050 */
1051 public static final int FLAG_EDITOR_ACTION = 0x10;
1052
1053 /**
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07001054 * When associated with up key events, this indicates that the key press
1055 * has been canceled. Typically this is used with virtual touch screen
1056 * keys, where the user can slide from the virtual key area on to the
1057 * display: in that case, the application will receive a canceled up
1058 * event and should not perform the action normally associated with the
1059 * key. Note that for this to work, the application can not perform an
1060 * action for a key until it receives an up or the long press timeout has
1061 * expired.
1062 */
1063 public static final int FLAG_CANCELED = 0x20;
1064
1065 /**
1066 * This key event was generated by a virtual (on-screen) hard key area.
1067 * Typically this is an area of the touchscreen, outside of the regular
1068 * display, dedicated to "hardware" buttons.
1069 */
1070 public static final int FLAG_VIRTUAL_HARD_KEY = 0x40;
1071
1072 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001073 * This flag is set for the first key repeat that occurs after the
1074 * long press timeout.
1075 */
1076 public static final int FLAG_LONG_PRESS = 0x80;
1077
1078 /**
1079 * Set when a key event has {@link #FLAG_CANCELED} set because a long
1080 * press action was executed while it was down.
1081 */
1082 public static final int FLAG_CANCELED_LONG_PRESS = 0x100;
1083
1084 /**
1085 * Set for {@link #ACTION_UP} when this event's key code is still being
1086 * tracked from its initial down. That is, somebody requested that tracking
1087 * started on the key down and a long press has not caused
1088 * the tracking to be canceled.
1089 */
1090 public static final int FLAG_TRACKING = 0x200;
Jeff Brown49ed71d2010-12-06 17:13:33 -08001091
1092 /**
1093 * Set when a key event has been synthesized to implement default behavior
1094 * for an event that the application did not handle.
1095 * Fallback key events are generated by unhandled trackball motions
1096 * (to emulate a directional keypad) and by certain unhandled key presses
1097 * that are declared in the key map (such as special function numeric keypad
1098 * keys when numlock is off).
1099 */
1100 public static final int FLAG_FALLBACK = 0x400;
1101
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001102 /**
1103 * Private control to determine when an app is tracking a key sequence.
1104 * @hide
1105 */
1106 public static final int FLAG_START_TRACKING = 0x40000000;
1107
1108 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 * Returns the maximum keycode.
1110 */
1111 public static int getMaxKeyCode() {
1112 return LAST_KEYCODE;
1113 }
1114
1115 /**
1116 * Get the character that is produced by putting accent on the character
1117 * c.
1118 * For example, getDeadChar('`', 'e') returns &egrave;.
1119 */
1120 public static int getDeadChar(int accent, int c) {
1121 return KeyCharacterMap.getDeadChar(accent, c);
1122 }
1123
Dianne Hackborn8d374262009-09-14 21:21:52 -07001124 static final boolean DEBUG = false;
1125 static final String TAG = "KeyEvent";
Jeff Brown1f245102010-11-18 20:53:46 -08001126
1127 private static final int MAX_RECYCLED = 10;
1128 private static final Object gRecyclerLock = new Object();
1129 private static int gRecyclerUsed;
1130 private static KeyEvent gRecyclerTop;
1131
1132 private KeyEvent mNext;
1133 private boolean mRecycled;
1134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 private int mMetaState;
1136 private int mAction;
1137 private int mKeyCode;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001138 private int mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 private int mRepeatCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 private int mFlags;
1141 private long mDownTime;
1142 private long mEventTime;
1143 private String mCharacters;
1144
1145 public interface Callback {
1146 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001147 * Called when a key down event has occurred. If you return true,
1148 * you can first call {@link KeyEvent#startTracking()
1149 * KeyEvent.startTracking()} to have the framework track the event
1150 * through its {@link #onKeyUp(int, KeyEvent)} and also call your
1151 * {@link #onKeyLongPress(int, KeyEvent)} if it occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 *
1153 * @param keyCode The value in event.getKeyCode().
1154 * @param event Description of the key event.
1155 *
1156 * @return If you handled the event, return true. If you want to allow
1157 * the event to be handled by the next receiver, return false.
1158 */
1159 boolean onKeyDown(int keyCode, KeyEvent event);
1160
1161 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001162 * Called when a long press has occurred. If you return true,
1163 * the final key up will have {@link KeyEvent#FLAG_CANCELED} and
1164 * {@link KeyEvent#FLAG_CANCELED_LONG_PRESS} set. Note that in
1165 * order to receive this callback, someone in the event change
1166 * <em>must</em> return true from {@link #onKeyDown} <em>and</em>
1167 * call {@link KeyEvent#startTracking()} on the event.
1168 *
1169 * @param keyCode The value in event.getKeyCode().
1170 * @param event Description of the key event.
1171 *
1172 * @return If you handled the event, return true. If you want to allow
1173 * the event to be handled by the next receiver, return false.
1174 */
1175 boolean onKeyLongPress(int keyCode, KeyEvent event);
1176
1177 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 * Called when a key up event has occurred.
1179 *
1180 * @param keyCode The value in event.getKeyCode().
1181 * @param event Description of the key event.
1182 *
1183 * @return If you handled the event, return true. If you want to allow
1184 * the event to be handled by the next receiver, return false.
1185 */
1186 boolean onKeyUp(int keyCode, KeyEvent event);
1187
1188 /**
1189 * Called when multiple down/up pairs of the same key have occurred
1190 * in a row.
1191 *
1192 * @param keyCode The value in event.getKeyCode().
1193 * @param count Number of pairs as returned by event.getRepeatCount().
1194 * @param event Description of the key event.
1195 *
1196 * @return If you handled the event, return true. If you want to allow
1197 * the event to be handled by the next receiver, return false.
1198 */
1199 boolean onKeyMultiple(int keyCode, int count, KeyEvent event);
1200 }
1201
Jeff Brown497a92c2010-09-12 17:55:08 -07001202 static {
1203 if (META_SYMBOLIC_NAMES.length != 32) {
1204 throw new IllegalStateException(
1205 "META_SYMBOLIC_NAMES array should contain exactly 32 entries.");
1206 }
1207 if (KEYCODE_SYMBOLIC_NAMES.length != LAST_KEYCODE + 1) {
1208 throw new IllegalStateException(
1209 "KEYCODE_SYMBOLIC_NAMES array is out of sync with the keycode constants.");
1210 }
1211 }
1212
Jeff Brown1f245102010-11-18 20:53:46 -08001213 private KeyEvent() {
1214 }
1215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 /**
1217 * Create a new key event.
1218 *
1219 * @param action Action code: either {@link #ACTION_DOWN},
1220 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1221 * @param code The key code.
1222 */
1223 public KeyEvent(int action, int code) {
1224 mAction = action;
1225 mKeyCode = code;
1226 mRepeatCount = 0;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001227 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 }
1229
1230 /**
1231 * Create a new key event.
1232 *
1233 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1234 * at which this key code originally went down.
1235 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1236 * at which this event happened.
1237 * @param action Action code: either {@link #ACTION_DOWN},
1238 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1239 * @param code The key code.
1240 * @param repeat A repeat count for down events (> 0 if this is after the
1241 * initial down) or event count for multiple events.
1242 */
1243 public KeyEvent(long downTime, long eventTime, int action,
1244 int code, int repeat) {
1245 mDownTime = downTime;
1246 mEventTime = eventTime;
1247 mAction = action;
1248 mKeyCode = code;
1249 mRepeatCount = repeat;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001250 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 }
1252
1253 /**
1254 * Create a new key event.
1255 *
1256 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1257 * at which this key code originally went down.
1258 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1259 * at which this event happened.
1260 * @param action Action code: either {@link #ACTION_DOWN},
1261 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1262 * @param code The key code.
1263 * @param repeat A repeat count for down events (> 0 if this is after the
1264 * initial down) or event count for multiple events.
1265 * @param metaState Flags indicating which meta keys are currently pressed.
1266 */
1267 public KeyEvent(long downTime, long eventTime, int action,
1268 int code, int repeat, int metaState) {
1269 mDownTime = downTime;
1270 mEventTime = eventTime;
1271 mAction = action;
1272 mKeyCode = code;
1273 mRepeatCount = repeat;
1274 mMetaState = metaState;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001275 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 }
1277
1278 /**
1279 * Create a new key event.
1280 *
1281 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1282 * at which this key code originally went down.
1283 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1284 * at which this event happened.
1285 * @param action Action code: either {@link #ACTION_DOWN},
1286 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1287 * @param code The key code.
1288 * @param repeat A repeat count for down events (> 0 if this is after the
1289 * initial down) or event count for multiple events.
1290 * @param metaState Flags indicating which meta keys are currently pressed.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001291 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 * @param scancode Raw device scan code of the event.
1293 */
1294 public KeyEvent(long downTime, long eventTime, int action,
1295 int code, int repeat, int metaState,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001296 int deviceId, int scancode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 mDownTime = downTime;
1298 mEventTime = eventTime;
1299 mAction = action;
1300 mKeyCode = code;
1301 mRepeatCount = repeat;
1302 mMetaState = metaState;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001303 mDeviceId = deviceId;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001304 mScanCode = scancode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 }
1306
1307 /**
1308 * Create a new key event.
1309 *
1310 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1311 * at which this key code originally went down.
1312 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1313 * at which this event happened.
1314 * @param action Action code: either {@link #ACTION_DOWN},
1315 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1316 * @param code The key code.
1317 * @param repeat A repeat count for down events (> 0 if this is after the
1318 * initial down) or event count for multiple events.
1319 * @param metaState Flags indicating which meta keys are currently pressed.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001320 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 * @param scancode Raw device scan code of the event.
1322 * @param flags The flags for this key event
1323 */
1324 public KeyEvent(long downTime, long eventTime, int action,
1325 int code, int repeat, int metaState,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001326 int deviceId, int scancode, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 mDownTime = downTime;
1328 mEventTime = eventTime;
1329 mAction = action;
1330 mKeyCode = code;
1331 mRepeatCount = repeat;
1332 mMetaState = metaState;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001333 mDeviceId = deviceId;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001334 mScanCode = scancode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 mFlags = flags;
1336 }
1337
1338 /**
Jeff Brownc5ed5912010-07-14 18:48:53 -07001339 * Create a new key event.
1340 *
1341 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1342 * at which this key code originally went down.
1343 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1344 * at which this event happened.
1345 * @param action Action code: either {@link #ACTION_DOWN},
1346 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1347 * @param code The key code.
1348 * @param repeat A repeat count for down events (> 0 if this is after the
1349 * initial down) or event count for multiple events.
1350 * @param metaState Flags indicating which meta keys are currently pressed.
1351 * @param deviceId The device ID that generated the key event.
1352 * @param scancode Raw device scan code of the event.
1353 * @param flags The flags for this key event
1354 * @param source The input source such as {@link InputDevice#SOURCE_KEYBOARD}.
1355 */
1356 public KeyEvent(long downTime, long eventTime, int action,
1357 int code, int repeat, int metaState,
1358 int deviceId, int scancode, int flags, int source) {
1359 mDownTime = downTime;
1360 mEventTime = eventTime;
1361 mAction = action;
1362 mKeyCode = code;
1363 mRepeatCount = repeat;
1364 mMetaState = metaState;
1365 mDeviceId = deviceId;
1366 mScanCode = scancode;
1367 mFlags = flags;
1368 mSource = source;
1369 }
1370
1371 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 * Create a new key event for a string of characters. The key code,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001373 * action, repeat count and source will automatically be set to
1374 * {@link #KEYCODE_UNKNOWN}, {@link #ACTION_MULTIPLE}, 0, and
1375 * {@link InputDevice#SOURCE_KEYBOARD} for you.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 *
1377 * @param time The time (in {@link android.os.SystemClock#uptimeMillis})
1378 * at which this event occured.
1379 * @param characters The string of characters.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001380 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 * @param flags The flags for this key event
1382 */
Jeff Brownc5ed5912010-07-14 18:48:53 -07001383 public KeyEvent(long time, String characters, int deviceId, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 mDownTime = time;
1385 mEventTime = time;
1386 mCharacters = characters;
1387 mAction = ACTION_MULTIPLE;
1388 mKeyCode = KEYCODE_UNKNOWN;
1389 mRepeatCount = 0;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001390 mDeviceId = deviceId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 mFlags = flags;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001392 mSource = InputDevice.SOURCE_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 }
1394
1395 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001396 * Make an exact copy of an existing key event.
1397 */
1398 public KeyEvent(KeyEvent origEvent) {
1399 mDownTime = origEvent.mDownTime;
1400 mEventTime = origEvent.mEventTime;
1401 mAction = origEvent.mAction;
1402 mKeyCode = origEvent.mKeyCode;
1403 mRepeatCount = origEvent.mRepeatCount;
1404 mMetaState = origEvent.mMetaState;
1405 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001406 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001407 mScanCode = origEvent.mScanCode;
The Android Open Source Project10592532009-03-18 17:39:46 -07001408 mFlags = origEvent.mFlags;
1409 mCharacters = origEvent.mCharacters;
1410 }
1411
1412 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 * Copy an existing key event, modifying its time and repeat count.
1414 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001415 * @deprecated Use {@link #changeTimeRepeat(KeyEvent, long, int)}
1416 * instead.
1417 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 * @param origEvent The existing event to be copied.
1419 * @param eventTime The new event time
1420 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1421 * @param newRepeat The new repeat count of the event.
1422 */
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001423 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 public KeyEvent(KeyEvent origEvent, long eventTime, int newRepeat) {
1425 mDownTime = origEvent.mDownTime;
1426 mEventTime = eventTime;
1427 mAction = origEvent.mAction;
1428 mKeyCode = origEvent.mKeyCode;
1429 mRepeatCount = newRepeat;
1430 mMetaState = origEvent.mMetaState;
1431 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001432 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001433 mScanCode = origEvent.mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 mFlags = origEvent.mFlags;
1435 mCharacters = origEvent.mCharacters;
1436 }
1437
Jeff Brown1f245102010-11-18 20:53:46 -08001438 private static KeyEvent obtain() {
1439 final KeyEvent ev;
1440 synchronized (gRecyclerLock) {
1441 ev = gRecyclerTop;
1442 if (ev == null) {
1443 return new KeyEvent();
1444 }
1445 gRecyclerTop = ev.mNext;
1446 gRecyclerUsed -= 1;
1447 }
1448 ev.mRecycled = false;
1449 ev.mNext = null;
1450 return ev;
1451 }
1452
1453 /**
1454 * Obtains a (potentially recycled) key event.
1455 *
1456 * @hide
1457 */
1458 public static KeyEvent obtain(long downTime, long eventTime, int action,
1459 int code, int repeat, int metaState,
1460 int deviceId, int scancode, int flags, int source, String characters) {
1461 KeyEvent ev = obtain();
1462 ev.mDownTime = downTime;
1463 ev.mEventTime = eventTime;
1464 ev.mAction = action;
1465 ev.mKeyCode = code;
1466 ev.mRepeatCount = repeat;
1467 ev.mMetaState = metaState;
1468 ev.mDeviceId = deviceId;
1469 ev.mScanCode = scancode;
1470 ev.mFlags = flags;
1471 ev.mSource = source;
1472 ev.mCharacters = characters;
1473 return ev;
1474 }
1475
1476 /**
1477 * Recycles a key event.
1478 * Key events should only be recycled if they are owned by the system since user
1479 * code expects them to be essentially immutable, "tracking" notwithstanding.
1480 *
1481 * @hide
1482 */
1483 public final void recycle() {
1484 if (mRecycled) {
1485 throw new RuntimeException(toString() + " recycled twice!");
1486 }
1487 mRecycled = true;
1488 mCharacters = null;
1489
1490 synchronized (gRecyclerLock) {
1491 if (gRecyclerUsed < MAX_RECYCLED) {
1492 gRecyclerUsed++;
1493 mNext = gRecyclerTop;
1494 gRecyclerTop = this;
1495 }
1496 }
1497 }
1498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001500 * Create a new key event that is the same as the given one, but whose
1501 * event time and repeat count are replaced with the given value.
1502 *
1503 * @param event The existing event to be copied. This is not modified.
1504 * @param eventTime The new event time
1505 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1506 * @param newRepeat The new repeat count of the event.
1507 */
1508 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime,
1509 int newRepeat) {
1510 return new KeyEvent(event, eventTime, newRepeat);
1511 }
1512
1513 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001514 * Create a new key event that is the same as the given one, but whose
1515 * event time and repeat count are replaced with the given value.
1516 *
1517 * @param event The existing event to be copied. This is not modified.
1518 * @param eventTime The new event time
1519 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1520 * @param newRepeat The new repeat count of the event.
1521 * @param newFlags New flags for the event, replacing the entire value
1522 * in the original event.
1523 */
1524 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime,
1525 int newRepeat, int newFlags) {
1526 KeyEvent ret = new KeyEvent(event);
1527 ret.mEventTime = eventTime;
1528 ret.mRepeatCount = newRepeat;
1529 ret.mFlags = newFlags;
1530 return ret;
1531 }
1532
1533 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 * Copy an existing key event, modifying its action.
1535 *
1536 * @param origEvent The existing event to be copied.
1537 * @param action The new action code of the event.
1538 */
The Android Open Source Project10592532009-03-18 17:39:46 -07001539 private KeyEvent(KeyEvent origEvent, int action) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 mDownTime = origEvent.mDownTime;
1541 mEventTime = origEvent.mEventTime;
1542 mAction = action;
1543 mKeyCode = origEvent.mKeyCode;
1544 mRepeatCount = origEvent.mRepeatCount;
1545 mMetaState = origEvent.mMetaState;
1546 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001547 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001548 mScanCode = origEvent.mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 mFlags = origEvent.mFlags;
1550 // Don't copy mCharacters, since one way or the other we'll lose it
1551 // when changing the action.
1552 }
1553
1554 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001555 * Create a new key event that is the same as the given one, but whose
1556 * action is replaced with the given value.
1557 *
1558 * @param event The existing event to be copied. This is not modified.
1559 * @param action The new action code of the event.
1560 */
1561 public static KeyEvent changeAction(KeyEvent event, int action) {
1562 return new KeyEvent(event, action);
1563 }
1564
1565 /**
1566 * Create a new key event that is the same as the given one, but whose
1567 * flags are replaced with the given value.
1568 *
1569 * @param event The existing event to be copied. This is not modified.
1570 * @param flags The new flags constant.
1571 */
1572 public static KeyEvent changeFlags(KeyEvent event, int flags) {
1573 event = new KeyEvent(event);
1574 event.mFlags = flags;
1575 return event;
1576 }
1577
1578 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 * Don't use in new code, instead explicitly check
1580 * {@link #getAction()}.
1581 *
1582 * @return If the action is ACTION_DOWN, returns true; else false.
1583 *
1584 * @deprecated
1585 * @hide
1586 */
1587 @Deprecated public final boolean isDown() {
1588 return mAction == ACTION_DOWN;
1589 }
1590
1591 /**
1592 * Is this a system key? System keys can not be used for menu shortcuts.
1593 *
1594 * TODO: this information should come from a table somewhere.
1595 * TODO: should the dpad keys be here? arguably, because they also shouldn't be menu shortcuts
1596 */
1597 public final boolean isSystem() {
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -07001598 return native_isSystemKey(mKeyCode);
1599 }
1600
1601 /** @hide */
1602 public final boolean hasDefaultAction() {
1603 return native_hasDefaultAction(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 }
1605
1606
1607 /**
1608 * <p>Returns the state of the meta keys.</p>
1609 *
1610 * @return an integer in which each bit set to 1 represents a pressed
1611 * meta key
1612 *
1613 * @see #isAltPressed()
1614 * @see #isShiftPressed()
1615 * @see #isSymPressed()
Jeff Brown497a92c2010-09-12 17:55:08 -07001616 * @see #isCtrlPressed()
1617 * @see #isMetaPressed()
1618 * @see #isFunctionPressed()
Jeff Brown51e7fe72010-10-29 22:19:53 -07001619 * @see #isCapsLockOn()
1620 * @see #isNumLockOn()
1621 * @see #isScrollLockOn()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622 * @see #META_ALT_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001623 * @see #META_ALT_LEFT_ON
1624 * @see #META_ALT_RIGHT_ON
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 * @see #META_SHIFT_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001626 * @see #META_SHIFT_LEFT_ON
1627 * @see #META_SHIFT_RIGHT_ON
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 * @see #META_SYM_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001629 * @see #META_FUNCTION_ON
1630 * @see #META_CTRL_ON
1631 * @see #META_CTRL_LEFT_ON
1632 * @see #META_CTRL_RIGHT_ON
1633 * @see #META_META_ON
1634 * @see #META_META_LEFT_ON
1635 * @see #META_META_RIGHT_ON
Jeff Brown51e7fe72010-10-29 22:19:53 -07001636 * @see #META_CAPS_LOCK_ON
1637 * @see #META_NUM_LOCK_ON
1638 * @see #META_SCROLL_LOCK_ON
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 */
1640 public final int getMetaState() {
1641 return mMetaState;
1642 }
1643
1644 /**
1645 * Returns the flags for this key event.
1646 *
1647 * @see #FLAG_WOKE_HERE
1648 */
1649 public final int getFlags() {
1650 return mFlags;
1651 }
1652
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001653 // Mask of all modifier key meta states. Specifically excludes locked keys like caps lock.
1654 private static final int META_MODIFIER_MASK =
1655 META_SHIFT_ON | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON
1656 | META_ALT_ON | META_ALT_LEFT_ON | META_ALT_RIGHT_ON
1657 | META_CTRL_ON | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON
1658 | META_META_ON | META_META_LEFT_ON | META_META_RIGHT_ON
1659 | META_SYM_ON | META_FUNCTION_ON;
1660
1661 // Mask of all lock key meta states.
1662 private static final int META_LOCK_MASK =
1663 META_CAPS_LOCK_ON | META_NUM_LOCK_ON | META_SCROLL_LOCK_ON;
1664
1665 // Mask of all valid meta states.
1666 private static final int META_ALL_MASK = META_MODIFIER_MASK | META_LOCK_MASK;
1667
1668 // Mask of all synthetic meta states that are reserved for API compatibility with
1669 // historical uses in MetaKeyKeyListener.
1670 private static final int META_SYNTHETIC_MASK =
1671 META_CAP_LOCKED | META_ALT_LOCKED | META_SYM_LOCKED | META_SELECTING;
1672
1673 // Mask of all meta states that are not valid use in specifying a modifier key.
1674 // These bits are known to be used for purposes other than specifying modifiers.
1675 private static final int META_INVALID_MODIFIER_MASK =
1676 META_LOCK_MASK | META_SYNTHETIC_MASK;
1677
1678 /**
1679 * Gets a mask that includes all valid modifier key meta state bits.
1680 * <p>
1681 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1682 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1683 * not considered modifier keys. Consequently, the mask specifically excludes
1684 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1685 * </p>
1686 *
1687 * @return The modifier meta state mask which is a combination of
1688 * {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON}, {@link #META_SHIFT_RIGHT_ON},
1689 * {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON}, {@link #META_ALT_RIGHT_ON},
1690 * {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON}, {@link #META_CTRL_RIGHT_ON},
1691 * {@link #META_META_ON}, {@link #META_META_LEFT_ON}, {@link #META_META_RIGHT_ON},
1692 * {@link #META_SYM_ON}, {@link #META_FUNCTION_ON}.
1693 */
1694 public static int getModifierMetaStateMask() {
1695 return META_MODIFIER_MASK;
1696 }
1697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 /**
1699 * Returns true if this key code is a modifier key.
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001700 * <p>
1701 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1702 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1703 * not considered modifier keys. Consequently, this function return false
1704 * for those keys.
1705 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 *
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001707 * @return True if the key code is one of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 * {@link #KEYCODE_SHIFT_LEFT} {@link #KEYCODE_SHIFT_RIGHT},
Jeff Brown497a92c2010-09-12 17:55:08 -07001709 * {@link #KEYCODE_ALT_LEFT}, {@link #KEYCODE_ALT_RIGHT},
Jeff Brown497a92c2010-09-12 17:55:08 -07001710 * {@link #KEYCODE_CTRL_LEFT}, {@link #KEYCODE_CTRL_RIGHT},
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001711 * {@link #KEYCODE_META_LEFT}, or {@link #KEYCODE_META_RIGHT},
1712 * {@link #KEYCODE_SYM}, {@link #KEYCODE_NUM}, {@link #KEYCODE_FUNCTION}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 */
1714 public static boolean isModifierKey(int keyCode) {
Jeff Brown497a92c2010-09-12 17:55:08 -07001715 switch (keyCode) {
1716 case KEYCODE_SHIFT_LEFT:
1717 case KEYCODE_SHIFT_RIGHT:
1718 case KEYCODE_ALT_LEFT:
1719 case KEYCODE_ALT_RIGHT:
Jeff Brown497a92c2010-09-12 17:55:08 -07001720 case KEYCODE_CTRL_LEFT:
1721 case KEYCODE_CTRL_RIGHT:
1722 case KEYCODE_META_LEFT:
1723 case KEYCODE_META_RIGHT:
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001724 case KEYCODE_SYM:
1725 case KEYCODE_NUM:
1726 case KEYCODE_FUNCTION:
Jeff Brown497a92c2010-09-12 17:55:08 -07001727 return true;
1728 default:
1729 return false;
1730 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 }
1732
1733 /**
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001734 * Normalizes the specified meta state.
1735 * <p>
1736 * The meta state is normalized such that if either the left or right modifier meta state
1737 * bits are set then the result will also include the universal bit for that modifier.
1738 * </p><p>
1739 * If the specified meta state contains {@link #META_ALT_LEFT_ON} then
1740 * the result will also contain {@link #META_ALT_ON} in addition to {@link #META_ALT_LEFT_ON}
1741 * and the other bits that were specified in the input. The same is process is
1742 * performed for shift, control and meta.
1743 * </p><p>
1744 * If the specified meta state contains synthetic meta states defined by
1745 * {@link MetaKeyKeyListener}, then those states are translated here and the original
1746 * synthetic meta states are removed from the result.
1747 * {@link MetaKeyKeyListener#META_CAP_LOCKED} is translated to {@link #META_CAPS_LOCK_ON}.
1748 * {@link MetaKeyKeyListener#META_ALT_LOCKED} is translated to {@link #META_ALT_ON}.
1749 * {@link MetaKeyKeyListener#META_SYM_LOCKED} is translated to {@link #META_SYM_ON}.
1750 * </p><p>
1751 * Undefined meta state bits are removed.
1752 * </p>
1753 *
1754 * @param metaState The meta state.
1755 * @return The normalized meta state.
1756 */
1757 public static int normalizeMetaState(int metaState) {
1758 if ((metaState & (META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON)) != 0) {
1759 metaState |= META_SHIFT_ON;
1760 }
1761 if ((metaState & (META_ALT_LEFT_ON | META_ALT_RIGHT_ON)) != 0) {
1762 metaState |= META_ALT_ON;
1763 }
1764 if ((metaState & (META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON)) != 0) {
1765 metaState |= META_CTRL_ON;
1766 }
1767 if ((metaState & (META_META_LEFT_ON | META_META_RIGHT_ON)) != 0) {
1768 metaState |= META_META_ON;
1769 }
1770 if ((metaState & MetaKeyKeyListener.META_CAP_LOCKED) != 0) {
1771 metaState |= META_CAPS_LOCK_ON;
1772 }
1773 if ((metaState & MetaKeyKeyListener.META_ALT_LOCKED) != 0) {
1774 metaState |= META_ALT_ON;
1775 }
1776 if ((metaState & MetaKeyKeyListener.META_SYM_LOCKED) != 0) {
1777 metaState |= META_SYM_ON;
1778 }
1779 return metaState & META_ALL_MASK;
1780 }
1781
1782 /**
1783 * Returns true if no modifiers keys are pressed according to the specified meta state.
1784 * <p>
1785 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1786 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1787 * not considered modifier keys. Consequently, this function ignores
1788 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1789 * </p><p>
1790 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}.
1791 * </p>
1792 *
1793 * @param metaState The meta state to consider.
1794 * @return True if no modifier keys are pressed.
1795 * @see #hasNoModifiers()
1796 */
1797 public static boolean metaStateHasNoModifiers(int metaState) {
1798 return (normalizeMetaState(metaState) & META_MODIFIER_MASK) == 0;
1799 }
1800
1801 /**
1802 * Returns true if only the specified modifier keys are pressed according to
1803 * the specified meta state. Returns false if a different combination of modifier
1804 * keys are pressed.
1805 * <p>
1806 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1807 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1808 * not considered modifier keys. Consequently, this function ignores
1809 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1810 * </p><p>
1811 * If the specified modifier mask includes directional modifiers, such as
1812 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the
1813 * modifier is pressed on that side.
1814 * If the specified modifier mask includes non-directional modifiers, such as
1815 * {@link #META_SHIFT_ON}, then this method ensures that the modifier
1816 * is pressed on either side.
1817 * If the specified modifier mask includes both directional and non-directional modifiers
1818 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON},
1819 * then this method throws an illegal argument exception.
1820 * </p>
1821 *
1822 * @param metaState The meta state to consider.
1823 * @param modifiers The meta state of the modifier keys to check. May be a combination
1824 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to
1825 * ensure that no modifier keys are pressed.
1826 * @return True if only the specified modifier keys are pressed.
1827 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers
1828 * @see #hasModifiers
1829 */
1830 public static boolean metaStateHasModifiers(int metaState, int modifiers) {
1831 // Note: For forward compatibility, we allow the parameter to contain meta states
1832 // that we do not recognize but we explicitly disallow meta states that
1833 // are not valid modifiers.
1834 if ((modifiers & META_INVALID_MODIFIER_MASK) != 0) {
1835 throw new IllegalArgumentException("modifiers must not contain "
1836 + "META_CAPS_LOCK_ON, META_NUM_LOCK_ON, META_SCROLL_LOCK_ON, "
1837 + "META_CAP_LOCKED, META_ALT_LOCKED, META_SYM_LOCKED, "
1838 + "or META_SELECTING");
1839 }
1840
1841 metaState = normalizeMetaState(metaState) & META_MODIFIER_MASK;
1842 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
1843 META_SHIFT_ON, META_SHIFT_LEFT_ON, META_SHIFT_RIGHT_ON);
1844 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
1845 META_ALT_ON, META_ALT_LEFT_ON, META_ALT_RIGHT_ON);
1846 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
1847 META_CTRL_ON, META_CTRL_LEFT_ON, META_CTRL_RIGHT_ON);
1848 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
1849 META_META_ON, META_META_LEFT_ON, META_META_RIGHT_ON);
1850 return metaState == modifiers;
1851 }
1852
1853 private static int metaStateFilterDirectionalModifiers(int metaState,
1854 int modifiers, int basic, int left, int right) {
1855 final boolean wantBasic = (modifiers & basic) != 0;
1856 final int directional = left | right;
1857 final boolean wantLeftOrRight = (modifiers & directional) != 0;
1858
1859 if (wantBasic) {
1860 if (wantLeftOrRight) {
1861 throw new IllegalArgumentException("modifiers must not contain "
1862 + metaStateToString(basic) + " combined with "
1863 + metaStateToString(left) + " or " + metaStateToString(right));
1864 }
1865 return metaState & ~directional;
1866 } else if (wantLeftOrRight) {
1867 return metaState & ~basic;
1868 } else {
1869 return metaState;
1870 }
1871 }
1872
1873 /**
1874 * Returns true if no modifier keys are pressed.
1875 * <p>
1876 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1877 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1878 * not considered modifier keys. Consequently, this function ignores
1879 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1880 * </p><p>
1881 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}.
1882 * </p>
1883 *
1884 * @return True if no modifier keys are pressed.
1885 * @see #metaStateHasNoModifiers
1886 */
1887 public final boolean hasNoModifiers() {
1888 return metaStateHasNoModifiers(mMetaState);
1889 }
1890
1891 /**
1892 * Returns true if only the specified modifiers keys are pressed.
1893 * Returns false if a different combination of modifier keys are pressed.
1894 * <p>
1895 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1896 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1897 * not considered modifier keys. Consequently, this function ignores
1898 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1899 * </p><p>
1900 * If the specified modifier mask includes directional modifiers, such as
1901 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the
1902 * modifier is pressed on that side.
1903 * If the specified modifier mask includes non-directional modifiers, such as
1904 * {@link #META_SHIFT_ON}, then this method ensures that the modifier
1905 * is pressed on either side.
1906 * If the specified modifier mask includes both directional and non-directional modifiers
1907 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON},
1908 * then this method throws an illegal argument exception.
1909 * </p>
1910 *
1911 * @param modifiers The meta state of the modifier keys to check. May be a combination
1912 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to
1913 * ensure that no modifier keys are pressed.
1914 * @return True if only the specified modifier keys are pressed.
1915 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers
1916 * @see #metaStateHasModifiers
1917 */
1918 public final boolean hasModifiers(int modifiers) {
1919 return metaStateHasModifiers(mMetaState, modifiers);
1920 }
1921
1922 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 * <p>Returns the pressed state of the ALT meta key.</p>
1924 *
1925 * @return true if the ALT key is pressed, false otherwise
1926 *
1927 * @see #KEYCODE_ALT_LEFT
1928 * @see #KEYCODE_ALT_RIGHT
1929 * @see #META_ALT_ON
1930 */
1931 public final boolean isAltPressed() {
1932 return (mMetaState & META_ALT_ON) != 0;
1933 }
1934
1935 /**
1936 * <p>Returns the pressed state of the SHIFT meta key.</p>
1937 *
1938 * @return true if the SHIFT key is pressed, false otherwise
1939 *
1940 * @see #KEYCODE_SHIFT_LEFT
1941 * @see #KEYCODE_SHIFT_RIGHT
1942 * @see #META_SHIFT_ON
1943 */
1944 public final boolean isShiftPressed() {
1945 return (mMetaState & META_SHIFT_ON) != 0;
1946 }
1947
1948 /**
1949 * <p>Returns the pressed state of the SYM meta key.</p>
1950 *
1951 * @return true if the SYM key is pressed, false otherwise
1952 *
1953 * @see #KEYCODE_SYM
1954 * @see #META_SYM_ON
1955 */
1956 public final boolean isSymPressed() {
1957 return (mMetaState & META_SYM_ON) != 0;
1958 }
1959
1960 /**
Jeff Brown497a92c2010-09-12 17:55:08 -07001961 * <p>Returns the pressed state of the CTRL meta key.</p>
1962 *
1963 * @return true if the CTRL key is pressed, false otherwise
1964 *
1965 * @see #KEYCODE_CTRL_LEFT
1966 * @see #KEYCODE_CTRL_RIGHT
1967 * @see #META_CTRL_ON
1968 */
1969 public final boolean isCtrlPressed() {
1970 return (mMetaState & META_CTRL_ON) != 0;
1971 }
1972
1973 /**
1974 * <p>Returns the pressed state of the META meta key.</p>
1975 *
1976 * @return true if the META key is pressed, false otherwise
1977 *
1978 * @see #KEYCODE_META_LEFT
1979 * @see #KEYCODE_META_RIGHT
1980 * @see #META_META_ON
1981 */
1982 public final boolean isMetaPressed() {
1983 return (mMetaState & META_META_ON) != 0;
1984 }
1985
1986 /**
1987 * <p>Returns the pressed state of the FUNCTION meta key.</p>
1988 *
1989 * @return true if the FUNCTION key is pressed, false otherwise
1990 *
1991 * @see #KEYCODE_FUNCTION
1992 * @see #META_FUNCTION_ON
1993 */
1994 public final boolean isFunctionPressed() {
1995 return (mMetaState & META_FUNCTION_ON) != 0;
1996 }
1997
1998 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07001999 * <p>Returns the locked state of the CAPS LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002000 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002001 * @return true if the CAPS LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002002 *
2003 * @see #KEYCODE_CAPS_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002004 * @see #META_CAPS_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002005 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002006 public final boolean isCapsLockOn() {
2007 return (mMetaState & META_CAPS_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 NUM LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002012 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002013 * @return true if the NUM LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002014 *
2015 * @see #KEYCODE_NUM_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002016 * @see #META_NUM_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002017 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002018 public final boolean isNumLockOn() {
2019 return (mMetaState & META_NUM_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002020 }
2021
2022 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07002023 * <p>Returns the locked state of the SCROLL LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002024 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002025 * @return true if the SCROLL LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002026 *
2027 * @see #KEYCODE_SCROLL_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002028 * @see #META_SCROLL_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002029 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002030 public final boolean isScrollLockOn() {
2031 return (mMetaState & META_SCROLL_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002032 }
2033
2034 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 * Retrieve the action of this key event. May be either
2036 * {@link #ACTION_DOWN}, {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
2037 *
2038 * @return The event action: ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.
2039 */
2040 public final int getAction() {
2041 return mAction;
2042 }
2043
2044 /**
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07002045 * For {@link #ACTION_UP} events, indicates that the event has been
2046 * canceled as per {@link #FLAG_CANCELED}.
2047 */
2048 public final boolean isCanceled() {
2049 return (mFlags&FLAG_CANCELED) != 0;
2050 }
2051
2052 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002053 * Call this during {@link Callback#onKeyDown} to have the system track
2054 * the key through its final up (possibly including a long press). Note
2055 * that only one key can be tracked at a time -- if another key down
2056 * event is received while a previous one is being tracked, tracking is
2057 * stopped on the previous event.
2058 */
2059 public final void startTracking() {
2060 mFlags |= FLAG_START_TRACKING;
2061 }
2062
2063 /**
2064 * For {@link #ACTION_UP} events, indicates that the event is still being
2065 * tracked from its initial down event as per
2066 * {@link #FLAG_TRACKING}.
2067 */
2068 public final boolean isTracking() {
2069 return (mFlags&FLAG_TRACKING) != 0;
2070 }
2071
2072 /**
2073 * For {@link #ACTION_DOWN} events, indicates that the event has been
2074 * canceled as per {@link #FLAG_LONG_PRESS}.
2075 */
2076 public final boolean isLongPress() {
2077 return (mFlags&FLAG_LONG_PRESS) != 0;
2078 }
2079
2080 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081 * Retrieve the key code of the key event. This is the physical key that
2082 * was pressed, <em>not</em> the Unicode character.
2083 *
2084 * @return The key code of the event.
2085 */
2086 public final int getKeyCode() {
2087 return mKeyCode;
2088 }
2089
2090 /**
2091 * For the special case of a {@link #ACTION_MULTIPLE} event with key
2092 * code of {@link #KEYCODE_UNKNOWN}, this is a raw string of characters
2093 * associated with the event. In all other cases it is null.
2094 *
2095 * @return Returns a String of 1 or more characters associated with
2096 * the event.
2097 */
2098 public final String getCharacters() {
2099 return mCharacters;
2100 }
2101
2102 /**
2103 * Retrieve the hardware key id of this key event. These values are not
2104 * reliable and vary from device to device.
2105 *
2106 * {@more}
2107 * Mostly this is here for debugging purposes.
2108 */
2109 public final int getScanCode() {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002110 return mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002111 }
2112
2113 /**
2114 * Retrieve the repeat count of the event. For both key up and key down
2115 * events, this is the number of times the key has repeated with the first
2116 * down starting at 0 and counting up from there. For multiple key
2117 * events, this is the number of down/up pairs that have occurred.
2118 *
2119 * @return The number of times the key has repeated.
2120 */
2121 public final int getRepeatCount() {
2122 return mRepeatCount;
2123 }
2124
2125 /**
2126 * Retrieve the time of the most recent key down event,
2127 * in the {@link android.os.SystemClock#uptimeMillis} time base. If this
2128 * is a down event, this will be the same as {@link #getEventTime()}.
2129 * Note that when chording keys, this value is the down time of the
2130 * most recently pressed key, which may <em>not</em> be the same physical
2131 * key of this event.
2132 *
2133 * @return Returns the most recent key down time, in the
2134 * {@link android.os.SystemClock#uptimeMillis} time base
2135 */
2136 public final long getDownTime() {
2137 return mDownTime;
2138 }
2139
2140 /**
2141 * Retrieve the time this event occurred,
2142 * in the {@link android.os.SystemClock#uptimeMillis} time base.
2143 *
2144 * @return Returns the time this event occurred,
2145 * in the {@link android.os.SystemClock#uptimeMillis} time base.
2146 */
2147 public final long getEventTime() {
2148 return mEventTime;
2149 }
2150
2151 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 * Renamed to {@link #getDeviceId}.
2153 *
2154 * @hide
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002155 * @deprecated use {@link #getDeviceId()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002156 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002157 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 public final int getKeyboardDevice() {
2159 return mDeviceId;
2160 }
2161
2162 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002163 * Gets the {@link KeyCharacterMap} associated with the keyboard device.
2164 *
2165 * @return The associated key character map.
2166 * @throws {@link KeyCharacterMapUnavailableException} if the key character map
2167 * could not be loaded because it was malformed or the default key character map
2168 * is missing from the system.
2169 *
2170 * @see {@link KeyCharacterMap#load}
2171 */
2172 public final KeyCharacterMap getKeyCharacterMap() {
2173 return KeyCharacterMap.load(mDeviceId);
2174 }
2175
2176 /**
2177 * Gets the primary character for this key.
2178 * In other words, the label that is physically printed on it.
2179 *
2180 * @return The display label character, or 0 if none (eg. for non-printing keys).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181 */
2182 public char getDisplayLabel() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002183 return getKeyCharacterMap().getDisplayLabel(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002184 }
2185
2186 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002187 * Gets the Unicode character generated by the specified key and meta
2188 * key state combination.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 * <p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002190 * Returns the Unicode character that the specified key would produce
2191 * when the specified meta bits (see {@link MetaKeyKeyListener})
2192 * were active.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193 * </p><p>
2194 * Returns 0 if the key is not one that is used to type Unicode
2195 * characters.
2196 * </p><p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002197 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the
2198 * key is a "dead key" that should be combined with another to
2199 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} --
2200 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002201 * </p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002202 *
2203 * @return The associated character or combining accent, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 */
2205 public int getUnicodeChar() {
2206 return getUnicodeChar(mMetaState);
2207 }
2208
2209 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002210 * Gets the Unicode character generated by the specified key and meta
2211 * key state combination.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 * <p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002213 * Returns the Unicode character that the specified key would produce
2214 * when the specified meta bits (see {@link MetaKeyKeyListener})
2215 * were active.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 * </p><p>
2217 * Returns 0 if the key is not one that is used to type Unicode
2218 * characters.
2219 * </p><p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002220 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the
2221 * key is a "dead key" that should be combined with another to
2222 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} --
2223 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 * </p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002225 *
2226 * @param metaState The meta key modifier state.
2227 * @return The associated character or combining accent, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002228 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002229 public int getUnicodeChar(int metaState) {
2230 return getKeyCharacterMap().get(mKeyCode, metaState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002231 }
2232
2233 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002234 * Get the character conversion data for a given key code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235 *
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002236 * @param results A {@link KeyCharacterMap.KeyData} instance that will be
2237 * filled with the results.
2238 * @return True if the key was mapped. If the key was not mapped, results is not modified.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239 *
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002240 * @deprecated instead use {@link #getDisplayLabel()},
2241 * {@link #getNumber()} or {@link #getUnicodeChar(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002242 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002243 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244 public boolean getKeyData(KeyData results) {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002245 return getKeyCharacterMap().getKeyData(mKeyCode, results);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 }
2247
2248 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002249 * Gets the first character in the character array that can be generated
2250 * by the specified key code.
2251 * <p>
2252 * This is a convenience function that returns the same value as
2253 * {@link #getMatch(char[],int) getMatch(chars, 0)}.
2254 * </p>
2255 *
2256 * @param chars The array of matching characters to consider.
2257 * @return The matching associated character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002258 */
2259 public char getMatch(char[] chars) {
2260 return getMatch(chars, 0);
2261 }
2262
2263 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002264 * Gets the first character in the character array that can be generated
2265 * by the specified key code. If there are multiple choices, prefers
2266 * the one that would be generated with the specified meta key modifier state.
2267 *
2268 * @param chars The array of matching characters to consider.
2269 * @param metaState The preferred meta key modifier state.
2270 * @return The matching associated character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002271 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002272 public char getMatch(char[] chars, int metaState) {
2273 return getKeyCharacterMap().getMatch(mKeyCode, chars, metaState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274 }
2275
2276 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002277 * Gets the number or symbol associated with the key.
2278 * <p>
2279 * The character value is returned, not the numeric value.
2280 * If the key is not a number, but is a symbol, the symbol is retuned.
2281 * </p><p>
2282 * This method is intended to to support dial pads and other numeric or
2283 * symbolic entry on keyboards where certain keys serve dual function
2284 * as alphabetic and symbolic keys. This method returns the number
2285 * or symbol associated with the key independent of whether the user
2286 * has pressed the required modifier.
2287 * </p><p>
2288 * For example, on one particular keyboard the keys on the top QWERTY row generate
2289 * numbers when ALT is pressed such that ALT-Q maps to '1'. So for that keyboard
2290 * when {@link #getNumber} is called with {@link KeyEvent#KEYCODE_Q} it returns '1'
2291 * so that the user can type numbers without pressing ALT when it makes sense.
2292 * </p>
2293 *
2294 * @return The associated numeric or symbolic character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002295 */
2296 public char getNumber() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002297 return getKeyCharacterMap().getNumber(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002298 }
2299
2300 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002301 * Returns true if this key produces a glyph.
2302 *
2303 * @return True if the key is a printing key.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002304 */
2305 public boolean isPrintingKey() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002306 return getKeyCharacterMap().isPrintingKey(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002307 }
2308
2309 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002310 * @deprecated Use {@link #dispatch(Callback, DispatcherState, Object)} instead.
2311 */
2312 @Deprecated
2313 public final boolean dispatch(Callback receiver) {
2314 return dispatch(receiver, null, null);
2315 }
2316
2317 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002318 * Deliver this key event to a {@link Callback} interface. If this is
2319 * an ACTION_MULTIPLE event and it is not handled, then an attempt will
2320 * be made to deliver a single normal event.
2321 *
2322 * @param receiver The Callback that will be given the event.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002323 * @param state State information retained across events.
2324 * @param target The target of the dispatch, for use in tracking.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 *
2326 * @return The return value from the Callback method that was called.
2327 */
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002328 public final boolean dispatch(Callback receiver, DispatcherState state,
2329 Object target) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002330 switch (mAction) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002331 case ACTION_DOWN: {
2332 mFlags &= ~FLAG_START_TRACKING;
Dianne Hackborn8d374262009-09-14 21:21:52 -07002333 if (DEBUG) Log.v(TAG, "Key down to " + target + " in " + state
2334 + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002335 boolean res = receiver.onKeyDown(mKeyCode, this);
2336 if (state != null) {
2337 if (res && mRepeatCount == 0 && (mFlags&FLAG_START_TRACKING) != 0) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002338 if (DEBUG) Log.v(TAG, " Start tracking!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002339 state.startTracking(this, target);
2340 } else if (isLongPress() && state.isTracking(this)) {
2341 try {
2342 if (receiver.onKeyLongPress(mKeyCode, this)) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002343 if (DEBUG) Log.v(TAG, " Clear from long press!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002344 state.performedLongPress(this);
2345 res = true;
2346 }
2347 } catch (AbstractMethodError e) {
2348 }
2349 }
2350 }
2351 return res;
2352 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 case ACTION_UP:
Dianne Hackborn8d374262009-09-14 21:21:52 -07002354 if (DEBUG) Log.v(TAG, "Key up to " + target + " in " + state
2355 + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002356 if (state != null) {
2357 state.handleUpEvent(this);
2358 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 return receiver.onKeyUp(mKeyCode, this);
2360 case ACTION_MULTIPLE:
2361 final int count = mRepeatCount;
2362 final int code = mKeyCode;
2363 if (receiver.onKeyMultiple(code, count, this)) {
2364 return true;
2365 }
2366 if (code != KeyEvent.KEYCODE_UNKNOWN) {
2367 mAction = ACTION_DOWN;
2368 mRepeatCount = 0;
2369 boolean handled = receiver.onKeyDown(code, this);
2370 if (handled) {
2371 mAction = ACTION_UP;
2372 receiver.onKeyUp(code, this);
2373 }
2374 mAction = ACTION_MULTIPLE;
2375 mRepeatCount = count;
2376 return handled;
2377 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002378 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379 }
2380 return false;
2381 }
2382
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002383 /**
2384 * Use with {@link KeyEvent#dispatch(Callback, DispatcherState, Object)}
2385 * for more advanced key dispatching, such as long presses.
2386 */
2387 public static class DispatcherState {
2388 int mDownKeyCode;
2389 Object mDownTarget;
2390 SparseIntArray mActiveLongPresses = new SparseIntArray();
2391
2392 /**
2393 * Reset back to initial state.
2394 */
2395 public void reset() {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002396 if (DEBUG) Log.v(TAG, "Reset: " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002397 mDownKeyCode = 0;
2398 mDownTarget = null;
2399 mActiveLongPresses.clear();
2400 }
2401
2402 /**
2403 * Stop any tracking associated with this target.
2404 */
2405 public void reset(Object target) {
2406 if (mDownTarget == target) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002407 if (DEBUG) Log.v(TAG, "Reset in " + target + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002408 mDownKeyCode = 0;
2409 mDownTarget = null;
2410 }
2411 }
2412
2413 /**
2414 * Start tracking the key code associated with the given event. This
2415 * can only be called on a key down. It will allow you to see any
2416 * long press associated with the key, and will result in
2417 * {@link KeyEvent#isTracking} return true on the long press and up
2418 * events.
2419 *
2420 * <p>This is only needed if you are directly dispatching events, rather
2421 * than handling them in {@link Callback#onKeyDown}.
2422 */
2423 public void startTracking(KeyEvent event, Object target) {
2424 if (event.getAction() != ACTION_DOWN) {
2425 throw new IllegalArgumentException(
2426 "Can only start tracking on a down event");
2427 }
Dianne Hackborn8d374262009-09-14 21:21:52 -07002428 if (DEBUG) Log.v(TAG, "Start trackingt in " + target + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002429 mDownKeyCode = event.getKeyCode();
2430 mDownTarget = target;
2431 }
2432
2433 /**
2434 * Return true if the key event is for a key code that is currently
2435 * being tracked by the dispatcher.
2436 */
2437 public boolean isTracking(KeyEvent event) {
2438 return mDownKeyCode == event.getKeyCode();
2439 }
2440
2441 /**
2442 * Keep track of the given event's key code as having performed an
2443 * action with a long press, so no action should occur on the up.
2444 * <p>This is only needed if you are directly dispatching events, rather
2445 * than handling them in {@link Callback#onKeyLongPress}.
2446 */
2447 public void performedLongPress(KeyEvent event) {
2448 mActiveLongPresses.put(event.getKeyCode(), 1);
2449 }
2450
2451 /**
2452 * Handle key up event to stop tracking. This resets the dispatcher state,
2453 * and updates the key event state based on it.
2454 * <p>This is only needed if you are directly dispatching events, rather
2455 * than handling them in {@link Callback#onKeyUp}.
2456 */
2457 public void handleUpEvent(KeyEvent event) {
2458 final int keyCode = event.getKeyCode();
Dianne Hackborn8d374262009-09-14 21:21:52 -07002459 if (DEBUG) Log.v(TAG, "Handle key up " + event + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002460 int index = mActiveLongPresses.indexOfKey(keyCode);
2461 if (index >= 0) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002462 if (DEBUG) Log.v(TAG, " Index: " + index);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002463 event.mFlags |= FLAG_CANCELED | FLAG_CANCELED_LONG_PRESS;
2464 mActiveLongPresses.removeAt(index);
2465 }
2466 if (mDownKeyCode == keyCode) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002467 if (DEBUG) Log.v(TAG, " Tracking!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002468 event.mFlags |= FLAG_TRACKING;
2469 mDownKeyCode = 0;
2470 mDownTarget = null;
2471 }
2472 }
2473 }
Jeff Brown497a92c2010-09-12 17:55:08 -07002474
2475 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002476 public String toString() {
Jeff Brown497a92c2010-09-12 17:55:08 -07002477 return "KeyEvent{action=" + actionToString(mAction)
2478 + " keycode=" + keyCodeToString(mKeyCode)
2479 + " scancode=" + mScanCode
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002480 + " metaState=" + metaStateToString(mMetaState)
Jeff Brown497a92c2010-09-12 17:55:08 -07002481 + " flags=0x" + Integer.toHexString(mFlags)
2482 + " repeat=" + mRepeatCount
2483 + " device=" + mDeviceId
2484 + " source=0x" + Integer.toHexString(mSource)
2485 + "}";
2486 }
2487
2488 /**
2489 * Returns a string that represents the symbolic name of the specified action
2490 * such as "ACTION_DOWN", or "35" (if unknown).
2491 *
2492 * @param action The action.
2493 * @return The symbolic name of the specified action.
2494 * @hide
2495 */
2496 public static String actionToString(int action) {
2497 switch (action) {
2498 case ACTION_DOWN:
2499 return "ACTION_DOWN";
2500 case ACTION_UP:
2501 return "ACTION_UP";
2502 case ACTION_MULTIPLE:
2503 return "ACTION_MULTIPLE";
2504 default:
2505 return Integer.toString(action);
2506 }
2507 }
2508
2509 /**
2510 * Returns a string that represents the symbolic name of the specified keycode
2511 * such as "KEYCODE_A", "KEYCODE_DPAD_UP", or "1001" (if unknown).
2512 *
2513 * @param keyCode The key code.
2514 * @return The symbolic name of the specified keycode.
2515 *
2516 * @see KeyCharacterMap#getDisplayLabel
2517 * @hide
2518 */
2519 public static String keyCodeToString(int keyCode) {
2520 if (keyCode >= 0 && keyCode < KEYCODE_SYMBOLIC_NAMES.length) {
2521 return KEYCODE_SYMBOLIC_NAMES[keyCode];
2522 }
2523 return Integer.toString(keyCode);
2524 }
2525
2526 /**
2527 * Gets a keycode by its symbolic name such as "KEYCODE_A" or "1001" (if unknown).
2528 *
2529 * @param symbolicName The symbolic name of the keycode.
2530 * @return The keycode or -1 if not found.
2531 * @see #keycodeToString
2532 * @hide
2533 */
2534 public static int keyCodeFromString(String symbolicName) {
2535 if (symbolicName == null) {
2536 throw new IllegalArgumentException("symbolicName must not be null");
2537 }
2538
2539 final int count = KEYCODE_SYMBOLIC_NAMES.length;
2540 for (int i = 0; i < count; i++) {
2541 if (symbolicName.equals(KEYCODE_SYMBOLIC_NAMES[i])) {
2542 return i;
2543 }
2544 }
2545
2546 try {
2547 return Integer.parseInt(symbolicName,10);
2548 } catch (NumberFormatException ex) {
2549 return -1;
2550 }
2551 }
2552
2553 /**
2554 * Returns a string that represents the symbolic name of the specified combined meta
2555 * key modifier state flags such as "0", "META_SHIFT_ON",
2556 * "META_ALT_ON|META_SHIFT_ON" or "0x10000000" (if unknown).
2557 *
2558 * @param metaState The meta state.
2559 * @return The symbolic name of the specified combined meta state flags.
2560 * @hide
2561 */
2562 public static String metaStateToString(int metaState) {
2563 if (metaState == 0) {
2564 return "0";
2565 }
2566 StringBuilder result = null;
2567 int i = 0;
2568 while (metaState != 0) {
2569 final boolean isSet = (metaState & 1) != 0;
2570 metaState >>>= 1; // unsigned shift!
2571 if (isSet) {
2572 final String name = META_SYMBOLIC_NAMES[i];
2573 if (result == null) {
2574 if (metaState == 0) {
2575 return name;
2576 }
2577 result = new StringBuilder(name);
2578 } else {
2579 result.append('|');
2580 result.append(name);
2581 }
2582 }
2583 i += 1;
2584 }
2585 return result.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002586 }
2587
2588 public static final Parcelable.Creator<KeyEvent> CREATOR
2589 = new Parcelable.Creator<KeyEvent>() {
2590 public KeyEvent createFromParcel(Parcel in) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07002591 in.readInt(); // skip token, we already know this is a KeyEvent
2592 return KeyEvent.createFromParcelBody(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002593 }
2594
2595 public KeyEvent[] newArray(int size) {
2596 return new KeyEvent[size];
2597 }
2598 };
Jeff Brown6ec402b2010-07-28 15:48:59 -07002599
2600 /** @hide */
2601 public static KeyEvent createFromParcelBody(Parcel in) {
2602 return new KeyEvent(in);
2603 }
2604
2605 private KeyEvent(Parcel in) {
2606 readBaseFromParcel(in);
2607
2608 mAction = in.readInt();
2609 mKeyCode = in.readInt();
2610 mRepeatCount = in.readInt();
2611 mMetaState = in.readInt();
2612 mScanCode = in.readInt();
2613 mFlags = in.readInt();
2614 mDownTime = in.readLong();
2615 mEventTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 }
2617
2618 public void writeToParcel(Parcel out, int flags) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07002619 out.writeInt(PARCEL_TOKEN_KEY_EVENT);
2620
2621 writeBaseToParcel(out);
2622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623 out.writeInt(mAction);
2624 out.writeInt(mKeyCode);
2625 out.writeInt(mRepeatCount);
2626 out.writeInt(mMetaState);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002627 out.writeInt(mScanCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628 out.writeInt(mFlags);
2629 out.writeLong(mDownTime);
2630 out.writeLong(mEventTime);
2631 }
2632
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -07002633 private native boolean native_isSystemKey(int keyCode);
2634 private native boolean native_hasDefaultAction(int keyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002635}