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

# StringEventListener

StringEventListener.cs is a script that listens for a sequence of key presses that match a specified string. When the string is matched, a UnityEvent is invoked.

**Listener Type**

* `Listener` (ListenerType): Determines where this script should listen for key press events. Can be either `KeyPressEventSenderClass`, `ThockClass`, or `PublicMethodOnly`.

**Event Trigger String**

* `eventTriggerString` (string): The string that this script is listening for. When a sequence of key presses matches this string, `onEventTriggerString` is invoked.
* `timeOut` (float): The maximum amount of time allowed between key presses. If this time is exceeded, the current string is reset.
* `caseSensitive` (bool): If true, the case of the key presses must match the case of `eventTriggerString`.

**Events**

* `onEventTriggerString` (UnityEvent): Event that is invoked when the specified string is matched. The string is passed as a parameter.

#### Public Methods

* `SetEventTriggerString(string s)` (void): Sets the string that this script is listening for. The first character of the string is converted to a KeyCode and stored in `Key`.
* `SetCaseSensitive(bool b)` (void): Sets whether the case of the key presses must match the case of `eventTriggerString`.

#### Private Methods

* `OnKeyPress(KeyCode obj)` (void): Method that is called when a key is pressed. If the pressed key matches the next character in `eventTriggerString`, the key is added to `currentString`. If `currentString` matches `eventTriggerString`, `onEventTriggerString` is invoked.
* `GetKeyCodeFromString(char c)` (KeyCode): Converts a character to a KeyCode.
* `CheckForString()` (IEnumerator): Coroutine that checks if `currentString` matches `eventTriggerString`. If the match is successful, `onEventTriggerString` is invoked. If `timeOut` is exceeded, `currentString` is reset.
* `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 string to listen for, the listener type, and whether the case of the key presses must match the case of the string in the Inspector. When the specified string is matched, the `onEventTriggerString` event will be invoked.
