1e41f4b71Sopenharmony_ci# Key Event 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciA key event is triggered when a focusable component, such as **Button**, interacts with a keyboard, remote control, or any other input device with keys. To use a key event for components that are not focusable by default, such as **Text** and **Image**, first set their **focusable** attribute to **true**. 4e41f4b71Sopenharmony_ciFor details about the process and specific timing of the key event triggering, see [Key Event Data Flow](../../../ui/arkts-common-events-device-input-event.md#key-event-data-flow). 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci> **NOTE** 7e41f4b71Sopenharmony_ci> 8e41f4b71Sopenharmony_ci> The APIs of this module are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version. 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci## onKeyEvent 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_cionKeyEvent(event: (event: KeyEvent) => void): T 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ciTriggered when a key event occurs. 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci**Parameters** 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 23e41f4b71Sopenharmony_ci| ------ | ----------------------------- | ---- | ------------------ | 24e41f4b71Sopenharmony_ci| event | [KeyEvent](#keyevent) | Yes | **KeyEvent** object. | 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci**Return value** 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci| Type | Description | 29e41f4b71Sopenharmony_ci| -------- | -------- | 30e41f4b71Sopenharmony_ci| T | Current component. | 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci## onKeyPreIme<sup>12+</sup> 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_cionKeyPreIme(event: Callback<KeyEvent, boolean>): T 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ciTriggered before other callbacks when a key event occurs. 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ciIf the return value of this callback is **true**, it is considered that the key event has been consumed, and subsequent event callbacks (**keyboardShortcut**, input method events, **onKeyEvent**) will be intercepted and no longer triggered. 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci**Parameters** 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 47e41f4b71Sopenharmony_ci| ------ | ----------------------------- | ---- | ------------------ | 48e41f4b71Sopenharmony_ci| event | [Callback](./ts-types.md#callback12)<[KeyEvent](#keyevent), boolean> | Yes | Callback for processing the key event. | 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci**Return value** 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci| Type | Description | 53e41f4b71Sopenharmony_ci| -------- | -------- | 54e41f4b71Sopenharmony_ci| T | Current component. | 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci## KeyEvent 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci| Name | Type | Description | 63e41f4b71Sopenharmony_ci| ------------------------------------- | ---------------------------------------- | -------------------------- | 64e41f4b71Sopenharmony_ci| type | [KeyType](ts-appendix-enums.md#keytype) | Key type. | 65e41f4b71Sopenharmony_ci| [keyCode](../../apis-input-kit/js-apis-keycode.md#keycode) | number | Key code. | 66e41f4b71Sopenharmony_ci| keyText | string | Key value. | 67e41f4b71Sopenharmony_ci| keySource | [KeySource](ts-appendix-enums.md#keysource) | Type of the input device that triggers the key event. | 68e41f4b71Sopenharmony_ci| deviceId | number | ID of the input device that triggers the key event. | 69e41f4b71Sopenharmony_ci| metaKey | number | State of the metakey (that is, the **WIN** key on the Windows keyboard or the **Command** key on the Mac keyboard) when the key is pressed. The value **1** indicates the pressed state, and **0** indicates the unpressed state. | 70e41f4b71Sopenharmony_ci| timestamp | number | Timestamp of the event. It is the interval between the time when the event is triggered and the time when the system starts, in nanoseconds. | 71e41f4b71Sopenharmony_ci| stopPropagation | () => void | Stops the event from bubbling upwards or downwards. | 72e41f4b71Sopenharmony_ci| intentionCode<sup>10+</sup> | [IntentionCode](../../apis-input-kit/js-apis-intentioncode.md) | Intention corresponding to the key. | 73e41f4b71Sopenharmony_ci| getModifierKeyState<sup>12+</sup> | (Array<string>) => bool | Obtains the pressed status of modifier keys. For details about the error message, see the following error codes. The following modifier keys are supported: 'Ctrl'\|'Alt'\|'Shift'\|'Fn'. This API does not work for the Fn key on an externally connected keyboard.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci**Error codes** 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../../errorcode-universal.md). 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci| ID | Error Message | 80e41f4b71Sopenharmony_ci| ------- | -------- | 81e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Incorrect parameter types. 2. Parameter verification failed. | 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci## Example 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci```ts 86e41f4b71Sopenharmony_ci// xxx.ets 87e41f4b71Sopenharmony_ci@Entry 88e41f4b71Sopenharmony_ci@Component 89e41f4b71Sopenharmony_cistruct KeyEventExample { 90e41f4b71Sopenharmony_ci @State text: string = '' 91e41f4b71Sopenharmony_ci @State eventType: string = '' 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci build() { 94e41f4b71Sopenharmony_ci Column() { 95e41f4b71Sopenharmony_ci Button('KeyEvent') 96e41f4b71Sopenharmony_ci .onKeyEvent((event?: KeyEvent) => { 97e41f4b71Sopenharmony_ci if(event){ 98e41f4b71Sopenharmony_ci if (event.type === KeyType.Down) { 99e41f4b71Sopenharmony_ci this.eventType = 'Down' 100e41f4b71Sopenharmony_ci } 101e41f4b71Sopenharmony_ci if (event.type === KeyType.Up) { 102e41f4b71Sopenharmony_ci this.eventType = 'Up' 103e41f4b71Sopenharmony_ci } 104e41f4b71Sopenharmony_ci this.text = 'KeyType:' + this.eventType + '\nkeyCode:' + event.keyCode + '\nkeyText:' + event.keyText + '\nintentionCode:' + event.intentionCode 105e41f4b71Sopenharmony_ci } 106e41f4b71Sopenharmony_ci }) 107e41f4b71Sopenharmony_ci Text(this.text).padding(15) 108e41f4b71Sopenharmony_ci }.height(300).width('100%').padding(35) 109e41f4b71Sopenharmony_ci } 110e41f4b71Sopenharmony_ci} 111e41f4b71Sopenharmony_ci``` 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci  114