-
-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathKeyboardModifiersState.java
More file actions
104 lines (90 loc) · 3.03 KB
/
KeyboardModifiersState.java
File metadata and controls
104 lines (90 loc) · 3.03 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
// 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.htmlunit;
import java.util.HashSet;
import java.util.Set;
import org.htmlunit.html.Keyboard;
import org.openqa.selenium.Keys;
/**
* Holds the state of the modifier keys (Shift, ctrl, alt).
*
* @author Alexei Barantsev
* @author Ahmed Ashour
* @author Ronald Brill
*/
class KeyboardModifiersState {
private final Set<Character> set_ = new HashSet<>();
private boolean shiftPressed_;
private boolean ctrlPressed_;
private boolean altPressed_;
public boolean isShiftPressed() {
return shiftPressed_;
}
public boolean isCtrlPressed() {
return ctrlPressed_;
}
public boolean isAltPressed() {
return altPressed_;
}
public void storeKeyDown(final char key) {
storeIfEqualsShift(key, true);
storeIfEqualsCtrl(key, true);
storeIfEqualsAlt(key, true);
set_.add(key);
}
public void storeKeyUp(final char key) {
storeIfEqualsShift(key, false);
storeIfEqualsCtrl(key, false);
storeIfEqualsAlt(key, false);
set_.remove(key);
}
public void addToKeyboard(final Keyboard keyboard, final char ch, final boolean isPress) {
if (HtmlUnitKeyboardMapping.isSpecialKey(ch)) {
final int keyCode = HtmlUnitKeyboardMapping.getKeysMapping(ch);
if (isPress) {
keyboard.press(keyCode);
storeKeyDown(ch);
} else {
keyboard.release(keyCode);
storeKeyUp(ch);
}
} else {
keyboard.type(ch);
}
}
private void storeIfEqualsShift(final char key, final boolean keyState) {
if (key == Keys.SHIFT.charAt(0)) {
shiftPressed_ = keyState;
}
}
private void storeIfEqualsCtrl(final char key, final boolean keyState) {
if (key == Keys.CONTROL.charAt(0)) {
ctrlPressed_ = keyState;
}
}
private void storeIfEqualsAlt(final char key, final boolean keyState) {
if (key == Keys.ALT.charAt(0)) {
altPressed_ = keyState;
}
}
boolean isPressed(final Keys keys) {
return isPressed(keys.charAt(0));
}
boolean isPressed(final char ch) {
return set_.contains(ch);
}
}