-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Expand file tree
/
Copy pathKeys.java
More file actions
215 lines (194 loc) · 5.83 KB
/
Keys.java
File metadata and controls
215 lines (194 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.openqa.selenium;
import java.util.List;
import org.jspecify.annotations.Nullable;
/**
* Representations of pressable keys that aren't text. These are stored in the Unicode PUA (Private
* Use Area) code points, 0xE000–0xF8FF. These values are used internally by WebDriver to simulate
* keyboard input where standard Unicode characters are insufficient, such as modifier and control
* keys.
*
* <p>The codes follow conventions partially established by the W3C WebDriver specification and the
* Selenium project. Some values (e.g., RIGHT_SHIFT, RIGHT_COMMAND) are used in ChromeDriver but are
* not currently part of the W3C specification. Others (e.g., OPTION) are symbolic aliases for
* existing keys.
*
* <p>For consistency across platforms and drivers, values should be verified before assuming native
* support.
*
* @see <a href="https://www.w3.org/TR/webdriver/#keyboard-actions">W3C WebDriver Keyboard
* Actions</a>
* @see <a href="http://www.google.com.au/search?&q=unicode+pua&btnK=Search">Unicode PUA
* Overview</a>
*/
public enum Keys implements CharSequence {
// Basic control characters
NULL('\uE000'),
CANCEL('\uE001'), // ^break
HELP('\uE002'),
BACK_SPACE('\uE003'),
TAB('\uE004'),
CLEAR('\uE005'),
RETURN('\uE006'),
ENTER('\uE007'),
SHIFT('\uE008'),
LEFT_SHIFT(Keys.SHIFT),
CONTROL('\uE009'),
LEFT_CONTROL(Keys.CONTROL),
ALT('\uE00A'),
LEFT_ALT(Keys.ALT),
PAUSE('\uE00B'),
ESCAPE('\uE00C'),
SPACE('\uE00D'),
PAGE_UP('\uE00E'),
PAGE_DOWN('\uE00F'),
END('\uE010'),
HOME('\uE011'),
LEFT('\uE012'),
ARROW_LEFT(Keys.LEFT),
UP('\uE013'),
ARROW_UP(Keys.UP),
RIGHT('\uE014'),
ARROW_RIGHT(Keys.RIGHT),
DOWN('\uE015'),
ARROW_DOWN(Keys.DOWN),
INSERT('\uE016'),
DELETE('\uE017'),
SEMICOLON('\uE018'),
EQUALS('\uE019'),
// Number pad keys
NUMPAD0('\uE01A'),
NUMPAD1('\uE01B'),
NUMPAD2('\uE01C'),
NUMPAD3('\uE01D'),
NUMPAD4('\uE01E'),
NUMPAD5('\uE01F'),
NUMPAD6('\uE020'),
NUMPAD7('\uE021'),
NUMPAD8('\uE022'),
NUMPAD9('\uE023'),
MULTIPLY('\uE024'),
ADD('\uE025'),
SEPARATOR('\uE026'),
SUBTRACT('\uE027'),
DECIMAL('\uE028'),
DIVIDE('\uE029'),
// Function keys
F1('\uE031'),
F2('\uE032'),
F3('\uE033'),
F4('\uE034'),
F5('\uE035'),
F6('\uE036'),
F7('\uE037'),
F8('\uE038'),
F9('\uE039'),
F10('\uE03A'),
F11('\uE03B'),
F12('\uE03C'),
META('\uE03D'),
COMMAND(Keys.META),
// Extended macOS/ChromeDriver keys (based on observed Chrome usage)
RIGHT_SHIFT('\uE050'), // aligns with ChromeDriver usage
RIGHT_CONTROL('\uE051'),
RIGHT_ALT('\uE052'),
RIGHT_COMMAND('\uE053'),
// macOS-friendly alias (do NOT introduce new codes)
OPTION(Keys.ALT),
/**
* @deprecated The FN key is not part of the W3C WebDriver specification and does not have a
* standardized Unicode mapping. Its behavior is not guaranteed across drivers/platforms.
*/
@Deprecated
FN(Keys.RIGHT_CONTROL),
ZENKAKU_HANKAKU('\uE040');
private final char keyCode;
private final int codePoint;
Keys(Keys key) {
this(key.charAt(0));
}
Keys(char keyCode) {
this.keyCode = keyCode;
this.codePoint = String.valueOf(keyCode).codePoints().findFirst().getAsInt();
}
public int getCodePoint() {
return codePoint;
}
@Override
public char charAt(int index) {
if (index != 0) {
throw new IndexOutOfBoundsException("Index: " + index + ", Length: 1");
}
return keyCode;
}
@Override
public int length() {
return 1;
}
@Override
public CharSequence subSequence(int start, int end) {
if (start == 0 && end == 1) {
return String.valueOf(keyCode);
}
throw new IndexOutOfBoundsException();
}
@Override
public String toString() {
return String.valueOf(keyCode);
}
/**
* Simulate pressing many keys at once in a "chord". Takes a sequence of Keys.XXXX or strings;
* appends each to a string, adds the chord termination key (Keys.NULL), and returns it.
*
* <p>Note: Keys.NULL signals release of modifier keys like CTRL/ALT/SHIFT via keyup events.
*
* @param value characters to send
* @return String representation of the char sequence
*/
public static String chord(CharSequence... value) {
return chord(List.of(value));
}
/**
* Overload of {@link #chord(CharSequence...)} that accepts an iterable.
*
* @param value characters to send
* @return String representation of the char sequence
*/
public static String chord(Iterable<CharSequence> value) {
StringBuilder builder = new StringBuilder();
for (CharSequence seq : value) {
builder.append(seq);
}
builder.append(Keys.NULL);
return builder.toString();
}
/**
* Retrieves the {@link Keys} enum constant corresponding to the given Unicode character.
*
* @param key unicode character code
* @return special key linked to the character code, or null if not found
*/
public static @Nullable Keys getKeyFromUnicode(char key) {
for (Keys unicodeKey : values()) {
if (unicodeKey.charAt(0) == key) {
return unicodeKey;
}
}
return null;
}
}