Skip to content

KeyBind ​

com.jsmacrosce.jsmacros.client.api.library.impl.FKeyBind

Gets and changes key and keybind states through the script global KeyBind.

Key names identify inputs (for example, key.keyboard.w); keybind names identify actions (for example, key.forward). key methods take key names, while keyBind methods and setKeyBind(String, String) take keybind names. State-setting calls are ignored while a screen is open. Press calls mark the state down and register a click; release calls mark it up. Use the matching release call after a press to avoid leaving a key held.

js
// Look up the current key assigned to forward.  
const binds = KeyBind.getKeyBindings();  
Chat.log(`forward is on ${binds.get("key.forward")}`);  

// Hold the forward action briefly, regardless of its assigned key.  
KeyBind.pressKey("key.keyboard.w");  
Client.waitTick(2);  
// and releasing marks it up again
KeyBind.releaseKey("key.keyboard.w");  

// the same thing by keybind name, which is what to use when the binding might be
// on a different key than the default
KeyBind.pressKeyBind("key.forward");  
Client.waitTick(2);  
KeyBind.releaseKeyBind("key.forward");  

// rebind jump to the numpad 0 key, and put it back afterwards
KeyBind.setKeyBind("key.jump", "key.keyboard.numpad.0");  
KeyBind.setKeyBind("key.jump", null);  

// what is held down at this moment
for (const key of KeyBind.getPressedKeys()) {
 Chat.log(`held: ${key}`);  
}

Accessible in scripts via the global KeyBind variable.

Nested types ​

Methods ​

getKeyBindings ​

getKeyBindings(): Map<String, String>Since1.2.2#
Returns a snapshot mapping each keybind name to its currently assigned key name.
Returns: a map from keybind names to assigned key names

getKeyCode ​

getKeyCode(String keyName): InputConstants.Key#
Resolves a serialized key name to Minecraft's key representation. An unrecognized name resolves to the unknown key rather than indicating failure.

Parameters:

  • keyName: the name of the key, for example key.keyboard.w
Returns: the resolved key, or Minecraft's unknown key for an unrecognized name

getPressedKeys ​

getPressedKeys(): Set<String>Since1.2.6#
Returns a snapshot of key names tracked as pressed by JsMacros. This does not report physical keys held by the player; names are added by key/keybind state calls and removed when those calls release them.
Returns: a snapshot of tracked pressed key names

Note: turned into set instead of list in 1.6.5

key ​

key(String keyName, boolean keyState): void#
Sets the state of the named key, affecting every keybind currently mapped to that key. Setting it down also registers a click. State changes are ignored while a screen is open; an unrecognized name resolves to the unknown key.

Parameters:

  • keyName: the name of the key, for example key.keyboard.w
  • keyState: true to mark the key down, false to mark it up

keyBind ​

keyBind(String keyBind, boolean keyState): voidSince1.2.2#
Sets the state of the named keybind, independent of which key it is assigned to. Setting it down also registers a click. Has no effect while a screen is open or if no keybind matches.

Parameters:

  • keyBind: the name of the keybind, for example key.forward
  • keyState: true to mark the keybind down, false to mark it up

pressKey ​

pressKey(String keyName): voidSince1.8.4#
Marks the named key down and registers a click on every keybind mapped to it. Has no effect while a screen is open.

Parameters:

  • keyName: the name of the key to press

pressKeyBind ​

pressKeyBind(String keyBind): voidSince1.8.4#
Marks the named keybind down and registers a click. Has no effect while a screen is open or if no keybind matches.

Parameters:

  • keyBind: the name of the keybinding to press

releaseKey ​

releaseKey(String keyName): voidSince1.8.4#
Marks the named key up and removes it from getPressedKeys(). Has no effect while a screen is open.

Parameters:

  • keyName: the name of the key to release

releaseKeyBind ​

releaseKeyBind(String keyBind): voidSince1.8.4#
Calls keyBind(String, boolean) with keyState set to false. Marks the matching keybind up and removes its name from getPressedKeys(). Does nothing while a screen is open or when no keybind has this name.

Parameters:

  • keyBind: the name of the keybinding to release

setKeyBind ​

setKeyBind(String bind, String key): voidSince1.2.2#
Reassigns the first keybind with the given name; null unbinds it. This changes the binding, not its pressed state. An unmatched name has no effect.

Parameters:

  • bind: the name of the keybind, for example key.forward
  • key: the name of the key to put it on, or null to unbind it