> For the complete documentation index, see [llms.txt](https://docs.servoy.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.servoy.com/reference/servoyextensions/browser-plugins/key-listener.md).

# Key Listener (ref)

(part of package '[Key Listener](/reference/servoyextensions/packages/browser-plugin-packages/key-listener.md)')

You can access it in code via: **plugins.keyListener**

A Servoy Extra Component that handles keyboard events and provides key listener functionality.

## Properties

## API

### addKeyListener(callbackKey,callback,clearCB,delay,regexPattern,regexReplacement)

Registers the listener on the components which have the "keylistener" attribute with the given callbackKey value, to call the given callback when a key is pressed.

**Example:**

```js
plugins.keyListener.addKeyListener('test', onKey)
```

**Parameters:**

> * {[String](/reference/servoycore/dev-api/js-lib/string.md)} callbackKey The key for which the listener will be added. This key has to match value of "keylistener" attribute in component.
> * {[Function](/reference/servoy-developer/component_and_service_property_types.md#function)} callback The callback function to be called when a key is pressed.
> * {[Boolean](/reference/servoycore/dev-api/js-lib/boolean.md)} \[clearCB] If true a removeKeyListener will be called for this key before adding the new one. Default value is false.
> * {[Number](/reference/servoycore/dev-api/js-lib/number.md)} \[delay] Number of milliseconds to wait before callback method is called. Default value is 1000.
> * {[String](/reference/servoycore/dev-api/js-lib/string.md)} \[regexPattern] Javascript regular expression string to modify current value of the component.
> * {[String](/reference/servoycore/dev-api/js-lib/string.md)} \[regexReplacement] The value that is used to replace the regular expression with in the current value of the component.

***

### removeKeyListener(callbackKey)

Removes the listener associated to the given callback key. Returns true if the listener was removed.

**Example:**

```js
plugins.keyListener.removeKeyListener('test')
```

**Parameters:**

> * {[String](/reference/servoycore/dev-api/js-lib/string.md)} callbackKey The key listener to remove.

**Returns:** [Boolean](/reference/servoycore/dev-api/js-lib/boolean.md) Succes of removal

***

## Types

## callback

Represents a key listener callback configuration. scripting type: CustomType\<keyListener.callback>

* callback
  * The callback function that will be executed when the key event occurs.
  * **Type**: [function](/reference/servoy-developer/component_and_service_property_types.md#function)
* callbackKey
  * The key identifier for the callback. This should match the "keylistener" attribute value in the component.
  * **Type**: [string](/reference/servoycore/dev-api/js-lib/string.md)
* delay
  * The delay in milliseconds before the callback is executed after the key event.
  * **Type**: [int](/reference/servoycore/dev-api/js-lib/number.md)
* isRunning
  * Indicates whether the callback is currently running.
  * **Type**: [int](/reference/servoycore/dev-api/js-lib/number.md)
* regexPattern
  * A JavaScript regular expression string used to modify the current value of the component.
  * **Type**: [string](/reference/servoycore/dev-api/js-lib/string.md)
* regexReplacement
  * The string used to replace the parts of the component's value that match the regexPattern.
  * **Type**: [string](/reference/servoycore/dev-api/js-lib/string.md)

***


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.servoy.com/reference/servoyextensions/browser-plugins/key-listener.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
