# KeyboardEvent

Class · category: Input Devices

Source: https://github.com/playcanvas/engine/blob/970d89f6d3bc6667f4e1f88153abd6e7e82ed52c/src/platform/input/keyboard-event.js#L14

The KeyboardEvent is passed into all event handlers registered on the [Keyboard](https://api.playcanvas.com/engine/classes/Keyboard.md). The
events are:

- [Keyboard.EVENT_KEYDOWN](https://api.playcanvas.com/engine/classes/Keyboard.md#event_keydown)
- [Keyboard.EVENT_KEYUP](https://api.playcanvas.com/engine/classes/Keyboard.md#event_keyup)

## Constructors

### constructor

```ts
new KeyboardEvent(keyboard?: Keyboard, event?: KeyboardEvent)
```

Create a new KeyboardEvent.

**Parameters**

- `keyboard` ([`Keyboard`](https://api.playcanvas.com/engine/classes/Keyboard.md), optional): The keyboard object which is firing the event.
- `event` (`KeyboardEvent`, optional): The original browser event that was fired.

**Example**

```ts
const onKeyDown = function (e) {
    if (e.key === KEY_SPACE) {
        // space key pressed
    }
    e.event.preventDefault(); // Use original browser event to prevent browser action.
};
app.keyboard.on("keydown", onKeyDown, this);
```

## Properties

### element

```ts
element: Element | null = null
```

The element that fired the keyboard event.

### event

```ts
event: KeyboardEvent | null = null
```

The original browser event which was fired.

### key

```ts
key: number | null = null
```

The keyCode of the key that has changed. See the KEY_* constants.
