|
| 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 | +} |
0 commit comments