Key
com.jsmacrosce.jsmacros.client.api.event.impl.EventKey
Fires for keyboard and mouse-button input before the game's input handling. Cancelling causes the game's calling input callback to be cancelled. Unknown keys and repeats are dropped, and with a screen open input may be suppressed by the JsMacros setting or focused-screen/widget rules. Releases for keys pressed before a screen opened are still reported.
JsMacros updates its pressed-key tracker before filtering or firing the event, so cancellation does not undo that update. Use key to distinguish keyboard keys from mouse buttons.
js
JsMacros.on("Key", JavaWrapper.methodToJava(function (event) {
if (event.action === 0) {
// 0 is the release, 1 is the press
return;
}
if (event.key === "key.keyboard.j") {
if (event.mods === "key.keyboard.left.control") {
Chat.log("ctrl+J swallowed, the game will not see it");
event.cancel();
}
}
}))Constructors
new EventKey
Initializes an event with the supplied action and input names without validating or dispatching them. The action is stored unchanged;
1 conventionally means press and 0 release. The strings are also stored unchanged, including arbitrary or null values. This constructor does not update the pressed-key tracker or apply input filters.Parameters:
action: action value to storekey: input name to store, conventionally an internal keyboard or mouse-button namemods: modifier names to store, conventionally joined by+, or an empty string
Fields
action
Input action:
1 for a press and 0 for a release. Repeats are filtered before an event is created.key
mods
Held modifiers as left-side game key names joined by
+ in shift, control, alt order; empty when none are held. See getModInt(String) and getKeyModifiers(int) for conversions to and from the bitmask form.Methods
getKeyModifiers
Converts modifier bits into the game's left-side modifier names, joined by
+ in shift, control, alt order. Other bits are ignored; no set bits produces an empty string.Parameters:
mods: the modifier bitmask, where1is shift,2is control and4is alt
Returns: the held modifiers as
+ joined game modifier names, or an empty string when no modifier is setgetModInt
Converts
+-separated modifier names to bits: 1 for shift, 2 for control, and 4 for alt. Both left- and right-side names are accepted; unrecognized parts are ignored, and an empty or wholly unrecognized string yields 0.Parameters:
mods: the held modifiers as+joined game modifier names, such as the mods field carries, or an empty string for none
Returns: the combined bitmask, where
1 is shift, 2 is control and 4 is altparse
static parse(int key, int scancode, int action, int mods): boolean#Converts a raw keyboard or mouse-button report into a
Key event. It updates JsMacros' pressed-key tracker, applies the screen filters, and then fires the event. The scancode is unused. Calling this method directly fires a synthetic event; only the game's input callback uses its cancellation result to stop game handling. The integer overload retains legacy GLFW numbering on every target, including Minecraft 26.3: left/right/middle mouse buttons are 0/1/2 and the A key is 65. Recognized legacy keys are translated to their native equivalents; keys without an SDL equivalent are ignored on 26.3.Parameters:
key: legacy GLFW code; values through7select mouse buttons, larger values select keyboard keysscancode: the raw scancode, which this method does not readaction: what happened, using the windowing library's numbering, where1is a press and0is a releasemods: the modifier bitmask, where1is shift,2is control and4is alt
Returns: whether a listener cancelled the event