blob: 30b1e523546bcc800a27aed3e3492749915b504f [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 Brown6f2fba42011-02-19 01:08:02 -080023import android.util.SparseArray;
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>
Jean Chalard405bc512012-05-29 19:12:34 +090054 * As soft input methods can use multiple and inventive ways of inputting text,
55 * there is no guarantee that any key press on a soft keyboard will generate a key
56 * event: this is left to the IME's discretion, and in fact sending such events is
57 * discouraged. You should never rely on receiving KeyEvents for any key on a soft
58 * input method. In particular, the default software keyboard will never send any
59 * key event to any application targetting Jelly Bean or later, and will only send
60 * events for some presses of the delete and return keys to applications targetting
61 * Ice Cream Sandwich or earlier. Be aware that other software input methods may
62 * never send key events regardless of the version. Consider using editor actions
63 * like {@link android.view.inputmethod.EditorInfo#IME_ACTION_DONE} if you need
64 * specific interaction with the software keyboard, as it gives more visibility to
65 * the user as to how your application will react to key presses.
66 * </p><p>
Jeff Browndc1ab4b2010-09-14 18:03:38 -070067 * When interacting with an IME, the framework may deliver key events
68 * with the special action {@link #ACTION_MULTIPLE} that either specifies
69 * that single repeated key code or a sequence of characters to insert.
70 * </p><p>
Jeff Brownb6997262010-10-08 22:31:17 -070071 * In general, the framework cannot guarantee that the key events it delivers
72 * to a view always constitute complete key sequences since some events may be dropped
73 * or modified by containing views before they are delivered. The view implementation
74 * should be prepared to handle {@link #FLAG_CANCELED} and should tolerate anomalous
75 * situations such as receiving a new {@link #ACTION_DOWN} without first having
76 * received an {@link #ACTION_UP} for the prior key press.
Jeff Browndc1ab4b2010-09-14 18:03:38 -070077 * </p><p>
78 * Refer to {@link InputDevice} for more information about how different kinds of
79 * input devices and sources represent keys and buttons.
80 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 */
Jeff Brownc5ed5912010-07-14 18:48:53 -070082public class KeyEvent extends InputEvent implements Parcelable {
Jeff Browndc1ab4b2010-09-14 18:03:38 -070083 /** Key code constant: Unknown key code. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 public static final int KEYCODE_UNKNOWN = 0;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070085 /** Key code constant: Soft Left key.
86 * Usually situated below the display on phones and used as a multi-function
87 * feature key for selecting a software defined function shown on the bottom left
88 * of the display. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 public static final int KEYCODE_SOFT_LEFT = 1;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070090 /** Key code constant: Soft Right key.
91 * Usually situated below the display on phones and used as a multi-function
92 * feature key for selecting a software defined function shown on the bottom right
93 * of the display. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 public static final int KEYCODE_SOFT_RIGHT = 2;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070095 /** Key code constant: Home key.
96 * This key is handled by the framework and is never delivered to applications. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 public static final int KEYCODE_HOME = 3;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070098 /** Key code constant: Back key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 public static final int KEYCODE_BACK = 4;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700100 /** Key code constant: Call key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 public static final int KEYCODE_CALL = 5;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700102 /** Key code constant: End Call key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 public static final int KEYCODE_ENDCALL = 6;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700104 /** Key code constant: '0' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 public static final int KEYCODE_0 = 7;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700106 /** Key code constant: '1' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 public static final int KEYCODE_1 = 8;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700108 /** Key code constant: '2' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 public static final int KEYCODE_2 = 9;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700110 /** Key code constant: '3' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 public static final int KEYCODE_3 = 10;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700112 /** Key code constant: '4' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 public static final int KEYCODE_4 = 11;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700114 /** Key code constant: '5' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 public static final int KEYCODE_5 = 12;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700116 /** Key code constant: '6' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 public static final int KEYCODE_6 = 13;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700118 /** Key code constant: '7' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 public static final int KEYCODE_7 = 14;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700120 /** Key code constant: '8' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 public static final int KEYCODE_8 = 15;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700122 /** Key code constant: '9' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 public static final int KEYCODE_9 = 16;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700124 /** Key code constant: '*' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 public static final int KEYCODE_STAR = 17;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700126 /** Key code constant: '#' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 public static final int KEYCODE_POUND = 18;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700128 /** Key code constant: Directional Pad Up key.
129 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 public static final int KEYCODE_DPAD_UP = 19;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700131 /** Key code constant: Directional Pad Down key.
132 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 public static final int KEYCODE_DPAD_DOWN = 20;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700134 /** Key code constant: Directional Pad Left key.
135 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 public static final int KEYCODE_DPAD_LEFT = 21;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700137 /** Key code constant: Directional Pad Right key.
138 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 public static final int KEYCODE_DPAD_RIGHT = 22;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700140 /** Key code constant: Directional Pad Center key.
141 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 public static final int KEYCODE_DPAD_CENTER = 23;
Jeff Brownb0418da2010-11-01 15:24:01 -0700143 /** Key code constant: Volume Up key.
144 * Adjusts the speaker volume up. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 public static final int KEYCODE_VOLUME_UP = 24;
Jeff Brownb0418da2010-11-01 15:24:01 -0700146 /** Key code constant: Volume Down key.
147 * Adjusts the speaker volume down. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 public static final int KEYCODE_VOLUME_DOWN = 25;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700149 /** Key code constant: Power key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 public static final int KEYCODE_POWER = 26;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700151 /** Key code constant: Camera key.
152 * Used to launch a camera application or take pictures. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 public static final int KEYCODE_CAMERA = 27;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700154 /** Key code constant: Clear key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 public static final int KEYCODE_CLEAR = 28;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700156 /** Key code constant: 'A' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 public static final int KEYCODE_A = 29;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700158 /** Key code constant: 'B' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 public static final int KEYCODE_B = 30;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700160 /** Key code constant: 'C' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 public static final int KEYCODE_C = 31;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700162 /** Key code constant: 'D' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 public static final int KEYCODE_D = 32;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700164 /** Key code constant: 'E' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 public static final int KEYCODE_E = 33;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700166 /** Key code constant: 'F' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 public static final int KEYCODE_F = 34;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700168 /** Key code constant: 'G' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 public static final int KEYCODE_G = 35;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700170 /** Key code constant: 'H' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 public static final int KEYCODE_H = 36;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700172 /** Key code constant: 'I' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 public static final int KEYCODE_I = 37;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700174 /** Key code constant: 'J' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 public static final int KEYCODE_J = 38;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700176 /** Key code constant: 'K' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 public static final int KEYCODE_K = 39;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700178 /** Key code constant: 'L' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 public static final int KEYCODE_L = 40;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700180 /** Key code constant: 'M' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 public static final int KEYCODE_M = 41;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700182 /** Key code constant: 'N' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 public static final int KEYCODE_N = 42;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700184 /** Key code constant: 'O' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 public static final int KEYCODE_O = 43;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700186 /** Key code constant: 'P' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 public static final int KEYCODE_P = 44;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700188 /** Key code constant: 'Q' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 public static final int KEYCODE_Q = 45;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700190 /** Key code constant: 'R' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 public static final int KEYCODE_R = 46;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700192 /** Key code constant: 'S' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 public static final int KEYCODE_S = 47;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700194 /** Key code constant: 'T' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 public static final int KEYCODE_T = 48;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700196 /** Key code constant: 'U' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 public static final int KEYCODE_U = 49;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700198 /** Key code constant: 'V' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 public static final int KEYCODE_V = 50;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700200 /** Key code constant: 'W' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 public static final int KEYCODE_W = 51;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700202 /** Key code constant: 'X' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 public static final int KEYCODE_X = 52;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700204 /** Key code constant: 'Y' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 public static final int KEYCODE_Y = 53;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700206 /** Key code constant: 'Z' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 public static final int KEYCODE_Z = 54;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700208 /** Key code constant: ',' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 public static final int KEYCODE_COMMA = 55;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700210 /** Key code constant: '.' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 public static final int KEYCODE_PERIOD = 56;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700212 /** Key code constant: Left Alt modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 public static final int KEYCODE_ALT_LEFT = 57;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700214 /** Key code constant: Right Alt modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 public static final int KEYCODE_ALT_RIGHT = 58;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700216 /** Key code constant: Left Shift modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 public static final int KEYCODE_SHIFT_LEFT = 59;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700218 /** Key code constant: Right Shift modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 public static final int KEYCODE_SHIFT_RIGHT = 60;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700220 /** Key code constant: Tab key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 public static final int KEYCODE_TAB = 61;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700222 /** Key code constant: Space key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 public static final int KEYCODE_SPACE = 62;
Jeff Brown224d4a12010-10-07 20:28:53 -0700224 /** Key code constant: Symbol modifier key.
225 * Used to enter alternate symbols. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 public static final int KEYCODE_SYM = 63;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700227 /** Key code constant: Explorer special function key.
228 * Used to launch a browser application. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 public static final int KEYCODE_EXPLORER = 64;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700230 /** Key code constant: Envelope special function key.
231 * Used to launch a mail application. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 public static final int KEYCODE_ENVELOPE = 65;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700233 /** Key code constant: Enter key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 public static final int KEYCODE_ENTER = 66;
Jeff Brown224d4a12010-10-07 20:28:53 -0700235 /** Key code constant: Backspace key.
Jeff Brown497a92c2010-09-12 17:55:08 -0700236 * Deletes characters before the insertion point, unlike {@link #KEYCODE_FORWARD_DEL}. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 public static final int KEYCODE_DEL = 67;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700238 /** Key code constant: '`' (backtick) key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 public static final int KEYCODE_GRAVE = 68;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700240 /** Key code constant: '-'. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 public static final int KEYCODE_MINUS = 69;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700242 /** Key code constant: '=' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 public static final int KEYCODE_EQUALS = 70;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700244 /** Key code constant: '[' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 public static final int KEYCODE_LEFT_BRACKET = 71;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700246 /** Key code constant: ']' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 public static final int KEYCODE_RIGHT_BRACKET = 72;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700248 /** Key code constant: '\' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 public static final int KEYCODE_BACKSLASH = 73;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700250 /** Key code constant: ';' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 public static final int KEYCODE_SEMICOLON = 74;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700252 /** Key code constant: ''' (apostrophe) key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 public static final int KEYCODE_APOSTROPHE = 75;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700254 /** Key code constant: '/' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 public static final int KEYCODE_SLASH = 76;
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_AT = 77;
Jeff Brown224d4a12010-10-07 20:28:53 -0700258 /** Key code constant: Number modifier key.
259 * Used to enter numeric symbols.
260 * This key is not Num Lock; it is more like {@link #KEYCODE_ALT_LEFT} and is
261 * interpreted as an ALT key by {@link android.text.method.MetaKeyKeyListener}. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 public static final int KEYCODE_NUM = 78;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700263 /** Key code constant: Headset Hook key.
264 * Used to hang up calls and stop media. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 public static final int KEYCODE_HEADSETHOOK = 79;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700266 /** Key code constant: Camera Focus key.
267 * Used to focus the camera. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 public static final int KEYCODE_FOCUS = 80; // *Camera* focus
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700269 /** Key code constant: '+' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 public static final int KEYCODE_PLUS = 81;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700271 /** Key code constant: Menu key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 public static final int KEYCODE_MENU = 82;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700273 /** Key code constant: Notification key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 public static final int KEYCODE_NOTIFICATION = 83;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700275 /** Key code constant: Search key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 public static final int KEYCODE_SEARCH = 84;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700277 /** Key code constant: Play/Pause media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700278 public static final int KEYCODE_MEDIA_PLAY_PAUSE= 85;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700279 /** Key code constant: Stop media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700280 public static final int KEYCODE_MEDIA_STOP = 86;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700281 /** Key code constant: Play Next media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700282 public static final int KEYCODE_MEDIA_NEXT = 87;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700283 /** Key code constant: Play Previous media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700284 public static final int KEYCODE_MEDIA_PREVIOUS = 88;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700285 /** Key code constant: Rewind media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700286 public static final int KEYCODE_MEDIA_REWIND = 89;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700287 /** Key code constant: Fast Forward media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700288 public static final int KEYCODE_MEDIA_FAST_FORWARD = 90;
Jeff Brownb0418da2010-11-01 15:24:01 -0700289 /** Key code constant: Mute key.
290 * Mutes the microphone, unlike {@link #KEYCODE_VOLUME_MUTE}. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 public static final int KEYCODE_MUTE = 91;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700292 /** Key code constant: Page Up key. */
Chih-Wei Huang4fedd802009-05-27 15:52:50 +0800293 public static final int KEYCODE_PAGE_UP = 92;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700294 /** Key code constant: Page Down key. */
Chih-Wei Huang4fedd802009-05-27 15:52:50 +0800295 public static final int KEYCODE_PAGE_DOWN = 93;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700296 /** Key code constant: Picture Symbols modifier key.
297 * Used to switch symbol sets (Emoji, Kao-moji). */
mogimob032bc02009-10-03 03:13:56 +0900298 public static final int KEYCODE_PICTSYMBOLS = 94; // switch symbol-sets (Emoji,Kao-moji)
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700299 /** Key code constant: Switch Charset modifier key.
300 * Used to switch character sets (Kanji, Katakana). */
mogimob032bc02009-10-03 03:13:56 +0900301 public static final int KEYCODE_SWITCH_CHARSET = 95; // switch char-sets (Kanji,Katakana)
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700302 /** Key code constant: A Button key.
303 * On a game controller, the A button should be either the button labeled A
Michael Wright6b57bde2013-01-28 20:35:58 -0800304 * or the first button on the bottom row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700305 public static final int KEYCODE_BUTTON_A = 96;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700306 /** Key code constant: B Button key.
307 * On a game controller, the B button should be either the button labeled B
Michael Wright6b57bde2013-01-28 20:35:58 -0800308 * or the second button on the bottom row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700309 public static final int KEYCODE_BUTTON_B = 97;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700310 /** Key code constant: C Button key.
311 * On a game controller, the C button should be either the button labeled C
Michael Wright6b57bde2013-01-28 20:35:58 -0800312 * or the third button on the bottom row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700313 public static final int KEYCODE_BUTTON_C = 98;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700314 /** Key code constant: X Button key.
315 * On a game controller, the X button should be either the button labeled X
Michael Wright6b57bde2013-01-28 20:35:58 -0800316 * or the first button on the upper row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700317 public static final int KEYCODE_BUTTON_X = 99;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700318 /** Key code constant: Y Button key.
319 * On a game controller, the Y button should be either the button labeled Y
Michael Wright6b57bde2013-01-28 20:35:58 -0800320 * or the second button on the upper row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700321 public static final int KEYCODE_BUTTON_Y = 100;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700322 /** Key code constant: Z Button key.
323 * On a game controller, the Z button should be either the button labeled Z
Michael Wright6b57bde2013-01-28 20:35:58 -0800324 * or the third button on the upper row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700325 public static final int KEYCODE_BUTTON_Z = 101;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700326 /** Key code constant: L1 Button key.
327 * On a game controller, the L1 button should be either the button labeled L1 (or L)
328 * or the top left trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700329 public static final int KEYCODE_BUTTON_L1 = 102;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700330 /** Key code constant: R1 Button key.
331 * On a game controller, the R1 button should be either the button labeled R1 (or R)
332 * or the top right trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700333 public static final int KEYCODE_BUTTON_R1 = 103;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700334 /** Key code constant: L2 Button key.
335 * On a game controller, the L2 button should be either the button labeled L2
336 * or the bottom left trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700337 public static final int KEYCODE_BUTTON_L2 = 104;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700338 /** Key code constant: R2 Button key.
339 * On a game controller, the R2 button should be either the button labeled R2
340 * or the bottom right trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700341 public static final int KEYCODE_BUTTON_R2 = 105;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700342 /** Key code constant: Left Thumb Button key.
343 * On a game controller, the left thumb button indicates that the left (or only)
344 * joystick is pressed. */
Jeff Brownfd035822010-06-30 16:10:35 -0700345 public static final int KEYCODE_BUTTON_THUMBL = 106;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700346 /** Key code constant: Right Thumb Button key.
347 * On a game controller, the right thumb button indicates that the right
348 * joystick is pressed. */
Jeff Brownfd035822010-06-30 16:10:35 -0700349 public static final int KEYCODE_BUTTON_THUMBR = 107;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700350 /** Key code constant: Start Button key.
351 * On a game controller, the button labeled Start. */
Jeff Brownfd035822010-06-30 16:10:35 -0700352 public static final int KEYCODE_BUTTON_START = 108;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700353 /** Key code constant: Select Button key.
354 * On a game controller, the button labeled Select. */
Jeff Brownfd035822010-06-30 16:10:35 -0700355 public static final int KEYCODE_BUTTON_SELECT = 109;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700356 /** Key code constant: Mode Button key.
357 * On a game controller, the button labeled Mode. */
Jeff Brownfd035822010-06-30 16:10:35 -0700358 public static final int KEYCODE_BUTTON_MODE = 110;
Jeff Brown497a92c2010-09-12 17:55:08 -0700359 /** Key code constant: Escape key. */
360 public static final int KEYCODE_ESCAPE = 111;
361 /** Key code constant: Forward Delete key.
362 * Deletes characters ahead of the insertion point, unlike {@link #KEYCODE_DEL}. */
363 public static final int KEYCODE_FORWARD_DEL = 112;
364 /** Key code constant: Left Control modifier key. */
365 public static final int KEYCODE_CTRL_LEFT = 113;
366 /** Key code constant: Right Control modifier key. */
367 public static final int KEYCODE_CTRL_RIGHT = 114;
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800368 /** Key code constant: Caps Lock key. */
Jeff Brown497a92c2010-09-12 17:55:08 -0700369 public static final int KEYCODE_CAPS_LOCK = 115;
370 /** Key code constant: Scroll Lock key. */
371 public static final int KEYCODE_SCROLL_LOCK = 116;
372 /** Key code constant: Left Meta modifier key. */
373 public static final int KEYCODE_META_LEFT = 117;
374 /** Key code constant: Right Meta modifier key. */
375 public static final int KEYCODE_META_RIGHT = 118;
376 /** Key code constant: Function modifier key. */
377 public static final int KEYCODE_FUNCTION = 119;
378 /** Key code constant: System Request / Print Screen key. */
379 public static final int KEYCODE_SYSRQ = 120;
380 /** Key code constant: Break / Pause key. */
381 public static final int KEYCODE_BREAK = 121;
382 /** Key code constant: Home Movement key.
383 * Used for scrolling or moving the cursor around to the start of a line
384 * or to the top of a list. */
385 public static final int KEYCODE_MOVE_HOME = 122;
386 /** Key code constant: End Movement key.
387 * Used for scrolling or moving the cursor around to the end of a line
388 * or to the bottom of a list. */
389 public static final int KEYCODE_MOVE_END = 123;
390 /** Key code constant: Insert key.
391 * Toggles insert / overwrite edit mode. */
392 public static final int KEYCODE_INSERT = 124;
393 /** Key code constant: Forward key.
394 * Navigates forward in the history stack. Complement of {@link #KEYCODE_BACK}. */
395 public static final int KEYCODE_FORWARD = 125;
396 /** Key code constant: Play media key. */
397 public static final int KEYCODE_MEDIA_PLAY = 126;
398 /** Key code constant: Pause media key. */
399 public static final int KEYCODE_MEDIA_PAUSE = 127;
400 /** Key code constant: Close media key.
401 * May be used to close a CD tray, for example. */
402 public static final int KEYCODE_MEDIA_CLOSE = 128;
403 /** Key code constant: Eject media key.
404 * May be used to eject a CD tray, for example. */
405 public static final int KEYCODE_MEDIA_EJECT = 129;
406 /** Key code constant: Record media key. */
407 public static final int KEYCODE_MEDIA_RECORD = 130;
408 /** Key code constant: F1 key. */
409 public static final int KEYCODE_F1 = 131;
410 /** Key code constant: F2 key. */
411 public static final int KEYCODE_F2 = 132;
412 /** Key code constant: F3 key. */
413 public static final int KEYCODE_F3 = 133;
414 /** Key code constant: F4 key. */
415 public static final int KEYCODE_F4 = 134;
416 /** Key code constant: F5 key. */
417 public static final int KEYCODE_F5 = 135;
418 /** Key code constant: F6 key. */
419 public static final int KEYCODE_F6 = 136;
420 /** Key code constant: F7 key. */
421 public static final int KEYCODE_F7 = 137;
422 /** Key code constant: F8 key. */
423 public static final int KEYCODE_F8 = 138;
424 /** Key code constant: F9 key. */
425 public static final int KEYCODE_F9 = 139;
426 /** Key code constant: F10 key. */
427 public static final int KEYCODE_F10 = 140;
428 /** Key code constant: F11 key. */
429 public static final int KEYCODE_F11 = 141;
430 /** Key code constant: F12 key. */
431 public static final int KEYCODE_F12 = 142;
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800432 /** Key code constant: Num Lock key.
Jeff Brown497a92c2010-09-12 17:55:08 -0700433 * This is the Num Lock key; it is different from {@link #KEYCODE_NUM}.
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800434 * This key alters the behavior of other keys on the numeric keypad. */
Jeff Brown497a92c2010-09-12 17:55:08 -0700435 public static final int KEYCODE_NUM_LOCK = 143;
436 /** Key code constant: Numeric keypad '0' key. */
437 public static final int KEYCODE_NUMPAD_0 = 144;
438 /** Key code constant: Numeric keypad '1' key. */
439 public static final int KEYCODE_NUMPAD_1 = 145;
440 /** Key code constant: Numeric keypad '2' key. */
441 public static final int KEYCODE_NUMPAD_2 = 146;
442 /** Key code constant: Numeric keypad '3' key. */
443 public static final int KEYCODE_NUMPAD_3 = 147;
444 /** Key code constant: Numeric keypad '4' key. */
445 public static final int KEYCODE_NUMPAD_4 = 148;
446 /** Key code constant: Numeric keypad '5' key. */
447 public static final int KEYCODE_NUMPAD_5 = 149;
448 /** Key code constant: Numeric keypad '6' key. */
449 public static final int KEYCODE_NUMPAD_6 = 150;
450 /** Key code constant: Numeric keypad '7' key. */
451 public static final int KEYCODE_NUMPAD_7 = 151;
452 /** Key code constant: Numeric keypad '8' key. */
453 public static final int KEYCODE_NUMPAD_8 = 152;
454 /** Key code constant: Numeric keypad '9' key. */
455 public static final int KEYCODE_NUMPAD_9 = 153;
456 /** Key code constant: Numeric keypad '/' key (for division). */
457 public static final int KEYCODE_NUMPAD_DIVIDE = 154;
458 /** Key code constant: Numeric keypad '*' key (for multiplication). */
459 public static final int KEYCODE_NUMPAD_MULTIPLY = 155;
460 /** Key code constant: Numeric keypad '-' key (for subtraction). */
461 public static final int KEYCODE_NUMPAD_SUBTRACT = 156;
462 /** Key code constant: Numeric keypad '+' key (for addition). */
463 public static final int KEYCODE_NUMPAD_ADD = 157;
464 /** Key code constant: Numeric keypad '.' key (for decimals or digit grouping). */
465 public static final int KEYCODE_NUMPAD_DOT = 158;
466 /** Key code constant: Numeric keypad ',' key (for decimals or digit grouping). */
467 public static final int KEYCODE_NUMPAD_COMMA = 159;
468 /** Key code constant: Numeric keypad Enter key. */
469 public static final int KEYCODE_NUMPAD_ENTER = 160;
470 /** Key code constant: Numeric keypad '=' key. */
471 public static final int KEYCODE_NUMPAD_EQUALS = 161;
472 /** Key code constant: Numeric keypad '(' key. */
473 public static final int KEYCODE_NUMPAD_LEFT_PAREN = 162;
474 /** Key code constant: Numeric keypad ')' key. */
475 public static final int KEYCODE_NUMPAD_RIGHT_PAREN = 163;
Jeff Brownb0418da2010-11-01 15:24:01 -0700476 /** Key code constant: Volume Mute key.
477 * Mutes the speaker, unlike {@link #KEYCODE_MUTE}.
478 * This key should normally be implemented as a toggle such that the first press
479 * mutes the speaker and the second press restores the original volume. */
480 public static final int KEYCODE_VOLUME_MUTE = 164;
Jason Bayer3adf4902010-11-09 14:54:55 -0800481 /** Key code constant: Info key.
482 * Common on TV remotes to show additional information related to what is
483 * currently being viewed. */
484 public static final int KEYCODE_INFO = 165;
485 /** Key code constant: Channel up key.
486 * On TV remotes, increments the television channel. */
487 public static final int KEYCODE_CHANNEL_UP = 166;
488 /** Key code constant: Channel down key.
489 * On TV remotes, decrements the television channel. */
490 public static final int KEYCODE_CHANNEL_DOWN = 167;
491 /** Key code constant: Zoom in key. */
492 public static final int KEYCODE_ZOOM_IN = 168;
493 /** Key code constant: Zoom out key. */
494 public static final int KEYCODE_ZOOM_OUT = 169;
495 /** Key code constant: TV key.
496 * On TV remotes, switches to viewing live TV. */
497 public static final int KEYCODE_TV = 170;
498 /** Key code constant: Window key.
499 * On TV remotes, toggles picture-in-picture mode or other windowing functions. */
500 public static final int KEYCODE_WINDOW = 171;
501 /** Key code constant: Guide key.
502 * On TV remotes, shows a programming guide. */
503 public static final int KEYCODE_GUIDE = 172;
504 /** Key code constant: DVR key.
505 * On some TV remotes, switches to a DVR mode for recorded shows. */
506 public static final int KEYCODE_DVR = 173;
507 /** Key code constant: Bookmark key.
508 * On some TV remotes, bookmarks content or web pages. */
509 public static final int KEYCODE_BOOKMARK = 174;
510 /** Key code constant: Toggle captions key.
511 * Switches the mode for closed-captioning text, for example during television shows. */
512 public static final int KEYCODE_CAPTIONS = 175;
513 /** Key code constant: Settings key.
514 * Starts the system settings activity. */
515 public static final int KEYCODE_SETTINGS = 176;
516 /** Key code constant: TV power key.
517 * On TV remotes, toggles the power on a television screen. */
518 public static final int KEYCODE_TV_POWER = 177;
519 /** Key code constant: TV input key.
520 * On TV remotes, switches the input on a television screen. */
521 public static final int KEYCODE_TV_INPUT = 178;
522 /** Key code constant: Set-top-box power key.
523 * On TV remotes, toggles the power on an external Set-top-box. */
524 public static final int KEYCODE_STB_POWER = 179;
525 /** Key code constant: Set-top-box input key.
526 * On TV remotes, switches the input mode on an external Set-top-box. */
527 public static final int KEYCODE_STB_INPUT = 180;
528 /** Key code constant: A/V Receiver power key.
529 * On TV remotes, toggles the power on an external A/V Receiver. */
530 public static final int KEYCODE_AVR_POWER = 181;
531 /** Key code constant: A/V Receiver input key.
532 * On TV remotes, switches the input mode on an external A/V Receiver. */
533 public static final int KEYCODE_AVR_INPUT = 182;
534 /** Key code constant: Red "programmable" key.
535 * On TV remotes, acts as a contextual/programmable key. */
536 public static final int KEYCODE_PROG_RED = 183;
537 /** Key code constant: Green "programmable" key.
538 * On TV remotes, actsas a contextual/programmable key. */
539 public static final int KEYCODE_PROG_GREEN = 184;
540 /** Key code constant: Yellow "programmable" key.
541 * On TV remotes, acts as a contextual/programmable key. */
542 public static final int KEYCODE_PROG_YELLOW = 185;
543 /** Key code constant: Blue "programmable" key.
544 * On TV remotes, acts as a contextual/programmable key. */
545 public static final int KEYCODE_PROG_BLUE = 186;
Jeff Brown49ed71d2010-12-06 17:13:33 -0800546 /** Key code constant: App switch key.
547 * Should bring up the application switcher dialog. */
548 public static final int KEYCODE_APP_SWITCH = 187;
Jeff Browncb1404e2011-01-15 18:14:15 -0800549 /** Key code constant: Generic Game Pad Button #1.*/
550 public static final int KEYCODE_BUTTON_1 = 188;
551 /** Key code constant: Generic Game Pad Button #2.*/
552 public static final int KEYCODE_BUTTON_2 = 189;
553 /** Key code constant: Generic Game Pad Button #3.*/
554 public static final int KEYCODE_BUTTON_3 = 190;
555 /** Key code constant: Generic Game Pad Button #4.*/
556 public static final int KEYCODE_BUTTON_4 = 191;
557 /** Key code constant: Generic Game Pad Button #5.*/
558 public static final int KEYCODE_BUTTON_5 = 192;
559 /** Key code constant: Generic Game Pad Button #6.*/
560 public static final int KEYCODE_BUTTON_6 = 193;
561 /** Key code constant: Generic Game Pad Button #7.*/
562 public static final int KEYCODE_BUTTON_7 = 194;
563 /** Key code constant: Generic Game Pad Button #8.*/
564 public static final int KEYCODE_BUTTON_8 = 195;
565 /** Key code constant: Generic Game Pad Button #9.*/
566 public static final int KEYCODE_BUTTON_9 = 196;
567 /** Key code constant: Generic Game Pad Button #10.*/
568 public static final int KEYCODE_BUTTON_10 = 197;
569 /** Key code constant: Generic Game Pad Button #11.*/
570 public static final int KEYCODE_BUTTON_11 = 198;
571 /** Key code constant: Generic Game Pad Button #12.*/
572 public static final int KEYCODE_BUTTON_12 = 199;
573 /** Key code constant: Generic Game Pad Button #13.*/
574 public static final int KEYCODE_BUTTON_13 = 200;
575 /** Key code constant: Generic Game Pad Button #14.*/
576 public static final int KEYCODE_BUTTON_14 = 201;
577 /** Key code constant: Generic Game Pad Button #15.*/
578 public static final int KEYCODE_BUTTON_15 = 202;
579 /** Key code constant: Generic Game Pad Button #16.*/
580 public static final int KEYCODE_BUTTON_16 = 203;
Jeff Brown9812aed2011-03-07 17:09:51 -0800581 /** Key code constant: Language Switch key.
582 * Toggles the current input language such as switching between English and Japanese on
583 * a QWERTY keyboard. On some devices, the same function may be performed by
584 * pressing Shift+Spacebar. */
585 public static final int KEYCODE_LANGUAGE_SWITCH = 204;
586 /** Key code constant: Manner Mode key.
587 * Toggles silent or vibrate mode on and off to make the device behave more politely
588 * in certain settings such as on a crowded train. On some devices, the key may only
589 * operate when long-pressed. */
590 public static final int KEYCODE_MANNER_MODE = 205;
591 /** Key code constant: 3D Mode key.
592 * Toggles the display between 2D and 3D mode. */
593 public static final int KEYCODE_3D_MODE = 206;
Jeff Brown6651a632011-11-28 12:59:11 -0800594 /** Key code constant: Contacts special function key.
595 * Used to launch an address book application. */
596 public static final int KEYCODE_CONTACTS = 207;
597 /** Key code constant: Calendar special function key.
598 * Used to launch a calendar application. */
599 public static final int KEYCODE_CALENDAR = 208;
600 /** Key code constant: Music special function key.
601 * Used to launch a music player application. */
602 public static final int KEYCODE_MUSIC = 209;
603 /** Key code constant: Calculator special function key.
604 * Used to launch a calculator application. */
605 public static final int KEYCODE_CALCULATOR = 210;
Yang Chuang7511f9c2012-02-10 15:18:26 +0800606 /** Key code constant: Japanese full-width / half-width key. */
607 public static final int KEYCODE_ZENKAKU_HANKAKU = 211;
608 /** Key code constant: Japanese alphanumeric key. */
609 public static final int KEYCODE_EISU = 212;
610 /** Key code constant: Japanese non-conversion key. */
611 public static final int KEYCODE_MUHENKAN = 213;
612 /** Key code constant: Japanese conversion key. */
613 public static final int KEYCODE_HENKAN = 214;
614 /** Key code constant: Japanese katakana / hiragana key. */
615 public static final int KEYCODE_KATAKANA_HIRAGANA = 215;
616 /** Key code constant: Japanese Yen key. */
617 public static final int KEYCODE_YEN = 216;
618 /** Key code constant: Japanese Ro key. */
619 public static final int KEYCODE_RO = 217;
620 /** Key code constant: Japanese kana key. */
621 public static final int KEYCODE_KANA = 218;
Jeff Brownde7a8ea2012-06-13 18:28:57 -0700622 /** Key code constant: Assist key.
623 * Launches the global assist activity. Not delivered to applications. */
624 public static final int KEYCODE_ASSIST = 219;
Michael Wright1df477a2013-01-31 16:19:18 -0800625 /** Key code constant: Brightness Down key.
626 * Adjusts the screen brightness down. */
627 public static final int KEYCODE_BRIGHTNESS_DOWN = 220;
628 /** Key code constant: Brightness Up key.
629 * Adjusts the screen brightness up. */
630 public static final int KEYCODE_BRIGHTNESS_UP = 221;
Jaekyun Seokbfdad8e2013-07-08 13:53:21 +0900631 /** Key code constant: Audio Track key
632 * Switches the audio tracks. */
633 public static final int KEYCODE_MEDIA_AUDIO_TRACK = 222;
Jeff Brown497a92c2010-09-12 17:55:08 -0700634
Jaekyun Seokbfdad8e2013-07-08 13:53:21 +0900635 private static final int LAST_KEYCODE = KEYCODE_MEDIA_AUDIO_TRACK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636
637 // NOTE: If you add a new keycode here you must also add it to:
638 // isSystem()
Chirayu Desai61c37ae2013-04-15 20:11:37 +0530639 // frameworks/native/include/android/keycodes.h
640 // frameworks/base/include/androidfw/KeycodeLabels.h
Jeff Brownfd035822010-06-30 16:10:35 -0700641 // external/webkit/WebKit/android/plugins/ANPKeyCodes.h
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 // frameworks/base/core/res/res/values/attrs.xml
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 // emulator?
Jeff Brown6651a632011-11-28 12:59:11 -0800644 // LAST_KEYCODE
645 // KEYCODE_SYMBOLIC_NAMES
Dianne Hackborn935ae462009-04-13 16:11:55 -0700646 //
647 // Also Android currently does not reserve code ranges for vendor-
648 // specific key codes. If you have new key codes to have, you
649 // MUST contribute a patch to the open source project to define
650 // those new codes. This is intended to maintain a consistent
651 // set of key code definitions across all Android devices.
Jeff Brown497a92c2010-09-12 17:55:08 -0700652
Jeff Brown6f2fba42011-02-19 01:08:02 -0800653 // Symbolic names of all key codes.
654 private static final SparseArray<String> KEYCODE_SYMBOLIC_NAMES = new SparseArray<String>();
655 private static void populateKeycodeSymbolicNames() {
656 SparseArray<String> names = KEYCODE_SYMBOLIC_NAMES;
657 names.append(KEYCODE_UNKNOWN, "KEYCODE_UNKNOWN");
658 names.append(KEYCODE_SOFT_LEFT, "KEYCODE_SOFT_LEFT");
659 names.append(KEYCODE_SOFT_RIGHT, "KEYCODE_SOFT_RIGHT");
660 names.append(KEYCODE_HOME, "KEYCODE_HOME");
661 names.append(KEYCODE_BACK, "KEYCODE_BACK");
662 names.append(KEYCODE_CALL, "KEYCODE_CALL");
663 names.append(KEYCODE_ENDCALL, "KEYCODE_ENDCALL");
664 names.append(KEYCODE_0, "KEYCODE_0");
665 names.append(KEYCODE_1, "KEYCODE_1");
666 names.append(KEYCODE_2, "KEYCODE_2");
667 names.append(KEYCODE_3, "KEYCODE_3");
668 names.append(KEYCODE_4, "KEYCODE_4");
669 names.append(KEYCODE_5, "KEYCODE_5");
670 names.append(KEYCODE_6, "KEYCODE_6");
671 names.append(KEYCODE_7, "KEYCODE_7");
672 names.append(KEYCODE_8, "KEYCODE_8");
673 names.append(KEYCODE_9, "KEYCODE_9");
674 names.append(KEYCODE_STAR, "KEYCODE_STAR");
675 names.append(KEYCODE_POUND, "KEYCODE_POUND");
676 names.append(KEYCODE_DPAD_UP, "KEYCODE_DPAD_UP");
677 names.append(KEYCODE_DPAD_DOWN, "KEYCODE_DPAD_DOWN");
678 names.append(KEYCODE_DPAD_LEFT, "KEYCODE_DPAD_LEFT");
679 names.append(KEYCODE_DPAD_RIGHT, "KEYCODE_DPAD_RIGHT");
680 names.append(KEYCODE_DPAD_CENTER, "KEYCODE_DPAD_CENTER");
681 names.append(KEYCODE_VOLUME_UP, "KEYCODE_VOLUME_UP");
682 names.append(KEYCODE_VOLUME_DOWN, "KEYCODE_VOLUME_DOWN");
683 names.append(KEYCODE_POWER, "KEYCODE_POWER");
684 names.append(KEYCODE_CAMERA, "KEYCODE_CAMERA");
685 names.append(KEYCODE_CLEAR, "KEYCODE_CLEAR");
686 names.append(KEYCODE_A, "KEYCODE_A");
687 names.append(KEYCODE_B, "KEYCODE_B");
688 names.append(KEYCODE_C, "KEYCODE_C");
689 names.append(KEYCODE_D, "KEYCODE_D");
690 names.append(KEYCODE_E, "KEYCODE_E");
691 names.append(KEYCODE_F, "KEYCODE_F");
692 names.append(KEYCODE_G, "KEYCODE_G");
693 names.append(KEYCODE_H, "KEYCODE_H");
694 names.append(KEYCODE_I, "KEYCODE_I");
695 names.append(KEYCODE_J, "KEYCODE_J");
696 names.append(KEYCODE_K, "KEYCODE_K");
697 names.append(KEYCODE_L, "KEYCODE_L");
698 names.append(KEYCODE_M, "KEYCODE_M");
699 names.append(KEYCODE_N, "KEYCODE_N");
700 names.append(KEYCODE_O, "KEYCODE_O");
701 names.append(KEYCODE_P, "KEYCODE_P");
702 names.append(KEYCODE_Q, "KEYCODE_Q");
703 names.append(KEYCODE_R, "KEYCODE_R");
704 names.append(KEYCODE_S, "KEYCODE_S");
705 names.append(KEYCODE_T, "KEYCODE_T");
706 names.append(KEYCODE_U, "KEYCODE_U");
707 names.append(KEYCODE_V, "KEYCODE_V");
708 names.append(KEYCODE_W, "KEYCODE_W");
709 names.append(KEYCODE_X, "KEYCODE_X");
710 names.append(KEYCODE_Y, "KEYCODE_Y");
711 names.append(KEYCODE_Z, "KEYCODE_Z");
712 names.append(KEYCODE_COMMA, "KEYCODE_COMMA");
713 names.append(KEYCODE_PERIOD, "KEYCODE_PERIOD");
714 names.append(KEYCODE_ALT_LEFT, "KEYCODE_ALT_LEFT");
715 names.append(KEYCODE_ALT_RIGHT, "KEYCODE_ALT_RIGHT");
716 names.append(KEYCODE_SHIFT_LEFT, "KEYCODE_SHIFT_LEFT");
717 names.append(KEYCODE_SHIFT_RIGHT, "KEYCODE_SHIFT_RIGHT");
718 names.append(KEYCODE_TAB, "KEYCODE_TAB");
719 names.append(KEYCODE_SPACE, "KEYCODE_SPACE");
720 names.append(KEYCODE_SYM, "KEYCODE_SYM");
721 names.append(KEYCODE_EXPLORER, "KEYCODE_EXPLORER");
722 names.append(KEYCODE_ENVELOPE, "KEYCODE_ENVELOPE");
723 names.append(KEYCODE_ENTER, "KEYCODE_ENTER");
724 names.append(KEYCODE_DEL, "KEYCODE_DEL");
725 names.append(KEYCODE_GRAVE, "KEYCODE_GRAVE");
726 names.append(KEYCODE_MINUS, "KEYCODE_MINUS");
727 names.append(KEYCODE_EQUALS, "KEYCODE_EQUALS");
728 names.append(KEYCODE_LEFT_BRACKET, "KEYCODE_LEFT_BRACKET");
729 names.append(KEYCODE_RIGHT_BRACKET, "KEYCODE_RIGHT_BRACKET");
730 names.append(KEYCODE_BACKSLASH, "KEYCODE_BACKSLASH");
731 names.append(KEYCODE_SEMICOLON, "KEYCODE_SEMICOLON");
732 names.append(KEYCODE_APOSTROPHE, "KEYCODE_APOSTROPHE");
733 names.append(KEYCODE_SLASH, "KEYCODE_SLASH");
734 names.append(KEYCODE_AT, "KEYCODE_AT");
735 names.append(KEYCODE_NUM, "KEYCODE_NUM");
736 names.append(KEYCODE_HEADSETHOOK, "KEYCODE_HEADSETHOOK");
737 names.append(KEYCODE_FOCUS, "KEYCODE_FOCUS");
738 names.append(KEYCODE_PLUS, "KEYCODE_PLUS");
739 names.append(KEYCODE_MENU, "KEYCODE_MENU");
740 names.append(KEYCODE_NOTIFICATION, "KEYCODE_NOTIFICATION");
741 names.append(KEYCODE_SEARCH, "KEYCODE_SEARCH");
742 names.append(KEYCODE_MEDIA_PLAY_PAUSE, "KEYCODE_MEDIA_PLAY_PAUSE");
743 names.append(KEYCODE_MEDIA_STOP, "KEYCODE_MEDIA_STOP");
744 names.append(KEYCODE_MEDIA_NEXT, "KEYCODE_MEDIA_NEXT");
745 names.append(KEYCODE_MEDIA_PREVIOUS, "KEYCODE_MEDIA_PREVIOUS");
746 names.append(KEYCODE_MEDIA_REWIND, "KEYCODE_MEDIA_REWIND");
747 names.append(KEYCODE_MEDIA_FAST_FORWARD, "KEYCODE_MEDIA_FAST_FORWARD");
748 names.append(KEYCODE_MUTE, "KEYCODE_MUTE");
749 names.append(KEYCODE_PAGE_UP, "KEYCODE_PAGE_UP");
750 names.append(KEYCODE_PAGE_DOWN, "KEYCODE_PAGE_DOWN");
751 names.append(KEYCODE_PICTSYMBOLS, "KEYCODE_PICTSYMBOLS");
752 names.append(KEYCODE_SWITCH_CHARSET, "KEYCODE_SWITCH_CHARSET");
753 names.append(KEYCODE_BUTTON_A, "KEYCODE_BUTTON_A");
754 names.append(KEYCODE_BUTTON_B, "KEYCODE_BUTTON_B");
755 names.append(KEYCODE_BUTTON_C, "KEYCODE_BUTTON_C");
756 names.append(KEYCODE_BUTTON_X, "KEYCODE_BUTTON_X");
757 names.append(KEYCODE_BUTTON_Y, "KEYCODE_BUTTON_Y");
758 names.append(KEYCODE_BUTTON_Z, "KEYCODE_BUTTON_Z");
759 names.append(KEYCODE_BUTTON_L1, "KEYCODE_BUTTON_L1");
760 names.append(KEYCODE_BUTTON_R1, "KEYCODE_BUTTON_R1");
761 names.append(KEYCODE_BUTTON_L2, "KEYCODE_BUTTON_L2");
762 names.append(KEYCODE_BUTTON_R2, "KEYCODE_BUTTON_R2");
763 names.append(KEYCODE_BUTTON_THUMBL, "KEYCODE_BUTTON_THUMBL");
764 names.append(KEYCODE_BUTTON_THUMBR, "KEYCODE_BUTTON_THUMBR");
765 names.append(KEYCODE_BUTTON_START, "KEYCODE_BUTTON_START");
766 names.append(KEYCODE_BUTTON_SELECT, "KEYCODE_BUTTON_SELECT");
767 names.append(KEYCODE_BUTTON_MODE, "KEYCODE_BUTTON_MODE");
768 names.append(KEYCODE_ESCAPE, "KEYCODE_ESCAPE");
769 names.append(KEYCODE_FORWARD_DEL, "KEYCODE_FORWARD_DEL");
770 names.append(KEYCODE_CTRL_LEFT, "KEYCODE_CTRL_LEFT");
771 names.append(KEYCODE_CTRL_RIGHT, "KEYCODE_CTRL_RIGHT");
772 names.append(KEYCODE_CAPS_LOCK, "KEYCODE_CAPS_LOCK");
773 names.append(KEYCODE_SCROLL_LOCK, "KEYCODE_SCROLL_LOCK");
774 names.append(KEYCODE_META_LEFT, "KEYCODE_META_LEFT");
775 names.append(KEYCODE_META_RIGHT, "KEYCODE_META_RIGHT");
776 names.append(KEYCODE_FUNCTION, "KEYCODE_FUNCTION");
777 names.append(KEYCODE_SYSRQ, "KEYCODE_SYSRQ");
778 names.append(KEYCODE_BREAK, "KEYCODE_BREAK");
779 names.append(KEYCODE_MOVE_HOME, "KEYCODE_MOVE_HOME");
780 names.append(KEYCODE_MOVE_END, "KEYCODE_MOVE_END");
781 names.append(KEYCODE_INSERT, "KEYCODE_INSERT");
782 names.append(KEYCODE_FORWARD, "KEYCODE_FORWARD");
783 names.append(KEYCODE_MEDIA_PLAY, "KEYCODE_MEDIA_PLAY");
784 names.append(KEYCODE_MEDIA_PAUSE, "KEYCODE_MEDIA_PAUSE");
785 names.append(KEYCODE_MEDIA_CLOSE, "KEYCODE_MEDIA_CLOSE");
786 names.append(KEYCODE_MEDIA_EJECT, "KEYCODE_MEDIA_EJECT");
787 names.append(KEYCODE_MEDIA_RECORD, "KEYCODE_MEDIA_RECORD");
788 names.append(KEYCODE_F1, "KEYCODE_F1");
789 names.append(KEYCODE_F2, "KEYCODE_F2");
790 names.append(KEYCODE_F3, "KEYCODE_F3");
791 names.append(KEYCODE_F4, "KEYCODE_F4");
792 names.append(KEYCODE_F5, "KEYCODE_F5");
793 names.append(KEYCODE_F6, "KEYCODE_F6");
794 names.append(KEYCODE_F7, "KEYCODE_F7");
795 names.append(KEYCODE_F8, "KEYCODE_F8");
796 names.append(KEYCODE_F9, "KEYCODE_F9");
797 names.append(KEYCODE_F10, "KEYCODE_F10");
798 names.append(KEYCODE_F11, "KEYCODE_F11");
799 names.append(KEYCODE_F12, "KEYCODE_F12");
800 names.append(KEYCODE_NUM_LOCK, "KEYCODE_NUM_LOCK");
801 names.append(KEYCODE_NUMPAD_0, "KEYCODE_NUMPAD_0");
802 names.append(KEYCODE_NUMPAD_1, "KEYCODE_NUMPAD_1");
803 names.append(KEYCODE_NUMPAD_2, "KEYCODE_NUMPAD_2");
804 names.append(KEYCODE_NUMPAD_3, "KEYCODE_NUMPAD_3");
805 names.append(KEYCODE_NUMPAD_4, "KEYCODE_NUMPAD_4");
806 names.append(KEYCODE_NUMPAD_5, "KEYCODE_NUMPAD_5");
807 names.append(KEYCODE_NUMPAD_6, "KEYCODE_NUMPAD_6");
808 names.append(KEYCODE_NUMPAD_7, "KEYCODE_NUMPAD_7");
809 names.append(KEYCODE_NUMPAD_8, "KEYCODE_NUMPAD_8");
810 names.append(KEYCODE_NUMPAD_9, "KEYCODE_NUMPAD_9");
811 names.append(KEYCODE_NUMPAD_DIVIDE, "KEYCODE_NUMPAD_DIVIDE");
812 names.append(KEYCODE_NUMPAD_MULTIPLY, "KEYCODE_NUMPAD_MULTIPLY");
813 names.append(KEYCODE_NUMPAD_SUBTRACT, "KEYCODE_NUMPAD_SUBTRACT");
814 names.append(KEYCODE_NUMPAD_ADD, "KEYCODE_NUMPAD_ADD");
815 names.append(KEYCODE_NUMPAD_DOT, "KEYCODE_NUMPAD_DOT");
816 names.append(KEYCODE_NUMPAD_COMMA, "KEYCODE_NUMPAD_COMMA");
817 names.append(KEYCODE_NUMPAD_ENTER, "KEYCODE_NUMPAD_ENTER");
818 names.append(KEYCODE_NUMPAD_EQUALS, "KEYCODE_NUMPAD_EQUALS");
819 names.append(KEYCODE_NUMPAD_LEFT_PAREN, "KEYCODE_NUMPAD_LEFT_PAREN");
820 names.append(KEYCODE_NUMPAD_RIGHT_PAREN, "KEYCODE_NUMPAD_RIGHT_PAREN");
821 names.append(KEYCODE_VOLUME_MUTE, "KEYCODE_VOLUME_MUTE");
822 names.append(KEYCODE_INFO, "KEYCODE_INFO");
823 names.append(KEYCODE_CHANNEL_UP, "KEYCODE_CHANNEL_UP");
824 names.append(KEYCODE_CHANNEL_DOWN, "KEYCODE_CHANNEL_DOWN");
825 names.append(KEYCODE_ZOOM_IN, "KEYCODE_ZOOM_IN");
826 names.append(KEYCODE_ZOOM_OUT, "KEYCODE_ZOOM_OUT");
827 names.append(KEYCODE_TV, "KEYCODE_TV");
828 names.append(KEYCODE_WINDOW, "KEYCODE_WINDOW");
829 names.append(KEYCODE_GUIDE, "KEYCODE_GUIDE");
830 names.append(KEYCODE_DVR, "KEYCODE_DVR");
831 names.append(KEYCODE_BOOKMARK, "KEYCODE_BOOKMARK");
832 names.append(KEYCODE_CAPTIONS, "KEYCODE_CAPTIONS");
833 names.append(KEYCODE_SETTINGS, "KEYCODE_SETTINGS");
834 names.append(KEYCODE_TV_POWER, "KEYCODE_TV_POWER");
835 names.append(KEYCODE_TV_INPUT, "KEYCODE_TV_INPUT");
836 names.append(KEYCODE_STB_INPUT, "KEYCODE_STB_INPUT");
837 names.append(KEYCODE_STB_POWER, "KEYCODE_STB_POWER");
838 names.append(KEYCODE_AVR_POWER, "KEYCODE_AVR_POWER");
839 names.append(KEYCODE_AVR_INPUT, "KEYCODE_AVR_INPUT");
840 names.append(KEYCODE_PROG_RED, "KEYCODE_PROG_RED");
841 names.append(KEYCODE_PROG_GREEN, "KEYCODE_PROG_GREEN");
842 names.append(KEYCODE_PROG_YELLOW, "KEYCODE_PROG_YELLOW");
843 names.append(KEYCODE_PROG_BLUE, "KEYCODE_PROG_BLUE");
844 names.append(KEYCODE_APP_SWITCH, "KEYCODE_APP_SWITCH");
845 names.append(KEYCODE_BUTTON_1, "KEYCODE_BUTTON_1");
846 names.append(KEYCODE_BUTTON_2, "KEYCODE_BUTTON_2");
847 names.append(KEYCODE_BUTTON_3, "KEYCODE_BUTTON_3");
848 names.append(KEYCODE_BUTTON_4, "KEYCODE_BUTTON_4");
849 names.append(KEYCODE_BUTTON_5, "KEYCODE_BUTTON_5");
850 names.append(KEYCODE_BUTTON_6, "KEYCODE_BUTTON_6");
851 names.append(KEYCODE_BUTTON_7, "KEYCODE_BUTTON_7");
852 names.append(KEYCODE_BUTTON_8, "KEYCODE_BUTTON_8");
853 names.append(KEYCODE_BUTTON_9, "KEYCODE_BUTTON_9");
854 names.append(KEYCODE_BUTTON_10, "KEYCODE_BUTTON_10");
855 names.append(KEYCODE_BUTTON_11, "KEYCODE_BUTTON_11");
856 names.append(KEYCODE_BUTTON_12, "KEYCODE_BUTTON_12");
857 names.append(KEYCODE_BUTTON_13, "KEYCODE_BUTTON_13");
858 names.append(KEYCODE_BUTTON_14, "KEYCODE_BUTTON_14");
859 names.append(KEYCODE_BUTTON_15, "KEYCODE_BUTTON_15");
860 names.append(KEYCODE_BUTTON_16, "KEYCODE_BUTTON_16");
Jeff Brown9812aed2011-03-07 17:09:51 -0800861 names.append(KEYCODE_LANGUAGE_SWITCH, "KEYCODE_LANGUAGE_SWITCH");
862 names.append(KEYCODE_MANNER_MODE, "KEYCODE_MANNER_MODE");
863 names.append(KEYCODE_3D_MODE, "KEYCODE_3D_MODE");
Jeff Brown6651a632011-11-28 12:59:11 -0800864 names.append(KEYCODE_CONTACTS, "KEYCODE_CONTACTS");
865 names.append(KEYCODE_CALENDAR, "KEYCODE_CALENDAR");
866 names.append(KEYCODE_MUSIC, "KEYCODE_MUSIC");
867 names.append(KEYCODE_CALCULATOR, "KEYCODE_CALCULATOR");
Yang Chuang7511f9c2012-02-10 15:18:26 +0800868 names.append(KEYCODE_ZENKAKU_HANKAKU, "KEYCODE_ZENKAKU_HANKAKU");
869 names.append(KEYCODE_EISU, "KEYCODE_EISU");
870 names.append(KEYCODE_MUHENKAN, "KEYCODE_MUHENKAN");
871 names.append(KEYCODE_HENKAN, "KEYCODE_HENKAN");
872 names.append(KEYCODE_KATAKANA_HIRAGANA, "KEYCODE_KATAKANA_HIRAGANA");
873 names.append(KEYCODE_YEN, "KEYCODE_YEN");
874 names.append(KEYCODE_RO, "KEYCODE_RO");
875 names.append(KEYCODE_KANA, "KEYCODE_KANA");
Jeff Brownde7a8ea2012-06-13 18:28:57 -0700876 names.append(KEYCODE_ASSIST, "KEYCODE_ASSIST");
Michael Wright1df477a2013-01-31 16:19:18 -0800877 names.append(KEYCODE_BRIGHTNESS_DOWN, "KEYCODE_BRIGHTNESS_DOWN");
878 names.append(KEYCODE_BRIGHTNESS_UP, "KEYCODE_BRIGHTNESS_UP");
Jaekyun Seokbfdad8e2013-07-08 13:53:21 +0900879 names.append(KEYCODE_MEDIA_AUDIO_TRACK, "KEYCODE_MEDIA_AUDIO_TRACK");
Jeff Brown497a92c2010-09-12 17:55:08 -0700880 };
881
882 // Symbolic names of all metakeys in bit order from least significant to most significant.
883 // Accordingly there are exactly 32 values in this table.
884 private static final String[] META_SYMBOLIC_NAMES = new String[] {
885 "META_SHIFT_ON",
886 "META_ALT_ON",
887 "META_SYM_ON",
888 "META_FUNCTION_ON",
889 "META_ALT_LEFT_ON",
890 "META_ALT_RIGHT_ON",
891 "META_SHIFT_LEFT_ON",
892 "META_SHIFT_RIGHT_ON",
893 "META_CAP_LOCKED",
894 "META_ALT_LOCKED",
895 "META_SYM_LOCKED",
896 "0x00000800",
897 "META_CTRL_ON",
898 "META_CTRL_LEFT_ON",
899 "META_CTRL_RIGHT_ON",
900 "0x00008000",
901 "META_META_ON",
902 "META_META_LEFT_ON",
903 "META_META_RIGHT_ON",
904 "0x00080000",
Jeff Brown51e7fe72010-10-29 22:19:53 -0700905 "META_CAPS_LOCK_ON",
906 "META_NUM_LOCK_ON",
907 "META_SCROLL_LOCK_ON",
Jeff Brown497a92c2010-09-12 17:55:08 -0700908 "0x00800000",
909 "0x01000000",
910 "0x02000000",
911 "0x04000000",
912 "0x08000000",
913 "0x10000000",
914 "0x20000000",
915 "0x40000000",
916 "0x80000000",
917 };
918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 /**
920 * @deprecated There are now more than MAX_KEYCODE keycodes.
921 * Use {@link #getMaxKeyCode()} instead.
922 */
923 @Deprecated
924 public static final int MAX_KEYCODE = 84;
925
926 /**
927 * {@link #getAction} value: the key has been pressed down.
928 */
929 public static final int ACTION_DOWN = 0;
930 /**
931 * {@link #getAction} value: the key has been released.
932 */
933 public static final int ACTION_UP = 1;
934 /**
935 * {@link #getAction} value: multiple duplicate key events have
936 * occurred in a row, or a complex string is being delivered. If the
937 * key code is not {#link {@link #KEYCODE_UNKNOWN} then the
938 * {#link {@link #getRepeatCount()} method returns the number of times
939 * the given key code should be executed.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700940 * Otherwise, if the key code is {@link #KEYCODE_UNKNOWN}, then
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 * this is a sequence of characters as returned by {@link #getCharacters}.
942 */
943 public static final int ACTION_MULTIPLE = 2;
944
945 /**
Jeff Brown497a92c2010-09-12 17:55:08 -0700946 * SHIFT key locked in CAPS mode.
947 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
948 * @hide
949 */
950 public static final int META_CAP_LOCKED = 0x100;
951
952 /**
953 * ALT key locked.
954 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
955 * @hide
956 */
957 public static final int META_ALT_LOCKED = 0x200;
958
959 /**
960 * SYM key locked.
961 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
962 * @hide
963 */
964 public static final int META_SYM_LOCKED = 0x400;
965
966 /**
967 * Text is in selection mode.
968 * Reserved for use by {@link MetaKeyKeyListener} for a private unpublished constant
969 * in its API that is currently being retained for legacy reasons.
970 * @hide
971 */
972 public static final int META_SELECTING = 0x800;
973
974 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 * <p>This mask is used to check whether one of the ALT meta keys is pressed.</p>
976 *
977 * @see #isAltPressed()
978 * @see #getMetaState()
979 * @see #KEYCODE_ALT_LEFT
980 * @see #KEYCODE_ALT_RIGHT
981 */
982 public static final int META_ALT_ON = 0x02;
983
984 /**
985 * <p>This mask is used to check whether the left ALT meta key is pressed.</p>
986 *
987 * @see #isAltPressed()
988 * @see #getMetaState()
989 * @see #KEYCODE_ALT_LEFT
990 */
991 public static final int META_ALT_LEFT_ON = 0x10;
992
993 /**
994 * <p>This mask is used to check whether the right the ALT meta key is pressed.</p>
995 *
996 * @see #isAltPressed()
997 * @see #getMetaState()
998 * @see #KEYCODE_ALT_RIGHT
999 */
1000 public static final int META_ALT_RIGHT_ON = 0x20;
1001
1002 /**
1003 * <p>This mask is used to check whether one of the SHIFT meta keys is pressed.</p>
1004 *
1005 * @see #isShiftPressed()
1006 * @see #getMetaState()
1007 * @see #KEYCODE_SHIFT_LEFT
1008 * @see #KEYCODE_SHIFT_RIGHT
1009 */
1010 public static final int META_SHIFT_ON = 0x1;
1011
1012 /**
1013 * <p>This mask is used to check whether the left SHIFT meta key is pressed.</p>
1014 *
1015 * @see #isShiftPressed()
1016 * @see #getMetaState()
1017 * @see #KEYCODE_SHIFT_LEFT
1018 */
1019 public static final int META_SHIFT_LEFT_ON = 0x40;
1020
1021 /**
1022 * <p>This mask is used to check whether the right SHIFT meta key is pressed.</p>
1023 *
1024 * @see #isShiftPressed()
1025 * @see #getMetaState()
1026 * @see #KEYCODE_SHIFT_RIGHT
1027 */
1028 public static final int META_SHIFT_RIGHT_ON = 0x80;
1029
1030 /**
1031 * <p>This mask is used to check whether the SYM meta key is pressed.</p>
1032 *
1033 * @see #isSymPressed()
1034 * @see #getMetaState()
1035 */
1036 public static final int META_SYM_ON = 0x4;
1037
1038 /**
Jeff Brown497a92c2010-09-12 17:55:08 -07001039 * <p>This mask is used to check whether the FUNCTION meta key is pressed.</p>
1040 *
1041 * @see #isFunctionPressed()
1042 * @see #getMetaState()
1043 */
1044 public static final int META_FUNCTION_ON = 0x8;
1045
1046 /**
1047 * <p>This mask is used to check whether one of the CTRL meta keys is pressed.</p>
1048 *
1049 * @see #isCtrlPressed()
1050 * @see #getMetaState()
1051 * @see #KEYCODE_CTRL_LEFT
1052 * @see #KEYCODE_CTRL_RIGHT
1053 */
1054 public static final int META_CTRL_ON = 0x1000;
1055
1056 /**
1057 * <p>This mask is used to check whether the left CTRL meta key is pressed.</p>
1058 *
1059 * @see #isCtrlPressed()
1060 * @see #getMetaState()
1061 * @see #KEYCODE_CTRL_LEFT
1062 */
1063 public static final int META_CTRL_LEFT_ON = 0x2000;
1064
1065 /**
1066 * <p>This mask is used to check whether the right CTRL meta key is pressed.</p>
1067 *
1068 * @see #isCtrlPressed()
1069 * @see #getMetaState()
1070 * @see #KEYCODE_CTRL_RIGHT
1071 */
1072 public static final int META_CTRL_RIGHT_ON = 0x4000;
1073
1074 /**
1075 * <p>This mask is used to check whether one of the META meta keys is pressed.</p>
1076 *
1077 * @see #isMetaPressed()
1078 * @see #getMetaState()
1079 * @see #KEYCODE_META_LEFT
1080 * @see #KEYCODE_META_RIGHT
1081 */
1082 public static final int META_META_ON = 0x10000;
1083
1084 /**
1085 * <p>This mask is used to check whether the left META meta key is pressed.</p>
1086 *
1087 * @see #isMetaPressed()
1088 * @see #getMetaState()
1089 * @see #KEYCODE_META_LEFT
1090 */
1091 public static final int META_META_LEFT_ON = 0x20000;
1092
1093 /**
1094 * <p>This mask is used to check whether the right META meta key is pressed.</p>
1095 *
1096 * @see #isMetaPressed()
1097 * @see #getMetaState()
1098 * @see #KEYCODE_META_RIGHT
1099 */
1100 public static final int META_META_RIGHT_ON = 0x40000;
1101
1102 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07001103 * <p>This mask is used to check whether the CAPS LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07001104 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07001105 * @see #isCapsLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -07001106 * @see #getMetaState()
1107 * @see #KEYCODE_CAPS_LOCK
1108 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07001109 public static final int META_CAPS_LOCK_ON = 0x100000;
Jeff Brown497a92c2010-09-12 17:55:08 -07001110
1111 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07001112 * <p>This mask is used to check whether the NUM LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07001113 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07001114 * @see #isNumLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -07001115 * @see #getMetaState()
1116 * @see #KEYCODE_NUM_LOCK
1117 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07001118 public static final int META_NUM_LOCK_ON = 0x200000;
Jeff Brown497a92c2010-09-12 17:55:08 -07001119
1120 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07001121 * <p>This mask is used to check whether the SCROLL LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07001122 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07001123 * @see #isScrollLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -07001124 * @see #getMetaState()
1125 * @see #KEYCODE_SCROLL_LOCK
1126 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07001127 public static final int META_SCROLL_LOCK_ON = 0x400000;
Jeff Brown497a92c2010-09-12 17:55:08 -07001128
Jeff Brown64da12a2011-01-04 19:57:47 -08001129 /**
1130 * This mask is a combination of {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON}
1131 * and {@link #META_SHIFT_RIGHT_ON}.
1132 */
Jeff Brownc1df9072010-12-21 16:38:50 -08001133 public static final int META_SHIFT_MASK = META_SHIFT_ON
1134 | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON;
1135
Jeff Brown64da12a2011-01-04 19:57:47 -08001136 /**
1137 * This mask is a combination of {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON}
1138 * and {@link #META_ALT_RIGHT_ON}.
1139 */
Jeff Brownc1df9072010-12-21 16:38:50 -08001140 public static final int META_ALT_MASK = META_ALT_ON
1141 | META_ALT_LEFT_ON | META_ALT_RIGHT_ON;
1142
Jeff Brown64da12a2011-01-04 19:57:47 -08001143 /**
1144 * This mask is a combination of {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON}
1145 * and {@link #META_CTRL_RIGHT_ON}.
1146 */
Jeff Brownc1df9072010-12-21 16:38:50 -08001147 public static final int META_CTRL_MASK = META_CTRL_ON
1148 | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON;
1149
Jeff Brown64da12a2011-01-04 19:57:47 -08001150 /**
1151 * This mask is a combination of {@link #META_META_ON}, {@link #META_META_LEFT_ON}
1152 * and {@link #META_META_RIGHT_ON}.
1153 */
1154 public static final int META_META_MASK = META_META_ON
Jeff Brownc1df9072010-12-21 16:38:50 -08001155 | META_META_LEFT_ON | META_META_RIGHT_ON;
1156
Jeff Brown497a92c2010-09-12 17:55:08 -07001157 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 * This mask is set if the device woke because of this key event.
1159 */
1160 public static final int FLAG_WOKE_HERE = 0x1;
1161
1162 /**
1163 * This mask is set if the key event was generated by a software keyboard.
1164 */
1165 public static final int FLAG_SOFT_KEYBOARD = 0x2;
1166
1167 /**
1168 * This mask is set if we don't want the key event to cause us to leave
1169 * touch mode.
1170 */
1171 public static final int FLAG_KEEP_TOUCH_MODE = 0x4;
1172
1173 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001174 * This mask is set if an event was known to come from a trusted part
1175 * of the system. That is, the event is known to come from the user,
1176 * and could not have been spoofed by a third party component.
1177 */
1178 public static final int FLAG_FROM_SYSTEM = 0x8;
1179
1180 /**
1181 * This mask is used for compatibility, to identify enter keys that are
1182 * coming from an IME whose enter key has been auto-labelled "next" or
1183 * "done". This allows TextView to dispatch these as normal enter keys
1184 * for old applications, but still do the appropriate action when
1185 * receiving them.
1186 */
1187 public static final int FLAG_EDITOR_ACTION = 0x10;
1188
1189 /**
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07001190 * When associated with up key events, this indicates that the key press
1191 * has been canceled. Typically this is used with virtual touch screen
1192 * keys, where the user can slide from the virtual key area on to the
1193 * display: in that case, the application will receive a canceled up
1194 * event and should not perform the action normally associated with the
1195 * key. Note that for this to work, the application can not perform an
1196 * action for a key until it receives an up or the long press timeout has
1197 * expired.
1198 */
1199 public static final int FLAG_CANCELED = 0x20;
1200
1201 /**
1202 * This key event was generated by a virtual (on-screen) hard key area.
1203 * Typically this is an area of the touchscreen, outside of the regular
1204 * display, dedicated to "hardware" buttons.
1205 */
1206 public static final int FLAG_VIRTUAL_HARD_KEY = 0x40;
1207
1208 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001209 * This flag is set for the first key repeat that occurs after the
1210 * long press timeout.
1211 */
1212 public static final int FLAG_LONG_PRESS = 0x80;
1213
1214 /**
1215 * Set when a key event has {@link #FLAG_CANCELED} set because a long
1216 * press action was executed while it was down.
1217 */
1218 public static final int FLAG_CANCELED_LONG_PRESS = 0x100;
1219
1220 /**
1221 * Set for {@link #ACTION_UP} when this event's key code is still being
1222 * tracked from its initial down. That is, somebody requested that tracking
1223 * started on the key down and a long press has not caused
1224 * the tracking to be canceled.
1225 */
1226 public static final int FLAG_TRACKING = 0x200;
Jeff Brown49ed71d2010-12-06 17:13:33 -08001227
1228 /**
1229 * Set when a key event has been synthesized to implement default behavior
1230 * for an event that the application did not handle.
1231 * Fallback key events are generated by unhandled trackball motions
1232 * (to emulate a directional keypad) and by certain unhandled key presses
1233 * that are declared in the key map (such as special function numeric keypad
1234 * keys when numlock is off).
1235 */
1236 public static final int FLAG_FALLBACK = 0x400;
1237
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001238 /**
Michael Wrighta44dd262013-04-10 21:12:00 -07001239 * Signifies that the key is being predispatched.
1240 * @hide
1241 */
1242 public static final int FLAG_PREDISPATCH = 0x20000000;
1243
1244 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001245 * Private control to determine when an app is tracking a key sequence.
1246 * @hide
1247 */
1248 public static final int FLAG_START_TRACKING = 0x40000000;
Jeff Brown21bc5c92011-02-28 18:27:14 -08001249
1250 /**
1251 * Private flag that indicates when the system has detected that this key event
1252 * may be inconsistent with respect to the sequence of previously delivered key events,
1253 * such as when a key up event is sent but the key was not down.
1254 *
1255 * @hide
1256 * @see #isTainted
1257 * @see #setTainted
1258 */
1259 public static final int FLAG_TAINTED = 0x80000000;
1260
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001261 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 * Returns the maximum keycode.
1263 */
1264 public static int getMaxKeyCode() {
1265 return LAST_KEYCODE;
1266 }
1267
1268 /**
1269 * Get the character that is produced by putting accent on the character
1270 * c.
1271 * For example, getDeadChar('`', 'e') returns &egrave;.
1272 */
1273 public static int getDeadChar(int accent, int c) {
1274 return KeyCharacterMap.getDeadChar(accent, c);
1275 }
1276
Dianne Hackborn8d374262009-09-14 21:21:52 -07001277 static final boolean DEBUG = false;
1278 static final String TAG = "KeyEvent";
Jeff Brown1f245102010-11-18 20:53:46 -08001279
1280 private static final int MAX_RECYCLED = 10;
1281 private static final Object gRecyclerLock = new Object();
1282 private static int gRecyclerUsed;
1283 private static KeyEvent gRecyclerTop;
1284
1285 private KeyEvent mNext;
Jeff Brown1f245102010-11-18 20:53:46 -08001286
Jeff Brown91c69ab2011-02-14 17:03:18 -08001287 private int mDeviceId;
1288 private int mSource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 private int mMetaState;
1290 private int mAction;
1291 private int mKeyCode;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001292 private int mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 private int mRepeatCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 private int mFlags;
1295 private long mDownTime;
1296 private long mEventTime;
1297 private String mCharacters;
1298
1299 public interface Callback {
1300 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001301 * Called when a key down event has occurred. If you return true,
1302 * you can first call {@link KeyEvent#startTracking()
1303 * KeyEvent.startTracking()} to have the framework track the event
1304 * through its {@link #onKeyUp(int, KeyEvent)} and also call your
1305 * {@link #onKeyLongPress(int, KeyEvent)} if it occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 *
1307 * @param keyCode The value in event.getKeyCode().
1308 * @param event Description of the key event.
1309 *
1310 * @return If you handled the event, return true. If you want to allow
1311 * the event to be handled by the next receiver, return false.
1312 */
1313 boolean onKeyDown(int keyCode, KeyEvent event);
1314
1315 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001316 * Called when a long press has occurred. If you return true,
1317 * the final key up will have {@link KeyEvent#FLAG_CANCELED} and
1318 * {@link KeyEvent#FLAG_CANCELED_LONG_PRESS} set. Note that in
1319 * order to receive this callback, someone in the event change
1320 * <em>must</em> return true from {@link #onKeyDown} <em>and</em>
1321 * call {@link KeyEvent#startTracking()} on the event.
1322 *
1323 * @param keyCode The value in event.getKeyCode().
1324 * @param event Description of the key event.
1325 *
1326 * @return If you handled the event, return true. If you want to allow
1327 * the event to be handled by the next receiver, return false.
1328 */
1329 boolean onKeyLongPress(int keyCode, KeyEvent event);
1330
1331 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 * Called when a key up event has occurred.
1333 *
1334 * @param keyCode The value in event.getKeyCode().
1335 * @param event Description of the key event.
1336 *
1337 * @return If you handled the event, return true. If you want to allow
1338 * the event to be handled by the next receiver, return false.
1339 */
1340 boolean onKeyUp(int keyCode, KeyEvent event);
1341
1342 /**
1343 * Called when multiple down/up pairs of the same key have occurred
1344 * in a row.
1345 *
1346 * @param keyCode The value in event.getKeyCode().
1347 * @param count Number of pairs as returned by event.getRepeatCount().
1348 * @param event Description of the key event.
1349 *
1350 * @return If you handled the event, return true. If you want to allow
1351 * the event to be handled by the next receiver, return false.
1352 */
1353 boolean onKeyMultiple(int keyCode, int count, KeyEvent event);
1354 }
1355
Jeff Brown497a92c2010-09-12 17:55:08 -07001356 static {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001357 populateKeycodeSymbolicNames();
Jeff Brown497a92c2010-09-12 17:55:08 -07001358 }
1359
Jeff Brown1f245102010-11-18 20:53:46 -08001360 private KeyEvent() {
1361 }
1362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 /**
1364 * Create a new key event.
1365 *
1366 * @param action Action code: either {@link #ACTION_DOWN},
1367 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1368 * @param code The key code.
1369 */
1370 public KeyEvent(int action, int code) {
1371 mAction = action;
1372 mKeyCode = code;
1373 mRepeatCount = 0;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001374 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 }
1376
1377 /**
1378 * Create a new key event.
1379 *
1380 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1381 * at which this key code originally went down.
1382 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1383 * at which this event happened.
1384 * @param action Action code: either {@link #ACTION_DOWN},
1385 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1386 * @param code The key code.
1387 * @param repeat A repeat count for down events (> 0 if this is after the
1388 * initial down) or event count for multiple events.
1389 */
1390 public KeyEvent(long downTime, long eventTime, int action,
1391 int code, int repeat) {
1392 mDownTime = downTime;
1393 mEventTime = eventTime;
1394 mAction = action;
1395 mKeyCode = code;
1396 mRepeatCount = repeat;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001397 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 }
1399
1400 /**
1401 * Create a new key event.
1402 *
1403 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1404 * at which this key code originally went down.
1405 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1406 * at which this event happened.
1407 * @param action Action code: either {@link #ACTION_DOWN},
1408 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1409 * @param code The key code.
1410 * @param repeat A repeat count for down events (> 0 if this is after the
1411 * initial down) or event count for multiple events.
1412 * @param metaState Flags indicating which meta keys are currently pressed.
1413 */
1414 public KeyEvent(long downTime, long eventTime, int action,
1415 int code, int repeat, int metaState) {
1416 mDownTime = downTime;
1417 mEventTime = eventTime;
1418 mAction = action;
1419 mKeyCode = code;
1420 mRepeatCount = repeat;
1421 mMetaState = metaState;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001422 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 }
1424
1425 /**
1426 * Create a new key event.
1427 *
1428 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1429 * at which this key code originally went down.
1430 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1431 * at which this event happened.
1432 * @param action Action code: either {@link #ACTION_DOWN},
1433 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1434 * @param code The key code.
1435 * @param repeat A repeat count for down events (> 0 if this is after the
1436 * initial down) or event count for multiple events.
1437 * @param metaState Flags indicating which meta keys are currently pressed.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001438 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 * @param scancode Raw device scan code of the event.
1440 */
1441 public KeyEvent(long downTime, long eventTime, int action,
1442 int code, int repeat, int metaState,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001443 int deviceId, int scancode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 mDownTime = downTime;
1445 mEventTime = eventTime;
1446 mAction = action;
1447 mKeyCode = code;
1448 mRepeatCount = repeat;
1449 mMetaState = metaState;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001450 mDeviceId = deviceId;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001451 mScanCode = scancode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 }
1453
1454 /**
1455 * Create a new key event.
1456 *
1457 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1458 * at which this key code originally went down.
1459 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1460 * at which this event happened.
1461 * @param action Action code: either {@link #ACTION_DOWN},
1462 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1463 * @param code The key code.
1464 * @param repeat A repeat count for down events (> 0 if this is after the
1465 * initial down) or event count for multiple events.
1466 * @param metaState Flags indicating which meta keys are currently pressed.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001467 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 * @param scancode Raw device scan code of the event.
1469 * @param flags The flags for this key event
1470 */
1471 public KeyEvent(long downTime, long eventTime, int action,
1472 int code, int repeat, int metaState,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001473 int deviceId, int scancode, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 mDownTime = downTime;
1475 mEventTime = eventTime;
1476 mAction = action;
1477 mKeyCode = code;
1478 mRepeatCount = repeat;
1479 mMetaState = metaState;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001480 mDeviceId = deviceId;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001481 mScanCode = scancode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 mFlags = flags;
1483 }
1484
1485 /**
Jeff Brownc5ed5912010-07-14 18:48:53 -07001486 * Create a new key event.
1487 *
1488 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1489 * at which this key code originally went down.
1490 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1491 * at which this event happened.
1492 * @param action Action code: either {@link #ACTION_DOWN},
1493 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1494 * @param code The key code.
1495 * @param repeat A repeat count for down events (> 0 if this is after the
1496 * initial down) or event count for multiple events.
1497 * @param metaState Flags indicating which meta keys are currently pressed.
1498 * @param deviceId The device ID that generated the key event.
1499 * @param scancode Raw device scan code of the event.
1500 * @param flags The flags for this key event
1501 * @param source The input source such as {@link InputDevice#SOURCE_KEYBOARD}.
1502 */
1503 public KeyEvent(long downTime, long eventTime, int action,
1504 int code, int repeat, int metaState,
1505 int deviceId, int scancode, int flags, int source) {
1506 mDownTime = downTime;
1507 mEventTime = eventTime;
1508 mAction = action;
1509 mKeyCode = code;
1510 mRepeatCount = repeat;
1511 mMetaState = metaState;
1512 mDeviceId = deviceId;
1513 mScanCode = scancode;
1514 mFlags = flags;
1515 mSource = source;
1516 }
1517
1518 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 * Create a new key event for a string of characters. The key code,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001520 * action, repeat count and source will automatically be set to
1521 * {@link #KEYCODE_UNKNOWN}, {@link #ACTION_MULTIPLE}, 0, and
1522 * {@link InputDevice#SOURCE_KEYBOARD} for you.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 *
1524 * @param time The time (in {@link android.os.SystemClock#uptimeMillis})
1525 * at which this event occured.
1526 * @param characters The string of characters.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001527 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 * @param flags The flags for this key event
1529 */
Jeff Brownc5ed5912010-07-14 18:48:53 -07001530 public KeyEvent(long time, String characters, int deviceId, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 mDownTime = time;
1532 mEventTime = time;
1533 mCharacters = characters;
1534 mAction = ACTION_MULTIPLE;
1535 mKeyCode = KEYCODE_UNKNOWN;
1536 mRepeatCount = 0;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001537 mDeviceId = deviceId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 mFlags = flags;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001539 mSource = InputDevice.SOURCE_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 }
1541
1542 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001543 * Make an exact copy of an existing key event.
1544 */
1545 public KeyEvent(KeyEvent origEvent) {
1546 mDownTime = origEvent.mDownTime;
1547 mEventTime = origEvent.mEventTime;
1548 mAction = origEvent.mAction;
1549 mKeyCode = origEvent.mKeyCode;
1550 mRepeatCount = origEvent.mRepeatCount;
1551 mMetaState = origEvent.mMetaState;
1552 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001553 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001554 mScanCode = origEvent.mScanCode;
The Android Open Source Project10592532009-03-18 17:39:46 -07001555 mFlags = origEvent.mFlags;
1556 mCharacters = origEvent.mCharacters;
1557 }
1558
1559 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 * Copy an existing key event, modifying its time and repeat count.
1561 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001562 * @deprecated Use {@link #changeTimeRepeat(KeyEvent, long, int)}
1563 * instead.
1564 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 * @param origEvent The existing event to be copied.
1566 * @param eventTime The new event time
1567 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1568 * @param newRepeat The new repeat count of the event.
1569 */
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001570 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 public KeyEvent(KeyEvent origEvent, long eventTime, int newRepeat) {
1572 mDownTime = origEvent.mDownTime;
1573 mEventTime = eventTime;
1574 mAction = origEvent.mAction;
1575 mKeyCode = origEvent.mKeyCode;
1576 mRepeatCount = newRepeat;
1577 mMetaState = origEvent.mMetaState;
1578 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001579 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001580 mScanCode = origEvent.mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 mFlags = origEvent.mFlags;
1582 mCharacters = origEvent.mCharacters;
1583 }
1584
Jeff Brown1f245102010-11-18 20:53:46 -08001585 private static KeyEvent obtain() {
1586 final KeyEvent ev;
1587 synchronized (gRecyclerLock) {
1588 ev = gRecyclerTop;
1589 if (ev == null) {
1590 return new KeyEvent();
1591 }
1592 gRecyclerTop = ev.mNext;
1593 gRecyclerUsed -= 1;
1594 }
Jeff Brown1f245102010-11-18 20:53:46 -08001595 ev.mNext = null;
Jeff Brown32cbc38552011-12-01 14:01:49 -08001596 ev.prepareForReuse();
Jeff Brown1f245102010-11-18 20:53:46 -08001597 return ev;
1598 }
1599
1600 /**
1601 * Obtains a (potentially recycled) key event.
1602 *
1603 * @hide
1604 */
1605 public static KeyEvent obtain(long downTime, long eventTime, int action,
1606 int code, int repeat, int metaState,
1607 int deviceId, int scancode, int flags, int source, String characters) {
1608 KeyEvent ev = obtain();
1609 ev.mDownTime = downTime;
1610 ev.mEventTime = eventTime;
1611 ev.mAction = action;
1612 ev.mKeyCode = code;
1613 ev.mRepeatCount = repeat;
1614 ev.mMetaState = metaState;
1615 ev.mDeviceId = deviceId;
1616 ev.mScanCode = scancode;
1617 ev.mFlags = flags;
1618 ev.mSource = source;
1619 ev.mCharacters = characters;
1620 return ev;
1621 }
1622
1623 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08001624 * Obtains a (potentially recycled) copy of another key event.
1625 *
1626 * @hide
1627 */
1628 public static KeyEvent obtain(KeyEvent other) {
1629 KeyEvent ev = obtain();
1630 ev.mDownTime = other.mDownTime;
1631 ev.mEventTime = other.mEventTime;
1632 ev.mAction = other.mAction;
1633 ev.mKeyCode = other.mKeyCode;
1634 ev.mRepeatCount = other.mRepeatCount;
1635 ev.mMetaState = other.mMetaState;
1636 ev.mDeviceId = other.mDeviceId;
1637 ev.mScanCode = other.mScanCode;
1638 ev.mFlags = other.mFlags;
1639 ev.mSource = other.mSource;
1640 ev.mCharacters = other.mCharacters;
1641 return ev;
1642 }
1643
1644 /** @hide */
1645 @Override
1646 public KeyEvent copy() {
1647 return obtain(this);
1648 }
1649
1650 /**
Jeff Brown1f245102010-11-18 20:53:46 -08001651 * Recycles a key event.
1652 * Key events should only be recycled if they are owned by the system since user
1653 * code expects them to be essentially immutable, "tracking" notwithstanding.
1654 *
1655 * @hide
1656 */
Jeff Brown92cc2d82011-12-02 01:19:47 -08001657 @Override
Jeff Brown1f245102010-11-18 20:53:46 -08001658 public final void recycle() {
Jeff Brown32cbc38552011-12-01 14:01:49 -08001659 super.recycle();
Jeff Brown1f245102010-11-18 20:53:46 -08001660 mCharacters = null;
1661
1662 synchronized (gRecyclerLock) {
1663 if (gRecyclerUsed < MAX_RECYCLED) {
1664 gRecyclerUsed++;
1665 mNext = gRecyclerTop;
1666 gRecyclerTop = this;
1667 }
1668 }
1669 }
1670
Jeff Brown92cc2d82011-12-02 01:19:47 -08001671 /** @hide */
1672 @Override
1673 public final void recycleIfNeededAfterDispatch() {
1674 // Do nothing.
1675 }
1676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001678 * Create a new key event that is the same as the given one, but whose
1679 * event time and repeat count are replaced with the given value.
1680 *
1681 * @param event The existing event to be copied. This is not modified.
1682 * @param eventTime The new event time
1683 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1684 * @param newRepeat The new repeat count of the event.
1685 */
1686 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime,
1687 int newRepeat) {
1688 return new KeyEvent(event, eventTime, newRepeat);
1689 }
1690
1691 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001692 * Create a new key event that is the same as the given one, but whose
1693 * event time and repeat count are replaced with the given value.
1694 *
1695 * @param event The existing event to be copied. This is not modified.
1696 * @param eventTime The new event time
1697 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1698 * @param newRepeat The new repeat count of the event.
1699 * @param newFlags New flags for the event, replacing the entire value
1700 * in the original event.
1701 */
1702 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime,
1703 int newRepeat, int newFlags) {
1704 KeyEvent ret = new KeyEvent(event);
1705 ret.mEventTime = eventTime;
1706 ret.mRepeatCount = newRepeat;
1707 ret.mFlags = newFlags;
1708 return ret;
1709 }
1710
1711 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 * Copy an existing key event, modifying its action.
1713 *
1714 * @param origEvent The existing event to be copied.
1715 * @param action The new action code of the event.
1716 */
The Android Open Source Project10592532009-03-18 17:39:46 -07001717 private KeyEvent(KeyEvent origEvent, int action) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 mDownTime = origEvent.mDownTime;
1719 mEventTime = origEvent.mEventTime;
1720 mAction = action;
1721 mKeyCode = origEvent.mKeyCode;
1722 mRepeatCount = origEvent.mRepeatCount;
1723 mMetaState = origEvent.mMetaState;
1724 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001725 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001726 mScanCode = origEvent.mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727 mFlags = origEvent.mFlags;
1728 // Don't copy mCharacters, since one way or the other we'll lose it
1729 // when changing the action.
1730 }
1731
1732 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001733 * Create a new key event that is the same as the given one, but whose
1734 * action is replaced with the given value.
1735 *
1736 * @param event The existing event to be copied. This is not modified.
1737 * @param action The new action code of the event.
1738 */
1739 public static KeyEvent changeAction(KeyEvent event, int action) {
1740 return new KeyEvent(event, action);
1741 }
1742
1743 /**
1744 * Create a new key event that is the same as the given one, but whose
1745 * flags are replaced with the given value.
1746 *
1747 * @param event The existing event to be copied. This is not modified.
1748 * @param flags The new flags constant.
1749 */
1750 public static KeyEvent changeFlags(KeyEvent event, int flags) {
1751 event = new KeyEvent(event);
1752 event.mFlags = flags;
1753 return event;
1754 }
Jeff Brown21bc5c92011-02-28 18:27:14 -08001755
1756 /** @hide */
1757 @Override
1758 public final boolean isTainted() {
1759 return (mFlags & FLAG_TAINTED) != 0;
1760 }
1761
1762 /** @hide */
1763 @Override
1764 public final void setTainted(boolean tainted) {
1765 mFlags = tainted ? mFlags | FLAG_TAINTED : mFlags & ~FLAG_TAINTED;
1766 }
1767
The Android Open Source Project10592532009-03-18 17:39:46 -07001768 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 * Don't use in new code, instead explicitly check
1770 * {@link #getAction()}.
1771 *
1772 * @return If the action is ACTION_DOWN, returns true; else false.
1773 *
1774 * @deprecated
1775 * @hide
1776 */
1777 @Deprecated public final boolean isDown() {
1778 return mAction == ACTION_DOWN;
1779 }
1780
1781 /**
1782 * Is this a system key? System keys can not be used for menu shortcuts.
1783 *
1784 * TODO: this information should come from a table somewhere.
1785 * TODO: should the dpad keys be here? arguably, because they also shouldn't be menu shortcuts
1786 */
1787 public final boolean isSystem() {
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -07001788 return native_isSystemKey(mKeyCode);
1789 }
1790
1791 /** @hide */
1792 public final boolean hasDefaultAction() {
1793 return native_hasDefaultAction(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 }
1795
Jeff Brown6f2fba42011-02-19 01:08:02 -08001796 /**
1797 * Returns true if the specified keycode is a gamepad button.
1798 * @return True if the keycode is a gamepad button, such as {@link #KEYCODE_BUTTON_A}.
1799 */
1800 public static final boolean isGamepadButton(int keyCode) {
1801 switch (keyCode) {
1802 case KeyEvent.KEYCODE_BUTTON_A:
1803 case KeyEvent.KEYCODE_BUTTON_B:
1804 case KeyEvent.KEYCODE_BUTTON_C:
1805 case KeyEvent.KEYCODE_BUTTON_X:
1806 case KeyEvent.KEYCODE_BUTTON_Y:
1807 case KeyEvent.KEYCODE_BUTTON_Z:
1808 case KeyEvent.KEYCODE_BUTTON_L1:
1809 case KeyEvent.KEYCODE_BUTTON_R1:
1810 case KeyEvent.KEYCODE_BUTTON_L2:
1811 case KeyEvent.KEYCODE_BUTTON_R2:
1812 case KeyEvent.KEYCODE_BUTTON_THUMBL:
1813 case KeyEvent.KEYCODE_BUTTON_THUMBR:
1814 case KeyEvent.KEYCODE_BUTTON_START:
1815 case KeyEvent.KEYCODE_BUTTON_SELECT:
1816 case KeyEvent.KEYCODE_BUTTON_MODE:
1817 case KeyEvent.KEYCODE_BUTTON_1:
1818 case KeyEvent.KEYCODE_BUTTON_2:
1819 case KeyEvent.KEYCODE_BUTTON_3:
1820 case KeyEvent.KEYCODE_BUTTON_4:
1821 case KeyEvent.KEYCODE_BUTTON_5:
1822 case KeyEvent.KEYCODE_BUTTON_6:
1823 case KeyEvent.KEYCODE_BUTTON_7:
1824 case KeyEvent.KEYCODE_BUTTON_8:
1825 case KeyEvent.KEYCODE_BUTTON_9:
1826 case KeyEvent.KEYCODE_BUTTON_10:
1827 case KeyEvent.KEYCODE_BUTTON_11:
1828 case KeyEvent.KEYCODE_BUTTON_12:
1829 case KeyEvent.KEYCODE_BUTTON_13:
1830 case KeyEvent.KEYCODE_BUTTON_14:
1831 case KeyEvent.KEYCODE_BUTTON_15:
1832 case KeyEvent.KEYCODE_BUTTON_16:
1833 return true;
1834 default:
1835 return false;
1836 }
1837 }
1838
Michael Wright25b0c302013-07-10 12:54:06 -07001839 /** Whether key will, by default, trigger a click on the focused view.
1840 * @hide
1841 */
1842 public static final boolean isConfirmKey(int keyCode) {
1843 switch (keyCode) {
1844 case KeyEvent.KEYCODE_DPAD_CENTER:
1845 case KeyEvent.KEYCODE_ENTER:
1846 return true;
1847 default:
1848 return false;
1849 }
1850 }
1851
Jeff Brown91c69ab2011-02-14 17:03:18 -08001852 /** {@inheritDoc} */
1853 @Override
1854 public final int getDeviceId() {
1855 return mDeviceId;
1856 }
1857
1858 /** {@inheritDoc} */
1859 @Override
1860 public final int getSource() {
1861 return mSource;
1862 }
1863
1864 /** {@inheritDoc} */
1865 @Override
1866 public final void setSource(int source) {
1867 mSource = source;
1868 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869
1870 /**
1871 * <p>Returns the state of the meta keys.</p>
1872 *
1873 * @return an integer in which each bit set to 1 represents a pressed
1874 * meta key
1875 *
1876 * @see #isAltPressed()
1877 * @see #isShiftPressed()
1878 * @see #isSymPressed()
Jeff Brown497a92c2010-09-12 17:55:08 -07001879 * @see #isCtrlPressed()
1880 * @see #isMetaPressed()
1881 * @see #isFunctionPressed()
Jeff Brown51e7fe72010-10-29 22:19:53 -07001882 * @see #isCapsLockOn()
1883 * @see #isNumLockOn()
1884 * @see #isScrollLockOn()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 * @see #META_ALT_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001886 * @see #META_ALT_LEFT_ON
1887 * @see #META_ALT_RIGHT_ON
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 * @see #META_SHIFT_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001889 * @see #META_SHIFT_LEFT_ON
1890 * @see #META_SHIFT_RIGHT_ON
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 * @see #META_SYM_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001892 * @see #META_FUNCTION_ON
1893 * @see #META_CTRL_ON
1894 * @see #META_CTRL_LEFT_ON
1895 * @see #META_CTRL_RIGHT_ON
1896 * @see #META_META_ON
1897 * @see #META_META_LEFT_ON
1898 * @see #META_META_RIGHT_ON
Jeff Brown51e7fe72010-10-29 22:19:53 -07001899 * @see #META_CAPS_LOCK_ON
1900 * @see #META_NUM_LOCK_ON
1901 * @see #META_SCROLL_LOCK_ON
Jeff Brown54875002011-04-06 15:33:01 -07001902 * @see #getModifiers
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001903 */
1904 public final int getMetaState() {
1905 return mMetaState;
1906 }
1907
1908 /**
Jeff Brown54875002011-04-06 15:33:01 -07001909 * Returns the state of the modifier keys.
1910 * <p>
1911 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1912 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1913 * not considered modifier keys. Consequently, this function specifically masks out
1914 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1915 * </p><p>
1916 * The value returned consists of the meta state (from {@link #getMetaState})
1917 * normalized using {@link #normalizeMetaState(int)} and then masked with
1918 * {@link #getModifierMetaStateMask} so that only valid modifier bits are retained.
1919 * </p>
1920 *
1921 * @return An integer in which each bit set to 1 represents a pressed modifier key.
1922 * @see #getMetaState
1923 */
1924 public final int getModifiers() {
1925 return normalizeMetaState(mMetaState) & META_MODIFIER_MASK;
1926 }
1927
1928 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929 * Returns the flags for this key event.
1930 *
1931 * @see #FLAG_WOKE_HERE
1932 */
1933 public final int getFlags() {
1934 return mFlags;
1935 }
1936
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001937 // Mask of all modifier key meta states. Specifically excludes locked keys like caps lock.
1938 private static final int META_MODIFIER_MASK =
1939 META_SHIFT_ON | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON
1940 | META_ALT_ON | META_ALT_LEFT_ON | META_ALT_RIGHT_ON
1941 | META_CTRL_ON | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON
1942 | META_META_ON | META_META_LEFT_ON | META_META_RIGHT_ON
1943 | META_SYM_ON | META_FUNCTION_ON;
1944
1945 // Mask of all lock key meta states.
1946 private static final int META_LOCK_MASK =
1947 META_CAPS_LOCK_ON | META_NUM_LOCK_ON | META_SCROLL_LOCK_ON;
1948
1949 // Mask of all valid meta states.
1950 private static final int META_ALL_MASK = META_MODIFIER_MASK | META_LOCK_MASK;
1951
1952 // Mask of all synthetic meta states that are reserved for API compatibility with
1953 // historical uses in MetaKeyKeyListener.
1954 private static final int META_SYNTHETIC_MASK =
1955 META_CAP_LOCKED | META_ALT_LOCKED | META_SYM_LOCKED | META_SELECTING;
1956
1957 // Mask of all meta states that are not valid use in specifying a modifier key.
1958 // These bits are known to be used for purposes other than specifying modifiers.
1959 private static final int META_INVALID_MODIFIER_MASK =
1960 META_LOCK_MASK | META_SYNTHETIC_MASK;
1961
1962 /**
1963 * Gets a mask that includes all valid modifier key meta state bits.
1964 * <p>
1965 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1966 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1967 * not considered modifier keys. Consequently, the mask specifically excludes
1968 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1969 * </p>
1970 *
1971 * @return The modifier meta state mask which is a combination of
1972 * {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON}, {@link #META_SHIFT_RIGHT_ON},
1973 * {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON}, {@link #META_ALT_RIGHT_ON},
1974 * {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON}, {@link #META_CTRL_RIGHT_ON},
1975 * {@link #META_META_ON}, {@link #META_META_LEFT_ON}, {@link #META_META_RIGHT_ON},
1976 * {@link #META_SYM_ON}, {@link #META_FUNCTION_ON}.
1977 */
1978 public static int getModifierMetaStateMask() {
1979 return META_MODIFIER_MASK;
1980 }
1981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 /**
1983 * Returns true if this key code is a modifier key.
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001984 * <p>
1985 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1986 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1987 * not considered modifier keys. Consequently, this function return false
1988 * for those keys.
1989 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 *
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001991 * @return True if the key code is one of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 * {@link #KEYCODE_SHIFT_LEFT} {@link #KEYCODE_SHIFT_RIGHT},
Jeff Brown497a92c2010-09-12 17:55:08 -07001993 * {@link #KEYCODE_ALT_LEFT}, {@link #KEYCODE_ALT_RIGHT},
Jeff Brown497a92c2010-09-12 17:55:08 -07001994 * {@link #KEYCODE_CTRL_LEFT}, {@link #KEYCODE_CTRL_RIGHT},
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001995 * {@link #KEYCODE_META_LEFT}, or {@link #KEYCODE_META_RIGHT},
1996 * {@link #KEYCODE_SYM}, {@link #KEYCODE_NUM}, {@link #KEYCODE_FUNCTION}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 */
1998 public static boolean isModifierKey(int keyCode) {
Jeff Brown497a92c2010-09-12 17:55:08 -07001999 switch (keyCode) {
2000 case KEYCODE_SHIFT_LEFT:
2001 case KEYCODE_SHIFT_RIGHT:
2002 case KEYCODE_ALT_LEFT:
2003 case KEYCODE_ALT_RIGHT:
Jeff Brown497a92c2010-09-12 17:55:08 -07002004 case KEYCODE_CTRL_LEFT:
2005 case KEYCODE_CTRL_RIGHT:
2006 case KEYCODE_META_LEFT:
2007 case KEYCODE_META_RIGHT:
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002008 case KEYCODE_SYM:
2009 case KEYCODE_NUM:
2010 case KEYCODE_FUNCTION:
Jeff Brown497a92c2010-09-12 17:55:08 -07002011 return true;
2012 default:
2013 return false;
2014 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 }
2016
2017 /**
Jeff Brown28cbf4b2010-12-13 10:33:20 -08002018 * Normalizes the specified meta state.
2019 * <p>
2020 * The meta state is normalized such that if either the left or right modifier meta state
2021 * bits are set then the result will also include the universal bit for that modifier.
2022 * </p><p>
2023 * If the specified meta state contains {@link #META_ALT_LEFT_ON} then
2024 * the result will also contain {@link #META_ALT_ON} in addition to {@link #META_ALT_LEFT_ON}
2025 * and the other bits that were specified in the input. The same is process is
2026 * performed for shift, control and meta.
2027 * </p><p>
2028 * If the specified meta state contains synthetic meta states defined by
2029 * {@link MetaKeyKeyListener}, then those states are translated here and the original
2030 * synthetic meta states are removed from the result.
2031 * {@link MetaKeyKeyListener#META_CAP_LOCKED} is translated to {@link #META_CAPS_LOCK_ON}.
2032 * {@link MetaKeyKeyListener#META_ALT_LOCKED} is translated to {@link #META_ALT_ON}.
2033 * {@link MetaKeyKeyListener#META_SYM_LOCKED} is translated to {@link #META_SYM_ON}.
2034 * </p><p>
2035 * Undefined meta state bits are removed.
2036 * </p>
2037 *
2038 * @param metaState The meta state.
2039 * @return The normalized meta state.
2040 */
2041 public static int normalizeMetaState(int metaState) {
2042 if ((metaState & (META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON)) != 0) {
2043 metaState |= META_SHIFT_ON;
2044 }
2045 if ((metaState & (META_ALT_LEFT_ON | META_ALT_RIGHT_ON)) != 0) {
2046 metaState |= META_ALT_ON;
2047 }
2048 if ((metaState & (META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON)) != 0) {
2049 metaState |= META_CTRL_ON;
2050 }
2051 if ((metaState & (META_META_LEFT_ON | META_META_RIGHT_ON)) != 0) {
2052 metaState |= META_META_ON;
2053 }
2054 if ((metaState & MetaKeyKeyListener.META_CAP_LOCKED) != 0) {
2055 metaState |= META_CAPS_LOCK_ON;
2056 }
2057 if ((metaState & MetaKeyKeyListener.META_ALT_LOCKED) != 0) {
2058 metaState |= META_ALT_ON;
2059 }
2060 if ((metaState & MetaKeyKeyListener.META_SYM_LOCKED) != 0) {
2061 metaState |= META_SYM_ON;
2062 }
2063 return metaState & META_ALL_MASK;
2064 }
2065
2066 /**
2067 * Returns true if no modifiers keys are pressed according to the specified meta state.
2068 * <p>
2069 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2070 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2071 * not considered modifier keys. Consequently, this function ignores
2072 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2073 * </p><p>
2074 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}.
2075 * </p>
2076 *
2077 * @param metaState The meta state to consider.
2078 * @return True if no modifier keys are pressed.
2079 * @see #hasNoModifiers()
2080 */
2081 public static boolean metaStateHasNoModifiers(int metaState) {
2082 return (normalizeMetaState(metaState) & META_MODIFIER_MASK) == 0;
2083 }
2084
2085 /**
2086 * Returns true if only the specified modifier keys are pressed according to
2087 * the specified meta state. Returns false if a different combination of modifier
2088 * keys are pressed.
2089 * <p>
2090 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2091 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2092 * not considered modifier keys. Consequently, this function ignores
2093 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2094 * </p><p>
2095 * If the specified modifier mask includes directional modifiers, such as
2096 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the
2097 * modifier is pressed on that side.
2098 * If the specified modifier mask includes non-directional modifiers, such as
2099 * {@link #META_SHIFT_ON}, then this method ensures that the modifier
2100 * is pressed on either side.
2101 * If the specified modifier mask includes both directional and non-directional modifiers
2102 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON},
2103 * then this method throws an illegal argument exception.
2104 * </p>
2105 *
2106 * @param metaState The meta state to consider.
2107 * @param modifiers The meta state of the modifier keys to check. May be a combination
2108 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to
2109 * ensure that no modifier keys are pressed.
2110 * @return True if only the specified modifier keys are pressed.
2111 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers
2112 * @see #hasModifiers
2113 */
2114 public static boolean metaStateHasModifiers(int metaState, int modifiers) {
2115 // Note: For forward compatibility, we allow the parameter to contain meta states
2116 // that we do not recognize but we explicitly disallow meta states that
2117 // are not valid modifiers.
2118 if ((modifiers & META_INVALID_MODIFIER_MASK) != 0) {
2119 throw new IllegalArgumentException("modifiers must not contain "
2120 + "META_CAPS_LOCK_ON, META_NUM_LOCK_ON, META_SCROLL_LOCK_ON, "
2121 + "META_CAP_LOCKED, META_ALT_LOCKED, META_SYM_LOCKED, "
2122 + "or META_SELECTING");
2123 }
2124
2125 metaState = normalizeMetaState(metaState) & META_MODIFIER_MASK;
2126 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2127 META_SHIFT_ON, META_SHIFT_LEFT_ON, META_SHIFT_RIGHT_ON);
2128 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2129 META_ALT_ON, META_ALT_LEFT_ON, META_ALT_RIGHT_ON);
2130 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2131 META_CTRL_ON, META_CTRL_LEFT_ON, META_CTRL_RIGHT_ON);
2132 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2133 META_META_ON, META_META_LEFT_ON, META_META_RIGHT_ON);
2134 return metaState == modifiers;
2135 }
2136
2137 private static int metaStateFilterDirectionalModifiers(int metaState,
2138 int modifiers, int basic, int left, int right) {
2139 final boolean wantBasic = (modifiers & basic) != 0;
2140 final int directional = left | right;
2141 final boolean wantLeftOrRight = (modifiers & directional) != 0;
2142
2143 if (wantBasic) {
2144 if (wantLeftOrRight) {
2145 throw new IllegalArgumentException("modifiers must not contain "
2146 + metaStateToString(basic) + " combined with "
2147 + metaStateToString(left) + " or " + metaStateToString(right));
2148 }
2149 return metaState & ~directional;
2150 } else if (wantLeftOrRight) {
2151 return metaState & ~basic;
2152 } else {
2153 return metaState;
2154 }
2155 }
2156
2157 /**
2158 * Returns true if no modifier keys are pressed.
2159 * <p>
2160 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2161 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2162 * not considered modifier keys. Consequently, this function ignores
2163 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2164 * </p><p>
2165 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}.
2166 * </p>
2167 *
2168 * @return True if no modifier keys are pressed.
2169 * @see #metaStateHasNoModifiers
2170 */
2171 public final boolean hasNoModifiers() {
2172 return metaStateHasNoModifiers(mMetaState);
2173 }
2174
2175 /**
2176 * Returns true if only the specified modifiers keys are pressed.
2177 * Returns false if a different combination of modifier keys are pressed.
2178 * <p>
2179 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2180 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2181 * not considered modifier keys. Consequently, this function ignores
2182 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2183 * </p><p>
2184 * If the specified modifier mask includes directional modifiers, such as
2185 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the
2186 * modifier is pressed on that side.
2187 * If the specified modifier mask includes non-directional modifiers, such as
2188 * {@link #META_SHIFT_ON}, then this method ensures that the modifier
2189 * is pressed on either side.
2190 * If the specified modifier mask includes both directional and non-directional modifiers
2191 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON},
2192 * then this method throws an illegal argument exception.
2193 * </p>
2194 *
2195 * @param modifiers The meta state of the modifier keys to check. May be a combination
2196 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to
2197 * ensure that no modifier keys are pressed.
2198 * @return True if only the specified modifier keys are pressed.
2199 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers
2200 * @see #metaStateHasModifiers
2201 */
2202 public final boolean hasModifiers(int modifiers) {
2203 return metaStateHasModifiers(mMetaState, modifiers);
2204 }
2205
2206 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 * <p>Returns the pressed state of the ALT meta key.</p>
2208 *
2209 * @return true if the ALT key is pressed, false otherwise
2210 *
2211 * @see #KEYCODE_ALT_LEFT
2212 * @see #KEYCODE_ALT_RIGHT
2213 * @see #META_ALT_ON
2214 */
2215 public final boolean isAltPressed() {
2216 return (mMetaState & META_ALT_ON) != 0;
2217 }
2218
2219 /**
2220 * <p>Returns the pressed state of the SHIFT meta key.</p>
2221 *
2222 * @return true if the SHIFT key is pressed, false otherwise
2223 *
2224 * @see #KEYCODE_SHIFT_LEFT
2225 * @see #KEYCODE_SHIFT_RIGHT
2226 * @see #META_SHIFT_ON
2227 */
2228 public final boolean isShiftPressed() {
2229 return (mMetaState & META_SHIFT_ON) != 0;
2230 }
2231
2232 /**
2233 * <p>Returns the pressed state of the SYM meta key.</p>
2234 *
2235 * @return true if the SYM key is pressed, false otherwise
2236 *
2237 * @see #KEYCODE_SYM
2238 * @see #META_SYM_ON
2239 */
2240 public final boolean isSymPressed() {
2241 return (mMetaState & META_SYM_ON) != 0;
2242 }
2243
2244 /**
Jeff Brown497a92c2010-09-12 17:55:08 -07002245 * <p>Returns the pressed state of the CTRL meta key.</p>
2246 *
2247 * @return true if the CTRL key is pressed, false otherwise
2248 *
2249 * @see #KEYCODE_CTRL_LEFT
2250 * @see #KEYCODE_CTRL_RIGHT
2251 * @see #META_CTRL_ON
2252 */
2253 public final boolean isCtrlPressed() {
2254 return (mMetaState & META_CTRL_ON) != 0;
2255 }
2256
2257 /**
2258 * <p>Returns the pressed state of the META meta key.</p>
2259 *
2260 * @return true if the META key is pressed, false otherwise
2261 *
2262 * @see #KEYCODE_META_LEFT
2263 * @see #KEYCODE_META_RIGHT
2264 * @see #META_META_ON
2265 */
2266 public final boolean isMetaPressed() {
2267 return (mMetaState & META_META_ON) != 0;
2268 }
2269
2270 /**
2271 * <p>Returns the pressed state of the FUNCTION meta key.</p>
2272 *
2273 * @return true if the FUNCTION key is pressed, false otherwise
2274 *
2275 * @see #KEYCODE_FUNCTION
2276 * @see #META_FUNCTION_ON
2277 */
2278 public final boolean isFunctionPressed() {
2279 return (mMetaState & META_FUNCTION_ON) != 0;
2280 }
2281
2282 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07002283 * <p>Returns the locked state of the CAPS LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002284 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002285 * @return true if the CAPS LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002286 *
2287 * @see #KEYCODE_CAPS_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002288 * @see #META_CAPS_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002289 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002290 public final boolean isCapsLockOn() {
2291 return (mMetaState & META_CAPS_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002292 }
2293
2294 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07002295 * <p>Returns the locked state of the NUM LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002296 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002297 * @return true if the NUM LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002298 *
2299 * @see #KEYCODE_NUM_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002300 * @see #META_NUM_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002301 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002302 public final boolean isNumLockOn() {
2303 return (mMetaState & META_NUM_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002304 }
2305
2306 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07002307 * <p>Returns the locked state of the SCROLL LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002308 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002309 * @return true if the SCROLL LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002310 *
2311 * @see #KEYCODE_SCROLL_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002312 * @see #META_SCROLL_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002313 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002314 public final boolean isScrollLockOn() {
2315 return (mMetaState & META_SCROLL_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002316 }
2317
2318 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319 * Retrieve the action of this key event. May be either
2320 * {@link #ACTION_DOWN}, {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
2321 *
2322 * @return The event action: ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.
2323 */
2324 public final int getAction() {
2325 return mAction;
2326 }
2327
2328 /**
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07002329 * For {@link #ACTION_UP} events, indicates that the event has been
2330 * canceled as per {@link #FLAG_CANCELED}.
2331 */
2332 public final boolean isCanceled() {
2333 return (mFlags&FLAG_CANCELED) != 0;
2334 }
2335
2336 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002337 * Call this during {@link Callback#onKeyDown} to have the system track
2338 * the key through its final up (possibly including a long press). Note
2339 * that only one key can be tracked at a time -- if another key down
2340 * event is received while a previous one is being tracked, tracking is
2341 * stopped on the previous event.
2342 */
2343 public final void startTracking() {
2344 mFlags |= FLAG_START_TRACKING;
2345 }
2346
2347 /**
2348 * For {@link #ACTION_UP} events, indicates that the event is still being
2349 * tracked from its initial down event as per
2350 * {@link #FLAG_TRACKING}.
2351 */
2352 public final boolean isTracking() {
2353 return (mFlags&FLAG_TRACKING) != 0;
2354 }
2355
2356 /**
2357 * For {@link #ACTION_DOWN} events, indicates that the event has been
2358 * canceled as per {@link #FLAG_LONG_PRESS}.
2359 */
2360 public final boolean isLongPress() {
2361 return (mFlags&FLAG_LONG_PRESS) != 0;
2362 }
2363
2364 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365 * Retrieve the key code of the key event. This is the physical key that
2366 * was pressed, <em>not</em> the Unicode character.
2367 *
2368 * @return The key code of the event.
2369 */
2370 public final int getKeyCode() {
2371 return mKeyCode;
2372 }
2373
2374 /**
2375 * For the special case of a {@link #ACTION_MULTIPLE} event with key
2376 * code of {@link #KEYCODE_UNKNOWN}, this is a raw string of characters
2377 * associated with the event. In all other cases it is null.
2378 *
2379 * @return Returns a String of 1 or more characters associated with
2380 * the event.
2381 */
2382 public final String getCharacters() {
2383 return mCharacters;
2384 }
2385
2386 /**
2387 * Retrieve the hardware key id of this key event. These values are not
2388 * reliable and vary from device to device.
2389 *
2390 * {@more}
2391 * Mostly this is here for debugging purposes.
2392 */
2393 public final int getScanCode() {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002394 return mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 }
2396
2397 /**
2398 * Retrieve the repeat count of the event. For both key up and key down
2399 * events, this is the number of times the key has repeated with the first
2400 * down starting at 0 and counting up from there. For multiple key
2401 * events, this is the number of down/up pairs that have occurred.
2402 *
2403 * @return The number of times the key has repeated.
2404 */
2405 public final int getRepeatCount() {
2406 return mRepeatCount;
2407 }
2408
2409 /**
2410 * Retrieve the time of the most recent key down event,
2411 * in the {@link android.os.SystemClock#uptimeMillis} time base. If this
2412 * is a down event, this will be the same as {@link #getEventTime()}.
2413 * Note that when chording keys, this value is the down time of the
2414 * most recently pressed key, which may <em>not</em> be the same physical
2415 * key of this event.
2416 *
2417 * @return Returns the most recent key down time, in the
2418 * {@link android.os.SystemClock#uptimeMillis} time base
2419 */
2420 public final long getDownTime() {
2421 return mDownTime;
2422 }
2423
2424 /**
Jeff Brownb11499d2012-04-20 19:54:22 -07002425 * Retrieve the time this event occurred,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002426 * in the {@link android.os.SystemClock#uptimeMillis} time base.
Jeff Brownb11499d2012-04-20 19:54:22 -07002427 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002428 * @return Returns the time this event occurred,
2429 * in the {@link android.os.SystemClock#uptimeMillis} time base.
2430 */
Jeff Brownb11499d2012-04-20 19:54:22 -07002431 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432 public final long getEventTime() {
2433 return mEventTime;
2434 }
2435
Jeff Brownb11499d2012-04-20 19:54:22 -07002436 /**
2437 * Retrieve the time this event occurred,
2438 * in the {@link android.os.SystemClock#uptimeMillis} time base but with
2439 * nanosecond (instead of millisecond) precision.
2440 * <p>
2441 * The value is in nanosecond precision but it may not have nanosecond accuracy.
2442 * </p>
2443 *
2444 * @return Returns the time this event occurred,
2445 * in the {@link android.os.SystemClock#uptimeMillis} time base but with
2446 * nanosecond (instead of millisecond) precision.
2447 *
2448 * @hide
2449 */
Jeff Brown4e91a182011-04-07 11:38:09 -07002450 @Override
2451 public final long getEventTimeNano() {
2452 return mEventTime * 1000000L;
2453 }
2454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456 * Renamed to {@link #getDeviceId}.
2457 *
2458 * @hide
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002459 * @deprecated use {@link #getDeviceId()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002460 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002461 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002462 public final int getKeyboardDevice() {
2463 return mDeviceId;
2464 }
2465
2466 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002467 * Gets the {@link KeyCharacterMap} associated with the keyboard device.
2468 *
2469 * @return The associated key character map.
Andrew Sapperstein8ab3dc72011-06-23 18:56:44 -07002470 * @throws {@link KeyCharacterMap.UnavailableException} if the key character map
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002471 * could not be loaded because it was malformed or the default key character map
2472 * is missing from the system.
2473 *
Andrew Sapperstein8ab3dc72011-06-23 18:56:44 -07002474 * @see KeyCharacterMap#load
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002475 */
2476 public final KeyCharacterMap getKeyCharacterMap() {
2477 return KeyCharacterMap.load(mDeviceId);
2478 }
2479
2480 /**
2481 * Gets the primary character for this key.
2482 * In other words, the label that is physically printed on it.
2483 *
2484 * @return The display label character, or 0 if none (eg. for non-printing keys).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002485 */
2486 public char getDisplayLabel() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002487 return getKeyCharacterMap().getDisplayLabel(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002488 }
2489
2490 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002491 * Gets the Unicode character generated by the specified key and meta
2492 * key state combination.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 * <p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002494 * Returns the Unicode character that the specified key would produce
2495 * when the specified meta bits (see {@link MetaKeyKeyListener})
2496 * were active.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497 * </p><p>
2498 * Returns 0 if the key is not one that is used to type Unicode
2499 * characters.
2500 * </p><p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002501 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the
2502 * key is a "dead key" that should be combined with another to
2503 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} --
2504 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505 * </p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002506 *
2507 * @return The associated character or combining accent, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002508 */
2509 public int getUnicodeChar() {
2510 return getUnicodeChar(mMetaState);
2511 }
2512
2513 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002514 * Gets the Unicode character generated by the specified key and meta
2515 * key state combination.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516 * <p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002517 * Returns the Unicode character that the specified key would produce
2518 * when the specified meta bits (see {@link MetaKeyKeyListener})
2519 * were active.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 * </p><p>
2521 * Returns 0 if the key is not one that is used to type Unicode
2522 * characters.
2523 * </p><p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002524 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the
2525 * key is a "dead key" that should be combined with another to
2526 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} --
2527 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 * </p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002529 *
2530 * @param metaState The meta key modifier state.
2531 * @return The associated character or combining accent, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002532 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002533 public int getUnicodeChar(int metaState) {
2534 return getKeyCharacterMap().get(mKeyCode, metaState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002535 }
2536
2537 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002538 * Get the character conversion data for a given key code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002539 *
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002540 * @param results A {@link KeyCharacterMap.KeyData} instance that will be
2541 * filled with the results.
2542 * @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 -08002543 *
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002544 * @deprecated instead use {@link #getDisplayLabel()},
2545 * {@link #getNumber()} or {@link #getUnicodeChar(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002546 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002547 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002548 public boolean getKeyData(KeyData results) {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002549 return getKeyCharacterMap().getKeyData(mKeyCode, results);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 }
2551
2552 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002553 * Gets the first character in the character array that can be generated
2554 * by the specified key code.
2555 * <p>
2556 * This is a convenience function that returns the same value as
2557 * {@link #getMatch(char[],int) getMatch(chars, 0)}.
2558 * </p>
2559 *
2560 * @param chars The array of matching characters to consider.
2561 * @return The matching associated character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562 */
2563 public char getMatch(char[] chars) {
2564 return getMatch(chars, 0);
2565 }
2566
2567 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002568 * Gets the first character in the character array that can be generated
2569 * by the specified key code. If there are multiple choices, prefers
2570 * the one that would be generated with the specified meta key modifier state.
2571 *
2572 * @param chars The array of matching characters to consider.
2573 * @param metaState The preferred meta key modifier state.
2574 * @return The matching associated character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002575 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002576 public char getMatch(char[] chars, int metaState) {
2577 return getKeyCharacterMap().getMatch(mKeyCode, chars, metaState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 }
2579
2580 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002581 * Gets the number or symbol associated with the key.
2582 * <p>
2583 * The character value is returned, not the numeric value.
2584 * If the key is not a number, but is a symbol, the symbol is retuned.
2585 * </p><p>
2586 * This method is intended to to support dial pads and other numeric or
2587 * symbolic entry on keyboards where certain keys serve dual function
2588 * as alphabetic and symbolic keys. This method returns the number
2589 * or symbol associated with the key independent of whether the user
2590 * has pressed the required modifier.
2591 * </p><p>
2592 * For example, on one particular keyboard the keys on the top QWERTY row generate
2593 * numbers when ALT is pressed such that ALT-Q maps to '1'. So for that keyboard
2594 * when {@link #getNumber} is called with {@link KeyEvent#KEYCODE_Q} it returns '1'
2595 * so that the user can type numbers without pressing ALT when it makes sense.
2596 * </p>
2597 *
2598 * @return The associated numeric or symbolic character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002599 */
2600 public char getNumber() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002601 return getKeyCharacterMap().getNumber(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002602 }
2603
2604 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002605 * Returns true if this key produces a glyph.
2606 *
2607 * @return True if the key is a printing key.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002608 */
2609 public boolean isPrintingKey() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002610 return getKeyCharacterMap().isPrintingKey(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002611 }
2612
2613 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002614 * @deprecated Use {@link #dispatch(Callback, DispatcherState, Object)} instead.
2615 */
2616 @Deprecated
2617 public final boolean dispatch(Callback receiver) {
2618 return dispatch(receiver, null, null);
2619 }
2620
2621 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002622 * Deliver this key event to a {@link Callback} interface. If this is
2623 * an ACTION_MULTIPLE event and it is not handled, then an attempt will
2624 * be made to deliver a single normal event.
2625 *
2626 * @param receiver The Callback that will be given the event.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002627 * @param state State information retained across events.
2628 * @param target The target of the dispatch, for use in tracking.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002629 *
2630 * @return The return value from the Callback method that was called.
2631 */
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002632 public final boolean dispatch(Callback receiver, DispatcherState state,
2633 Object target) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634 switch (mAction) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002635 case ACTION_DOWN: {
2636 mFlags &= ~FLAG_START_TRACKING;
Dianne Hackborn8d374262009-09-14 21:21:52 -07002637 if (DEBUG) Log.v(TAG, "Key down to " + target + " in " + state
2638 + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002639 boolean res = receiver.onKeyDown(mKeyCode, this);
2640 if (state != null) {
2641 if (res && mRepeatCount == 0 && (mFlags&FLAG_START_TRACKING) != 0) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002642 if (DEBUG) Log.v(TAG, " Start tracking!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002643 state.startTracking(this, target);
2644 } else if (isLongPress() && state.isTracking(this)) {
2645 try {
2646 if (receiver.onKeyLongPress(mKeyCode, this)) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002647 if (DEBUG) Log.v(TAG, " Clear from long press!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002648 state.performedLongPress(this);
2649 res = true;
2650 }
2651 } catch (AbstractMethodError e) {
2652 }
2653 }
2654 }
2655 return res;
2656 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002657 case ACTION_UP:
Dianne Hackborn8d374262009-09-14 21:21:52 -07002658 if (DEBUG) Log.v(TAG, "Key up to " + target + " in " + state
2659 + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002660 if (state != null) {
2661 state.handleUpEvent(this);
2662 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002663 return receiver.onKeyUp(mKeyCode, this);
2664 case ACTION_MULTIPLE:
2665 final int count = mRepeatCount;
2666 final int code = mKeyCode;
2667 if (receiver.onKeyMultiple(code, count, this)) {
2668 return true;
2669 }
2670 if (code != KeyEvent.KEYCODE_UNKNOWN) {
2671 mAction = ACTION_DOWN;
2672 mRepeatCount = 0;
2673 boolean handled = receiver.onKeyDown(code, this);
2674 if (handled) {
2675 mAction = ACTION_UP;
2676 receiver.onKeyUp(code, this);
2677 }
2678 mAction = ACTION_MULTIPLE;
2679 mRepeatCount = count;
2680 return handled;
2681 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002682 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683 }
2684 return false;
2685 }
2686
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002687 /**
2688 * Use with {@link KeyEvent#dispatch(Callback, DispatcherState, Object)}
2689 * for more advanced key dispatching, such as long presses.
2690 */
2691 public static class DispatcherState {
2692 int mDownKeyCode;
2693 Object mDownTarget;
2694 SparseIntArray mActiveLongPresses = new SparseIntArray();
2695
2696 /**
2697 * Reset back to initial state.
2698 */
2699 public void reset() {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002700 if (DEBUG) Log.v(TAG, "Reset: " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002701 mDownKeyCode = 0;
2702 mDownTarget = null;
2703 mActiveLongPresses.clear();
2704 }
2705
2706 /**
2707 * Stop any tracking associated with this target.
2708 */
2709 public void reset(Object target) {
2710 if (mDownTarget == target) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002711 if (DEBUG) Log.v(TAG, "Reset in " + target + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002712 mDownKeyCode = 0;
2713 mDownTarget = null;
2714 }
2715 }
2716
2717 /**
2718 * Start tracking the key code associated with the given event. This
2719 * can only be called on a key down. It will allow you to see any
2720 * long press associated with the key, and will result in
2721 * {@link KeyEvent#isTracking} return true on the long press and up
2722 * events.
2723 *
2724 * <p>This is only needed if you are directly dispatching events, rather
2725 * than handling them in {@link Callback#onKeyDown}.
2726 */
2727 public void startTracking(KeyEvent event, Object target) {
2728 if (event.getAction() != ACTION_DOWN) {
2729 throw new IllegalArgumentException(
2730 "Can only start tracking on a down event");
2731 }
Dianne Hackborn8d374262009-09-14 21:21:52 -07002732 if (DEBUG) Log.v(TAG, "Start trackingt in " + target + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002733 mDownKeyCode = event.getKeyCode();
2734 mDownTarget = target;
2735 }
2736
2737 /**
2738 * Return true if the key event is for a key code that is currently
2739 * being tracked by the dispatcher.
2740 */
2741 public boolean isTracking(KeyEvent event) {
2742 return mDownKeyCode == event.getKeyCode();
2743 }
2744
2745 /**
2746 * Keep track of the given event's key code as having performed an
2747 * action with a long press, so no action should occur on the up.
2748 * <p>This is only needed if you are directly dispatching events, rather
2749 * than handling them in {@link Callback#onKeyLongPress}.
2750 */
2751 public void performedLongPress(KeyEvent event) {
2752 mActiveLongPresses.put(event.getKeyCode(), 1);
2753 }
2754
2755 /**
2756 * Handle key up event to stop tracking. This resets the dispatcher state,
2757 * and updates the key event state based on it.
2758 * <p>This is only needed if you are directly dispatching events, rather
2759 * than handling them in {@link Callback#onKeyUp}.
2760 */
2761 public void handleUpEvent(KeyEvent event) {
2762 final int keyCode = event.getKeyCode();
Dianne Hackborn8d374262009-09-14 21:21:52 -07002763 if (DEBUG) Log.v(TAG, "Handle key up " + event + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002764 int index = mActiveLongPresses.indexOfKey(keyCode);
2765 if (index >= 0) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002766 if (DEBUG) Log.v(TAG, " Index: " + index);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002767 event.mFlags |= FLAG_CANCELED | FLAG_CANCELED_LONG_PRESS;
2768 mActiveLongPresses.removeAt(index);
2769 }
2770 if (mDownKeyCode == keyCode) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002771 if (DEBUG) Log.v(TAG, " Tracking!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002772 event.mFlags |= FLAG_TRACKING;
2773 mDownKeyCode = 0;
2774 mDownTarget = null;
2775 }
2776 }
2777 }
Jeff Brown497a92c2010-09-12 17:55:08 -07002778
2779 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 public String toString() {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002781 StringBuilder msg = new StringBuilder();
2782 msg.append("KeyEvent { action=").append(actionToString(mAction));
2783 msg.append(", keyCode=").append(keyCodeToString(mKeyCode));
2784 msg.append(", scanCode=").append(mScanCode);
2785 if (mCharacters != null) {
2786 msg.append(", characters=\"").append(mCharacters).append("\"");
2787 }
2788 msg.append(", metaState=").append(metaStateToString(mMetaState));
2789 msg.append(", flags=0x").append(Integer.toHexString(mFlags));
2790 msg.append(", repeatCount=").append(mRepeatCount);
2791 msg.append(", eventTime=").append(mEventTime);
2792 msg.append(", downTime=").append(mDownTime);
2793 msg.append(", deviceId=").append(mDeviceId);
2794 msg.append(", source=0x").append(Integer.toHexString(mSource));
2795 msg.append(" }");
2796 return msg.toString();
Jeff Brown497a92c2010-09-12 17:55:08 -07002797 }
2798
2799 /**
2800 * Returns a string that represents the symbolic name of the specified action
Jeff Brown6f2fba42011-02-19 01:08:02 -08002801 * such as "ACTION_DOWN", or an equivalent numeric constant such as "35" if unknown.
Jeff Brown497a92c2010-09-12 17:55:08 -07002802 *
2803 * @param action The action.
2804 * @return The symbolic name of the specified action.
2805 * @hide
2806 */
2807 public static String actionToString(int action) {
2808 switch (action) {
2809 case ACTION_DOWN:
2810 return "ACTION_DOWN";
2811 case ACTION_UP:
2812 return "ACTION_UP";
2813 case ACTION_MULTIPLE:
2814 return "ACTION_MULTIPLE";
2815 default:
2816 return Integer.toString(action);
2817 }
2818 }
2819
2820 /**
2821 * Returns a string that represents the symbolic name of the specified keycode
Jeff Brown6f2fba42011-02-19 01:08:02 -08002822 * such as "KEYCODE_A", "KEYCODE_DPAD_UP", or an equivalent numeric constant
2823 * such as "1001" if unknown.
Jeff Brown497a92c2010-09-12 17:55:08 -07002824 *
2825 * @param keyCode The key code.
2826 * @return The symbolic name of the specified keycode.
2827 *
2828 * @see KeyCharacterMap#getDisplayLabel
Jeff Brown497a92c2010-09-12 17:55:08 -07002829 */
2830 public static String keyCodeToString(int keyCode) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08002831 String symbolicName = KEYCODE_SYMBOLIC_NAMES.get(keyCode);
2832 return symbolicName != null ? symbolicName : Integer.toString(keyCode);
Jeff Brown497a92c2010-09-12 17:55:08 -07002833 }
2834
2835 /**
Jeff Brown6f2fba42011-02-19 01:08:02 -08002836 * Gets a keycode by its symbolic name such as "KEYCODE_A" or an equivalent
2837 * numeric constant such as "1001".
Jeff Brown497a92c2010-09-12 17:55:08 -07002838 *
2839 * @param symbolicName The symbolic name of the keycode.
Jeff Brown6f2fba42011-02-19 01:08:02 -08002840 * @return The keycode or {@link #KEYCODE_UNKNOWN} if not found.
Michael Wright072137c2013-04-24 20:41:20 -07002841 * @see #keycodeToString(int)
Jeff Brown497a92c2010-09-12 17:55:08 -07002842 */
2843 public static int keyCodeFromString(String symbolicName) {
2844 if (symbolicName == null) {
2845 throw new IllegalArgumentException("symbolicName must not be null");
2846 }
2847
Jeff Brown6f2fba42011-02-19 01:08:02 -08002848 final int count = KEYCODE_SYMBOLIC_NAMES.size();
Jeff Brown497a92c2010-09-12 17:55:08 -07002849 for (int i = 0; i < count; i++) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08002850 if (symbolicName.equals(KEYCODE_SYMBOLIC_NAMES.valueAt(i))) {
Jeff Brown497a92c2010-09-12 17:55:08 -07002851 return i;
2852 }
2853 }
2854
2855 try {
Jeff Brown6f2fba42011-02-19 01:08:02 -08002856 return Integer.parseInt(symbolicName, 10);
Jeff Brown497a92c2010-09-12 17:55:08 -07002857 } catch (NumberFormatException ex) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08002858 return KEYCODE_UNKNOWN;
Jeff Brown497a92c2010-09-12 17:55:08 -07002859 }
2860 }
2861
2862 /**
2863 * Returns a string that represents the symbolic name of the specified combined meta
2864 * key modifier state flags such as "0", "META_SHIFT_ON",
Jeff Brown6f2fba42011-02-19 01:08:02 -08002865 * "META_ALT_ON|META_SHIFT_ON" or an equivalent numeric constant such as "0x10000000"
2866 * if unknown.
Jeff Brown497a92c2010-09-12 17:55:08 -07002867 *
2868 * @param metaState The meta state.
2869 * @return The symbolic name of the specified combined meta state flags.
2870 * @hide
2871 */
2872 public static String metaStateToString(int metaState) {
2873 if (metaState == 0) {
2874 return "0";
2875 }
2876 StringBuilder result = null;
2877 int i = 0;
2878 while (metaState != 0) {
2879 final boolean isSet = (metaState & 1) != 0;
2880 metaState >>>= 1; // unsigned shift!
2881 if (isSet) {
2882 final String name = META_SYMBOLIC_NAMES[i];
2883 if (result == null) {
2884 if (metaState == 0) {
2885 return name;
2886 }
2887 result = new StringBuilder(name);
2888 } else {
2889 result.append('|');
2890 result.append(name);
2891 }
2892 }
2893 i += 1;
2894 }
2895 return result.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 }
2897
2898 public static final Parcelable.Creator<KeyEvent> CREATOR
2899 = new Parcelable.Creator<KeyEvent>() {
2900 public KeyEvent createFromParcel(Parcel in) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07002901 in.readInt(); // skip token, we already know this is a KeyEvent
2902 return KeyEvent.createFromParcelBody(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 }
2904
2905 public KeyEvent[] newArray(int size) {
2906 return new KeyEvent[size];
2907 }
2908 };
Jeff Brown6ec402b2010-07-28 15:48:59 -07002909
2910 /** @hide */
2911 public static KeyEvent createFromParcelBody(Parcel in) {
2912 return new KeyEvent(in);
2913 }
2914
2915 private KeyEvent(Parcel in) {
Jeff Brown91c69ab2011-02-14 17:03:18 -08002916 mDeviceId = in.readInt();
2917 mSource = in.readInt();
Jeff Brown6ec402b2010-07-28 15:48:59 -07002918 mAction = in.readInt();
2919 mKeyCode = in.readInt();
2920 mRepeatCount = in.readInt();
2921 mMetaState = in.readInt();
2922 mScanCode = in.readInt();
2923 mFlags = in.readInt();
2924 mDownTime = in.readLong();
2925 mEventTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002926 }
2927
2928 public void writeToParcel(Parcel out, int flags) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07002929 out.writeInt(PARCEL_TOKEN_KEY_EVENT);
Jeff Brown91c69ab2011-02-14 17:03:18 -08002930
2931 out.writeInt(mDeviceId);
2932 out.writeInt(mSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002933 out.writeInt(mAction);
2934 out.writeInt(mKeyCode);
2935 out.writeInt(mRepeatCount);
2936 out.writeInt(mMetaState);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002937 out.writeInt(mScanCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 out.writeInt(mFlags);
2939 out.writeLong(mDownTime);
2940 out.writeLong(mEventTime);
2941 }
2942
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -07002943 private native boolean native_isSystemKey(int keyCode);
2944 private native boolean native_hasDefaultAction(int keyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945}