> 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/keypresseventreceiver.md).

# KeyPressEventReceiver

**Key Selection**

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

**Settings**

* `Listener` (ListenerType): Determines where this script should listen for key press events. Can be either [`KeyPressEventSender`](/thock/documentation/utilities/keypresseventsender.md)`Class` or [`Thock`](/thock/documentation/thock.md)`Class via Singleton Instance`.

**Events**

* `OnKeyPressReceived` (UnityEvent): Event that is invoked when the specified key is pressed. The key is passed as a string.

#### Public Methods

* `OnKeyPress(KeyCode obj)` (void): Method that is called when a key is pressed. If the pressed key matches the specified key, `OnKeyPressReceived` is invoked.
* `StopListening()` (void): Unsubscribes this script from key press events.
* `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

* `UnsubscribeFromKeyPressEvents()` (void): Unsubscribes this script from key press events. This is called when the script is disabled or destroyed.

#### Unity Methods

* `Start()` (void): Subscribes this script to key press events based on the `Listener` setting.
* `OnDestroy()` (void): Unsubscribes this script from key press events when the script is destroyed.
* `OnDisable()` (void): Unsubscribes this script from key press events when the script is disabled.

This script should be attached to a GameObject in your scene. You can specify the key to listen for and the listener type in the Inspector. When the specified key is pressed, the `OnKeyPressReceived` event will be invoked.
