---
uid: input-system-keyboard
---
# Keyboard support

The [`Keyboard`](xref:UnityEngine.InputSystem.Keyboard) class defines a [Device](xref:input-system-devices) with a set of key Controls defined by the [`Key`](xref:UnityEngine.InputSystem.Key) enumeration.

The location of individual keys is agnostic to keyboard layout. This means that, for example, the `A` key is always the key to the right of the `Caps Lock` key, regardless of where the currently active keyboard layout places the key that generates an `a` character, or whether or not the layout doesn't have a key assigned to that character.

To query which (if any) character is generated by a given key, use the key Control's [`displayName`](xref:UnityEngine.InputSystem.InputControl.displayName) property. The value of this property changes automatically when the OS changes the keyboard layout.

You can look up keys based on the character they produce using [Control paths](xref:input-system-controls#control-paths). For example,  you can query the key that produces the producing the `a` character from [`Keyboard`](xref:UnityEngine.InputSystem.Keyboard) using [`Keyboard.current["#(a)"]`](xref:UnityEngine.InputSystem.Keyboard.Item(UnityEngine.InputSystem.Key)).

> [!NOTE]
> * Keyboards usually have hardware limitations on both the number of simultaneous keypresses they report, and the combinations of keys they support. This means that certain simultaneous keypresses may not register correctly. For example, a given keyboard might report a simultaneous press of the "QWERT" keys correctly, but might not report "QWERA" correctly.
> * At the moment, the new Input System doesn't support on-screen keyboards. For now, please Unity's existing API in `UnityEngine.TouchScreenKeyboard`.
> * At the moment, Unity platform backends generally do not support distinguishing between multiple keyboards. While the Input System supports having many [`Keyboard`](xref:UnityEngine.InputSystem.Keyboard) devices at any point, platform backends generally only report a single keyboard and route input from all attached keyboards to the one keyboard device.

## Controls

To retrieve a key from [`Keyboard`](xref:UnityEngine.InputSystem.Keyboard), you can use one of these methods:

* Use the key's accessor property, such [`Keyboard.spaceKey`](xref:UnityEngine.InputSystem.Keyboard.spaceKey).
* Use [`Keyboard`](xref:UnityEngine.InputSystem.Keyboard)'s indexer and the [`Key`](xref:UnityEngine.InputSystem.Key) enumeration (for example, `keyboard[Key.Space]`).

The [scripting API reference for the `Keyboard` class](xref:UnityEngine.InputSystem.Keyboard) lists all the properties for the individual key Controls.

Two special Controls, [`anyKey`](xref:UnityEngine.InputSystem.Keyboard.anyKey) and [`imeSelected`](xref:UnityEngine.InputSystem.Keyboard.imeSelected), don't directly map to individual keys. [`anyKey`](xref:UnityEngine.InputSystem.Keyboard.anyKey) is a [synthetic](xref:input-system-controls#synthetic-controls) button Control which reports whether any key on the keyboard is pressed, and [`imeSelected`](xref:UnityEngine.InputSystem.Keyboard.imeSelected) reports whether or not [IME](#ime) text processing is enabled.

In addition, [`Keyboard`](xref:UnityEngine.InputSystem.Keyboard)'s indexer and the [`Key`](xref:UnityEngine.InputSystem.Key) has three [synthetic](xref:input-system-controls#synthetic-controls) controls that represent combinations of modifier keys:

|Control|Description|
|-------|-----------|
|[`shiftKey`](xref:UnityEngine.InputSystem.Keyboard.shiftKey)|A button that is pressed if [`leftShiftKey`](xref:UnityEngine.InputSystem.Keyboard.leftShiftKey) and/or [`rightShiftKey`](xref:UnityEngine.InputSystem.Keyboard.rightShiftKey) is pressed.|
|[`ctrlKey`](xref:UnityEngine.InputSystem.Keyboard.ctrlKey)|A button that is pressed if [`leftCtrlKey`](xref:UnityEngine.InputSystem.Keyboard.leftCtrlKey) and/or [`rightCtrlKey`](xref:UnityEngine.InputSystem.Keyboard.rightCtrlKey) is pressed.|
|[`altKey`](xref:UnityEngine.InputSystem.Keyboard.altKey)|A button that is pressed if [`leftAltKey`](xref:UnityEngine.InputSystem.Keyboard.leftAltKey) and/or [`rightAltKey`](xref:UnityEngine.InputSystem.Keyboard.rightAltKey) is pressed.|

## Text input

As a best practice, you shouldn't manually translate text input from key presses by trying to string together the characters corresponding to the keys. Instead, to listen to text input, hook into [`Keyboard.onTextInput`](xref:UnityEngine.InputSystem.Keyboard.onTextInput). This delivers character-by-character input as reported by the platform, including input from on-screen keyboards.

Note that the text input API doesn't allocate GC memory because it doesn't deliver fully composed strings.

### IME

Some writing systems, such as some East-Asian scripts, are too complex to represent all characters as individual keys on a keyboard. For these layouts, operating systems implement IMEs (Input Method Editors) to allow composing input strings by other methods, for instance by typing several keys to produce a single character. Unity's UI frameworks for text input support IME without any additional configuration. If you want to build your own UI for text input, the [`Keyboard`](xref:UnityEngine.InputSystem.Keyboard) class allows you to work with input from IMEs using the following APIs:

* [`imeSelected`](xref:UnityEngine.InputSystem.Keyboard.imeSelected) is a virtual input Control that reports whether IME text processing is enabled.

* [`SetIMEEnabled()`](xref:UnityEngine.InputSystem.Keyboard.SetIMEEnabled(System.Boolean)) is a method which lets you turn IME processing on or off. Typically, IME processing is useful when the user wants to edit text, but not so much when the user is using the keyboard to control a game.

* [`SetIMECursorPosition()`](xref:UnityEngine.InputSystem.Keyboard.SetIMECursorPosition(UnityEngine.Vector2)). IMEs might open system windows that let users interactively edit the text they want to input. Typically, these open next to the text editing UI. You can use the [`SetIMECursorPosition()`](xref:UnityEngine.InputSystem.Keyboard.SetIMECursorPosition(UnityEngine.Vector2)) method to tell the OS where that is.

* [`onIMECompositionChange`](xref:UnityEngine.InputSystem.Keyboard.onIMECompositionChange) is an event you can subscribe to in order to receive all updates to the IME composition string. The composition string is the text input the user is currently editing using an IME. Typically, any UI dealing with text input displays this text (with some visual indication of it being actively edited, usually an underline) at the current text input cursor position.

## Keyboard layouts

You can query the name of the current keyboard layout using [`Keyboard.keyboardLayout`](xref:UnityEngine.InputSystem.Keyboard.keyboardLayout). Layout names are platform-specific.

There is no support for setting keyboard layouts from your application.

To monitor keyboard layout changes, hook into [`InputSystem.onDeviceChange`](xref:UnityEngine.InputSystem.InputSystem.onDeviceChange) and check for [`InputDeviceChange.ConfigurationChanged`](xref:UnityEngine.InputSystem.InputDeviceChange) on a [`Keyboard`](xref:UnityEngine.InputSystem.Keyboard) device.

To find the key control that corresponds to a specific display character sequence, call [`Keyboard.FindKeyOnCurrentKeyboardLayout`](xref:UnityEngine.InputSystem.Keyboard.FindKeyOnCurrentKeyboardLayout*).

```CSharp
// Find key that generates a 'q' character according to the current keyboard layout.
Keyboard.current.FindKeyOnCurrentKeyboardLayout("q");
```
