# 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)](#com.jsmacrosce.jsmacros.client.api.library.impl.FKeyBind_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

- [KeyTracker](FKeyBind.KeyTracker.md)

## Methods

### getKeyBindings {#com.jsmacrosce.jsmacros.client.api.library.impl.FKeyBind_getKeyBindings-}

```js
getKeyBindings(): Map<String, String>
```

Returns a snapshot mapping each keybind name to its currently assigned key name.

**Returns:** a map from keybind names to assigned key names

**Since:** 1.2.2

### getKeyCode {#com.jsmacrosce.jsmacros.client.api.library.impl.FKeyBind_getKeyCode-String-}

```js
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 {#com.jsmacrosce.jsmacros.client.api.library.impl.FKeyBind_getPressedKeys-}

```js
getPressedKeys(): Set<String>
```

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

**Since:** 1.2.6

**Note:** turned into set instead of list in 1.6.5

### key {#com.jsmacrosce.jsmacros.client.api.library.impl.FKeyBind_key-String-boolean-}

```js
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 {#com.jsmacrosce.jsmacros.client.api.library.impl.FKeyBind_keyBind-String-boolean-}

```js
keyBind(String keyBind, boolean keyState): void
```

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

**Since:** 1.2.2

### pressKey {#com.jsmacrosce.jsmacros.client.api.library.impl.FKeyBind_pressKey-String-}

```js
pressKey(String keyName): void
```

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

**Since:** 1.8.4

### pressKeyBind {#com.jsmacrosce.jsmacros.client.api.library.impl.FKeyBind_pressKeyBind-String-}

```js
pressKeyBind(String keyBind): void
```

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

**Since:** 1.8.4

### releaseKey {#com.jsmacrosce.jsmacros.client.api.library.impl.FKeyBind_releaseKey-String-}

```js
releaseKey(String keyName): void
```

Marks the named key up and removes it from [getPressedKeys()](#com.jsmacrosce.jsmacros.client.api.library.impl.FKeyBind_getPressedKeys-). Has no effect while a
 screen is open.

**Parameters:**
- `keyName`: the name of the key to release

**Since:** 1.8.4

### releaseKeyBind {#com.jsmacrosce.jsmacros.client.api.library.impl.FKeyBind_releaseKeyBind-String-}

```js
releaseKeyBind(String keyBind): void
```

Calls [keyBind(String, boolean)](#com.jsmacrosce.jsmacros.client.api.library.impl.FKeyBind_keyBind-String-boolean-) with keyState set to false.  
 
 Marks the matching keybind up and removes its name from [getPressedKeys()](#com.jsmacrosce.jsmacros.client.api.library.impl.FKeyBind_getPressedKeys-). Does
 nothing while a screen is open or when no keybind has this name.

**Parameters:**
- `keyBind`: the name of the keybinding to release

**Since:** 1.8.4

### setKeyBind {#com.jsmacrosce.jsmacros.client.api.library.impl.FKeyBind_setKeyBind-String-String-}

```js
setKeyBind(String bind, String key): void
```

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

**Since:** 1.2.2
