Skip to content

Commit 79f7350

Browse files
committed
fix #1097 Add wrappers and enumerations for keyboard keys and events
1 parent 32a5d76 commit 79f7350

File tree

5 files changed

+1153
-0
lines changed

5 files changed

+1153
-0
lines changed
Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
/*
2+
* Copyright © 2019 Dominokit
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+
package org.dominokit.domino.ui.keyboard;
17+
18+
import elemental2.dom.KeyboardEvent;
19+
20+
/**
21+
* Wrapper around a DOM {@link KeyboardEvent} that normalizes its {@code code} and {@code key}
22+
* values to the Domino UI {@link KeyboardKeyCode} and {@link KeyboardKey} enums.
23+
*
24+
* <p>Provides convenience accessors for modifier flags and common properties, plus helpers to stop
25+
* or prevent the underlying event.
26+
*/
27+
public class KeyEvent {
28+
29+
private final KeyboardEvent event;
30+
private final KeyboardKeyCode code;
31+
private final KeyboardKey key;
32+
33+
/**
34+
* Factory method for creating a {@link KeyEvent} from a DOM event.
35+
*
36+
* @param event the raw {@link KeyboardEvent}
37+
* @return a new {@link KeyEvent} wrapping the supplied event
38+
*/
39+
public static KeyEvent of(KeyboardEvent event) {
40+
return new KeyEvent(event);
41+
}
42+
43+
/**
44+
* Creates a new instance wrapping the provided keyboard event.
45+
*
46+
* @param event the raw {@link KeyboardEvent}
47+
*/
48+
public KeyEvent(KeyboardEvent event) {
49+
this.event = event;
50+
this.code = KeyboardKeyCodes.of(event.code);
51+
this.key = KeyboardKeys.of(event.key);
52+
}
53+
54+
/**
55+
* Returns the wrapped DOM event.
56+
*
57+
* @return the original {@link KeyboardEvent}
58+
*/
59+
public KeyboardEvent getEvent() {
60+
return event;
61+
}
62+
/**
63+
* Returns the normalized key code.
64+
*
65+
* @return a {@link KeyboardKeyCode} corresponding to {@code event.code}
66+
*/
67+
public KeyboardKeyCode getCode() {
68+
return code;
69+
}
70+
/**
71+
* Returns the normalized key value.
72+
*
73+
* @return a {@link KeyboardKey} corresponding to {@code event.key}
74+
*/
75+
public KeyboardKey getKey() {
76+
return key;
77+
}
78+
79+
/**
80+
* Checks whether the event code matches the provided code.
81+
*
82+
* @param code the code to compare against
83+
* @return {@code true} when the codes match
84+
*/
85+
public boolean is(KeyboardKeyCode code) {
86+
return this.code.equals(code);
87+
}
88+
89+
/**
90+
* Checks whether the event key matches the provided key.
91+
*
92+
* @param key the key to compare against
93+
* @return {@code true} when the keys match
94+
*/
95+
public boolean is(KeyboardKey key) {
96+
return this.key.equals(key);
97+
}
98+
99+
/**
100+
* Stops propagation and prevents the default action.
101+
*
102+
* @return this {@link KeyEvent} for chaining
103+
*/
104+
public KeyEvent stop() {
105+
event.stopPropagation();
106+
event.preventDefault();
107+
return this;
108+
}
109+
110+
/**
111+
* Prevents the default action.
112+
*
113+
* @return this {@link KeyEvent} for chaining
114+
*/
115+
public KeyEvent preventDefault() {
116+
event.preventDefault();
117+
return this;
118+
}
119+
120+
/**
121+
* Stops event propagation.
122+
*
123+
* @return this {@link KeyEvent} for chaining
124+
*/
125+
public KeyEvent stopPropagation() {
126+
event.stopPropagation();
127+
return this;
128+
}
129+
130+
/**
131+
* Indicates whether the key is being repeated (auto-repeat).
132+
*
133+
* @return {@code true} for repeated key events
134+
*/
135+
public boolean isRepeat() {
136+
return event.repeat;
137+
}
138+
139+
/**
140+
* Indicates whether the Ctrl modifier is active.
141+
*
142+
* @return {@code true} if {@code ctrlKey} is pressed
143+
*/
144+
public boolean isCtrl() {
145+
return event.ctrlKey;
146+
}
147+
148+
/**
149+
* Indicates whether the Shift modifier is active.
150+
*
151+
* @return {@code true} if {@code shiftKey} is pressed
152+
*/
153+
public boolean isShift() {
154+
return event.shiftKey;
155+
}
156+
157+
/**
158+
* Indicates whether the Alt modifier is active.
159+
*
160+
* @return {@code true} if {@code altKey} is pressed
161+
*/
162+
public boolean isAlt() {
163+
return event.altKey;
164+
}
165+
166+
/**
167+
* Indicates whether the Meta modifier is active.
168+
*
169+
* @return {@code true} if {@code metaKey} is pressed
170+
*/
171+
public boolean isMeta() {
172+
return event.metaKey;
173+
}
174+
175+
/**
176+
* Indicates whether the key code represents a lock key.
177+
*
178+
* @return {@code true} if the code is one of the lock keys
179+
*/
180+
public boolean isLockKey() {
181+
return code.isLockKey();
182+
}
183+
184+
/**
185+
* Indicates whether the key code belongs to the numpad group.
186+
*
187+
* @return {@code true} if the code is a numpad key
188+
*/
189+
public boolean isNumpadKey() {
190+
return code.isNumpadKey();
191+
}
192+
193+
/**
194+
* Indicates whether the key code represents a function key.
195+
*
196+
* @return {@code true} if the code is a function key
197+
*/
198+
public boolean isFunctionKey() {
199+
return code.isFunctionKey();
200+
}
201+
202+
/**
203+
* Indicates whether any modifier key is active (Ctrl, Shift, Alt, or Meta).
204+
*
205+
* @return {@code true} when a modifier is pressed
206+
*/
207+
public boolean isModifierKey() {
208+
return isCtrl() || isShift() || isAlt() || isMeta();
209+
}
210+
211+
/**
212+
* Returns the character associated with the key event, if provided.
213+
*
214+
* @return the character value from {@code event.char_}
215+
*/
216+
public String getChar() {
217+
return event.char_;
218+
}
219+
220+
/**
221+
* Returns the event locale.
222+
*
223+
* @return the locale string from the underlying event
224+
*/
225+
public String getLocale() {
226+
return event.locale;
227+
}
228+
229+
/**
230+
* Returns the location of the key on the keyboard (standard DOM locations).
231+
*
232+
* @return the numeric location from the underlying event
233+
*/
234+
public int getLocation() {
235+
return event.location;
236+
}
237+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright © 2019 Dominokit
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+
package org.dominokit.domino.ui.keyboard;
17+
18+
import java.util.Set;
19+
20+
/**
21+
* Defines a keyboard key representation used by Domino UI keyboard helpers.
22+
*
23+
* <p>Implementations typically map a key to one or more string values returned by the browser (for
24+
* example, {@code "ArrowUp"} and {@code "arrowup"}). Helper methods are provided to match an
25+
* incoming key string and to read a primary key value.
26+
*/
27+
public interface KeyboardKey {
28+
29+
/**
30+
* Returns all string representations associated with this key.
31+
*
32+
* @return a set of key strings, never {@code null}
33+
*/
34+
Set<String> getKeys();
35+
36+
/**
37+
* Checks whether the provided key string matches any of this key's representations.
38+
*
39+
* @param key the key value from a {@link elemental2.dom.KeyboardEvent}
40+
* @return {@code true} if the key matches, otherwise {@code false}
41+
*/
42+
default boolean matchesKey(String key) {
43+
return key != null && getKeys().contains(key);
44+
}
45+
46+
/**
47+
* Returns the first key representation for this key, or {@code null} if none exist.
48+
*
49+
* @return the primary key string or {@code null}
50+
*/
51+
default String getPrimaryKey() {
52+
return getKeys().stream().findFirst().orElse(null);
53+
}
54+
55+
/**
56+
* Indicates if the key is a modifier key such as Shift, Control, Alt, or Meta.
57+
*
58+
* @return {@code true} when the key is a modifier
59+
*/
60+
public boolean isModifier();
61+
62+
/**
63+
* Indicates if the key is one of the lock keys (CapsLock, NumLock, ScrollLock).
64+
*
65+
* @return {@code true} when the key is a lock key
66+
*/
67+
public boolean isLockKey();
68+
69+
/**
70+
* Indicates if the key is used for navigation (arrow keys, Home/End, PageUp/PageDown, Insert,
71+
* Delete).
72+
*
73+
* @return {@code true} when the key is a navigation key
74+
*/
75+
public boolean isNavigationKey();
76+
77+
/**
78+
* Indicates if the key is a function key (F1–F24).
79+
*
80+
* @return {@code true} when the key is a function key
81+
*/
82+
public boolean isFunctionKey();
83+
84+
/**
85+
* Indicates if the key belongs to the numpad group.
86+
*
87+
* @return {@code true} when the key is a numpad key
88+
*/
89+
public boolean isNumpadKey();
90+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright © 2019 Dominokit
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+
package org.dominokit.domino.ui.keyboard;
17+
18+
import java.util.Set;
19+
20+
/**
21+
* General contract for keyboard key codes. Allows the enum (and any future implementations) to
22+
* expose the raw browser KeyboardEvent.code strings.
23+
*/
24+
public interface KeyboardKeyCode {
25+
26+
/** All associated KeyboardEvent.code strings for this key. */
27+
Set<String> getCodes();
28+
29+
/** A convenience method to check if this key matches a given code. */
30+
default boolean matchesCode(String code) {
31+
return code != null && getCodes().contains(code);
32+
}
33+
34+
/** Optional convenience: primary / canonical code (first one). */
35+
default String getPrimaryCode() {
36+
return getCodes().stream().findFirst().orElse(null);
37+
}
38+
39+
/** @return {@code true} if this is any modifier key (Ctrl/Shift/Alt/Meta, any side). */
40+
public boolean isModifier();
41+
42+
/** @return {@code true} if this is a lock key (CapsLock, NumLock, ScrollLock). */
43+
public boolean isLockKey();
44+
45+
/** @return {@code true} if this is a navigation / cursor key. */
46+
public boolean isNavigationKey();
47+
48+
/** @return {@code true} if this is one of the function keys F1..F24. */
49+
public boolean isFunctionKey();
50+
51+
/** @return {@code true} if this is any numpad key. */
52+
public boolean isNumpadKey();
53+
}

0 commit comments

Comments
 (0)