1e41f4b71Sopenharmony_ci# Component Keyboard Shortcut Event 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciYou can set custom keyboard shortcuts for components, with the flexibility to define multiple shortcuts per component. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciA component will still respond to the set custom shortcuts even if it's not in focus or visible on the active page, as long as it's part of the component tree within a window that has focus. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciBetter yet, you can set custom events for custom keyboard shortcuts, so that when the defined keys of a keyboard shortcut are pressed, the custom event is triggered. If no custom event is set, the behavior of a keyboard shortcut is the same as that of a click. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci> **NOTE** 10e41f4b71Sopenharmony_ci> 11e41f4b71Sopenharmony_ci> The APIs of this module are supported since API version 10. Updates will be marked with a superscript to indicate their earliest API version. 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci## keyboardShortcut 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_cikeyboardShortcut(value: string | FunctionKey, keys: Array\<ModifierKey>, action?: () => void): T 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ciSets a keyboard shortcut for the component. 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci**Parameters** 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 26e41f4b71Sopenharmony_ci| ----- | ------------------------------------- | ---- | ---------------------------------------- | 27e41f4b71Sopenharmony_ci| value | string \| [FunctionKey](#functionkey) | Yes| Character key (which can be entered through the keyboard) or [function key](#functionkey).<br>An empty string means to disable the keyboard shortcut.<br>| 28e41f4b71Sopenharmony_ci| keys | Array\<[ModifierKey](#modifierkey)> | Yes| Modifier keys.<br>This parameter can be left empty only when **value** is set to a [function key](#functionkey).<br>| 29e41f4b71Sopenharmony_ci| action | () => void | No | Callback for a custom event after the keyboard shortcut is triggered.<br> | 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci**Return value** 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci| Type| Description| 34e41f4b71Sopenharmony_ci| -------- | -------- | 35e41f4b71Sopenharmony_ci| T | Current component.| 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci## ModifierKey 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci| Name | Description | 44e41f4b71Sopenharmony_ci| ----- | ------------ | 45e41f4b71Sopenharmony_ci| CTRL | Ctrl key on the keyboard. | 46e41f4b71Sopenharmony_ci| SHIFT | Shift key on the keyboard.| 47e41f4b71Sopenharmony_ci| ALT | Alt key on the keyboard. | 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci## FunctionKey 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci| Name | Description | 54e41f4b71Sopenharmony_ci| ---- | ------------ | 55e41f4b71Sopenharmony_ci| ESC | Esc key on the keyboard.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 56e41f4b71Sopenharmony_ci| F1 | F1 key on the keyboard.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 57e41f4b71Sopenharmony_ci| F2 | F2 key on the keyboard.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 58e41f4b71Sopenharmony_ci| F3 | F3 key on the keyboard.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 59e41f4b71Sopenharmony_ci| F4 | F4 key on the keyboard.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 60e41f4b71Sopenharmony_ci| F5 | F5 key on the keyboard.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 61e41f4b71Sopenharmony_ci| F6 | F6 key on the keyboard.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 62e41f4b71Sopenharmony_ci| F7 | F7 key on the keyboard.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 63e41f4b71Sopenharmony_ci| F8 | F8 key on the keyboard.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 64e41f4b71Sopenharmony_ci| F9 | F9 key on the keyboard.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 65e41f4b71Sopenharmony_ci| F10 | F10 key on the keyboard.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 66e41f4b71Sopenharmony_ci| F11 | F11 key on the keyboard.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 67e41f4b71Sopenharmony_ci| F12 | F12 key on the keyboard.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 68e41f4b71Sopenharmony_ci| TAB<sup>12+</sup> | Tab key on the keyboard.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 69e41f4b71Sopenharmony_ci| DPAD_UP<sup>12+</sup> | Up arrow key on the keyboard.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 70e41f4b71Sopenharmony_ci| DPAD_DOWN<sup>12+</sup> | Down arrow key on the keyboard.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 71e41f4b71Sopenharmony_ci| DPAD_LEFT<sup>12+</sup> | Left arrow key on the keyboard.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 72e41f4b71Sopenharmony_ci| DPAD_RIGHT<sup>12+</sup> | Right arrow key on the keyboard.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci## Precautions for Using Keyboard Shortcuts 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ciKeyboard shortcuts, as system keys, take precedence over the common key event **OnKeyEvent**. For details about the key event triggering logic, see [Key Event Data Flow](../../../ui/arkts-common-events-device-input-event.md#key-event-data-flow). 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci| Scenario | Processing Logic | Example | 79e41f4b71Sopenharmony_ci| ---------------------------------------- | ---------------------------------- | ---------------------------------------- | 80e41f4b71Sopenharmony_ci| All components that support the **onClick** event | Custom keyboard shortcuts are supported. | – | 81e41f4b71Sopenharmony_ci| Requirements for custom keyboard shortcuts | A custom keyboard shortcut consists of one or more modifier keys (Ctrl, Shift, Alt, or any combination thereof) and a character key or function key.| Button('button1').keyboardShortcut('a',[ModifierKey.CTRL]) | 82e41f4b71Sopenharmony_ci| Setting one custom keyboard shortcut for multiple components | Only the first component in the component tree responds to the custom keyboard shortcut. | Button('button1').keyboardShortcut('a',[ModifierKey.CTRL])<br>Button('button2').keyboardShortcut('a',[ModifierKey.CTRL]) | 83e41f4b71Sopenharmony_ci| When the component is focused or not | The component responds to the custom keyboard shortcut as long as the window is focused. | – | 84e41f4b71Sopenharmony_ci| Using a single function key to trigger a keyboard shortcut| A keyboard shortcut can consist of a single function key without any modifier keys.| Button('button1').keyboardShortcut(FunctionKey.F2,[]) | 85e41f4b71Sopenharmony_ci| The input parameter **value** of **keyboardShortcut** is empty| The keyboard shortcut is disabled.<br>This does not apply when there are multiple keyboard shortcuts.| Button('button1').keyboardShortcut('',[ModifierKey.CTRL])<br>Button('button2').keyboardShortcut('',[]) | 86e41f4b71Sopenharmony_ci| The independent pipeline sub-window and main window coexist | The focused window responds to the keyboard shortcut. | – | 87e41f4b71Sopenharmony_ci| The input parameter **keys** of **keyboardShortcut** is set to the Ctrl, Shift, or Alt key| Both the keys on the left or right sides of the keyboard work. | Button('button1').keyboardShortcut('a',[ModifierKey.CTRL, ModifierKey.ALT]) | 88e41f4b71Sopenharmony_ci| Character key in the **value** parameter of the **keyboardShortcut** API | The response is case-insensitive. | Button('button1').keyboardShortcut('a',[ModifierKey.CTRL])<br>Button('button2').keyboardShortcut('A',[ModifierKey.CTRL]) | 89e41f4b71Sopenharmony_ci| Response to keyboard shortcuts | The component responds to a keyboard shortcut when the keys specified by **keys** are pressed and the key specified by **value** triggers a down event. (Long-pressing leads to continuous response.) | – | 90e41f4b71Sopenharmony_ci| The component is hidden<br> | The component still responds to keyboard shortcuts. | – | 91e41f4b71Sopenharmony_ci| The component is in the disabled state | The component does not respond to keyboard shortcuts. | – | 92e41f4b71Sopenharmony_ci| 1. The keyboard shortcut is the same as an existing one (including the system-defined ones).<br>2. The **value** parameter contains multiple character keys.<br>3. The **key** parameter has a duplicate modifier key.| In these cases, the keyboard shortcut is not added, and the previously added keyboard shortcuts still work. | Button('button1').keyboardShortcut('c',[ModifierKey.CTRL])<br>Button('button2').keyboardShortcut('ab',[ModifierKey.CTRL])<br>Button('button3').keyboardShortcut('ab',[ModifierKey.CTRL,ModifierKey.CTRL]) | 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci### System-defined Keyboard Shortcuts That Cannot Be Bound 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ciThe following key combinations cannot function as keyboard shortcuts: 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci- `Ctrl` + `C` 99e41f4b71Sopenharmony_ci- `Ctrl` + `A` 100e41f4b71Sopenharmony_ci- `Ctrl` + `V` 101e41f4b71Sopenharmony_ci- `Ctrl` + `X` 102e41f4b71Sopenharmony_ci- `Ctrl` + `Shift` + `Z` 103e41f4b71Sopenharmony_ci- `Ctrl` + `Z` 104e41f4b71Sopenharmony_ci- `Ctrl` + `Y` 105e41f4b71Sopenharmony_ci 106e41f4b71Sopenharmony_ci### Predefined Key Events 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ciThe following table lists the predefined key events. 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ciThe predefined key events and custom key events have priorities. Events with higher priorities intercept those with lower priorities. For details about the response priorities, see [Key Event Data Flow](../../../ui/arkts-common-events-device-input-event.md#key-event-data-flow). 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ci| Keyboard Shortcut| Focused Component| Usage| Event Handling Category| 113e41f4b71Sopenharmony_ci| ----- | ---- | ---- | ---- | 114e41f4b71Sopenharmony_ci| Arrow keys, **Shift** + Arrow keys| Text box component| Moves the cursor.| Input method| 115e41f4b71Sopenharmony_ci| Arrow keys, **Shift** + Arrow keys| Universal component| Moves focus in navigation.| System keys| 116e41f4b71Sopenharmony_ci| **Tab**, **Shift** + **Tab**| Universal component| Triggers focus navigation or moves focus in navigation.| System keys| 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci## Example 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci### Example 1 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ciThis example sets a keyboard shortcut. You can press the modifier key and accompanying key at the same time to trigger the component to respond to the shortcut and trigger the **onClick** event or other custom event. 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ci```ts 125e41f4b71Sopenharmony_ci@Entry 126e41f4b71Sopenharmony_ci@Component 127e41f4b71Sopenharmony_cistruct Index { 128e41f4b71Sopenharmony_ci @State message: string = 'Hello World' 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ci build() { 131e41f4b71Sopenharmony_ci Row() { 132e41f4b71Sopenharmony_ci Column({ space: 5 }) { 133e41f4b71Sopenharmony_ci Text(this.message) 134e41f4b71Sopenharmony_ci Button("Test short cut 1").onClick((event: ClickEvent) => { 135e41f4b71Sopenharmony_ci this.message = "I clicked Button 1"; 136e41f4b71Sopenharmony_ci console.log("I clicked 1"); 137e41f4b71Sopenharmony_ci }).keyboardShortcut('.', [ModifierKey.SHIFT, ModifierKey.CTRL, ModifierKey.ALT]) 138e41f4b71Sopenharmony_ci .onKeyEvent((event: KeyEvent)=>{ 139e41f4b71Sopenharmony_ci console.log("event.keyCode: " + JSON.stringify(event)); 140e41f4b71Sopenharmony_ci }) 141e41f4b71Sopenharmony_ci Button("Test short cut 2").onClick((event: ClickEvent) => { 142e41f4b71Sopenharmony_ci this.message = "I clicked Button 2"; 143e41f4b71Sopenharmony_ci console.log("I clicked 2"); 144e41f4b71Sopenharmony_ci }).keyboardShortcut('1', [ModifierKey.CTRL]) 145e41f4b71Sopenharmony_ci Button("Test short cut 3").onClick((event: ClickEvent) => { 146e41f4b71Sopenharmony_ci this.message = "I clicked Button 3"; 147e41f4b71Sopenharmony_ci console.log("I clicked 3"); 148e41f4b71Sopenharmony_ci }).keyboardShortcut('A', [ModifierKey.SHIFT]) 149e41f4b71Sopenharmony_ci Button("Test short cut 4").onClick((event: ClickEvent) => { 150e41f4b71Sopenharmony_ci this.message = "I clicked Button 4"; 151e41f4b71Sopenharmony_ci console.log("I clicked 4"); 152e41f4b71Sopenharmony_ci }).keyboardShortcut(FunctionKey.F5, [], () => { 153e41f4b71Sopenharmony_ci this.message = "I clicked Button 4"; 154e41f4b71Sopenharmony_ci console.log("I clicked user callback."); 155e41f4b71Sopenharmony_ci }).keyboardShortcut(FunctionKey.F3, []) 156e41f4b71Sopenharmony_ci } 157e41f4b71Sopenharmony_ci .width('100%') 158e41f4b71Sopenharmony_ci } 159e41f4b71Sopenharmony_ci .height('100%') 160e41f4b71Sopenharmony_ci } 161e41f4b71Sopenharmony_ci} 162e41f4b71Sopenharmony_ci``` 163e41f4b71Sopenharmony_ci 164e41f4b71Sopenharmony_ci  165e41f4b71Sopenharmony_ci 166e41f4b71Sopenharmony_ci### Example 2 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ciThis example registers and deregisters a keyboard shortcut through clicks. 169e41f4b71Sopenharmony_ci 170e41f4b71Sopenharmony_ci```ts 171e41f4b71Sopenharmony_ci@Entry 172e41f4b71Sopenharmony_ci@Component 173e41f4b71Sopenharmony_cistruct Index { 174e41f4b71Sopenharmony_ci @State message: string = 'disable' 175e41f4b71Sopenharmony_ci @State shortCutEnable: boolean = false 176e41f4b71Sopenharmony_ci @State keyValue: string = '' 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_ci build() { 179e41f4b71Sopenharmony_ci Row() { 180e41f4b71Sopenharmony_ci Column({ space: 5 }) { 181e41f4b71Sopenharmony_ci Text('Ctrl+A is ' + this.message) 182e41f4b71Sopenharmony_ci Button("Test short cut").onClick((event: ClickEvent) => { 183e41f4b71Sopenharmony_ci this.message = "I clicked Button"; 184e41f4b71Sopenharmony_ci console.log("I clicked"); 185e41f4b71Sopenharmony_ci }).keyboardShortcut(this.keyValue, [ModifierKey.CTRL]) 186e41f4b71Sopenharmony_ci Button(this.message + 'shortCut').onClick((event: ClickEvent) => { 187e41f4b71Sopenharmony_ci this.shortCutEnable = !this.shortCutEnable; 188e41f4b71Sopenharmony_ci this.message = this.shortCutEnable ? 'enable' : 'disable'; 189e41f4b71Sopenharmony_ci this.keyValue = this.shortCutEnable ? 'a' : ''; 190e41f4b71Sopenharmony_ci }) 191e41f4b71Sopenharmony_ci Button('multi-shortcut').onClick((event: ClickEvent) => { 192e41f4b71Sopenharmony_ci console.log('Trigger keyboard shortcut success.') 193e41f4b71Sopenharmony_ci }).keyboardShortcut('q', [ModifierKey.CTRL]) 194e41f4b71Sopenharmony_ci .keyboardShortcut('w', [ModifierKey.CTRL]) 195e41f4b71Sopenharmony_ci .keyboardShortcut('', []) // Disabling does not work when there are multiple keyboard shortcuts. 196e41f4b71Sopenharmony_ci } 197e41f4b71Sopenharmony_ci .width('100%') 198e41f4b71Sopenharmony_ci } 199e41f4b71Sopenharmony_ci .height('100%') 200e41f4b71Sopenharmony_ci } 201e41f4b71Sopenharmony_ci} 202e41f4b71Sopenharmony_ci``` 203e41f4b71Sopenharmony_ci 204e41f4b71Sopenharmony_ci  205