1 /* 2 * Copyright (C) 2021 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 17 package android.hardware.input; 18 19 import android.annotation.IntDef; 20 import android.annotation.NonNull; 21 import android.annotation.SystemApi; 22 import android.os.Parcel; 23 import android.os.Parcelable; 24 import android.view.KeyEvent; 25 26 import java.lang.annotation.Retention; 27 import java.lang.annotation.RetentionPolicy; 28 29 /** 30 * An event describing a keyboard interaction originating from a remote device. 31 * 32 * When the user presses a key, an {@code ACTION_DOWN} event should be reported. When the user 33 * releases the key, an {@code ACTION_UP} event should be reported. 34 * 35 * See {@link android.view.KeyEvent}. 36 * 37 * @hide 38 */ 39 @SystemApi 40 public final class VirtualKeyEvent implements Parcelable { 41 42 /** @hide */ 43 public static final int ACTION_UNKNOWN = -1; 44 /** Action indicating the given key has been pressed. */ 45 public static final int ACTION_DOWN = KeyEvent.ACTION_DOWN; 46 /** Action indicating the previously pressed key has been lifted. */ 47 public static final int ACTION_UP = KeyEvent.ACTION_UP; 48 49 /** @hide */ 50 @IntDef(prefix = { "ACTION_" }, value = { 51 ACTION_UNKNOWN, 52 ACTION_DOWN, 53 ACTION_UP, 54 }) 55 @Retention(RetentionPolicy.SOURCE) 56 public @interface Action { 57 } 58 59 /** 60 * The set of allowed keycodes. 61 * @hide 62 */ 63 @IntDef(prefix = { "KEYCODE_" }, value = { 64 KeyEvent.KEYCODE_0, 65 KeyEvent.KEYCODE_1, 66 KeyEvent.KEYCODE_2, 67 KeyEvent.KEYCODE_3, 68 KeyEvent.KEYCODE_4, 69 KeyEvent.KEYCODE_5, 70 KeyEvent.KEYCODE_6, 71 KeyEvent.KEYCODE_7, 72 KeyEvent.KEYCODE_8, 73 KeyEvent.KEYCODE_9, 74 KeyEvent.KEYCODE_A, 75 KeyEvent.KEYCODE_B, 76 KeyEvent.KEYCODE_C, 77 KeyEvent.KEYCODE_D, 78 KeyEvent.KEYCODE_E, 79 KeyEvent.KEYCODE_F, 80 KeyEvent.KEYCODE_G, 81 KeyEvent.KEYCODE_H, 82 KeyEvent.KEYCODE_I, 83 KeyEvent.KEYCODE_J, 84 KeyEvent.KEYCODE_K, 85 KeyEvent.KEYCODE_L, 86 KeyEvent.KEYCODE_M, 87 KeyEvent.KEYCODE_N, 88 KeyEvent.KEYCODE_O, 89 KeyEvent.KEYCODE_P, 90 KeyEvent.KEYCODE_Q, 91 KeyEvent.KEYCODE_R, 92 KeyEvent.KEYCODE_S, 93 KeyEvent.KEYCODE_T, 94 KeyEvent.KEYCODE_U, 95 KeyEvent.KEYCODE_V, 96 KeyEvent.KEYCODE_W, 97 KeyEvent.KEYCODE_X, 98 KeyEvent.KEYCODE_Y, 99 KeyEvent.KEYCODE_Z, 100 KeyEvent.KEYCODE_F1, 101 KeyEvent.KEYCODE_F2, 102 KeyEvent.KEYCODE_F3, 103 KeyEvent.KEYCODE_F4, 104 KeyEvent.KEYCODE_F5, 105 KeyEvent.KEYCODE_F6, 106 KeyEvent.KEYCODE_F7, 107 KeyEvent.KEYCODE_F8, 108 KeyEvent.KEYCODE_F9, 109 KeyEvent.KEYCODE_F10, 110 KeyEvent.KEYCODE_F11, 111 KeyEvent.KEYCODE_F12, 112 KeyEvent.KEYCODE_NUMPAD_0, 113 KeyEvent.KEYCODE_NUMPAD_1, 114 KeyEvent.KEYCODE_NUMPAD_2, 115 KeyEvent.KEYCODE_NUMPAD_3, 116 KeyEvent.KEYCODE_NUMPAD_4, 117 KeyEvent.KEYCODE_NUMPAD_5, 118 KeyEvent.KEYCODE_NUMPAD_6, 119 KeyEvent.KEYCODE_NUMPAD_7, 120 KeyEvent.KEYCODE_NUMPAD_8, 121 KeyEvent.KEYCODE_NUMPAD_9, 122 KeyEvent.KEYCODE_NUMPAD_DIVIDE, 123 KeyEvent.KEYCODE_NUMPAD_MULTIPLY, 124 KeyEvent.KEYCODE_NUMPAD_SUBTRACT, 125 KeyEvent.KEYCODE_NUMPAD_ADD, 126 KeyEvent.KEYCODE_NUMPAD_DOT, 127 KeyEvent.KEYCODE_NUMPAD_COMMA, 128 KeyEvent.KEYCODE_NUMPAD_ENTER, 129 KeyEvent.KEYCODE_NUMPAD_EQUALS, 130 KeyEvent.KEYCODE_NUMPAD_LEFT_PAREN, 131 KeyEvent.KEYCODE_NUMPAD_RIGHT_PAREN, 132 KeyEvent.KEYCODE_GRAVE, 133 KeyEvent.KEYCODE_MINUS, 134 KeyEvent.KEYCODE_EQUALS, 135 KeyEvent.KEYCODE_LEFT_BRACKET, 136 KeyEvent.KEYCODE_RIGHT_BRACKET, 137 KeyEvent.KEYCODE_BACKSLASH, 138 KeyEvent.KEYCODE_SEMICOLON, 139 KeyEvent.KEYCODE_APOSTROPHE, 140 KeyEvent.KEYCODE_COMMA, 141 KeyEvent.KEYCODE_PERIOD, 142 KeyEvent.KEYCODE_SLASH, 143 KeyEvent.KEYCODE_ALT_LEFT, 144 KeyEvent.KEYCODE_ALT_RIGHT, 145 KeyEvent.KEYCODE_CTRL_LEFT, 146 KeyEvent.KEYCODE_CTRL_RIGHT, 147 KeyEvent.KEYCODE_SHIFT_LEFT, 148 KeyEvent.KEYCODE_SHIFT_RIGHT, 149 KeyEvent.KEYCODE_META_LEFT, 150 KeyEvent.KEYCODE_META_RIGHT, 151 KeyEvent.KEYCODE_CAPS_LOCK, 152 KeyEvent.KEYCODE_SCROLL_LOCK, 153 KeyEvent.KEYCODE_NUM_LOCK, 154 KeyEvent.KEYCODE_ENTER, 155 KeyEvent.KEYCODE_TAB, 156 KeyEvent.KEYCODE_SPACE, 157 KeyEvent.KEYCODE_DPAD_DOWN, 158 KeyEvent.KEYCODE_DPAD_UP, 159 KeyEvent.KEYCODE_DPAD_LEFT, 160 KeyEvent.KEYCODE_DPAD_RIGHT, 161 KeyEvent.KEYCODE_MOVE_END, 162 KeyEvent.KEYCODE_MOVE_HOME, 163 KeyEvent.KEYCODE_PAGE_DOWN, 164 KeyEvent.KEYCODE_PAGE_UP, 165 KeyEvent.KEYCODE_DEL, 166 KeyEvent.KEYCODE_FORWARD_DEL, 167 KeyEvent.KEYCODE_INSERT, 168 KeyEvent.KEYCODE_ESCAPE, 169 KeyEvent.KEYCODE_BREAK, 170 KeyEvent.KEYCODE_BACK, 171 KeyEvent.KEYCODE_FORWARD, 172 }) 173 @Retention(RetentionPolicy.SOURCE) 174 public @interface SupportedKeycode { 175 } 176 177 private final @Action int mAction; 178 private final int mKeyCode; 179 VirtualKeyEvent(@ction int action, int keyCode)180 private VirtualKeyEvent(@Action int action, int keyCode) { 181 mAction = action; 182 mKeyCode = keyCode; 183 } 184 VirtualKeyEvent(@onNull Parcel parcel)185 private VirtualKeyEvent(@NonNull Parcel parcel) { 186 mAction = parcel.readInt(); 187 mKeyCode = parcel.readInt(); 188 } 189 190 @Override writeToParcel(@onNull Parcel parcel, int parcelableFlags)191 public void writeToParcel(@NonNull Parcel parcel, int parcelableFlags) { 192 parcel.writeInt(mAction); 193 parcel.writeInt(mKeyCode); 194 } 195 196 @Override describeContents()197 public int describeContents() { 198 return 0; 199 } 200 201 /** 202 * Returns the key code associated with this event. 203 */ getKeyCode()204 public int getKeyCode() { 205 return mKeyCode; 206 } 207 208 /** 209 * Returns the action associated with this event. 210 */ getAction()211 public @Action int getAction() { 212 return mAction; 213 } 214 215 /** 216 * Builder for {@link VirtualKeyEvent}. 217 */ 218 public static final class Builder { 219 220 private @Action int mAction = ACTION_UNKNOWN; 221 private int mKeyCode = -1; 222 223 /** 224 * Creates a {@link VirtualKeyEvent} object with the current builder configuration. 225 */ build()226 public @NonNull VirtualKeyEvent build() { 227 if (mAction == ACTION_UNKNOWN || mKeyCode == -1) { 228 throw new IllegalArgumentException( 229 "Cannot build virtual key event with unset fields"); 230 } 231 return new VirtualKeyEvent(mAction, mKeyCode); 232 } 233 234 /** 235 * Sets the Android key code of the event. 236 * 237 * @return this builder, to allow for chaining of calls 238 */ setKeyCode(@upportedKeycode int keyCode)239 public @NonNull Builder setKeyCode(@SupportedKeycode int keyCode) { 240 mKeyCode = keyCode; 241 return this; 242 } 243 244 /** 245 * Sets the action of the event. 246 * 247 * @return this builder, to allow for chaining of calls 248 */ setAction(@ction int action)249 public @NonNull Builder setAction(@Action int action) { 250 if (action != ACTION_DOWN && action != ACTION_UP) { 251 throw new IllegalArgumentException("Unsupported action type"); 252 } 253 mAction = action; 254 return this; 255 } 256 } 257 258 public static final @NonNull Parcelable.Creator<VirtualKeyEvent> CREATOR = 259 new Parcelable.Creator<VirtualKeyEvent>() { 260 public VirtualKeyEvent createFromParcel(Parcel source) { 261 return new VirtualKeyEvent(source); 262 } 263 264 public VirtualKeyEvent[] newArray(int size) { 265 return new VirtualKeyEvent[size]; 266 } 267 }; 268 } 269