Skip to content

Commit f375257

Browse files
manuelsblancocgoldbergdiemol
authored
[FEAT] Add macOS-specific keys (OPTION, FN) to Keys enum for improved… (#15910)
* [FEAT] Add macOS-specific keys (OPTION, FN) to Keys enum for improved platform support * [FEAT] These additions (RIGHT_SHIFT, RIGHT_CONTROL, RIGHT_ALT, RIGHT_COMMAND) follow conventions already in use by ChromeDriver and allow for more accurate key simulations. While not currently in the W3C WebDriver specification, they represent a practical standard used in the field. * [FEAT] Add macOS-specific and ChromeDriver-aligned extended keys - Added RIGHT_SHIFT, RIGHT_CONTROL, RIGHT_ALT, and RIGHT_COMMAND using Unicode PUA codes observed in ChromeDriver. - Included symbolic macOS keys OPTION and FN, marked with TODO comments for future validation against W3C WebDriver spec. - Updated class-level Javadoc to clarify the role of PUA mappings and their interoperability considerations. These additions improve platform representation and lay the groundwork for consistent macOS key handling. * Format script --------- Co-authored-by: Corey Goldberg <1113081+cgoldberg@users.noreply.github.com> Co-authored-by: Diego Molina <diemol@users.noreply.github.com> Co-authored-by: Diego Molina <diemol@gmail.com>
1 parent 47af349 commit f375257

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

java/src/org/openqa/selenium/Keys.java

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,26 @@
2323

2424
/**
2525
* Representations of pressable keys that aren't text. These are stored in the Unicode PUA (Private
26-
* Use Area) code points, 0xE000-0xF8FF.
26+
* Use Area) code points, 0xE000–0xF8FF. These values are used internally by WebDriver to simulate
27+
* keyboard input where standard Unicode characters are insufficient, such as modifier and control
28+
* keys.
2729
*
28-
* @see <a
29-
* href="http://www.google.com.au/search?&amp;q=unicode+pua&amp;btnK=Search">http://www.google.com.au/search?&amp;q=unicode+pua&amp;btnK=Search</a>
30+
* <p>The codes follow conventions partially established by the W3C WebDriver specification and the
31+
* Selenium project. Some values (e.g., RIGHT_SHIFT, RIGHT_COMMAND) are used in ChromeDriver but are
32+
* not currently part of the W3C spec. Others (e.g., OPTION, FN) are symbolic and reserved for
33+
* possible future mapping.
34+
*
35+
* <p>For consistency across platforms and drivers, values should be verified before assuming native
36+
* support.
37+
*
38+
* @see <a href="https://www.w3.org/TR/webdriver/#keyboard-actions">W3C WebDriver Keyboard
39+
* Actions</a>
40+
* @see <a href="http://www.google.com.au/search?&q=unicode+pua&btnK=Search">Unicode PUA
41+
* Overview</a>
3042
*/
3143
@NullMarked
3244
public enum Keys implements CharSequence {
45+
// Basic control characters
3346
NULL('\uE000'),
3447
CANCEL('\uE001'), // ^break
3548
HELP('\uE002'),
@@ -99,6 +112,16 @@ public enum Keys implements CharSequence {
99112
META('\uE03D'),
100113
COMMAND(Keys.META),
101114

115+
// Extended macOS/ChromeDriver keys (based on observed Chrome usage)
116+
RIGHT_SHIFT('\uE050'), // aligns with ChromeDriver usage
117+
RIGHT_CONTROL('\uE051'),
118+
RIGHT_ALT('\uE052'),
119+
RIGHT_COMMAND('\uE053'),
120+
121+
// Symbolic macOS keys not yet standardized
122+
OPTION('\uE050'), // TODO: verify Unicode value with WebDriver spec
123+
FN('\uE051'), // TODO: symbolic only; confirm or remove in future
124+
102125
ZENKAKU_HANKAKU('\uE040');
103126

104127
private final char keyCode;
@@ -122,7 +145,6 @@ public char charAt(int index) {
122145
if (index == 0) {
123146
return keyCode;
124147
}
125-
126148
return 0;
127149
}
128150

@@ -136,7 +158,6 @@ public CharSequence subSequence(int start, int end) {
136158
if (start == 0 && end == 1) {
137159
return String.valueOf(keyCode);
138160
}
139-
140161
throw new IndexOutOfBoundsException();
141162
}
142163

@@ -147,11 +168,9 @@ public String toString() {
147168

148169
/**
149170
* Simulate pressing many keys at once in a "chord". Takes a sequence of Keys.XXXX or strings;
150-
* appends each of the values to a string, and adds the chord termination key (Keys.NULL) and
151-
* returns the resultant string.
171+
* appends each to a string, adds the chord termination key (Keys.NULL), and returns it.
152172
*
153-
* <p>Note: When the low-level webdriver key handlers see Keys.NULL, active modifier keys
154-
* (CTRL/ALT/SHIFT/etc) release via a keyup event.
173+
* <p>Note: Keys.NULL signals release of modifier keys like CTRL/ALT/SHIFT via keyup events.
155174
*
156175
* @param value characters to send
157176
* @return String representation of the char sequence
@@ -161,35 +180,32 @@ public static String chord(CharSequence... value) {
161180
}
162181

163182
/**
164-
* @see #chord(CharSequence...)
183+
* Overload of {@link #chord(CharSequence...)} that accepts an iterable.
184+
*
165185
* @param value characters to send
166186
* @return String representation of the char sequence
167187
*/
168188
public static String chord(Iterable<CharSequence> value) {
169189
StringBuilder builder = new StringBuilder();
170-
171190
for (CharSequence seq : value) {
172191
builder.append(seq);
173192
}
174-
175193
builder.append(Keys.NULL);
176194
return builder.toString();
177195
}
178196

179197
/**
180-
* Get the special key representation, {@link Keys}, of the supplied character if there is one. If
181-
* there is no special key tied to this character, null will be returned.
198+
* Retrieves the {@link Keys} enum constant corresponding to the given Unicode character.
182199
*
183200
* @param key unicode character code
184-
* @return special key linked to the character code, or null if character is not a special key
201+
* @return special key linked to the character code, or null if not found
185202
*/
186203
public static @Nullable Keys getKeyFromUnicode(char key) {
187204
for (Keys unicodeKey : values()) {
188205
if (unicodeKey.charAt(0) == key) {
189206
return unicodeKey;
190207
}
191208
}
192-
193209
return null;
194210
}
195211
}

0 commit comments

Comments
 (0)