> For the complete documentation index, see [llms.txt](https://tobar-io.gitbook.io/thock/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tobar-io.gitbook.io/thock/documentation/utilities/keypresseventsender.md).

# KeyPressEventSender

KeyPressEventSender.cs is a script that detects key press events and sends them to a specified receiver. It can send these events to a specific Thock instance or invoke a UnityEvent.

**Key Selection**

* `Key` (KeyCode): The key that this script is listening for.

**Settings**

* `ListenInUpdate` (bool): If true, the script will check for key presses in the Update method.
* `isSenderThockGameObject` (bool): If true, the script will send key press events to a specific Thock instance.
* `sender` (SenderType): Determines where this script should send key press events. Can be either `SendToThockGameObject` or `SendToThockInstance`.

**Thock Reference**

* `Reference` (Thock): The Thock instance that this script will send key press events to. This is only used if `sender` is set to `SendToThockGameObject`.

**Events**

* `OnKeyPress` (UnityEvent): Event that is invoked when the specified key is pressed.
* `OnKeyPressAction` (Action): Static event that is invoked when the specified key is pressed.

#### Public Methods

* `PressKey()` (void): Invokes the `OnKeyPress` event and sends a key press event to the specified Thock instance.
* `SetKey(string str)` (void): Sets the key that this script is listening for. The key is passed as a string and converted to a KeyCode.

#### Private Methods

* `CheckKey()` (void): Checks if the specified key is pressed. If it is, the `PressKey` method is called.
* `SendKeyPressToThock(KeyCode key)` (void): Sends a key press event to the specified Thock instance.

#### Unity Methods

* `Update()` (void): If `ListenInUpdate` is true, the `CheckKey` method is called in this method.
* `OnValidate()` (void): Sets `isSenderThockGameObject` based on the `sender` setting.

This script should be attached to a GameObject in your scene. You can specify the key to listen for and the sender type in the Inspector. When the specified key is pressed, the `OnKeyPress` event will be invoked and a key press event will be sent to the specified Thock instance.
