blob: 779560cb15f0b6950581873d4e14f1502c205814 [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;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070023import android.util.SparseIntArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.view.KeyCharacterMap;
25import android.view.KeyCharacterMap.KeyData;
26
27/**
Jeff Browndc1ab4b2010-09-14 18:03:38 -070028 * Object used to report key and button events.
29 * <p>
30 * Each key press is described by a sequence of key events. A key press
31 * starts with a key event with {@link #ACTION_DOWN}. If the key is held
32 * sufficiently long that it repeats, then the initial down is followed
33 * additional key events with {@link #ACTION_DOWN} and a non-zero value for
34 * {@link #getRepeatCount()}. The last key event is a {@link #ACTION_UP}
35 * for the key up. If the key press is canceled, the key up event will have the
36 * {@link #FLAG_CANCELED} flag set.
37 * </p><p>
38 * Key events are generally accompanied by a key code ({@link #getKeyCode()}),
39 * scan code ({@link #getScanCode()}) and meta state ({@link #getMetaState()}).
40 * Key code constants are defined in this class. Scan code constants are raw
41 * device-specific codes obtained from the OS and so are not generally meaningful
42 * to applications unless interpreted using the {@link KeyCharacterMap}.
43 * Meta states describe the pressed state of key modifiers
44 * such as {@link #META_SHIFT_ON} or {@link #META_ALT_ON}.
45 * </p><p>
Jeff Brown497a92c2010-09-12 17:55:08 -070046 * Key codes typically correspond one-to-one with individual keys on an input device.
47 * Many keys and key combinations serve quite different functions on different
48 * input devices so care must be taken when interpreting them. Always use the
49 * {@link KeyCharacterMap} associated with the input device when mapping keys
50 * to characters. Be aware that there may be multiple key input devices active
51 * at the same time and each will have its own key character map.
52 * </p><p>
Jean Chalard405bc512012-05-29 19:12:34 +090053 * As soft input methods can use multiple and inventive ways of inputting text,
54 * there is no guarantee that any key press on a soft keyboard will generate a key
55 * event: this is left to the IME's discretion, and in fact sending such events is
56 * discouraged. You should never rely on receiving KeyEvents for any key on a soft
57 * input method. In particular, the default software keyboard will never send any
58 * key event to any application targetting Jelly Bean or later, and will only send
59 * events for some presses of the delete and return keys to applications targetting
60 * Ice Cream Sandwich or earlier. Be aware that other software input methods may
61 * never send key events regardless of the version. Consider using editor actions
62 * like {@link android.view.inputmethod.EditorInfo#IME_ACTION_DONE} if you need
63 * specific interaction with the software keyboard, as it gives more visibility to
64 * the user as to how your application will react to key presses.
65 * </p><p>
Jeff Browndc1ab4b2010-09-14 18:03:38 -070066 * When interacting with an IME, the framework may deliver key events
67 * with the special action {@link #ACTION_MULTIPLE} that either specifies
68 * that single repeated key code or a sequence of characters to insert.
69 * </p><p>
Jeff Brownb6997262010-10-08 22:31:17 -070070 * In general, the framework cannot guarantee that the key events it delivers
71 * to a view always constitute complete key sequences since some events may be dropped
72 * or modified by containing views before they are delivered. The view implementation
73 * should be prepared to handle {@link #FLAG_CANCELED} and should tolerate anomalous
74 * situations such as receiving a new {@link #ACTION_DOWN} without first having
75 * received an {@link #ACTION_UP} for the prior key press.
Jeff Browndc1ab4b2010-09-14 18:03:38 -070076 * </p><p>
77 * Refer to {@link InputDevice} for more information about how different kinds of
78 * input devices and sources represent keys and buttons.
79 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 */
Jeff Brownc5ed5912010-07-14 18:48:53 -070081public class KeyEvent extends InputEvent implements Parcelable {
Jeff Browndc1ab4b2010-09-14 18:03:38 -070082 /** Key code constant: Unknown key code. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 public static final int KEYCODE_UNKNOWN = 0;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070084 /** Key code constant: Soft Left key.
85 * Usually situated below the display on phones and used as a multi-function
86 * feature key for selecting a software defined function shown on the bottom left
87 * of the display. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 public static final int KEYCODE_SOFT_LEFT = 1;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070089 /** Key code constant: Soft Right key.
90 * Usually situated below the display on phones and used as a multi-function
91 * feature key for selecting a software defined function shown on the bottom right
92 * of the display. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 public static final int KEYCODE_SOFT_RIGHT = 2;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070094 /** Key code constant: Home key.
95 * This key is handled by the framework and is never delivered to applications. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 public static final int KEYCODE_HOME = 3;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070097 /** Key code constant: Back key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 public static final int KEYCODE_BACK = 4;
Jeff Browndc1ab4b2010-09-14 18:03:38 -070099 /** Key code constant: Call key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 public static final int KEYCODE_CALL = 5;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700101 /** Key code constant: End Call key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 public static final int KEYCODE_ENDCALL = 6;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700103 /** Key code constant: '0' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 public static final int KEYCODE_0 = 7;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700105 /** Key code constant: '1' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 public static final int KEYCODE_1 = 8;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700107 /** Key code constant: '2' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 public static final int KEYCODE_2 = 9;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700109 /** Key code constant: '3' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 public static final int KEYCODE_3 = 10;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700111 /** Key code constant: '4' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 public static final int KEYCODE_4 = 11;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700113 /** Key code constant: '5' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 public static final int KEYCODE_5 = 12;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700115 /** Key code constant: '6' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 public static final int KEYCODE_6 = 13;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700117 /** Key code constant: '7' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 public static final int KEYCODE_7 = 14;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700119 /** Key code constant: '8' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 public static final int KEYCODE_8 = 15;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700121 /** Key code constant: '9' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 public static final int KEYCODE_9 = 16;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700123 /** Key code constant: '*' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 public static final int KEYCODE_STAR = 17;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700125 /** Key code constant: '#' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 public static final int KEYCODE_POUND = 18;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700127 /** Key code constant: Directional Pad Up key.
128 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 public static final int KEYCODE_DPAD_UP = 19;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700130 /** Key code constant: Directional Pad Down key.
131 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 public static final int KEYCODE_DPAD_DOWN = 20;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700133 /** Key code constant: Directional Pad Left key.
134 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 public static final int KEYCODE_DPAD_LEFT = 21;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700136 /** Key code constant: Directional Pad Right key.
137 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 public static final int KEYCODE_DPAD_RIGHT = 22;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700139 /** Key code constant: Directional Pad Center key.
140 * May also be synthesized from trackball motions. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 public static final int KEYCODE_DPAD_CENTER = 23;
Jeff Brownb0418da2010-11-01 15:24:01 -0700142 /** Key code constant: Volume Up key.
143 * Adjusts the speaker volume up. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 public static final int KEYCODE_VOLUME_UP = 24;
Jeff Brownb0418da2010-11-01 15:24:01 -0700145 /** Key code constant: Volume Down key.
146 * Adjusts the speaker volume down. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 public static final int KEYCODE_VOLUME_DOWN = 25;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700148 /** Key code constant: Power key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 public static final int KEYCODE_POWER = 26;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700150 /** Key code constant: Camera key.
151 * Used to launch a camera application or take pictures. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 public static final int KEYCODE_CAMERA = 27;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700153 /** Key code constant: Clear key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 public static final int KEYCODE_CLEAR = 28;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700155 /** Key code constant: 'A' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 public static final int KEYCODE_A = 29;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700157 /** Key code constant: 'B' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 public static final int KEYCODE_B = 30;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700159 /** Key code constant: 'C' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 public static final int KEYCODE_C = 31;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700161 /** Key code constant: 'D' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 public static final int KEYCODE_D = 32;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700163 /** Key code constant: 'E' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 public static final int KEYCODE_E = 33;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700165 /** Key code constant: 'F' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 public static final int KEYCODE_F = 34;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700167 /** Key code constant: 'G' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 public static final int KEYCODE_G = 35;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700169 /** Key code constant: 'H' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 public static final int KEYCODE_H = 36;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700171 /** Key code constant: 'I' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 public static final int KEYCODE_I = 37;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700173 /** Key code constant: 'J' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 public static final int KEYCODE_J = 38;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700175 /** Key code constant: 'K' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 public static final int KEYCODE_K = 39;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700177 /** Key code constant: 'L' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 public static final int KEYCODE_L = 40;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700179 /** Key code constant: 'M' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 public static final int KEYCODE_M = 41;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700181 /** Key code constant: 'N' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 public static final int KEYCODE_N = 42;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700183 /** Key code constant: 'O' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 public static final int KEYCODE_O = 43;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700185 /** Key code constant: 'P' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 public static final int KEYCODE_P = 44;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700187 /** Key code constant: 'Q' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 public static final int KEYCODE_Q = 45;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700189 /** Key code constant: 'R' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 public static final int KEYCODE_R = 46;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700191 /** Key code constant: 'S' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 public static final int KEYCODE_S = 47;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700193 /** Key code constant: 'T' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 public static final int KEYCODE_T = 48;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700195 /** Key code constant: 'U' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 public static final int KEYCODE_U = 49;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700197 /** Key code constant: 'V' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 public static final int KEYCODE_V = 50;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700199 /** Key code constant: 'W' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 public static final int KEYCODE_W = 51;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700201 /** Key code constant: 'X' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 public static final int KEYCODE_X = 52;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700203 /** Key code constant: 'Y' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 public static final int KEYCODE_Y = 53;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700205 /** Key code constant: 'Z' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 public static final int KEYCODE_Z = 54;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700207 /** Key code constant: ',' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 public static final int KEYCODE_COMMA = 55;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700209 /** Key code constant: '.' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 public static final int KEYCODE_PERIOD = 56;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700211 /** Key code constant: Left Alt modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 public static final int KEYCODE_ALT_LEFT = 57;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700213 /** Key code constant: Right Alt modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 public static final int KEYCODE_ALT_RIGHT = 58;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700215 /** Key code constant: Left Shift modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 public static final int KEYCODE_SHIFT_LEFT = 59;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700217 /** Key code constant: Right Shift modifier key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 public static final int KEYCODE_SHIFT_RIGHT = 60;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700219 /** Key code constant: Tab key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 public static final int KEYCODE_TAB = 61;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700221 /** Key code constant: Space key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 public static final int KEYCODE_SPACE = 62;
Jeff Brown224d4a12010-10-07 20:28:53 -0700223 /** Key code constant: Symbol modifier key.
224 * Used to enter alternate symbols. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 public static final int KEYCODE_SYM = 63;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700226 /** Key code constant: Explorer special function key.
227 * Used to launch a browser application. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 public static final int KEYCODE_EXPLORER = 64;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700229 /** Key code constant: Envelope special function key.
230 * Used to launch a mail application. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 public static final int KEYCODE_ENVELOPE = 65;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700232 /** Key code constant: Enter key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 public static final int KEYCODE_ENTER = 66;
Jeff Brown224d4a12010-10-07 20:28:53 -0700234 /** Key code constant: Backspace key.
Jeff Brown497a92c2010-09-12 17:55:08 -0700235 * Deletes characters before the insertion point, unlike {@link #KEYCODE_FORWARD_DEL}. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 public static final int KEYCODE_DEL = 67;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700237 /** Key code constant: '`' (backtick) key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 public static final int KEYCODE_GRAVE = 68;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700239 /** Key code constant: '-'. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 public static final int KEYCODE_MINUS = 69;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700241 /** Key code constant: '=' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 public static final int KEYCODE_EQUALS = 70;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700243 /** Key code constant: '[' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 public static final int KEYCODE_LEFT_BRACKET = 71;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700245 /** Key code constant: ']' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 public static final int KEYCODE_RIGHT_BRACKET = 72;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700247 /** Key code constant: '\' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 public static final int KEYCODE_BACKSLASH = 73;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700249 /** Key code constant: ';' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 public static final int KEYCODE_SEMICOLON = 74;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700251 /** Key code constant: ''' (apostrophe) key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 public static final int KEYCODE_APOSTROPHE = 75;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700253 /** Key code constant: '/' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 public static final int KEYCODE_SLASH = 76;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700255 /** Key code constant: '@' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 public static final int KEYCODE_AT = 77;
Jeff Brown224d4a12010-10-07 20:28:53 -0700257 /** Key code constant: Number modifier key.
258 * Used to enter numeric symbols.
259 * This key is not Num Lock; it is more like {@link #KEYCODE_ALT_LEFT} and is
260 * interpreted as an ALT key by {@link android.text.method.MetaKeyKeyListener}. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 public static final int KEYCODE_NUM = 78;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700262 /** Key code constant: Headset Hook key.
263 * Used to hang up calls and stop media. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 public static final int KEYCODE_HEADSETHOOK = 79;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700265 /** Key code constant: Camera Focus key.
266 * Used to focus the camera. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 public static final int KEYCODE_FOCUS = 80; // *Camera* focus
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700268 /** Key code constant: '+' key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 public static final int KEYCODE_PLUS = 81;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700270 /** Key code constant: Menu key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 public static final int KEYCODE_MENU = 82;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700272 /** Key code constant: Notification key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 public static final int KEYCODE_NOTIFICATION = 83;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700274 /** Key code constant: Search key. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 public static final int KEYCODE_SEARCH = 84;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700276 /** Key code constant: Play/Pause media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700277 public static final int KEYCODE_MEDIA_PLAY_PAUSE= 85;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700278 /** Key code constant: Stop media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700279 public static final int KEYCODE_MEDIA_STOP = 86;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700280 /** Key code constant: Play Next media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700281 public static final int KEYCODE_MEDIA_NEXT = 87;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700282 /** Key code constant: Play Previous media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700283 public static final int KEYCODE_MEDIA_PREVIOUS = 88;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700284 /** Key code constant: Rewind media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700285 public static final int KEYCODE_MEDIA_REWIND = 89;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700286 /** Key code constant: Fast Forward media key. */
Dianne Hackborn935ae462009-04-13 16:11:55 -0700287 public static final int KEYCODE_MEDIA_FAST_FORWARD = 90;
Jeff Brownb0418da2010-11-01 15:24:01 -0700288 /** Key code constant: Mute key.
289 * Mutes the microphone, unlike {@link #KEYCODE_VOLUME_MUTE}. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 public static final int KEYCODE_MUTE = 91;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700291 /** Key code constant: Page Up key. */
Chih-Wei Huang4fedd802009-05-27 15:52:50 +0800292 public static final int KEYCODE_PAGE_UP = 92;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700293 /** Key code constant: Page Down key. */
Chih-Wei Huang4fedd802009-05-27 15:52:50 +0800294 public static final int KEYCODE_PAGE_DOWN = 93;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700295 /** Key code constant: Picture Symbols modifier key.
296 * Used to switch symbol sets (Emoji, Kao-moji). */
mogimob032bc02009-10-03 03:13:56 +0900297 public static final int KEYCODE_PICTSYMBOLS = 94; // switch symbol-sets (Emoji,Kao-moji)
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700298 /** Key code constant: Switch Charset modifier key.
299 * Used to switch character sets (Kanji, Katakana). */
mogimob032bc02009-10-03 03:13:56 +0900300 public static final int KEYCODE_SWITCH_CHARSET = 95; // switch char-sets (Kanji,Katakana)
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700301 /** Key code constant: A Button key.
302 * On a game controller, the A button should be either the button labeled A
Michael Wright6b57bde2013-01-28 20:35:58 -0800303 * or the first button on the bottom row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700304 public static final int KEYCODE_BUTTON_A = 96;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700305 /** Key code constant: B Button key.
306 * On a game controller, the B button should be either the button labeled B
Michael Wright6b57bde2013-01-28 20:35:58 -0800307 * or the second button on the bottom row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700308 public static final int KEYCODE_BUTTON_B = 97;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700309 /** Key code constant: C Button key.
310 * On a game controller, the C button should be either the button labeled C
Michael Wright6b57bde2013-01-28 20:35:58 -0800311 * or the third button on the bottom row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700312 public static final int KEYCODE_BUTTON_C = 98;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700313 /** Key code constant: X Button key.
314 * On a game controller, the X button should be either the button labeled X
Michael Wright6b57bde2013-01-28 20:35:58 -0800315 * or the first button on the upper row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700316 public static final int KEYCODE_BUTTON_X = 99;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700317 /** Key code constant: Y Button key.
318 * On a game controller, the Y button should be either the button labeled Y
Michael Wright6b57bde2013-01-28 20:35:58 -0800319 * or the second button on the upper row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700320 public static final int KEYCODE_BUTTON_Y = 100;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700321 /** Key code constant: Z Button key.
322 * On a game controller, the Z button should be either the button labeled Z
Michael Wright6b57bde2013-01-28 20:35:58 -0800323 * or the third button on the upper row of controller buttons. */
Jeff Brownfd035822010-06-30 16:10:35 -0700324 public static final int KEYCODE_BUTTON_Z = 101;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700325 /** Key code constant: L1 Button key.
326 * On a game controller, the L1 button should be either the button labeled L1 (or L)
327 * or the top left trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700328 public static final int KEYCODE_BUTTON_L1 = 102;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700329 /** Key code constant: R1 Button key.
330 * On a game controller, the R1 button should be either the button labeled R1 (or R)
331 * or the top right trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700332 public static final int KEYCODE_BUTTON_R1 = 103;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700333 /** Key code constant: L2 Button key.
334 * On a game controller, the L2 button should be either the button labeled L2
335 * or the bottom left trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700336 public static final int KEYCODE_BUTTON_L2 = 104;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700337 /** Key code constant: R2 Button key.
338 * On a game controller, the R2 button should be either the button labeled R2
339 * or the bottom right trigger button. */
Jeff Brownfd035822010-06-30 16:10:35 -0700340 public static final int KEYCODE_BUTTON_R2 = 105;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700341 /** Key code constant: Left Thumb Button key.
342 * On a game controller, the left thumb button indicates that the left (or only)
343 * joystick is pressed. */
Jeff Brownfd035822010-06-30 16:10:35 -0700344 public static final int KEYCODE_BUTTON_THUMBL = 106;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700345 /** Key code constant: Right Thumb Button key.
346 * On a game controller, the right thumb button indicates that the right
347 * joystick is pressed. */
Jeff Brownfd035822010-06-30 16:10:35 -0700348 public static final int KEYCODE_BUTTON_THUMBR = 107;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700349 /** Key code constant: Start Button key.
350 * On a game controller, the button labeled Start. */
Jeff Brownfd035822010-06-30 16:10:35 -0700351 public static final int KEYCODE_BUTTON_START = 108;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700352 /** Key code constant: Select Button key.
353 * On a game controller, the button labeled Select. */
Jeff Brownfd035822010-06-30 16:10:35 -0700354 public static final int KEYCODE_BUTTON_SELECT = 109;
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700355 /** Key code constant: Mode Button key.
356 * On a game controller, the button labeled Mode. */
Jeff Brownfd035822010-06-30 16:10:35 -0700357 public static final int KEYCODE_BUTTON_MODE = 110;
Jeff Brown497a92c2010-09-12 17:55:08 -0700358 /** Key code constant: Escape key. */
359 public static final int KEYCODE_ESCAPE = 111;
360 /** Key code constant: Forward Delete key.
361 * Deletes characters ahead of the insertion point, unlike {@link #KEYCODE_DEL}. */
362 public static final int KEYCODE_FORWARD_DEL = 112;
363 /** Key code constant: Left Control modifier key. */
364 public static final int KEYCODE_CTRL_LEFT = 113;
365 /** Key code constant: Right Control modifier key. */
366 public static final int KEYCODE_CTRL_RIGHT = 114;
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800367 /** Key code constant: Caps Lock key. */
Jeff Brown497a92c2010-09-12 17:55:08 -0700368 public static final int KEYCODE_CAPS_LOCK = 115;
369 /** Key code constant: Scroll Lock key. */
370 public static final int KEYCODE_SCROLL_LOCK = 116;
371 /** Key code constant: Left Meta modifier key. */
372 public static final int KEYCODE_META_LEFT = 117;
373 /** Key code constant: Right Meta modifier key. */
374 public static final int KEYCODE_META_RIGHT = 118;
375 /** Key code constant: Function modifier key. */
376 public static final int KEYCODE_FUNCTION = 119;
377 /** Key code constant: System Request / Print Screen key. */
378 public static final int KEYCODE_SYSRQ = 120;
379 /** Key code constant: Break / Pause key. */
380 public static final int KEYCODE_BREAK = 121;
381 /** Key code constant: Home Movement key.
382 * Used for scrolling or moving the cursor around to the start of a line
383 * or to the top of a list. */
384 public static final int KEYCODE_MOVE_HOME = 122;
385 /** Key code constant: End Movement key.
386 * Used for scrolling or moving the cursor around to the end of a line
387 * or to the bottom of a list. */
388 public static final int KEYCODE_MOVE_END = 123;
389 /** Key code constant: Insert key.
390 * Toggles insert / overwrite edit mode. */
391 public static final int KEYCODE_INSERT = 124;
392 /** Key code constant: Forward key.
393 * Navigates forward in the history stack. Complement of {@link #KEYCODE_BACK}. */
394 public static final int KEYCODE_FORWARD = 125;
395 /** Key code constant: Play media key. */
396 public static final int KEYCODE_MEDIA_PLAY = 126;
397 /** Key code constant: Pause media key. */
398 public static final int KEYCODE_MEDIA_PAUSE = 127;
399 /** Key code constant: Close media key.
400 * May be used to close a CD tray, for example. */
401 public static final int KEYCODE_MEDIA_CLOSE = 128;
402 /** Key code constant: Eject media key.
403 * May be used to eject a CD tray, for example. */
404 public static final int KEYCODE_MEDIA_EJECT = 129;
405 /** Key code constant: Record media key. */
406 public static final int KEYCODE_MEDIA_RECORD = 130;
407 /** Key code constant: F1 key. */
408 public static final int KEYCODE_F1 = 131;
409 /** Key code constant: F2 key. */
410 public static final int KEYCODE_F2 = 132;
411 /** Key code constant: F3 key. */
412 public static final int KEYCODE_F3 = 133;
413 /** Key code constant: F4 key. */
414 public static final int KEYCODE_F4 = 134;
415 /** Key code constant: F5 key. */
416 public static final int KEYCODE_F5 = 135;
417 /** Key code constant: F6 key. */
418 public static final int KEYCODE_F6 = 136;
419 /** Key code constant: F7 key. */
420 public static final int KEYCODE_F7 = 137;
421 /** Key code constant: F8 key. */
422 public static final int KEYCODE_F8 = 138;
423 /** Key code constant: F9 key. */
424 public static final int KEYCODE_F9 = 139;
425 /** Key code constant: F10 key. */
426 public static final int KEYCODE_F10 = 140;
427 /** Key code constant: F11 key. */
428 public static final int KEYCODE_F11 = 141;
429 /** Key code constant: F12 key. */
430 public static final int KEYCODE_F12 = 142;
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800431 /** Key code constant: Num Lock key.
Jeff Brown497a92c2010-09-12 17:55:08 -0700432 * This is the Num Lock key; it is different from {@link #KEYCODE_NUM}.
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800433 * This key alters the behavior of other keys on the numeric keypad. */
Jeff Brown497a92c2010-09-12 17:55:08 -0700434 public static final int KEYCODE_NUM_LOCK = 143;
435 /** Key code constant: Numeric keypad '0' key. */
436 public static final int KEYCODE_NUMPAD_0 = 144;
437 /** Key code constant: Numeric keypad '1' key. */
438 public static final int KEYCODE_NUMPAD_1 = 145;
439 /** Key code constant: Numeric keypad '2' key. */
440 public static final int KEYCODE_NUMPAD_2 = 146;
441 /** Key code constant: Numeric keypad '3' key. */
442 public static final int KEYCODE_NUMPAD_3 = 147;
443 /** Key code constant: Numeric keypad '4' key. */
444 public static final int KEYCODE_NUMPAD_4 = 148;
445 /** Key code constant: Numeric keypad '5' key. */
446 public static final int KEYCODE_NUMPAD_5 = 149;
447 /** Key code constant: Numeric keypad '6' key. */
448 public static final int KEYCODE_NUMPAD_6 = 150;
449 /** Key code constant: Numeric keypad '7' key. */
450 public static final int KEYCODE_NUMPAD_7 = 151;
451 /** Key code constant: Numeric keypad '8' key. */
452 public static final int KEYCODE_NUMPAD_8 = 152;
453 /** Key code constant: Numeric keypad '9' key. */
454 public static final int KEYCODE_NUMPAD_9 = 153;
455 /** Key code constant: Numeric keypad '/' key (for division). */
456 public static final int KEYCODE_NUMPAD_DIVIDE = 154;
457 /** Key code constant: Numeric keypad '*' key (for multiplication). */
458 public static final int KEYCODE_NUMPAD_MULTIPLY = 155;
459 /** Key code constant: Numeric keypad '-' key (for subtraction). */
460 public static final int KEYCODE_NUMPAD_SUBTRACT = 156;
461 /** Key code constant: Numeric keypad '+' key (for addition). */
462 public static final int KEYCODE_NUMPAD_ADD = 157;
463 /** Key code constant: Numeric keypad '.' key (for decimals or digit grouping). */
464 public static final int KEYCODE_NUMPAD_DOT = 158;
465 /** Key code constant: Numeric keypad ',' key (for decimals or digit grouping). */
466 public static final int KEYCODE_NUMPAD_COMMA = 159;
467 /** Key code constant: Numeric keypad Enter key. */
468 public static final int KEYCODE_NUMPAD_ENTER = 160;
469 /** Key code constant: Numeric keypad '=' key. */
470 public static final int KEYCODE_NUMPAD_EQUALS = 161;
471 /** Key code constant: Numeric keypad '(' key. */
472 public static final int KEYCODE_NUMPAD_LEFT_PAREN = 162;
473 /** Key code constant: Numeric keypad ')' key. */
474 public static final int KEYCODE_NUMPAD_RIGHT_PAREN = 163;
Jeff Brownb0418da2010-11-01 15:24:01 -0700475 /** Key code constant: Volume Mute key.
476 * Mutes the speaker, unlike {@link #KEYCODE_MUTE}.
477 * This key should normally be implemented as a toggle such that the first press
478 * mutes the speaker and the second press restores the original volume. */
479 public static final int KEYCODE_VOLUME_MUTE = 164;
Jason Bayer3adf4902010-11-09 14:54:55 -0800480 /** Key code constant: Info key.
481 * Common on TV remotes to show additional information related to what is
482 * currently being viewed. */
483 public static final int KEYCODE_INFO = 165;
484 /** Key code constant: Channel up key.
485 * On TV remotes, increments the television channel. */
486 public static final int KEYCODE_CHANNEL_UP = 166;
487 /** Key code constant: Channel down key.
488 * On TV remotes, decrements the television channel. */
489 public static final int KEYCODE_CHANNEL_DOWN = 167;
490 /** Key code constant: Zoom in key. */
491 public static final int KEYCODE_ZOOM_IN = 168;
492 /** Key code constant: Zoom out key. */
493 public static final int KEYCODE_ZOOM_OUT = 169;
494 /** Key code constant: TV key.
495 * On TV remotes, switches to viewing live TV. */
496 public static final int KEYCODE_TV = 170;
497 /** Key code constant: Window key.
498 * On TV remotes, toggles picture-in-picture mode or other windowing functions. */
499 public static final int KEYCODE_WINDOW = 171;
500 /** Key code constant: Guide key.
501 * On TV remotes, shows a programming guide. */
502 public static final int KEYCODE_GUIDE = 172;
503 /** Key code constant: DVR key.
504 * On some TV remotes, switches to a DVR mode for recorded shows. */
505 public static final int KEYCODE_DVR = 173;
506 /** Key code constant: Bookmark key.
507 * On some TV remotes, bookmarks content or web pages. */
508 public static final int KEYCODE_BOOKMARK = 174;
509 /** Key code constant: Toggle captions key.
510 * Switches the mode for closed-captioning text, for example during television shows. */
511 public static final int KEYCODE_CAPTIONS = 175;
512 /** Key code constant: Settings key.
513 * Starts the system settings activity. */
514 public static final int KEYCODE_SETTINGS = 176;
515 /** Key code constant: TV power key.
516 * On TV remotes, toggles the power on a television screen. */
517 public static final int KEYCODE_TV_POWER = 177;
518 /** Key code constant: TV input key.
519 * On TV remotes, switches the input on a television screen. */
520 public static final int KEYCODE_TV_INPUT = 178;
521 /** Key code constant: Set-top-box power key.
522 * On TV remotes, toggles the power on an external Set-top-box. */
523 public static final int KEYCODE_STB_POWER = 179;
524 /** Key code constant: Set-top-box input key.
525 * On TV remotes, switches the input mode on an external Set-top-box. */
526 public static final int KEYCODE_STB_INPUT = 180;
527 /** Key code constant: A/V Receiver power key.
528 * On TV remotes, toggles the power on an external A/V Receiver. */
529 public static final int KEYCODE_AVR_POWER = 181;
530 /** Key code constant: A/V Receiver input key.
531 * On TV remotes, switches the input mode on an external A/V Receiver. */
532 public static final int KEYCODE_AVR_INPUT = 182;
533 /** Key code constant: Red "programmable" key.
534 * On TV remotes, acts as a contextual/programmable key. */
535 public static final int KEYCODE_PROG_RED = 183;
536 /** Key code constant: Green "programmable" key.
537 * On TV remotes, actsas a contextual/programmable key. */
538 public static final int KEYCODE_PROG_GREEN = 184;
539 /** Key code constant: Yellow "programmable" key.
540 * On TV remotes, acts as a contextual/programmable key. */
541 public static final int KEYCODE_PROG_YELLOW = 185;
542 /** Key code constant: Blue "programmable" key.
543 * On TV remotes, acts as a contextual/programmable key. */
544 public static final int KEYCODE_PROG_BLUE = 186;
Jeff Brown49ed71d2010-12-06 17:13:33 -0800545 /** Key code constant: App switch key.
546 * Should bring up the application switcher dialog. */
547 public static final int KEYCODE_APP_SWITCH = 187;
Jeff Browncb1404e2011-01-15 18:14:15 -0800548 /** Key code constant: Generic Game Pad Button #1.*/
549 public static final int KEYCODE_BUTTON_1 = 188;
550 /** Key code constant: Generic Game Pad Button #2.*/
551 public static final int KEYCODE_BUTTON_2 = 189;
552 /** Key code constant: Generic Game Pad Button #3.*/
553 public static final int KEYCODE_BUTTON_3 = 190;
554 /** Key code constant: Generic Game Pad Button #4.*/
555 public static final int KEYCODE_BUTTON_4 = 191;
556 /** Key code constant: Generic Game Pad Button #5.*/
557 public static final int KEYCODE_BUTTON_5 = 192;
558 /** Key code constant: Generic Game Pad Button #6.*/
559 public static final int KEYCODE_BUTTON_6 = 193;
560 /** Key code constant: Generic Game Pad Button #7.*/
561 public static final int KEYCODE_BUTTON_7 = 194;
562 /** Key code constant: Generic Game Pad Button #8.*/
563 public static final int KEYCODE_BUTTON_8 = 195;
564 /** Key code constant: Generic Game Pad Button #9.*/
565 public static final int KEYCODE_BUTTON_9 = 196;
566 /** Key code constant: Generic Game Pad Button #10.*/
567 public static final int KEYCODE_BUTTON_10 = 197;
568 /** Key code constant: Generic Game Pad Button #11.*/
569 public static final int KEYCODE_BUTTON_11 = 198;
570 /** Key code constant: Generic Game Pad Button #12.*/
571 public static final int KEYCODE_BUTTON_12 = 199;
572 /** Key code constant: Generic Game Pad Button #13.*/
573 public static final int KEYCODE_BUTTON_13 = 200;
574 /** Key code constant: Generic Game Pad Button #14.*/
575 public static final int KEYCODE_BUTTON_14 = 201;
576 /** Key code constant: Generic Game Pad Button #15.*/
577 public static final int KEYCODE_BUTTON_15 = 202;
578 /** Key code constant: Generic Game Pad Button #16.*/
579 public static final int KEYCODE_BUTTON_16 = 203;
Jeff Brown9812aed2011-03-07 17:09:51 -0800580 /** Key code constant: Language Switch key.
581 * Toggles the current input language such as switching between English and Japanese on
582 * a QWERTY keyboard. On some devices, the same function may be performed by
583 * pressing Shift+Spacebar. */
584 public static final int KEYCODE_LANGUAGE_SWITCH = 204;
585 /** Key code constant: Manner Mode key.
586 * Toggles silent or vibrate mode on and off to make the device behave more politely
587 * in certain settings such as on a crowded train. On some devices, the key may only
588 * operate when long-pressed. */
589 public static final int KEYCODE_MANNER_MODE = 205;
590 /** Key code constant: 3D Mode key.
591 * Toggles the display between 2D and 3D mode. */
592 public static final int KEYCODE_3D_MODE = 206;
Jeff Brown6651a632011-11-28 12:59:11 -0800593 /** Key code constant: Contacts special function key.
594 * Used to launch an address book application. */
595 public static final int KEYCODE_CONTACTS = 207;
596 /** Key code constant: Calendar special function key.
597 * Used to launch a calendar application. */
598 public static final int KEYCODE_CALENDAR = 208;
599 /** Key code constant: Music special function key.
600 * Used to launch a music player application. */
601 public static final int KEYCODE_MUSIC = 209;
602 /** Key code constant: Calculator special function key.
603 * Used to launch a calculator application. */
604 public static final int KEYCODE_CALCULATOR = 210;
Yang Chuang7511f9c2012-02-10 15:18:26 +0800605 /** Key code constant: Japanese full-width / half-width key. */
606 public static final int KEYCODE_ZENKAKU_HANKAKU = 211;
607 /** Key code constant: Japanese alphanumeric key. */
608 public static final int KEYCODE_EISU = 212;
609 /** Key code constant: Japanese non-conversion key. */
610 public static final int KEYCODE_MUHENKAN = 213;
611 /** Key code constant: Japanese conversion key. */
612 public static final int KEYCODE_HENKAN = 214;
613 /** Key code constant: Japanese katakana / hiragana key. */
614 public static final int KEYCODE_KATAKANA_HIRAGANA = 215;
615 /** Key code constant: Japanese Yen key. */
616 public static final int KEYCODE_YEN = 216;
617 /** Key code constant: Japanese Ro key. */
618 public static final int KEYCODE_RO = 217;
619 /** Key code constant: Japanese kana key. */
620 public static final int KEYCODE_KANA = 218;
Jeff Brownde7a8ea2012-06-13 18:28:57 -0700621 /** Key code constant: Assist key.
622 * Launches the global assist activity. Not delivered to applications. */
623 public static final int KEYCODE_ASSIST = 219;
Michael Wright1df477a2013-01-31 16:19:18 -0800624 /** Key code constant: Brightness Down key.
625 * Adjusts the screen brightness down. */
626 public static final int KEYCODE_BRIGHTNESS_DOWN = 220;
627 /** Key code constant: Brightness Up key.
628 * Adjusts the screen brightness up. */
629 public static final int KEYCODE_BRIGHTNESS_UP = 221;
Jeff Brown6212a492014-03-07 13:58:47 -0800630 /** Key code constant: Audio Track key.
Jaekyun Seokbfdad8e2013-07-08 13:53:21 +0900631 * Switches the audio tracks. */
632 public static final int KEYCODE_MEDIA_AUDIO_TRACK = 222;
Jeff Brown6212a492014-03-07 13:58:47 -0800633 /** Key code constant: Sleep key.
634 * Puts the device to sleep. Behaves somewhat like {@link #KEYCODE_POWER} but it
635 * has no effect if the device is already asleep. */
636 public static final int KEYCODE_SLEEP = 223;
637 /** Key code constant: Wakeup key.
638 * Wakes up the device. Behaves somewhat like {@link #KEYCODE_POWER} but it
639 * has no effect if the device is already awake. */
640 public static final int KEYCODE_WAKEUP = 224;
Tim Kilbourn87cd0dc2014-04-14 15:37:51 -0700641 /** Key code constant: Pairing key.
642 * Initiates peripheral pairing mode. Useful for pairing remote control
643 * devices or game controllers, especially if no other input mode is
644 * available. */
645 public static final int KEYCODE_PAIRING = 225;
Jinsuk Kim96658f72014-05-14 15:33:43 +0900646 /** Key code constant: Media Top Menu key.
647 * Goes to the top of media menu. */
648 public static final int KEYCODE_MEDIA_TOP_MENU = 226;
649 /** Key code constant: '11' key. */
650 public static final int KEYCODE_11 = 227;
651 /** Key code constant: '12' key. */
652 public static final int KEYCODE_12 = 228;
653 /** Key code constant: Last Channel key.
654 * Goes to the last viewed channel. */
655 public static final int KEYCODE_LAST_CHANNEL = 229;
656 /** Key code constant: TV data service key.
657 * Displays data services like weather, sports. */
658 public static final int KEYCODE_TV_DATA_SERVICE = 230;
Michael Wrightdc63f7b2014-08-21 19:05:21 -0700659 /** Key code constant: Voice Assist key.
660 * Launches the global voice assist activity. Not delivered to applications. */
661 public static final int KEYCODE_VOICE_ASSIST = 231;
ASAZU, Hidekidbd6aba2014-08-27 18:03:30 +0900662 /** Key code constant: Radio key.
663 * Toggles TV service / Radio service. */
664 public static final int KEYCODE_TV_RADIO_SERVICE = 232;
665 /** Key code constant: Teletext key.
666 * Displays Teletext service. */
667 public static final int KEYCODE_TV_TELETEXT = 233;
668 /** Key code constant: Number entry key.
669 * Initiates to enter multi-digit channel nubmber when each digit key is assigned
670 * for selecting separate channel. Corresponds to Number Entry Mode (0x1D) of CEC
671 * User Control Code. */
672 public static final int KEYCODE_TV_NUMBER_ENTRY = 234;
673 /** Key code constant: Analog Terrestrial key.
674 * Switches to analog terrestrial broadcast service. */
675 public static final int KEYCODE_TV_TERRESTRIAL_ANALOG = 235;
676 /** Key code constant: Digital Terrestrial key.
677 * Switches to digital terrestrial broadcast service. */
678 public static final int KEYCODE_TV_TERRESTRIAL_DIGITAL = 236;
679 /** Key code constant: Satellite key.
680 * Switches to digital satellite broadcast service. */
681 public static final int KEYCODE_TV_SATELLITE = 237;
682 /** Key code constant: BS key.
683 * Switches to BS digital satellite broadcasting service available in Japan. */
684 public static final int KEYCODE_TV_SATELLITE_BS = 238;
685 /** Key code constant: CS key.
686 * Switches to CS digital satellite broadcasting service available in Japan. */
687 public static final int KEYCODE_TV_SATELLITE_CS = 239;
688 /** Key code constant: BS/CS key.
689 * Toggles between BS and CS digital satellite services. */
690 public static final int KEYCODE_TV_SATELLITE_SERVICE = 240;
691 /** Key code constant: Toggle Network key.
692 * Toggles selecting broacast services. */
693 public static final int KEYCODE_TV_NETWORK = 241;
694 /** Key code constant: Antenna/Cable key.
695 * Toggles broadcast input source between antenna and cable. */
696 public static final int KEYCODE_TV_ANTENNA_CABLE = 242;
697 /** Key code constant: HDMI #1 key.
698 * Switches to HDMI input #1. */
699 public static final int KEYCODE_TV_INPUT_HDMI_1 = 243;
700 /** Key code constant: HDMI #2 key.
701 * Switches to HDMI input #2. */
702 public static final int KEYCODE_TV_INPUT_HDMI_2 = 244;
703 /** Key code constant: HDMI #3 key.
704 * Switches to HDMI input #3. */
705 public static final int KEYCODE_TV_INPUT_HDMI_3 = 245;
706 /** Key code constant: HDMI #4 key.
707 * Switches to HDMI input #4. */
708 public static final int KEYCODE_TV_INPUT_HDMI_4 = 246;
709 /** Key code constant: Composite #1 key.
710 * Switches to composite video input #1. */
711 public static final int KEYCODE_TV_INPUT_COMPOSITE_1 = 247;
712 /** Key code constant: Composite #2 key.
713 * Switches to composite video input #2. */
714 public static final int KEYCODE_TV_INPUT_COMPOSITE_2 = 248;
715 /** Key code constant: Component #1 key.
716 * Switches to component video input #1. */
717 public static final int KEYCODE_TV_INPUT_COMPONENT_1 = 249;
718 /** Key code constant: Component #2 key.
719 * Switches to component video input #2. */
720 public static final int KEYCODE_TV_INPUT_COMPONENT_2 = 250;
721 /** Key code constant: VGA #1 key.
722 * Switches to VGA (analog RGB) input #1. */
723 public static final int KEYCODE_TV_INPUT_VGA_1 = 251;
724 /** Key code constant: Audio description key.
725 * Toggles audio description off / on. */
726 public static final int KEYCODE_TV_AUDIO_DESCRIPTION = 252;
727 /** Key code constant: Audio description mixing volume up key.
728 * Louden audio description volume as compared with normal audio volume. */
729 public static final int KEYCODE_TV_AUDIO_DESCRIPTION_MIX_UP = 253;
730 /** Key code constant: Audio description mixing volume down key.
731 * Lessen audio description volume as compared with normal audio volume. */
732 public static final int KEYCODE_TV_AUDIO_DESCRIPTION_MIX_DOWN = 254;
733 /** Key code constant: Zoom mode key.
734 * Changes Zoom mode (Normal, Full, Zoom, Wide-zoom, etc.) */
735 public static final int KEYCODE_TV_ZOOM_MODE = 255;
736 /** Key code constant: Contents menu key.
737 * Goes to the title list. Corresponds to Contents Menu (0x0B) of CEC User Control
738 * Code */
739 public static final int KEYCODE_TV_CONTENTS_MENU = 256;
740 /** Key code constant: Media context menu key.
741 * Goes to the context menu of media contents. Corresponds to Media Context-sensitive
742 * Menu (0x11) of CEC User Control Code. */
743 public static final int KEYCODE_TV_MEDIA_CONTEXT_MENU = 257;
744 /** Key code constant: Timer programming key.
745 * Goes to the timer recording menu. Corresponds to Timer Programming (0x54) of
746 * CEC User Control Code. */
747 public static final int KEYCODE_TV_TIMER_PROGRAMMING = 258;
748 /** Key code constant: Help key. */
749 public static final int KEYCODE_HELP = 259;
Jeff Brown497a92c2010-09-12 17:55:08 -0700750
ASAZU, Hidekidbd6aba2014-08-27 18:03:30 +0900751 private static final int LAST_KEYCODE = KEYCODE_HELP;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752
753 // NOTE: If you add a new keycode here you must also add it to:
754 // isSystem()
Michael Wrightdc63f7b2014-08-21 19:05:21 -0700755 // isWakeKey()
Chirayu Desai61c37ae2013-04-15 20:11:37 +0530756 // frameworks/native/include/android/keycodes.h
Jinsuk Kim96658f72014-05-14 15:33:43 +0900757 // frameworks/native/include/input/InputEventLabels.h
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 // frameworks/base/core/res/res/values/attrs.xml
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 // emulator?
Jeff Brown6651a632011-11-28 12:59:11 -0800760 // LAST_KEYCODE
Dianne Hackborn935ae462009-04-13 16:11:55 -0700761 //
762 // Also Android currently does not reserve code ranges for vendor-
763 // specific key codes. If you have new key codes to have, you
764 // MUST contribute a patch to the open source project to define
765 // those new codes. This is intended to maintain a consistent
766 // set of key code definitions across all Android devices.
Jeff Brown497a92c2010-09-12 17:55:08 -0700767
Jeff Brown497a92c2010-09-12 17:55:08 -0700768 // Symbolic names of all metakeys in bit order from least significant to most significant.
769 // Accordingly there are exactly 32 values in this table.
770 private static final String[] META_SYMBOLIC_NAMES = new String[] {
771 "META_SHIFT_ON",
772 "META_ALT_ON",
773 "META_SYM_ON",
774 "META_FUNCTION_ON",
775 "META_ALT_LEFT_ON",
776 "META_ALT_RIGHT_ON",
777 "META_SHIFT_LEFT_ON",
778 "META_SHIFT_RIGHT_ON",
779 "META_CAP_LOCKED",
780 "META_ALT_LOCKED",
781 "META_SYM_LOCKED",
782 "0x00000800",
783 "META_CTRL_ON",
784 "META_CTRL_LEFT_ON",
785 "META_CTRL_RIGHT_ON",
786 "0x00008000",
787 "META_META_ON",
788 "META_META_LEFT_ON",
789 "META_META_RIGHT_ON",
790 "0x00080000",
Jeff Brown51e7fe72010-10-29 22:19:53 -0700791 "META_CAPS_LOCK_ON",
792 "META_NUM_LOCK_ON",
793 "META_SCROLL_LOCK_ON",
Jeff Brown497a92c2010-09-12 17:55:08 -0700794 "0x00800000",
795 "0x01000000",
796 "0x02000000",
797 "0x04000000",
798 "0x08000000",
799 "0x10000000",
800 "0x20000000",
801 "0x40000000",
802 "0x80000000",
803 };
804
Michael Wright337d9d22014-04-22 15:03:48 -0700805 private static final String LABEL_PREFIX = "KEYCODE_";
806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 /**
808 * @deprecated There are now more than MAX_KEYCODE keycodes.
809 * Use {@link #getMaxKeyCode()} instead.
810 */
811 @Deprecated
812 public static final int MAX_KEYCODE = 84;
813
814 /**
815 * {@link #getAction} value: the key has been pressed down.
816 */
817 public static final int ACTION_DOWN = 0;
818 /**
819 * {@link #getAction} value: the key has been released.
820 */
821 public static final int ACTION_UP = 1;
822 /**
823 * {@link #getAction} value: multiple duplicate key events have
824 * occurred in a row, or a complex string is being delivered. If the
825 * key code is not {#link {@link #KEYCODE_UNKNOWN} then the
826 * {#link {@link #getRepeatCount()} method returns the number of times
827 * the given key code should be executed.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700828 * Otherwise, if the key code is {@link #KEYCODE_UNKNOWN}, then
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 * this is a sequence of characters as returned by {@link #getCharacters}.
830 */
831 public static final int ACTION_MULTIPLE = 2;
832
833 /**
Jeff Brown497a92c2010-09-12 17:55:08 -0700834 * SHIFT key locked in CAPS mode.
835 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
836 * @hide
837 */
838 public static final int META_CAP_LOCKED = 0x100;
839
840 /**
841 * ALT key locked.
842 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
843 * @hide
844 */
845 public static final int META_ALT_LOCKED = 0x200;
846
847 /**
848 * SYM key locked.
849 * Reserved for use by {@link MetaKeyKeyListener} for a published constant in its API.
850 * @hide
851 */
852 public static final int META_SYM_LOCKED = 0x400;
853
854 /**
855 * Text is in selection mode.
856 * Reserved for use by {@link MetaKeyKeyListener} for a private unpublished constant
857 * in its API that is currently being retained for legacy reasons.
858 * @hide
859 */
860 public static final int META_SELECTING = 0x800;
861
862 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 * <p>This mask is used to check whether one of the ALT meta keys is pressed.</p>
864 *
865 * @see #isAltPressed()
866 * @see #getMetaState()
867 * @see #KEYCODE_ALT_LEFT
868 * @see #KEYCODE_ALT_RIGHT
869 */
870 public static final int META_ALT_ON = 0x02;
871
872 /**
873 * <p>This mask is used to check whether the left ALT meta key is pressed.</p>
874 *
875 * @see #isAltPressed()
876 * @see #getMetaState()
877 * @see #KEYCODE_ALT_LEFT
878 */
879 public static final int META_ALT_LEFT_ON = 0x10;
880
881 /**
882 * <p>This mask is used to check whether the right the ALT meta key is pressed.</p>
883 *
884 * @see #isAltPressed()
885 * @see #getMetaState()
886 * @see #KEYCODE_ALT_RIGHT
887 */
888 public static final int META_ALT_RIGHT_ON = 0x20;
889
890 /**
891 * <p>This mask is used to check whether one of the SHIFT meta keys is pressed.</p>
892 *
893 * @see #isShiftPressed()
894 * @see #getMetaState()
895 * @see #KEYCODE_SHIFT_LEFT
896 * @see #KEYCODE_SHIFT_RIGHT
897 */
898 public static final int META_SHIFT_ON = 0x1;
899
900 /**
901 * <p>This mask is used to check whether the left SHIFT meta key is pressed.</p>
902 *
903 * @see #isShiftPressed()
904 * @see #getMetaState()
905 * @see #KEYCODE_SHIFT_LEFT
906 */
907 public static final int META_SHIFT_LEFT_ON = 0x40;
908
909 /**
910 * <p>This mask is used to check whether the right SHIFT meta key is pressed.</p>
911 *
912 * @see #isShiftPressed()
913 * @see #getMetaState()
914 * @see #KEYCODE_SHIFT_RIGHT
915 */
916 public static final int META_SHIFT_RIGHT_ON = 0x80;
917
918 /**
919 * <p>This mask is used to check whether the SYM meta key is pressed.</p>
920 *
921 * @see #isSymPressed()
922 * @see #getMetaState()
923 */
924 public static final int META_SYM_ON = 0x4;
925
926 /**
Jeff Brown497a92c2010-09-12 17:55:08 -0700927 * <p>This mask is used to check whether the FUNCTION meta key is pressed.</p>
928 *
929 * @see #isFunctionPressed()
930 * @see #getMetaState()
931 */
932 public static final int META_FUNCTION_ON = 0x8;
933
934 /**
935 * <p>This mask is used to check whether one of the CTRL meta keys is pressed.</p>
936 *
937 * @see #isCtrlPressed()
938 * @see #getMetaState()
939 * @see #KEYCODE_CTRL_LEFT
940 * @see #KEYCODE_CTRL_RIGHT
941 */
942 public static final int META_CTRL_ON = 0x1000;
943
944 /**
945 * <p>This mask is used to check whether the left CTRL meta key is pressed.</p>
946 *
947 * @see #isCtrlPressed()
948 * @see #getMetaState()
949 * @see #KEYCODE_CTRL_LEFT
950 */
951 public static final int META_CTRL_LEFT_ON = 0x2000;
952
953 /**
954 * <p>This mask is used to check whether the right CTRL meta key is pressed.</p>
955 *
956 * @see #isCtrlPressed()
957 * @see #getMetaState()
958 * @see #KEYCODE_CTRL_RIGHT
959 */
960 public static final int META_CTRL_RIGHT_ON = 0x4000;
961
962 /**
963 * <p>This mask is used to check whether one of the META meta keys is pressed.</p>
964 *
965 * @see #isMetaPressed()
966 * @see #getMetaState()
967 * @see #KEYCODE_META_LEFT
968 * @see #KEYCODE_META_RIGHT
969 */
970 public static final int META_META_ON = 0x10000;
971
972 /**
973 * <p>This mask is used to check whether the left META meta key is pressed.</p>
974 *
975 * @see #isMetaPressed()
976 * @see #getMetaState()
977 * @see #KEYCODE_META_LEFT
978 */
979 public static final int META_META_LEFT_ON = 0x20000;
980
981 /**
982 * <p>This mask is used to check whether the right META meta key is pressed.</p>
983 *
984 * @see #isMetaPressed()
985 * @see #getMetaState()
986 * @see #KEYCODE_META_RIGHT
987 */
988 public static final int META_META_RIGHT_ON = 0x40000;
989
990 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -0700991 * <p>This mask is used to check whether the CAPS LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -0700992 *
Jeff Brown51e7fe72010-10-29 22:19:53 -0700993 * @see #isCapsLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -0700994 * @see #getMetaState()
995 * @see #KEYCODE_CAPS_LOCK
996 */
Jeff Brown51e7fe72010-10-29 22:19:53 -0700997 public static final int META_CAPS_LOCK_ON = 0x100000;
Jeff Brown497a92c2010-09-12 17:55:08 -0700998
999 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07001000 * <p>This mask is used to check whether the NUM LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07001001 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07001002 * @see #isNumLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -07001003 * @see #getMetaState()
1004 * @see #KEYCODE_NUM_LOCK
1005 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07001006 public static final int META_NUM_LOCK_ON = 0x200000;
Jeff Brown497a92c2010-09-12 17:55:08 -07001007
1008 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07001009 * <p>This mask is used to check whether the SCROLL LOCK meta key is on.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07001010 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07001011 * @see #isScrollLockOn()
Jeff Brown497a92c2010-09-12 17:55:08 -07001012 * @see #getMetaState()
1013 * @see #KEYCODE_SCROLL_LOCK
1014 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07001015 public static final int META_SCROLL_LOCK_ON = 0x400000;
Jeff Brown497a92c2010-09-12 17:55:08 -07001016
Jeff Brown64da12a2011-01-04 19:57:47 -08001017 /**
1018 * This mask is a combination of {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON}
1019 * and {@link #META_SHIFT_RIGHT_ON}.
1020 */
Jeff Brownc1df9072010-12-21 16:38:50 -08001021 public static final int META_SHIFT_MASK = META_SHIFT_ON
1022 | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON;
1023
Jeff Brown64da12a2011-01-04 19:57:47 -08001024 /**
1025 * This mask is a combination of {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON}
1026 * and {@link #META_ALT_RIGHT_ON}.
1027 */
Jeff Brownc1df9072010-12-21 16:38:50 -08001028 public static final int META_ALT_MASK = META_ALT_ON
1029 | META_ALT_LEFT_ON | META_ALT_RIGHT_ON;
1030
Jeff Brown64da12a2011-01-04 19:57:47 -08001031 /**
1032 * This mask is a combination of {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON}
1033 * and {@link #META_CTRL_RIGHT_ON}.
1034 */
Jeff Brownc1df9072010-12-21 16:38:50 -08001035 public static final int META_CTRL_MASK = META_CTRL_ON
1036 | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON;
1037
Jeff Brown64da12a2011-01-04 19:57:47 -08001038 /**
1039 * This mask is a combination of {@link #META_META_ON}, {@link #META_META_LEFT_ON}
1040 * and {@link #META_META_RIGHT_ON}.
1041 */
1042 public static final int META_META_MASK = META_META_ON
Jeff Brownc1df9072010-12-21 16:38:50 -08001043 | META_META_LEFT_ON | META_META_RIGHT_ON;
1044
Jeff Brown497a92c2010-09-12 17:55:08 -07001045 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 * This mask is set if the device woke because of this key event.
Jeff Brown037c33e2014-04-09 00:31:55 -07001047 *
1048 * @deprecated This flag will never be set by the system since the system
1049 * consumes all wake keys itself.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 */
Jeff Brown037c33e2014-04-09 00:31:55 -07001051 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 public static final int FLAG_WOKE_HERE = 0x1;
RoboErik01fe6612014-02-13 14:19:04 -08001053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 /**
1055 * This mask is set if the key event was generated by a software keyboard.
1056 */
1057 public static final int FLAG_SOFT_KEYBOARD = 0x2;
RoboErik01fe6612014-02-13 14:19:04 -08001058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 /**
1060 * This mask is set if we don't want the key event to cause us to leave
1061 * touch mode.
1062 */
1063 public static final int FLAG_KEEP_TOUCH_MODE = 0x4;
RoboErik01fe6612014-02-13 14:19:04 -08001064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001066 * This mask is set if an event was known to come from a trusted part
1067 * of the system. That is, the event is known to come from the user,
1068 * and could not have been spoofed by a third party component.
1069 */
1070 public static final int FLAG_FROM_SYSTEM = 0x8;
RoboErik01fe6612014-02-13 14:19:04 -08001071
The Android Open Source Project10592532009-03-18 17:39:46 -07001072 /**
1073 * This mask is used for compatibility, to identify enter keys that are
1074 * coming from an IME whose enter key has been auto-labelled "next" or
1075 * "done". This allows TextView to dispatch these as normal enter keys
1076 * for old applications, but still do the appropriate action when
1077 * receiving them.
1078 */
1079 public static final int FLAG_EDITOR_ACTION = 0x10;
RoboErik01fe6612014-02-13 14:19:04 -08001080
The Android Open Source Project10592532009-03-18 17:39:46 -07001081 /**
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07001082 * When associated with up key events, this indicates that the key press
1083 * has been canceled. Typically this is used with virtual touch screen
1084 * keys, where the user can slide from the virtual key area on to the
1085 * display: in that case, the application will receive a canceled up
1086 * event and should not perform the action normally associated with the
1087 * key. Note that for this to work, the application can not perform an
1088 * action for a key until it receives an up or the long press timeout has
RoboErik01fe6612014-02-13 14:19:04 -08001089 * expired.
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07001090 */
1091 public static final int FLAG_CANCELED = 0x20;
RoboErik01fe6612014-02-13 14:19:04 -08001092
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07001093 /**
1094 * This key event was generated by a virtual (on-screen) hard key area.
1095 * Typically this is an area of the touchscreen, outside of the regular
1096 * display, dedicated to "hardware" buttons.
1097 */
1098 public static final int FLAG_VIRTUAL_HARD_KEY = 0x40;
RoboErik01fe6612014-02-13 14:19:04 -08001099
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07001100 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001101 * This flag is set for the first key repeat that occurs after the
1102 * long press timeout.
1103 */
1104 public static final int FLAG_LONG_PRESS = 0x80;
RoboErik01fe6612014-02-13 14:19:04 -08001105
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001106 /**
1107 * Set when a key event has {@link #FLAG_CANCELED} set because a long
RoboErik01fe6612014-02-13 14:19:04 -08001108 * press action was executed while it was down.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001109 */
1110 public static final int FLAG_CANCELED_LONG_PRESS = 0x100;
RoboErik01fe6612014-02-13 14:19:04 -08001111
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001112 /**
1113 * Set for {@link #ACTION_UP} when this event's key code is still being
1114 * tracked from its initial down. That is, somebody requested that tracking
1115 * started on the key down and a long press has not caused
1116 * the tracking to be canceled.
1117 */
1118 public static final int FLAG_TRACKING = 0x200;
Jeff Brown49ed71d2010-12-06 17:13:33 -08001119
1120 /**
1121 * Set when a key event has been synthesized to implement default behavior
1122 * for an event that the application did not handle.
1123 * Fallback key events are generated by unhandled trackball motions
1124 * (to emulate a directional keypad) and by certain unhandled key presses
1125 * that are declared in the key map (such as special function numeric keypad
1126 * keys when numlock is off).
1127 */
1128 public static final int FLAG_FALLBACK = 0x400;
1129
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001130 /**
Michael Wrighta44dd262013-04-10 21:12:00 -07001131 * Signifies that the key is being predispatched.
1132 * @hide
1133 */
1134 public static final int FLAG_PREDISPATCH = 0x20000000;
1135
1136 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001137 * Private control to determine when an app is tracking a key sequence.
1138 * @hide
1139 */
1140 public static final int FLAG_START_TRACKING = 0x40000000;
Jeff Brown21bc5c92011-02-28 18:27:14 -08001141
1142 /**
1143 * Private flag that indicates when the system has detected that this key event
1144 * may be inconsistent with respect to the sequence of previously delivered key events,
1145 * such as when a key up event is sent but the key was not down.
1146 *
1147 * @hide
1148 * @see #isTainted
1149 * @see #setTainted
1150 */
1151 public static final int FLAG_TAINTED = 0x80000000;
1152
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001153 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 * Returns the maximum keycode.
1155 */
1156 public static int getMaxKeyCode() {
1157 return LAST_KEYCODE;
1158 }
1159
1160 /**
1161 * Get the character that is produced by putting accent on the character
1162 * c.
1163 * For example, getDeadChar('`', 'e') returns &egrave;.
1164 */
1165 public static int getDeadChar(int accent, int c) {
1166 return KeyCharacterMap.getDeadChar(accent, c);
1167 }
RoboErik01fe6612014-02-13 14:19:04 -08001168
Dianne Hackborn8d374262009-09-14 21:21:52 -07001169 static final boolean DEBUG = false;
1170 static final String TAG = "KeyEvent";
Jeff Brown1f245102010-11-18 20:53:46 -08001171
1172 private static final int MAX_RECYCLED = 10;
1173 private static final Object gRecyclerLock = new Object();
1174 private static int gRecyclerUsed;
1175 private static KeyEvent gRecyclerTop;
1176
1177 private KeyEvent mNext;
Jeff Brown1f245102010-11-18 20:53:46 -08001178
Jeff Brown91c69ab2011-02-14 17:03:18 -08001179 private int mDeviceId;
1180 private int mSource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 private int mMetaState;
1182 private int mAction;
1183 private int mKeyCode;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001184 private int mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 private int mRepeatCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 private int mFlags;
1187 private long mDownTime;
1188 private long mEventTime;
1189 private String mCharacters;
1190
1191 public interface Callback {
1192 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001193 * Called when a key down event has occurred. If you return true,
1194 * you can first call {@link KeyEvent#startTracking()
1195 * KeyEvent.startTracking()} to have the framework track the event
1196 * through its {@link #onKeyUp(int, KeyEvent)} and also call your
1197 * {@link #onKeyLongPress(int, KeyEvent)} if it occurs.
RoboErik01fe6612014-02-13 14:19:04 -08001198 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 * @param keyCode The value in event.getKeyCode().
1200 * @param event Description of the key event.
RoboErik01fe6612014-02-13 14:19:04 -08001201 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 * @return If you handled the event, return true. If you want to allow
1203 * the event to be handled by the next receiver, return false.
1204 */
1205 boolean onKeyDown(int keyCode, KeyEvent event);
1206
1207 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001208 * Called when a long press has occurred. If you return true,
1209 * the final key up will have {@link KeyEvent#FLAG_CANCELED} and
1210 * {@link KeyEvent#FLAG_CANCELED_LONG_PRESS} set. Note that in
1211 * order to receive this callback, someone in the event change
1212 * <em>must</em> return true from {@link #onKeyDown} <em>and</em>
1213 * call {@link KeyEvent#startTracking()} on the event.
RoboErik01fe6612014-02-13 14:19:04 -08001214 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001215 * @param keyCode The value in event.getKeyCode().
1216 * @param event Description of the key event.
RoboErik01fe6612014-02-13 14:19:04 -08001217 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001218 * @return If you handled the event, return true. If you want to allow
1219 * the event to be handled by the next receiver, return false.
1220 */
1221 boolean onKeyLongPress(int keyCode, KeyEvent event);
1222
1223 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 * Called when a key up event has occurred.
RoboErik01fe6612014-02-13 14:19:04 -08001225 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 * @param keyCode The value in event.getKeyCode().
1227 * @param event Description of the key event.
RoboErik01fe6612014-02-13 14:19:04 -08001228 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 * @return If you handled the event, return true. If you want to allow
1230 * the event to be handled by the next receiver, return false.
1231 */
1232 boolean onKeyUp(int keyCode, KeyEvent event);
1233
1234 /**
1235 * Called when multiple down/up pairs of the same key have occurred
1236 * in a row.
RoboErik01fe6612014-02-13 14:19:04 -08001237 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 * @param keyCode The value in event.getKeyCode().
1239 * @param count Number of pairs as returned by event.getRepeatCount().
1240 * @param event Description of the key event.
RoboErik01fe6612014-02-13 14:19:04 -08001241 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 * @return If you handled the event, return true. If you want to allow
1243 * the event to be handled by the next receiver, return false.
1244 */
1245 boolean onKeyMultiple(int keyCode, int count, KeyEvent event);
1246 }
1247
Michael Wright337d9d22014-04-22 15:03:48 -07001248 private static native String nativeKeyCodeToString(int keyCode);
1249 private static native int nativeKeyCodeFromString(String keyCode);
Jeff Brown497a92c2010-09-12 17:55:08 -07001250
Jeff Brown1f245102010-11-18 20:53:46 -08001251 private KeyEvent() {
1252 }
1253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 /**
1255 * Create a new key event.
RoboErik01fe6612014-02-13 14:19:04 -08001256 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 * @param action Action code: either {@link #ACTION_DOWN},
1258 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1259 * @param code The key code.
1260 */
1261 public KeyEvent(int action, int code) {
1262 mAction = action;
1263 mKeyCode = code;
1264 mRepeatCount = 0;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001265 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 }
1267
1268 /**
1269 * Create a new key event.
RoboErik01fe6612014-02-13 14:19:04 -08001270 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1272 * at which this key code originally went down.
1273 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1274 * at which this event happened.
1275 * @param action Action code: either {@link #ACTION_DOWN},
1276 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1277 * @param code The key code.
1278 * @param repeat A repeat count for down events (> 0 if this is after the
1279 * initial down) or event count for multiple events.
1280 */
1281 public KeyEvent(long downTime, long eventTime, int action,
1282 int code, int repeat) {
1283 mDownTime = downTime;
1284 mEventTime = eventTime;
1285 mAction = action;
1286 mKeyCode = code;
1287 mRepeatCount = repeat;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001288 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 }
1290
1291 /**
1292 * Create a new key event.
RoboErik01fe6612014-02-13 14:19:04 -08001293 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1295 * at which this key code originally went down.
1296 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1297 * at which this event happened.
1298 * @param action Action code: either {@link #ACTION_DOWN},
1299 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1300 * @param code The key code.
1301 * @param repeat A repeat count for down events (> 0 if this is after the
1302 * initial down) or event count for multiple events.
1303 * @param metaState Flags indicating which meta keys are currently pressed.
1304 */
1305 public KeyEvent(long downTime, long eventTime, int action,
1306 int code, int repeat, int metaState) {
1307 mDownTime = downTime;
1308 mEventTime = eventTime;
1309 mAction = action;
1310 mKeyCode = code;
1311 mRepeatCount = repeat;
1312 mMetaState = metaState;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001313 mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 }
1315
1316 /**
1317 * Create a new key event.
RoboErik01fe6612014-02-13 14:19:04 -08001318 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1320 * at which this key code originally went down.
1321 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1322 * at which this event happened.
1323 * @param action Action code: either {@link #ACTION_DOWN},
1324 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1325 * @param code The key code.
1326 * @param repeat A repeat count for down events (> 0 if this is after the
1327 * initial down) or event count for multiple events.
1328 * @param metaState Flags indicating which meta keys are currently pressed.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001329 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 * @param scancode Raw device scan code of the event.
1331 */
1332 public KeyEvent(long downTime, long eventTime, int action,
1333 int code, int repeat, int metaState,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001334 int deviceId, int scancode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 mDownTime = downTime;
1336 mEventTime = eventTime;
1337 mAction = action;
1338 mKeyCode = code;
1339 mRepeatCount = repeat;
1340 mMetaState = metaState;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001341 mDeviceId = deviceId;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001342 mScanCode = scancode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 }
1344
1345 /**
1346 * Create a new key event.
RoboErik01fe6612014-02-13 14:19:04 -08001347 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1349 * at which this key code originally went down.
1350 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1351 * at which this event happened.
1352 * @param action Action code: either {@link #ACTION_DOWN},
1353 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1354 * @param code The key code.
1355 * @param repeat A repeat count for down events (> 0 if this is after the
1356 * initial down) or event count for multiple events.
1357 * @param metaState Flags indicating which meta keys are currently pressed.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001358 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 * @param scancode Raw device scan code of the event.
1360 * @param flags The flags for this key event
1361 */
1362 public KeyEvent(long downTime, long eventTime, int action,
1363 int code, int repeat, int metaState,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001364 int deviceId, int scancode, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 mDownTime = downTime;
1366 mEventTime = eventTime;
1367 mAction = action;
1368 mKeyCode = code;
1369 mRepeatCount = repeat;
1370 mMetaState = metaState;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001371 mDeviceId = deviceId;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001372 mScanCode = scancode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 mFlags = flags;
1374 }
1375
1376 /**
Jeff Brownc5ed5912010-07-14 18:48:53 -07001377 * Create a new key event.
RoboErik01fe6612014-02-13 14:19:04 -08001378 *
Jeff Brownc5ed5912010-07-14 18:48:53 -07001379 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
1380 * at which this key code originally went down.
1381 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
1382 * at which this event happened.
1383 * @param action Action code: either {@link #ACTION_DOWN},
1384 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
1385 * @param code The key code.
1386 * @param repeat A repeat count for down events (> 0 if this is after the
1387 * initial down) or event count for multiple events.
1388 * @param metaState Flags indicating which meta keys are currently pressed.
1389 * @param deviceId The device ID that generated the key event.
1390 * @param scancode Raw device scan code of the event.
1391 * @param flags The flags for this key event
1392 * @param source The input source such as {@link InputDevice#SOURCE_KEYBOARD}.
1393 */
1394 public KeyEvent(long downTime, long eventTime, int action,
1395 int code, int repeat, int metaState,
1396 int deviceId, int scancode, int flags, int source) {
1397 mDownTime = downTime;
1398 mEventTime = eventTime;
1399 mAction = action;
1400 mKeyCode = code;
1401 mRepeatCount = repeat;
1402 mMetaState = metaState;
1403 mDeviceId = deviceId;
1404 mScanCode = scancode;
1405 mFlags = flags;
1406 mSource = source;
1407 }
1408
1409 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 * Create a new key event for a string of characters. The key code,
Jeff Brownc5ed5912010-07-14 18:48:53 -07001411 * action, repeat count and source will automatically be set to
1412 * {@link #KEYCODE_UNKNOWN}, {@link #ACTION_MULTIPLE}, 0, and
1413 * {@link InputDevice#SOURCE_KEYBOARD} for you.
RoboErik01fe6612014-02-13 14:19:04 -08001414 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 * @param time The time (in {@link android.os.SystemClock#uptimeMillis})
1416 * at which this event occured.
1417 * @param characters The string of characters.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001418 * @param deviceId The device ID that generated the key event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 * @param flags The flags for this key event
1420 */
Jeff Brownc5ed5912010-07-14 18:48:53 -07001421 public KeyEvent(long time, String characters, int deviceId, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 mDownTime = time;
1423 mEventTime = time;
1424 mCharacters = characters;
1425 mAction = ACTION_MULTIPLE;
1426 mKeyCode = KEYCODE_UNKNOWN;
1427 mRepeatCount = 0;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001428 mDeviceId = deviceId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 mFlags = flags;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001430 mSource = InputDevice.SOURCE_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 }
1432
1433 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001434 * Make an exact copy of an existing key event.
1435 */
1436 public KeyEvent(KeyEvent origEvent) {
1437 mDownTime = origEvent.mDownTime;
1438 mEventTime = origEvent.mEventTime;
1439 mAction = origEvent.mAction;
1440 mKeyCode = origEvent.mKeyCode;
1441 mRepeatCount = origEvent.mRepeatCount;
1442 mMetaState = origEvent.mMetaState;
1443 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001444 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001445 mScanCode = origEvent.mScanCode;
The Android Open Source Project10592532009-03-18 17:39:46 -07001446 mFlags = origEvent.mFlags;
1447 mCharacters = origEvent.mCharacters;
1448 }
1449
1450 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 * Copy an existing key event, modifying its time and repeat count.
RoboErik01fe6612014-02-13 14:19:04 -08001452 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001453 * @deprecated Use {@link #changeTimeRepeat(KeyEvent, long, int)}
1454 * instead.
RoboErik01fe6612014-02-13 14:19:04 -08001455 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 * @param origEvent The existing event to be copied.
1457 * @param eventTime The new event time
1458 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1459 * @param newRepeat The new repeat count of the event.
1460 */
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001461 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 public KeyEvent(KeyEvent origEvent, long eventTime, int newRepeat) {
1463 mDownTime = origEvent.mDownTime;
1464 mEventTime = eventTime;
1465 mAction = origEvent.mAction;
1466 mKeyCode = origEvent.mKeyCode;
1467 mRepeatCount = newRepeat;
1468 mMetaState = origEvent.mMetaState;
1469 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001470 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001471 mScanCode = origEvent.mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 mFlags = origEvent.mFlags;
1473 mCharacters = origEvent.mCharacters;
1474 }
1475
Jeff Brown1f245102010-11-18 20:53:46 -08001476 private static KeyEvent obtain() {
1477 final KeyEvent ev;
1478 synchronized (gRecyclerLock) {
1479 ev = gRecyclerTop;
1480 if (ev == null) {
1481 return new KeyEvent();
1482 }
1483 gRecyclerTop = ev.mNext;
1484 gRecyclerUsed -= 1;
1485 }
Jeff Brown1f245102010-11-18 20:53:46 -08001486 ev.mNext = null;
Jeff Brown32cbc38552011-12-01 14:01:49 -08001487 ev.prepareForReuse();
Jeff Brown1f245102010-11-18 20:53:46 -08001488 return ev;
1489 }
1490
1491 /**
1492 * Obtains a (potentially recycled) key event.
1493 *
1494 * @hide
1495 */
1496 public static KeyEvent obtain(long downTime, long eventTime, int action,
1497 int code, int repeat, int metaState,
1498 int deviceId, int scancode, int flags, int source, String characters) {
1499 KeyEvent ev = obtain();
1500 ev.mDownTime = downTime;
1501 ev.mEventTime = eventTime;
1502 ev.mAction = action;
1503 ev.mKeyCode = code;
1504 ev.mRepeatCount = repeat;
1505 ev.mMetaState = metaState;
1506 ev.mDeviceId = deviceId;
1507 ev.mScanCode = scancode;
1508 ev.mFlags = flags;
1509 ev.mSource = source;
1510 ev.mCharacters = characters;
1511 return ev;
1512 }
1513
1514 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08001515 * Obtains a (potentially recycled) copy of another key event.
1516 *
1517 * @hide
1518 */
1519 public static KeyEvent obtain(KeyEvent other) {
1520 KeyEvent ev = obtain();
1521 ev.mDownTime = other.mDownTime;
1522 ev.mEventTime = other.mEventTime;
1523 ev.mAction = other.mAction;
1524 ev.mKeyCode = other.mKeyCode;
1525 ev.mRepeatCount = other.mRepeatCount;
1526 ev.mMetaState = other.mMetaState;
1527 ev.mDeviceId = other.mDeviceId;
1528 ev.mScanCode = other.mScanCode;
1529 ev.mFlags = other.mFlags;
1530 ev.mSource = other.mSource;
1531 ev.mCharacters = other.mCharacters;
1532 return ev;
1533 }
1534
1535 /** @hide */
1536 @Override
1537 public KeyEvent copy() {
1538 return obtain(this);
1539 }
1540
1541 /**
Jeff Brown1f245102010-11-18 20:53:46 -08001542 * Recycles a key event.
1543 * Key events should only be recycled if they are owned by the system since user
1544 * code expects them to be essentially immutable, "tracking" notwithstanding.
1545 *
1546 * @hide
1547 */
Jeff Brown92cc2d82011-12-02 01:19:47 -08001548 @Override
Jeff Brown1f245102010-11-18 20:53:46 -08001549 public final void recycle() {
Jeff Brown32cbc38552011-12-01 14:01:49 -08001550 super.recycle();
Jeff Brown1f245102010-11-18 20:53:46 -08001551 mCharacters = null;
1552
1553 synchronized (gRecyclerLock) {
1554 if (gRecyclerUsed < MAX_RECYCLED) {
1555 gRecyclerUsed++;
1556 mNext = gRecyclerTop;
1557 gRecyclerTop = this;
1558 }
1559 }
1560 }
1561
Jeff Brown92cc2d82011-12-02 01:19:47 -08001562 /** @hide */
1563 @Override
1564 public final void recycleIfNeededAfterDispatch() {
1565 // Do nothing.
1566 }
1567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001569 * Create a new key event that is the same as the given one, but whose
1570 * event time and repeat count are replaced with the given value.
RoboErik01fe6612014-02-13 14:19:04 -08001571 *
The Android Open Source Project10592532009-03-18 17:39:46 -07001572 * @param event The existing event to be copied. This is not modified.
1573 * @param eventTime The new event time
1574 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1575 * @param newRepeat The new repeat count of the event.
1576 */
1577 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime,
1578 int newRepeat) {
1579 return new KeyEvent(event, eventTime, newRepeat);
1580 }
RoboErik01fe6612014-02-13 14:19:04 -08001581
The Android Open Source Project10592532009-03-18 17:39:46 -07001582 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001583 * Create a new key event that is the same as the given one, but whose
1584 * event time and repeat count are replaced with the given value.
RoboErik01fe6612014-02-13 14:19:04 -08001585 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001586 * @param event The existing event to be copied. This is not modified.
1587 * @param eventTime The new event time
1588 * (in {@link android.os.SystemClock#uptimeMillis}) of the event.
1589 * @param newRepeat The new repeat count of the event.
1590 * @param newFlags New flags for the event, replacing the entire value
1591 * in the original event.
1592 */
1593 public static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime,
1594 int newRepeat, int newFlags) {
1595 KeyEvent ret = new KeyEvent(event);
1596 ret.mEventTime = eventTime;
1597 ret.mRepeatCount = newRepeat;
1598 ret.mFlags = newFlags;
1599 return ret;
1600 }
RoboErik01fe6612014-02-13 14:19:04 -08001601
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001602 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 * Copy an existing key event, modifying its action.
RoboErik01fe6612014-02-13 14:19:04 -08001604 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 * @param origEvent The existing event to be copied.
1606 * @param action The new action code of the event.
1607 */
The Android Open Source Project10592532009-03-18 17:39:46 -07001608 private KeyEvent(KeyEvent origEvent, int action) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 mDownTime = origEvent.mDownTime;
1610 mEventTime = origEvent.mEventTime;
1611 mAction = action;
1612 mKeyCode = origEvent.mKeyCode;
1613 mRepeatCount = origEvent.mRepeatCount;
1614 mMetaState = origEvent.mMetaState;
1615 mDeviceId = origEvent.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001616 mSource = origEvent.mSource;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001617 mScanCode = origEvent.mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 mFlags = origEvent.mFlags;
1619 // Don't copy mCharacters, since one way or the other we'll lose it
1620 // when changing the action.
1621 }
1622
1623 /**
The Android Open Source Project10592532009-03-18 17:39:46 -07001624 * Create a new key event that is the same as the given one, but whose
1625 * action is replaced with the given value.
RoboErik01fe6612014-02-13 14:19:04 -08001626 *
The Android Open Source Project10592532009-03-18 17:39:46 -07001627 * @param event The existing event to be copied. This is not modified.
1628 * @param action The new action code of the event.
1629 */
1630 public static KeyEvent changeAction(KeyEvent event, int action) {
1631 return new KeyEvent(event, action);
1632 }
RoboErik01fe6612014-02-13 14:19:04 -08001633
The Android Open Source Project10592532009-03-18 17:39:46 -07001634 /**
1635 * Create a new key event that is the same as the given one, but whose
1636 * flags are replaced with the given value.
RoboErik01fe6612014-02-13 14:19:04 -08001637 *
The Android Open Source Project10592532009-03-18 17:39:46 -07001638 * @param event The existing event to be copied. This is not modified.
1639 * @param flags The new flags constant.
1640 */
1641 public static KeyEvent changeFlags(KeyEvent event, int flags) {
1642 event = new KeyEvent(event);
1643 event.mFlags = flags;
1644 return event;
1645 }
Jeff Brown21bc5c92011-02-28 18:27:14 -08001646
1647 /** @hide */
1648 @Override
1649 public final boolean isTainted() {
1650 return (mFlags & FLAG_TAINTED) != 0;
1651 }
1652
1653 /** @hide */
1654 @Override
1655 public final void setTainted(boolean tainted) {
1656 mFlags = tainted ? mFlags | FLAG_TAINTED : mFlags & ~FLAG_TAINTED;
1657 }
1658
The Android Open Source Project10592532009-03-18 17:39:46 -07001659 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 * Don't use in new code, instead explicitly check
1661 * {@link #getAction()}.
RoboErik01fe6612014-02-13 14:19:04 -08001662 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 * @return If the action is ACTION_DOWN, returns true; else false.
1664 *
1665 * @deprecated
1666 * @hide
1667 */
1668 @Deprecated public final boolean isDown() {
1669 return mAction == ACTION_DOWN;
1670 }
1671
Michael Wright337d9d22014-04-22 15:03:48 -07001672 /** Is this a system key? System keys can not be used for menu shortcuts.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 */
1674 public final boolean isSystem() {
Michael Wright337d9d22014-04-22 15:03:48 -07001675 return isSystemKey(mKeyCode);
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -07001676 }
1677
1678 /** @hide */
Michael Wright337d9d22014-04-22 15:03:48 -07001679 public final boolean isWakeKey() {
1680 return isWakeKey(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 }
1682
Jeff Brown6f2fba42011-02-19 01:08:02 -08001683 /**
1684 * Returns true if the specified keycode is a gamepad button.
1685 * @return True if the keycode is a gamepad button, such as {@link #KEYCODE_BUTTON_A}.
1686 */
1687 public static final boolean isGamepadButton(int keyCode) {
1688 switch (keyCode) {
1689 case KeyEvent.KEYCODE_BUTTON_A:
1690 case KeyEvent.KEYCODE_BUTTON_B:
1691 case KeyEvent.KEYCODE_BUTTON_C:
1692 case KeyEvent.KEYCODE_BUTTON_X:
1693 case KeyEvent.KEYCODE_BUTTON_Y:
1694 case KeyEvent.KEYCODE_BUTTON_Z:
1695 case KeyEvent.KEYCODE_BUTTON_L1:
1696 case KeyEvent.KEYCODE_BUTTON_R1:
1697 case KeyEvent.KEYCODE_BUTTON_L2:
1698 case KeyEvent.KEYCODE_BUTTON_R2:
1699 case KeyEvent.KEYCODE_BUTTON_THUMBL:
1700 case KeyEvent.KEYCODE_BUTTON_THUMBR:
1701 case KeyEvent.KEYCODE_BUTTON_START:
1702 case KeyEvent.KEYCODE_BUTTON_SELECT:
1703 case KeyEvent.KEYCODE_BUTTON_MODE:
1704 case KeyEvent.KEYCODE_BUTTON_1:
1705 case KeyEvent.KEYCODE_BUTTON_2:
1706 case KeyEvent.KEYCODE_BUTTON_3:
1707 case KeyEvent.KEYCODE_BUTTON_4:
1708 case KeyEvent.KEYCODE_BUTTON_5:
1709 case KeyEvent.KEYCODE_BUTTON_6:
1710 case KeyEvent.KEYCODE_BUTTON_7:
1711 case KeyEvent.KEYCODE_BUTTON_8:
1712 case KeyEvent.KEYCODE_BUTTON_9:
1713 case KeyEvent.KEYCODE_BUTTON_10:
1714 case KeyEvent.KEYCODE_BUTTON_11:
1715 case KeyEvent.KEYCODE_BUTTON_12:
1716 case KeyEvent.KEYCODE_BUTTON_13:
1717 case KeyEvent.KEYCODE_BUTTON_14:
1718 case KeyEvent.KEYCODE_BUTTON_15:
1719 case KeyEvent.KEYCODE_BUTTON_16:
1720 return true;
1721 default:
1722 return false;
1723 }
1724 }
1725
Michael Wright25b0c302013-07-10 12:54:06 -07001726 /** Whether key will, by default, trigger a click on the focused view.
1727 * @hide
1728 */
1729 public static final boolean isConfirmKey(int keyCode) {
1730 switch (keyCode) {
1731 case KeyEvent.KEYCODE_DPAD_CENTER:
1732 case KeyEvent.KEYCODE_ENTER:
1733 return true;
1734 default:
1735 return false;
1736 }
1737 }
1738
RoboErik01fe6612014-02-13 14:19:04 -08001739 /**
1740 * Whether this key is a media key, which can be send to apps that are
1741 * interested in media key events.
1742 *
1743 * @hide
1744 */
1745 public static final boolean isMediaKey(int keyCode) {
1746 switch (keyCode) {
1747 case KeyEvent.KEYCODE_MEDIA_PLAY:
1748 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1749 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1750 case KeyEvent.KEYCODE_MUTE:
1751 case KeyEvent.KEYCODE_HEADSETHOOK:
1752 case KeyEvent.KEYCODE_MEDIA_STOP:
1753 case KeyEvent.KEYCODE_MEDIA_NEXT:
1754 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1755 case KeyEvent.KEYCODE_MEDIA_REWIND:
1756 case KeyEvent.KEYCODE_MEDIA_RECORD:
1757 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1758 return true;
1759 }
1760 return false;
1761 }
1762
Michael Wright337d9d22014-04-22 15:03:48 -07001763
1764 /** Is this a system key? System keys can not be used for menu shortcuts.
1765 * @hide
1766 */
1767 public static final boolean isSystemKey(int keyCode) {
1768 switch (keyCode) {
1769 case KeyEvent.KEYCODE_MENU:
1770 case KeyEvent.KEYCODE_SOFT_RIGHT:
1771 case KeyEvent.KEYCODE_HOME:
1772 case KeyEvent.KEYCODE_BACK:
1773 case KeyEvent.KEYCODE_CALL:
1774 case KeyEvent.KEYCODE_ENDCALL:
1775 case KeyEvent.KEYCODE_VOLUME_UP:
1776 case KeyEvent.KEYCODE_VOLUME_DOWN:
1777 case KeyEvent.KEYCODE_VOLUME_MUTE:
1778 case KeyEvent.KEYCODE_MUTE:
1779 case KeyEvent.KEYCODE_POWER:
1780 case KeyEvent.KEYCODE_HEADSETHOOK:
1781 case KeyEvent.KEYCODE_MEDIA_PLAY:
1782 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1783 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1784 case KeyEvent.KEYCODE_MEDIA_STOP:
1785 case KeyEvent.KEYCODE_MEDIA_NEXT:
1786 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1787 case KeyEvent.KEYCODE_MEDIA_REWIND:
1788 case KeyEvent.KEYCODE_MEDIA_RECORD:
1789 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1790 case KeyEvent.KEYCODE_CAMERA:
1791 case KeyEvent.KEYCODE_FOCUS:
1792 case KeyEvent.KEYCODE_SEARCH:
1793 case KeyEvent.KEYCODE_BRIGHTNESS_DOWN:
1794 case KeyEvent.KEYCODE_BRIGHTNESS_UP:
1795 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
Michael Wright337d9d22014-04-22 15:03:48 -07001796 return true;
1797 }
1798
1799 return false;
1800 }
1801
1802 /** @hide */
1803 public static final boolean isWakeKey(int keyCode) {
1804 switch (keyCode) {
1805 case KeyEvent.KEYCODE_BACK:
1806 case KeyEvent.KEYCODE_POWER:
1807 case KeyEvent.KEYCODE_MENU:
1808 case KeyEvent.KEYCODE_SLEEP:
1809 case KeyEvent.KEYCODE_WAKEUP:
Michael Wright8ac485a2014-06-04 12:38:18 -07001810 case KeyEvent.KEYCODE_PAIRING:
Michael Wright337d9d22014-04-22 15:03:48 -07001811 return true;
1812 }
1813 return false;
1814 }
1815
Michael Wrightce0c13a2014-07-30 10:49:21 -07001816 /** @hide */
1817 public static final boolean isMetaKey(int keyCode) {
1818 return keyCode == KeyEvent.KEYCODE_META_LEFT || keyCode == KeyEvent.KEYCODE_META_RIGHT;
1819 }
1820
Jeff Brown91c69ab2011-02-14 17:03:18 -08001821 /** {@inheritDoc} */
1822 @Override
1823 public final int getDeviceId() {
1824 return mDeviceId;
1825 }
1826
1827 /** {@inheritDoc} */
1828 @Override
1829 public final int getSource() {
1830 return mSource;
1831 }
1832
1833 /** {@inheritDoc} */
1834 @Override
1835 public final void setSource(int source) {
1836 mSource = source;
1837 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838
1839 /**
1840 * <p>Returns the state of the meta keys.</p>
1841 *
1842 * @return an integer in which each bit set to 1 represents a pressed
1843 * meta key
1844 *
1845 * @see #isAltPressed()
1846 * @see #isShiftPressed()
1847 * @see #isSymPressed()
Jeff Brown497a92c2010-09-12 17:55:08 -07001848 * @see #isCtrlPressed()
1849 * @see #isMetaPressed()
1850 * @see #isFunctionPressed()
Jeff Brown51e7fe72010-10-29 22:19:53 -07001851 * @see #isCapsLockOn()
1852 * @see #isNumLockOn()
1853 * @see #isScrollLockOn()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 * @see #META_ALT_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001855 * @see #META_ALT_LEFT_ON
1856 * @see #META_ALT_RIGHT_ON
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 * @see #META_SHIFT_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001858 * @see #META_SHIFT_LEFT_ON
1859 * @see #META_SHIFT_RIGHT_ON
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 * @see #META_SYM_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07001861 * @see #META_FUNCTION_ON
1862 * @see #META_CTRL_ON
1863 * @see #META_CTRL_LEFT_ON
1864 * @see #META_CTRL_RIGHT_ON
1865 * @see #META_META_ON
1866 * @see #META_META_LEFT_ON
1867 * @see #META_META_RIGHT_ON
Jeff Brown51e7fe72010-10-29 22:19:53 -07001868 * @see #META_CAPS_LOCK_ON
1869 * @see #META_NUM_LOCK_ON
1870 * @see #META_SCROLL_LOCK_ON
Jeff Brown54875002011-04-06 15:33:01 -07001871 * @see #getModifiers
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 */
1873 public final int getMetaState() {
1874 return mMetaState;
1875 }
1876
1877 /**
Jeff Brown54875002011-04-06 15:33:01 -07001878 * Returns the state of the modifier keys.
1879 * <p>
1880 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1881 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1882 * not considered modifier keys. Consequently, this function specifically masks out
1883 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1884 * </p><p>
1885 * The value returned consists of the meta state (from {@link #getMetaState})
1886 * normalized using {@link #normalizeMetaState(int)} and then masked with
1887 * {@link #getModifierMetaStateMask} so that only valid modifier bits are retained.
1888 * </p>
1889 *
1890 * @return An integer in which each bit set to 1 represents a pressed modifier key.
1891 * @see #getMetaState
1892 */
1893 public final int getModifiers() {
1894 return normalizeMetaState(mMetaState) & META_MODIFIER_MASK;
1895 }
1896
1897 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 * Returns the flags for this key event.
1899 *
1900 * @see #FLAG_WOKE_HERE
1901 */
1902 public final int getFlags() {
1903 return mFlags;
1904 }
1905
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001906 // Mask of all modifier key meta states. Specifically excludes locked keys like caps lock.
1907 private static final int META_MODIFIER_MASK =
1908 META_SHIFT_ON | META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON
1909 | META_ALT_ON | META_ALT_LEFT_ON | META_ALT_RIGHT_ON
1910 | META_CTRL_ON | META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON
1911 | META_META_ON | META_META_LEFT_ON | META_META_RIGHT_ON
1912 | META_SYM_ON | META_FUNCTION_ON;
1913
1914 // Mask of all lock key meta states.
1915 private static final int META_LOCK_MASK =
1916 META_CAPS_LOCK_ON | META_NUM_LOCK_ON | META_SCROLL_LOCK_ON;
1917
1918 // Mask of all valid meta states.
1919 private static final int META_ALL_MASK = META_MODIFIER_MASK | META_LOCK_MASK;
1920
1921 // Mask of all synthetic meta states that are reserved for API compatibility with
1922 // historical uses in MetaKeyKeyListener.
1923 private static final int META_SYNTHETIC_MASK =
1924 META_CAP_LOCKED | META_ALT_LOCKED | META_SYM_LOCKED | META_SELECTING;
1925
1926 // Mask of all meta states that are not valid use in specifying a modifier key.
1927 // These bits are known to be used for purposes other than specifying modifiers.
1928 private static final int META_INVALID_MODIFIER_MASK =
1929 META_LOCK_MASK | META_SYNTHETIC_MASK;
1930
1931 /**
1932 * Gets a mask that includes all valid modifier key meta state bits.
1933 * <p>
1934 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1935 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1936 * not considered modifier keys. Consequently, the mask specifically excludes
1937 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
1938 * </p>
1939 *
1940 * @return The modifier meta state mask which is a combination of
1941 * {@link #META_SHIFT_ON}, {@link #META_SHIFT_LEFT_ON}, {@link #META_SHIFT_RIGHT_ON},
1942 * {@link #META_ALT_ON}, {@link #META_ALT_LEFT_ON}, {@link #META_ALT_RIGHT_ON},
1943 * {@link #META_CTRL_ON}, {@link #META_CTRL_LEFT_ON}, {@link #META_CTRL_RIGHT_ON},
1944 * {@link #META_META_ON}, {@link #META_META_LEFT_ON}, {@link #META_META_RIGHT_ON},
1945 * {@link #META_SYM_ON}, {@link #META_FUNCTION_ON}.
1946 */
1947 public static int getModifierMetaStateMask() {
1948 return META_MODIFIER_MASK;
1949 }
1950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 /**
1952 * Returns true if this key code is a modifier key.
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001953 * <p>
1954 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
1955 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
1956 * not considered modifier keys. Consequently, this function return false
1957 * for those keys.
1958 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 *
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001960 * @return True if the key code is one of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 * {@link #KEYCODE_SHIFT_LEFT} {@link #KEYCODE_SHIFT_RIGHT},
Jeff Brown497a92c2010-09-12 17:55:08 -07001962 * {@link #KEYCODE_ALT_LEFT}, {@link #KEYCODE_ALT_RIGHT},
Jeff Brown497a92c2010-09-12 17:55:08 -07001963 * {@link #KEYCODE_CTRL_LEFT}, {@link #KEYCODE_CTRL_RIGHT},
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001964 * {@link #KEYCODE_META_LEFT}, or {@link #KEYCODE_META_RIGHT},
1965 * {@link #KEYCODE_SYM}, {@link #KEYCODE_NUM}, {@link #KEYCODE_FUNCTION}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 */
1967 public static boolean isModifierKey(int keyCode) {
Jeff Brown497a92c2010-09-12 17:55:08 -07001968 switch (keyCode) {
1969 case KEYCODE_SHIFT_LEFT:
1970 case KEYCODE_SHIFT_RIGHT:
1971 case KEYCODE_ALT_LEFT:
1972 case KEYCODE_ALT_RIGHT:
Jeff Brown497a92c2010-09-12 17:55:08 -07001973 case KEYCODE_CTRL_LEFT:
1974 case KEYCODE_CTRL_RIGHT:
1975 case KEYCODE_META_LEFT:
1976 case KEYCODE_META_RIGHT:
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001977 case KEYCODE_SYM:
1978 case KEYCODE_NUM:
1979 case KEYCODE_FUNCTION:
Jeff Brown497a92c2010-09-12 17:55:08 -07001980 return true;
1981 default:
1982 return false;
1983 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 }
1985
1986 /**
Jeff Brown28cbf4b2010-12-13 10:33:20 -08001987 * Normalizes the specified meta state.
1988 * <p>
1989 * The meta state is normalized such that if either the left or right modifier meta state
1990 * bits are set then the result will also include the universal bit for that modifier.
1991 * </p><p>
1992 * If the specified meta state contains {@link #META_ALT_LEFT_ON} then
1993 * the result will also contain {@link #META_ALT_ON} in addition to {@link #META_ALT_LEFT_ON}
1994 * and the other bits that were specified in the input. The same is process is
1995 * performed for shift, control and meta.
1996 * </p><p>
1997 * If the specified meta state contains synthetic meta states defined by
1998 * {@link MetaKeyKeyListener}, then those states are translated here and the original
1999 * synthetic meta states are removed from the result.
2000 * {@link MetaKeyKeyListener#META_CAP_LOCKED} is translated to {@link #META_CAPS_LOCK_ON}.
2001 * {@link MetaKeyKeyListener#META_ALT_LOCKED} is translated to {@link #META_ALT_ON}.
2002 * {@link MetaKeyKeyListener#META_SYM_LOCKED} is translated to {@link #META_SYM_ON}.
2003 * </p><p>
2004 * Undefined meta state bits are removed.
2005 * </p>
2006 *
2007 * @param metaState The meta state.
2008 * @return The normalized meta state.
2009 */
2010 public static int normalizeMetaState(int metaState) {
2011 if ((metaState & (META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON)) != 0) {
2012 metaState |= META_SHIFT_ON;
2013 }
2014 if ((metaState & (META_ALT_LEFT_ON | META_ALT_RIGHT_ON)) != 0) {
2015 metaState |= META_ALT_ON;
2016 }
2017 if ((metaState & (META_CTRL_LEFT_ON | META_CTRL_RIGHT_ON)) != 0) {
2018 metaState |= META_CTRL_ON;
2019 }
2020 if ((metaState & (META_META_LEFT_ON | META_META_RIGHT_ON)) != 0) {
2021 metaState |= META_META_ON;
2022 }
2023 if ((metaState & MetaKeyKeyListener.META_CAP_LOCKED) != 0) {
2024 metaState |= META_CAPS_LOCK_ON;
2025 }
2026 if ((metaState & MetaKeyKeyListener.META_ALT_LOCKED) != 0) {
2027 metaState |= META_ALT_ON;
2028 }
2029 if ((metaState & MetaKeyKeyListener.META_SYM_LOCKED) != 0) {
2030 metaState |= META_SYM_ON;
2031 }
2032 return metaState & META_ALL_MASK;
2033 }
2034
2035 /**
2036 * Returns true if no modifiers keys are pressed according to the specified meta state.
2037 * <p>
2038 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2039 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2040 * not considered modifier keys. Consequently, this function ignores
2041 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2042 * </p><p>
2043 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}.
2044 * </p>
2045 *
2046 * @param metaState The meta state to consider.
2047 * @return True if no modifier keys are pressed.
2048 * @see #hasNoModifiers()
2049 */
2050 public static boolean metaStateHasNoModifiers(int metaState) {
2051 return (normalizeMetaState(metaState) & META_MODIFIER_MASK) == 0;
2052 }
2053
2054 /**
2055 * Returns true if only the specified modifier keys are pressed according to
2056 * the specified meta state. Returns false if a different combination of modifier
2057 * keys are pressed.
2058 * <p>
2059 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2060 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2061 * not considered modifier keys. Consequently, this function ignores
2062 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2063 * </p><p>
2064 * If the specified modifier mask includes directional modifiers, such as
2065 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the
2066 * modifier is pressed on that side.
2067 * If the specified modifier mask includes non-directional modifiers, such as
2068 * {@link #META_SHIFT_ON}, then this method ensures that the modifier
2069 * is pressed on either side.
2070 * If the specified modifier mask includes both directional and non-directional modifiers
2071 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON},
2072 * then this method throws an illegal argument exception.
2073 * </p>
2074 *
2075 * @param metaState The meta state to consider.
2076 * @param modifiers The meta state of the modifier keys to check. May be a combination
2077 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to
2078 * ensure that no modifier keys are pressed.
2079 * @return True if only the specified modifier keys are pressed.
2080 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers
2081 * @see #hasModifiers
2082 */
2083 public static boolean metaStateHasModifiers(int metaState, int modifiers) {
2084 // Note: For forward compatibility, we allow the parameter to contain meta states
2085 // that we do not recognize but we explicitly disallow meta states that
2086 // are not valid modifiers.
2087 if ((modifiers & META_INVALID_MODIFIER_MASK) != 0) {
2088 throw new IllegalArgumentException("modifiers must not contain "
2089 + "META_CAPS_LOCK_ON, META_NUM_LOCK_ON, META_SCROLL_LOCK_ON, "
2090 + "META_CAP_LOCKED, META_ALT_LOCKED, META_SYM_LOCKED, "
2091 + "or META_SELECTING");
2092 }
2093
2094 metaState = normalizeMetaState(metaState) & META_MODIFIER_MASK;
2095 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2096 META_SHIFT_ON, META_SHIFT_LEFT_ON, META_SHIFT_RIGHT_ON);
2097 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2098 META_ALT_ON, META_ALT_LEFT_ON, META_ALT_RIGHT_ON);
2099 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2100 META_CTRL_ON, META_CTRL_LEFT_ON, META_CTRL_RIGHT_ON);
2101 metaState = metaStateFilterDirectionalModifiers(metaState, modifiers,
2102 META_META_ON, META_META_LEFT_ON, META_META_RIGHT_ON);
2103 return metaState == modifiers;
2104 }
2105
2106 private static int metaStateFilterDirectionalModifiers(int metaState,
2107 int modifiers, int basic, int left, int right) {
2108 final boolean wantBasic = (modifiers & basic) != 0;
2109 final int directional = left | right;
2110 final boolean wantLeftOrRight = (modifiers & directional) != 0;
2111
2112 if (wantBasic) {
2113 if (wantLeftOrRight) {
2114 throw new IllegalArgumentException("modifiers must not contain "
2115 + metaStateToString(basic) + " combined with "
2116 + metaStateToString(left) + " or " + metaStateToString(right));
2117 }
2118 return metaState & ~directional;
2119 } else if (wantLeftOrRight) {
2120 return metaState & ~basic;
2121 } else {
2122 return metaState;
2123 }
2124 }
2125
2126 /**
2127 * Returns true if no modifier keys are pressed.
2128 * <p>
2129 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2130 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2131 * not considered modifier keys. Consequently, this function ignores
2132 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2133 * </p><p>
2134 * The meta state is normalized prior to comparison using {@link #normalizeMetaState(int)}.
2135 * </p>
2136 *
2137 * @return True if no modifier keys are pressed.
2138 * @see #metaStateHasNoModifiers
2139 */
2140 public final boolean hasNoModifiers() {
2141 return metaStateHasNoModifiers(mMetaState);
2142 }
2143
2144 /**
2145 * Returns true if only the specified modifiers keys are pressed.
2146 * Returns false if a different combination of modifier keys are pressed.
2147 * <p>
2148 * For the purposes of this function, {@link #KEYCODE_CAPS_LOCK},
2149 * {@link #KEYCODE_SCROLL_LOCK}, and {@link #KEYCODE_NUM_LOCK} are
2150 * not considered modifier keys. Consequently, this function ignores
2151 * {@link #META_CAPS_LOCK_ON}, {@link #META_SCROLL_LOCK_ON} and {@link #META_NUM_LOCK_ON}.
2152 * </p><p>
2153 * If the specified modifier mask includes directional modifiers, such as
2154 * {@link #META_SHIFT_LEFT_ON}, then this method ensures that the
2155 * modifier is pressed on that side.
2156 * If the specified modifier mask includes non-directional modifiers, such as
2157 * {@link #META_SHIFT_ON}, then this method ensures that the modifier
2158 * is pressed on either side.
2159 * If the specified modifier mask includes both directional and non-directional modifiers
2160 * for the same type of key, such as {@link #META_SHIFT_ON} and {@link #META_SHIFT_LEFT_ON},
2161 * then this method throws an illegal argument exception.
2162 * </p>
2163 *
2164 * @param modifiers The meta state of the modifier keys to check. May be a combination
2165 * of modifier meta states as defined by {@link #getModifierMetaStateMask()}. May be 0 to
2166 * ensure that no modifier keys are pressed.
2167 * @return True if only the specified modifier keys are pressed.
2168 * @throws IllegalArgumentException if the modifiers parameter contains invalid modifiers
2169 * @see #metaStateHasModifiers
2170 */
2171 public final boolean hasModifiers(int modifiers) {
2172 return metaStateHasModifiers(mMetaState, modifiers);
2173 }
2174
2175 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 * <p>Returns the pressed state of the ALT meta key.</p>
2177 *
2178 * @return true if the ALT key is pressed, false otherwise
2179 *
2180 * @see #KEYCODE_ALT_LEFT
2181 * @see #KEYCODE_ALT_RIGHT
2182 * @see #META_ALT_ON
2183 */
2184 public final boolean isAltPressed() {
2185 return (mMetaState & META_ALT_ON) != 0;
2186 }
2187
2188 /**
2189 * <p>Returns the pressed state of the SHIFT meta key.</p>
2190 *
2191 * @return true if the SHIFT key is pressed, false otherwise
2192 *
2193 * @see #KEYCODE_SHIFT_LEFT
2194 * @see #KEYCODE_SHIFT_RIGHT
2195 * @see #META_SHIFT_ON
2196 */
2197 public final boolean isShiftPressed() {
2198 return (mMetaState & META_SHIFT_ON) != 0;
2199 }
2200
2201 /**
2202 * <p>Returns the pressed state of the SYM meta key.</p>
2203 *
2204 * @return true if the SYM key is pressed, false otherwise
2205 *
2206 * @see #KEYCODE_SYM
2207 * @see #META_SYM_ON
2208 */
2209 public final boolean isSymPressed() {
2210 return (mMetaState & META_SYM_ON) != 0;
2211 }
2212
2213 /**
Jeff Brown497a92c2010-09-12 17:55:08 -07002214 * <p>Returns the pressed state of the CTRL meta key.</p>
2215 *
2216 * @return true if the CTRL key is pressed, false otherwise
2217 *
2218 * @see #KEYCODE_CTRL_LEFT
2219 * @see #KEYCODE_CTRL_RIGHT
2220 * @see #META_CTRL_ON
2221 */
2222 public final boolean isCtrlPressed() {
2223 return (mMetaState & META_CTRL_ON) != 0;
2224 }
2225
2226 /**
2227 * <p>Returns the pressed state of the META meta key.</p>
2228 *
2229 * @return true if the META key is pressed, false otherwise
2230 *
2231 * @see #KEYCODE_META_LEFT
2232 * @see #KEYCODE_META_RIGHT
2233 * @see #META_META_ON
2234 */
2235 public final boolean isMetaPressed() {
2236 return (mMetaState & META_META_ON) != 0;
2237 }
2238
2239 /**
2240 * <p>Returns the pressed state of the FUNCTION meta key.</p>
2241 *
2242 * @return true if the FUNCTION key is pressed, false otherwise
2243 *
2244 * @see #KEYCODE_FUNCTION
2245 * @see #META_FUNCTION_ON
2246 */
2247 public final boolean isFunctionPressed() {
2248 return (mMetaState & META_FUNCTION_ON) != 0;
2249 }
2250
2251 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07002252 * <p>Returns the locked state of the CAPS LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002253 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002254 * @return true if the CAPS LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002255 *
2256 * @see #KEYCODE_CAPS_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002257 * @see #META_CAPS_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002258 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002259 public final boolean isCapsLockOn() {
2260 return (mMetaState & META_CAPS_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002261 }
2262
2263 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07002264 * <p>Returns the locked state of the NUM LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002265 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002266 * @return true if the NUM LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002267 *
2268 * @see #KEYCODE_NUM_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002269 * @see #META_NUM_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002270 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002271 public final boolean isNumLockOn() {
2272 return (mMetaState & META_NUM_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002273 }
2274
2275 /**
Jeff Brown51e7fe72010-10-29 22:19:53 -07002276 * <p>Returns the locked state of the SCROLL LOCK meta key.</p>
Jeff Brown497a92c2010-09-12 17:55:08 -07002277 *
Jeff Brown51e7fe72010-10-29 22:19:53 -07002278 * @return true if the SCROLL LOCK key is on, false otherwise
Jeff Brown497a92c2010-09-12 17:55:08 -07002279 *
2280 * @see #KEYCODE_SCROLL_LOCK
Jeff Brown51e7fe72010-10-29 22:19:53 -07002281 * @see #META_SCROLL_LOCK_ON
Jeff Brown497a92c2010-09-12 17:55:08 -07002282 */
Jeff Brown51e7fe72010-10-29 22:19:53 -07002283 public final boolean isScrollLockOn() {
2284 return (mMetaState & META_SCROLL_LOCK_ON) != 0;
Jeff Brown497a92c2010-09-12 17:55:08 -07002285 }
2286
2287 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 * Retrieve the action of this key event. May be either
2289 * {@link #ACTION_DOWN}, {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
RoboErik01fe6612014-02-13 14:19:04 -08002290 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 * @return The event action: ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.
2292 */
2293 public final int getAction() {
2294 return mAction;
2295 }
2296
2297 /**
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07002298 * For {@link #ACTION_UP} events, indicates that the event has been
2299 * canceled as per {@link #FLAG_CANCELED}.
2300 */
2301 public final boolean isCanceled() {
2302 return (mFlags&FLAG_CANCELED) != 0;
2303 }
RoboErik01fe6612014-02-13 14:19:04 -08002304
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07002305 /**
Wale Ogunwalec3672cd2014-11-05 15:17:35 -08002306 * Set {@link #FLAG_CANCELED} flag for the key event.
2307 *
2308 * @hide
2309 */
2310 @Override
2311 public final void cancel() {
2312 mFlags |= FLAG_CANCELED;
2313 }
2314
2315 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002316 * Call this during {@link Callback#onKeyDown} to have the system track
2317 * the key through its final up (possibly including a long press). Note
2318 * that only one key can be tracked at a time -- if another key down
2319 * event is received while a previous one is being tracked, tracking is
2320 * stopped on the previous event.
2321 */
2322 public final void startTracking() {
2323 mFlags |= FLAG_START_TRACKING;
2324 }
RoboErik01fe6612014-02-13 14:19:04 -08002325
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002326 /**
2327 * For {@link #ACTION_UP} events, indicates that the event is still being
2328 * tracked from its initial down event as per
2329 * {@link #FLAG_TRACKING}.
2330 */
2331 public final boolean isTracking() {
2332 return (mFlags&FLAG_TRACKING) != 0;
2333 }
RoboErik01fe6612014-02-13 14:19:04 -08002334
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002335 /**
2336 * For {@link #ACTION_DOWN} events, indicates that the event has been
2337 * canceled as per {@link #FLAG_LONG_PRESS}.
2338 */
2339 public final boolean isLongPress() {
2340 return (mFlags&FLAG_LONG_PRESS) != 0;
2341 }
RoboErik01fe6612014-02-13 14:19:04 -08002342
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002343 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002344 * Retrieve the key code of the key event. This is the physical key that
2345 * was pressed, <em>not</em> the Unicode character.
RoboErik01fe6612014-02-13 14:19:04 -08002346 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347 * @return The key code of the event.
2348 */
2349 public final int getKeyCode() {
2350 return mKeyCode;
2351 }
2352
2353 /**
2354 * For the special case of a {@link #ACTION_MULTIPLE} event with key
2355 * code of {@link #KEYCODE_UNKNOWN}, this is a raw string of characters
2356 * associated with the event. In all other cases it is null.
RoboErik01fe6612014-02-13 14:19:04 -08002357 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002358 * @return Returns a String of 1 or more characters associated with
2359 * the event.
2360 */
2361 public final String getCharacters() {
2362 return mCharacters;
2363 }
RoboErik01fe6612014-02-13 14:19:04 -08002364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365 /**
2366 * Retrieve the hardware key id of this key event. These values are not
2367 * reliable and vary from device to device.
2368 *
2369 * {@more}
2370 * Mostly this is here for debugging purposes.
2371 */
2372 public final int getScanCode() {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002373 return mScanCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374 }
2375
2376 /**
2377 * Retrieve the repeat count of the event. For both key up and key down
2378 * events, this is the number of times the key has repeated with the first
2379 * down starting at 0 and counting up from there. For multiple key
2380 * events, this is the number of down/up pairs that have occurred.
RoboErik01fe6612014-02-13 14:19:04 -08002381 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382 * @return The number of times the key has repeated.
2383 */
2384 public final int getRepeatCount() {
2385 return mRepeatCount;
2386 }
2387
2388 /**
2389 * Retrieve the time of the most recent key down event,
2390 * in the {@link android.os.SystemClock#uptimeMillis} time base. If this
2391 * is a down event, this will be the same as {@link #getEventTime()}.
2392 * Note that when chording keys, this value is the down time of the
2393 * most recently pressed key, which may <em>not</em> be the same physical
2394 * key of this event.
RoboErik01fe6612014-02-13 14:19:04 -08002395 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396 * @return Returns the most recent key down time, in the
2397 * {@link android.os.SystemClock#uptimeMillis} time base
2398 */
2399 public final long getDownTime() {
2400 return mDownTime;
2401 }
2402
2403 /**
Jeff Brownb11499d2012-04-20 19:54:22 -07002404 * Retrieve the time this event occurred,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002405 * in the {@link android.os.SystemClock#uptimeMillis} time base.
Jeff Brownb11499d2012-04-20 19:54:22 -07002406 *
RoboErik01fe6612014-02-13 14:19:04 -08002407 * @return Returns the time this event occurred,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002408 * in the {@link android.os.SystemClock#uptimeMillis} time base.
2409 */
Jeff Brownb11499d2012-04-20 19:54:22 -07002410 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 public final long getEventTime() {
2412 return mEventTime;
2413 }
2414
Jeff Brownb11499d2012-04-20 19:54:22 -07002415 /**
2416 * Retrieve the time this event occurred,
2417 * in the {@link android.os.SystemClock#uptimeMillis} time base but with
2418 * nanosecond (instead of millisecond) precision.
2419 * <p>
2420 * The value is in nanosecond precision but it may not have nanosecond accuracy.
2421 * </p>
2422 *
2423 * @return Returns the time this event occurred,
2424 * in the {@link android.os.SystemClock#uptimeMillis} time base but with
2425 * nanosecond (instead of millisecond) precision.
2426 *
2427 * @hide
2428 */
Jeff Brown4e91a182011-04-07 11:38:09 -07002429 @Override
2430 public final long getEventTimeNano() {
2431 return mEventTime * 1000000L;
2432 }
2433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002434 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 * Renamed to {@link #getDeviceId}.
RoboErik01fe6612014-02-13 14:19:04 -08002436 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 * @hide
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002438 * @deprecated use {@link #getDeviceId()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002439 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002440 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002441 public final int getKeyboardDevice() {
2442 return mDeviceId;
2443 }
2444
2445 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002446 * Gets the {@link KeyCharacterMap} associated with the keyboard device.
2447 *
2448 * @return The associated key character map.
Andrew Sapperstein8ab3dc72011-06-23 18:56:44 -07002449 * @throws {@link KeyCharacterMap.UnavailableException} if the key character map
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002450 * could not be loaded because it was malformed or the default key character map
2451 * is missing from the system.
2452 *
Andrew Sapperstein8ab3dc72011-06-23 18:56:44 -07002453 * @see KeyCharacterMap#load
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002454 */
2455 public final KeyCharacterMap getKeyCharacterMap() {
2456 return KeyCharacterMap.load(mDeviceId);
2457 }
2458
2459 /**
2460 * Gets the primary character for this key.
2461 * In other words, the label that is physically printed on it.
2462 *
2463 * @return The display label character, or 0 if none (eg. for non-printing keys).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002464 */
2465 public char getDisplayLabel() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002466 return getKeyCharacterMap().getDisplayLabel(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 }
RoboErik01fe6612014-02-13 14:19:04 -08002468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002469 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002470 * Gets the Unicode character generated by the specified key and meta
2471 * key state combination.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002472 * <p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002473 * Returns the Unicode character that the specified key would produce
2474 * when the specified meta bits (see {@link MetaKeyKeyListener})
2475 * were active.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002476 * </p><p>
2477 * Returns 0 if the key is not one that is used to type Unicode
2478 * characters.
2479 * </p><p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002480 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the
2481 * key is a "dead key" that should be combined with another to
2482 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} --
2483 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002484 * </p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002485 *
2486 * @return The associated character or combining accent, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487 */
2488 public int getUnicodeChar() {
2489 return getUnicodeChar(mMetaState);
2490 }
RoboErik01fe6612014-02-13 14:19:04 -08002491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002492 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002493 * Gets the Unicode character generated by the specified key and meta
2494 * key state combination.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002495 * <p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002496 * Returns the Unicode character that the specified key would produce
2497 * when the specified meta bits (see {@link MetaKeyKeyListener})
2498 * were active.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002499 * </p><p>
2500 * Returns 0 if the key is not one that is used to type Unicode
2501 * characters.
2502 * </p><p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002503 * If the return value has bit {@link KeyCharacterMap#COMBINING_ACCENT} set, the
2504 * key is a "dead key" that should be combined with another to
2505 * actually produce a character -- see {@link KeyCharacterMap#getDeadChar} --
2506 * after masking with {@link KeyCharacterMap#COMBINING_ACCENT_MASK}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507 * </p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002508 *
2509 * @param metaState The meta key modifier state.
2510 * @return The associated character or combining accent, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002511 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002512 public int getUnicodeChar(int metaState) {
2513 return getKeyCharacterMap().get(mKeyCode, metaState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 }
RoboErik01fe6612014-02-13 14:19:04 -08002515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002517 * Get the character conversion data for a given key code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518 *
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002519 * @param results A {@link KeyCharacterMap.KeyData} instance that will be
2520 * filled with the results.
2521 * @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 -08002522 *
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002523 * @deprecated instead use {@link #getDisplayLabel()},
2524 * {@link #getNumber()} or {@link #getUnicodeChar(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002525 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002526 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002527 public boolean getKeyData(KeyData results) {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002528 return getKeyCharacterMap().getKeyData(mKeyCode, results);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002529 }
RoboErik01fe6612014-02-13 14:19:04 -08002530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002531 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002532 * Gets the first character in the character array that can be generated
2533 * by the specified key code.
2534 * <p>
2535 * This is a convenience function that returns the same value as
2536 * {@link #getMatch(char[],int) getMatch(chars, 0)}.
2537 * </p>
2538 *
2539 * @param chars The array of matching characters to consider.
2540 * @return The matching associated character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541 */
2542 public char getMatch(char[] chars) {
2543 return getMatch(chars, 0);
2544 }
RoboErik01fe6612014-02-13 14:19:04 -08002545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002546 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002547 * Gets the first character in the character array that can be generated
2548 * by the specified key code. If there are multiple choices, prefers
2549 * the one that would be generated with the specified meta key modifier state.
2550 *
2551 * @param chars The array of matching characters to consider.
2552 * @param metaState The preferred meta key modifier state.
2553 * @return The matching associated character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002555 public char getMatch(char[] chars, int metaState) {
2556 return getKeyCharacterMap().getMatch(mKeyCode, chars, metaState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002557 }
RoboErik01fe6612014-02-13 14:19:04 -08002558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002559 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002560 * Gets the number or symbol associated with the key.
2561 * <p>
2562 * The character value is returned, not the numeric value.
2563 * If the key is not a number, but is a symbol, the symbol is retuned.
2564 * </p><p>
2565 * This method is intended to to support dial pads and other numeric or
2566 * symbolic entry on keyboards where certain keys serve dual function
2567 * as alphabetic and symbolic keys. This method returns the number
2568 * or symbol associated with the key independent of whether the user
2569 * has pressed the required modifier.
2570 * </p><p>
2571 * For example, on one particular keyboard the keys on the top QWERTY row generate
2572 * numbers when ALT is pressed such that ALT-Q maps to '1'. So for that keyboard
2573 * when {@link #getNumber} is called with {@link KeyEvent#KEYCODE_Q} it returns '1'
2574 * so that the user can type numbers without pressing ALT when it makes sense.
2575 * </p>
2576 *
2577 * @return The associated numeric or symbolic character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 */
2579 public char getNumber() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002580 return getKeyCharacterMap().getNumber(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 }
RoboErik01fe6612014-02-13 14:19:04 -08002582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002584 * Returns true if this key produces a glyph.
2585 *
2586 * @return True if the key is a printing key.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 */
2588 public boolean isPrintingKey() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -08002589 return getKeyCharacterMap().isPrintingKey(mKeyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002590 }
RoboErik01fe6612014-02-13 14:19:04 -08002591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002593 * @deprecated Use {@link #dispatch(Callback, DispatcherState, Object)} instead.
2594 */
2595 @Deprecated
2596 public final boolean dispatch(Callback receiver) {
2597 return dispatch(receiver, null, null);
2598 }
RoboErik01fe6612014-02-13 14:19:04 -08002599
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002600 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002601 * Deliver this key event to a {@link Callback} interface. If this is
2602 * an ACTION_MULTIPLE event and it is not handled, then an attempt will
2603 * be made to deliver a single normal event.
RoboErik01fe6612014-02-13 14:19:04 -08002604 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002605 * @param receiver The Callback that will be given the event.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002606 * @param state State information retained across events.
2607 * @param target The target of the dispatch, for use in tracking.
RoboErik01fe6612014-02-13 14:19:04 -08002608 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 * @return The return value from the Callback method that was called.
2610 */
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002611 public final boolean dispatch(Callback receiver, DispatcherState state,
2612 Object target) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002613 switch (mAction) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002614 case ACTION_DOWN: {
2615 mFlags &= ~FLAG_START_TRACKING;
Dianne Hackborn8d374262009-09-14 21:21:52 -07002616 if (DEBUG) Log.v(TAG, "Key down to " + target + " in " + state
2617 + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002618 boolean res = receiver.onKeyDown(mKeyCode, this);
2619 if (state != null) {
2620 if (res && mRepeatCount == 0 && (mFlags&FLAG_START_TRACKING) != 0) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002621 if (DEBUG) Log.v(TAG, " Start tracking!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002622 state.startTracking(this, target);
2623 } else if (isLongPress() && state.isTracking(this)) {
2624 try {
2625 if (receiver.onKeyLongPress(mKeyCode, this)) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002626 if (DEBUG) Log.v(TAG, " Clear from long press!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002627 state.performedLongPress(this);
2628 res = true;
2629 }
2630 } catch (AbstractMethodError e) {
2631 }
2632 }
2633 }
2634 return res;
2635 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002636 case ACTION_UP:
Dianne Hackborn8d374262009-09-14 21:21:52 -07002637 if (DEBUG) Log.v(TAG, "Key up to " + target + " in " + state
2638 + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002639 if (state != null) {
2640 state.handleUpEvent(this);
2641 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642 return receiver.onKeyUp(mKeyCode, this);
2643 case ACTION_MULTIPLE:
2644 final int count = mRepeatCount;
2645 final int code = mKeyCode;
2646 if (receiver.onKeyMultiple(code, count, this)) {
2647 return true;
2648 }
2649 if (code != KeyEvent.KEYCODE_UNKNOWN) {
2650 mAction = ACTION_DOWN;
2651 mRepeatCount = 0;
2652 boolean handled = receiver.onKeyDown(code, this);
2653 if (handled) {
2654 mAction = ACTION_UP;
2655 receiver.onKeyUp(code, this);
2656 }
2657 mAction = ACTION_MULTIPLE;
2658 mRepeatCount = count;
2659 return handled;
2660 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002661 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002662 }
2663 return false;
2664 }
2665
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002666 /**
2667 * Use with {@link KeyEvent#dispatch(Callback, DispatcherState, Object)}
2668 * for more advanced key dispatching, such as long presses.
2669 */
2670 public static class DispatcherState {
2671 int mDownKeyCode;
2672 Object mDownTarget;
2673 SparseIntArray mActiveLongPresses = new SparseIntArray();
RoboErik01fe6612014-02-13 14:19:04 -08002674
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002675 /**
2676 * Reset back to initial state.
2677 */
2678 public void reset() {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002679 if (DEBUG) Log.v(TAG, "Reset: " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002680 mDownKeyCode = 0;
2681 mDownTarget = null;
2682 mActiveLongPresses.clear();
2683 }
RoboErik01fe6612014-02-13 14:19:04 -08002684
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002685 /**
2686 * Stop any tracking associated with this target.
2687 */
2688 public void reset(Object target) {
2689 if (mDownTarget == target) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002690 if (DEBUG) Log.v(TAG, "Reset in " + target + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002691 mDownKeyCode = 0;
2692 mDownTarget = null;
2693 }
2694 }
RoboErik01fe6612014-02-13 14:19:04 -08002695
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002696 /**
2697 * Start tracking the key code associated with the given event. This
2698 * can only be called on a key down. It will allow you to see any
2699 * long press associated with the key, and will result in
2700 * {@link KeyEvent#isTracking} return true on the long press and up
2701 * events.
RoboErik01fe6612014-02-13 14:19:04 -08002702 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002703 * <p>This is only needed if you are directly dispatching events, rather
2704 * than handling them in {@link Callback#onKeyDown}.
2705 */
2706 public void startTracking(KeyEvent event, Object target) {
2707 if (event.getAction() != ACTION_DOWN) {
2708 throw new IllegalArgumentException(
2709 "Can only start tracking on a down event");
2710 }
Dianne Hackborn8d374262009-09-14 21:21:52 -07002711 if (DEBUG) Log.v(TAG, "Start trackingt in " + target + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002712 mDownKeyCode = event.getKeyCode();
2713 mDownTarget = target;
2714 }
RoboErik01fe6612014-02-13 14:19:04 -08002715
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002716 /**
2717 * Return true if the key event is for a key code that is currently
2718 * being tracked by the dispatcher.
2719 */
2720 public boolean isTracking(KeyEvent event) {
2721 return mDownKeyCode == event.getKeyCode();
2722 }
RoboErik01fe6612014-02-13 14:19:04 -08002723
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002724 /**
2725 * Keep track of the given event's key code as having performed an
2726 * action with a long press, so no action should occur on the up.
2727 * <p>This is only needed if you are directly dispatching events, rather
2728 * than handling them in {@link Callback#onKeyLongPress}.
2729 */
2730 public void performedLongPress(KeyEvent event) {
2731 mActiveLongPresses.put(event.getKeyCode(), 1);
2732 }
RoboErik01fe6612014-02-13 14:19:04 -08002733
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002734 /**
2735 * Handle key up event to stop tracking. This resets the dispatcher state,
2736 * and updates the key event state based on it.
2737 * <p>This is only needed if you are directly dispatching events, rather
2738 * than handling them in {@link Callback#onKeyUp}.
2739 */
2740 public void handleUpEvent(KeyEvent event) {
2741 final int keyCode = event.getKeyCode();
Dianne Hackborn8d374262009-09-14 21:21:52 -07002742 if (DEBUG) Log.v(TAG, "Handle key up " + event + ": " + this);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002743 int index = mActiveLongPresses.indexOfKey(keyCode);
2744 if (index >= 0) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002745 if (DEBUG) Log.v(TAG, " Index: " + index);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002746 event.mFlags |= FLAG_CANCELED | FLAG_CANCELED_LONG_PRESS;
2747 mActiveLongPresses.removeAt(index);
2748 }
2749 if (mDownKeyCode == keyCode) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002750 if (DEBUG) Log.v(TAG, " Tracking!");
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002751 event.mFlags |= FLAG_TRACKING;
2752 mDownKeyCode = 0;
2753 mDownTarget = null;
2754 }
2755 }
2756 }
Jeff Brown497a92c2010-09-12 17:55:08 -07002757
2758 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759 public String toString() {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002760 StringBuilder msg = new StringBuilder();
2761 msg.append("KeyEvent { action=").append(actionToString(mAction));
2762 msg.append(", keyCode=").append(keyCodeToString(mKeyCode));
2763 msg.append(", scanCode=").append(mScanCode);
2764 if (mCharacters != null) {
2765 msg.append(", characters=\"").append(mCharacters).append("\"");
2766 }
2767 msg.append(", metaState=").append(metaStateToString(mMetaState));
2768 msg.append(", flags=0x").append(Integer.toHexString(mFlags));
2769 msg.append(", repeatCount=").append(mRepeatCount);
2770 msg.append(", eventTime=").append(mEventTime);
2771 msg.append(", downTime=").append(mDownTime);
2772 msg.append(", deviceId=").append(mDeviceId);
2773 msg.append(", source=0x").append(Integer.toHexString(mSource));
2774 msg.append(" }");
2775 return msg.toString();
Jeff Brown497a92c2010-09-12 17:55:08 -07002776 }
2777
2778 /**
2779 * Returns a string that represents the symbolic name of the specified action
Jeff Brown6f2fba42011-02-19 01:08:02 -08002780 * such as "ACTION_DOWN", or an equivalent numeric constant such as "35" if unknown.
Jeff Brown497a92c2010-09-12 17:55:08 -07002781 *
2782 * @param action The action.
2783 * @return The symbolic name of the specified action.
2784 * @hide
2785 */
2786 public static String actionToString(int action) {
2787 switch (action) {
2788 case ACTION_DOWN:
2789 return "ACTION_DOWN";
2790 case ACTION_UP:
2791 return "ACTION_UP";
2792 case ACTION_MULTIPLE:
2793 return "ACTION_MULTIPLE";
2794 default:
2795 return Integer.toString(action);
2796 }
2797 }
2798
2799 /**
2800 * Returns a string that represents the symbolic name of the specified keycode
Jeff Brown6f2fba42011-02-19 01:08:02 -08002801 * such as "KEYCODE_A", "KEYCODE_DPAD_UP", or an equivalent numeric constant
2802 * such as "1001" if unknown.
Jeff Brown497a92c2010-09-12 17:55:08 -07002803 *
2804 * @param keyCode The key code.
2805 * @return The symbolic name of the specified keycode.
2806 *
2807 * @see KeyCharacterMap#getDisplayLabel
Jeff Brown497a92c2010-09-12 17:55:08 -07002808 */
2809 public static String keyCodeToString(int keyCode) {
Michael Wright337d9d22014-04-22 15:03:48 -07002810 String symbolicName = nativeKeyCodeToString(keyCode);
2811 return symbolicName != null ? LABEL_PREFIX + symbolicName : Integer.toString(keyCode);
Jeff Brown497a92c2010-09-12 17:55:08 -07002812 }
2813
2814 /**
Jeff Brown6f2fba42011-02-19 01:08:02 -08002815 * Gets a keycode by its symbolic name such as "KEYCODE_A" or an equivalent
2816 * numeric constant such as "1001".
Jeff Brown497a92c2010-09-12 17:55:08 -07002817 *
2818 * @param symbolicName The symbolic name of the keycode.
Jeff Brown6f2fba42011-02-19 01:08:02 -08002819 * @return The keycode or {@link #KEYCODE_UNKNOWN} if not found.
Michael Wright072137c2013-04-24 20:41:20 -07002820 * @see #keycodeToString(int)
Jeff Brown497a92c2010-09-12 17:55:08 -07002821 */
2822 public static int keyCodeFromString(String symbolicName) {
Michael Wright337d9d22014-04-22 15:03:48 -07002823 if (symbolicName.startsWith(LABEL_PREFIX)) {
2824 symbolicName = symbolicName.substring(LABEL_PREFIX.length());
Michael Wright973efa02014-05-13 15:38:56 -07002825 int keyCode = nativeKeyCodeFromString(symbolicName);
2826 if (keyCode > 0) {
2827 return keyCode;
2828 }
Jeff Brown497a92c2010-09-12 17:55:08 -07002829 }
Jeff Brown497a92c2010-09-12 17:55:08 -07002830 try {
Jeff Brown6f2fba42011-02-19 01:08:02 -08002831 return Integer.parseInt(symbolicName, 10);
Jeff Brown497a92c2010-09-12 17:55:08 -07002832 } catch (NumberFormatException ex) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08002833 return KEYCODE_UNKNOWN;
Jeff Brown497a92c2010-09-12 17:55:08 -07002834 }
2835 }
2836
2837 /**
2838 * Returns a string that represents the symbolic name of the specified combined meta
2839 * key modifier state flags such as "0", "META_SHIFT_ON",
Jeff Brown6f2fba42011-02-19 01:08:02 -08002840 * "META_ALT_ON|META_SHIFT_ON" or an equivalent numeric constant such as "0x10000000"
2841 * if unknown.
Jeff Brown497a92c2010-09-12 17:55:08 -07002842 *
2843 * @param metaState The meta state.
2844 * @return The symbolic name of the specified combined meta state flags.
2845 * @hide
2846 */
2847 public static String metaStateToString(int metaState) {
2848 if (metaState == 0) {
2849 return "0";
2850 }
2851 StringBuilder result = null;
2852 int i = 0;
2853 while (metaState != 0) {
2854 final boolean isSet = (metaState & 1) != 0;
2855 metaState >>>= 1; // unsigned shift!
2856 if (isSet) {
2857 final String name = META_SYMBOLIC_NAMES[i];
2858 if (result == null) {
2859 if (metaState == 0) {
2860 return name;
2861 }
2862 result = new StringBuilder(name);
2863 } else {
2864 result.append('|');
2865 result.append(name);
2866 }
2867 }
2868 i += 1;
2869 }
2870 return result.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871 }
2872
2873 public static final Parcelable.Creator<KeyEvent> CREATOR
2874 = new Parcelable.Creator<KeyEvent>() {
2875 public KeyEvent createFromParcel(Parcel in) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07002876 in.readInt(); // skip token, we already know this is a KeyEvent
2877 return KeyEvent.createFromParcelBody(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002878 }
2879
2880 public KeyEvent[] newArray(int size) {
2881 return new KeyEvent[size];
2882 }
2883 };
RoboErik01fe6612014-02-13 14:19:04 -08002884
Jeff Brown6ec402b2010-07-28 15:48:59 -07002885 /** @hide */
2886 public static KeyEvent createFromParcelBody(Parcel in) {
2887 return new KeyEvent(in);
2888 }
RoboErik01fe6612014-02-13 14:19:04 -08002889
Jeff Brown6ec402b2010-07-28 15:48:59 -07002890 private KeyEvent(Parcel in) {
Jeff Brown91c69ab2011-02-14 17:03:18 -08002891 mDeviceId = in.readInt();
2892 mSource = in.readInt();
Jeff Brown6ec402b2010-07-28 15:48:59 -07002893 mAction = in.readInt();
2894 mKeyCode = in.readInt();
2895 mRepeatCount = in.readInt();
2896 mMetaState = in.readInt();
2897 mScanCode = in.readInt();
2898 mFlags = in.readInt();
2899 mDownTime = in.readLong();
2900 mEventTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 }
2902
2903 public void writeToParcel(Parcel out, int flags) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07002904 out.writeInt(PARCEL_TOKEN_KEY_EVENT);
Jeff Brown91c69ab2011-02-14 17:03:18 -08002905
2906 out.writeInt(mDeviceId);
2907 out.writeInt(mSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002908 out.writeInt(mAction);
2909 out.writeInt(mKeyCode);
2910 out.writeInt(mRepeatCount);
2911 out.writeInt(mMetaState);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002912 out.writeInt(mScanCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002913 out.writeInt(mFlags);
2914 out.writeLong(mDownTime);
2915 out.writeLong(mEventTime);
2916 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002917}