3
3
import com .denizenscript .denizencore .events .ScriptEvent ;
4
4
import com .denizenscript .denizencore .objects .ObjectTag ;
5
5
import com .denizenscript .denizencore .objects .core .ElementTag ;
6
+ import it .unimi .dsi .fastutil .ints .Int2ObjectMap ;
7
+ import it .unimi .dsi .fastutil .ints .Int2ObjectOpenHashMap ;
8
+ import net .minecraft .client .util .InputUtil ;
6
9
7
- import java .util .Arrays ;
8
- import java .util .Map ;
9
- import java .util .stream .Collectors ;
10
+ public class KeyPressReleaseScriptEvent extends ScriptEvent {
10
11
11
- public class KeyPressReleaseEvent extends ScriptEvent {
12
+ public static KeyPressReleaseScriptEvent instance ;
12
13
13
- public static KeyPressReleaseEvent instance ;
14
-
15
- public KeyboardKeys key ;
14
+ public Key key ;
16
15
public boolean pressed ;
16
+ public InputDevice device ;
17
17
18
18
19
- public KeyPressReleaseEvent () {
20
- registerCouldMatcher ("keyboard <'key'> pressed|released" );
19
+ public KeyPressReleaseScriptEvent () {
20
+ registerCouldMatcher ("<'input_device'> key pressed|released|toggled" );
21
+ registerSwitches ("name" );
21
22
instance = this ;
22
23
}
23
24
24
25
@ Override
25
26
public boolean matches (ScriptPath path ) {
26
- String keyMatcher = path .eventArgLowerAt (1 );
27
- if (!keyMatcher .equals ("key" ) && !runGenericCheck (keyMatcher , key .getName ())) {
27
+ if (!runGenericSwitchCheck (path , "name" , key .getName ())) {
28
+ return false ;
29
+ }
30
+ if (!runGenericCheck (path .eventArgLowerAt (0 ), device .name ())) {
31
+ return false ;
32
+ }
33
+ String operation = path .eventArgLowerAt (2 );
34
+ if (operation .equals ("pressed" ) && !pressed ) {
28
35
return false ;
29
36
}
30
- if (pressed != path . eventArgLowerAt ( 2 ). equals ("pressed" ) ) {
37
+ if (operation . equals ("released" ) && pressed ) {
31
38
return false ;
32
39
}
33
40
return super .matches (path );
34
41
}
35
42
36
43
@ Override
37
44
public ObjectTag getContext (String name ) {
38
- switch (name ) {
39
- case "key" : return new ElementTag (key .getName ());
40
- }
41
- return super .getContext (name );
45
+ return switch (name ) {
46
+ case "key" -> new ElementTag (key .getName (), true );
47
+ case "device" -> new ElementTag (device );
48
+ default -> super .getContext (name );
49
+ };
42
50
}
43
51
44
- public void handleKeyPressStateChange (int code , boolean pressed ) {
45
- key = KeyboardKeys .keysByCode .get (code );
52
+ public void handleKeyPressStateChange (InputUtil . Key key , boolean pressed ) {
53
+ this . key = Key .keysByCode .get (key . getCode () );
46
54
this .pressed = pressed ;
55
+ this .device = key .getCategory () == InputUtil .Type .KEYSYM ? InputDevice .KEYBOARD : InputDevice .MOUSE ;
47
56
fire ();
48
57
}
49
58
50
- enum KeyboardKeys {
59
+ enum InputDevice { KEYBOARD , MOUSE }
60
+
61
+ enum Key {
51
62
UNKNOWN (-1 ),
63
+ // Keyboard keys
52
64
SPACE (32 ),
53
65
APOSTROPHE (39 ),
54
66
COMMA (44 ),
@@ -143,23 +155,23 @@ enum KeyboardKeys {
143
155
F23 (312 ),
144
156
F24 (313 ),
145
157
F25 (314 ),
146
- KP_0 (320 ),
147
- KP_1 (321 ),
148
- KP_2 (322 ),
149
- KP_3 (323 ),
150
- KP_4 (324 ),
151
- KP_5 (325 ),
152
- KP_6 (326 ),
153
- KP_7 (327 ),
154
- KP_8 (328 ),
155
- KP_9 (329 ),
156
- KP_DECIMAL (330 ),
157
- KP_DIVIDE (331 ),
158
- KP_MULTIPLY (332 ),
159
- KP_SUBTRACT (333 ),
160
- KP_ADD (334 ),
161
- KP_ENTER (335 ),
162
- KP_EQUAL (336 ),
158
+ KEYPAD_0 (320 ),
159
+ KEYPAD_1 (321 ),
160
+ KEYPAD_2 (322 ),
161
+ KEYPAD_3 (323 ),
162
+ KEYPAD_4 (324 ),
163
+ KEYPAD_5 (325 ),
164
+ KEYPAD_6 (326 ),
165
+ KEYPAD_7 (327 ),
166
+ KEYPAD_8 (328 ),
167
+ KEYPAD_9 (329 ),
168
+ KEYPAD_DECIMAL (330 ),
169
+ KEYPAD_DIVIDE (331 ),
170
+ KEYPAD_MULTIPLY (332 ),
171
+ KEYPAD_SUBTRACT (333 ),
172
+ KEYPAD_ADD (334 ),
173
+ KEYPAD_ENTER (335 ),
174
+ KEYPAD_EQUAL (336 ),
163
175
LEFT_SHIFT (340 ),
164
176
LEFT_CONTROL (341 ),
165
177
LEFT_ALT (342 ),
@@ -168,16 +180,26 @@ enum KeyboardKeys {
168
180
RIGHT_CONTROL (345 ),
169
181
RIGHT_ALT (346 ),
170
182
RIGHT_SUPER (347 ),
171
- MENU (348 );
183
+ MENU (348 ),
184
+
185
+ // Mouse buttons
186
+ MOUSE_LEFT (0 ),
187
+ MOUSE_RIGHT (1 ),
188
+ MOUSE_MIDDLE (2 ),
189
+ MOUSE_BUTTON_4 (3 ),
190
+ MOUSE_BUTTON_5 (4 ),
191
+ MOUSE_BUTTON_6 (5 ),
192
+ MOUSE_BUTTON_7 (6 ),
193
+ MOUSE_BUTTON_8 (7 );
172
194
173
195
public final int code ;
174
196
public String alternateName ;
175
197
176
- KeyboardKeys (int code ) {
198
+ Key (int code ) {
177
199
this .code = code ;
178
200
}
179
201
180
- KeyboardKeys (int code , String alternateName ) {
202
+ Key (int code , String alternateName ) {
181
203
this .code = code ;
182
204
this .alternateName = alternateName ;
183
205
}
@@ -186,6 +208,14 @@ public String getName() {
186
208
return alternateName == null ? name () : alternateName ;
187
209
}
188
210
189
- public static final Map <Integer , KeyboardKeys > keysByCode = Arrays .stream (KeyboardKeys .values ()).collect (Collectors .toMap (key -> key .code , key -> key ));
211
+ public static final Int2ObjectMap <Key > keysByCode ;
212
+
213
+ static {
214
+ Key [] keys = Key .values ();
215
+ keysByCode = new Int2ObjectOpenHashMap <>(keys .length );
216
+ for (Key key : keys ) {
217
+ keysByCode .put (key .code , key );
218
+ }
219
+ }
190
220
}
191
221
}
0 commit comments