Skip to content

Key ​

com.jsmacrosce.jsmacros.client.api.event.impl.EventKey

Since1.2.7Cancellable

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 ​

new EventKey(int action, String key, String mods)#
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 store
  • key: input name to store, conventionally an internal keyboard or mouse-button name
  • mods: modifier names to store, conventionally joined by +, or an empty string

Fields ​

action ​

action: intread-only#
Input action: 1 for a press and 0 for a release. Repeats are filtered before an event is created.

key ​

key: Stringread-only#
The game's internal name for the input, rather than its display name. Keyboard and mouse button names share this field (for example, key.keyboard.a and key.mouse.left).

mods ​

mods: Stringread-only#
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 ​

static getKeyModifiers(int mods): String#
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, where 1 is shift, 2 is control and 4 is alt
Returns: the held modifiers as + joined game modifier names, or an empty string when no modifier is set

getModInt ​

static getModInt(String mods): int#
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 alt

parse ​

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 through 7 select mouse buttons, larger values select keyboard keys
  • scancode: the raw scancode, which this method does not read
  • action: what happened, using the windowing library's numbering, where 1 is a press and 0 is a release
  • mods: the modifier bitmask, where 1 is shift, 2 is control and 4 is alt
Returns: whether a listener cancelled the event

toString ​

toString(): String#
Returns an event-name-prefixed diagnostic string containing the key field only. The key is inserted without escaping, so the result is not guaranteed to be valid JSON.
Returns: a string in the form %s:{"key": "%s"}, with the event name and raw key substituted