ts-universal-events-key.md 2.6 KB
Newer Older
Z
zengyawen 已提交
1
# Key Event
Z
zengyawen 已提交
2

Z
zengyawen 已提交
3

E
ester.zhou 已提交
4 5 6
> **NOTE**
>
> This event is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
7 8 9


## Required Permissions
Z
zengyawen 已提交
10 11 12

None

Z
zengyawen 已提交
13 14 15

## Events

E
ester.zhou 已提交
16
| Name | Bubbling Supported | Description |
Z
zengyawen 已提交
17
| -------- | -------- | -------- |
E
esterzhou 已提交
18
| onKeyEvent(event: (event?: KeyEvent) => void) | Yes | Called when a key event occurs. For details about the event parameters, see [KeyEvent Object](#KeyEvent Object). |
Z
zengyawen 已提交
19 20


E
ester.zhou 已提交
21
## KeyEvent
Z
zengyawen 已提交
22 23

- Attributes
E
ester.zhou 已提交
24
  | Name | Type | Description |
Z
zengyawen 已提交
25
  | -------- | -------- | -------- |
E
ester.zhou 已提交
26 27 28 29 30 31 32
  | type | [KeyType](#keytype-enums) | Type of a key. |
  | keyCode | number | Key code. |
  | keyText | string | Key value. |
  | keySource | [KeySource](#keysource-enums) | Type of the input device that triggers the key event. |
  | deviceId | number | ID of the input device that triggers the key event. |
  | metaKey | number | State of the metakey when the key is pressed. The value **1** means the pressed state, and **0** means the unpressed state. |
  | timestamp | number | Timestamp when the key is pressed. |
Z
zengyawen 已提交
33 34

- APIs
E
ester.zhou 已提交
35
  | Name | Description |
Z
zengyawen 已提交
36
  | -------- | -------- |
E
ester.zhou 已提交
37
  | stopPropagation(): void | Stops the event from bubbling upwards or downwards. |
Z
zengyawen 已提交
38

E
ester.zhou 已提交
39
##  KeyType Enums
Z
zengyawen 已提交
40

E
ester.zhou 已提交
41 42 43 44
| Name | Description |
| -------- | -------- |
| Down | The key is pressed. |
| Up | The key is released. |
Z
zengyawen 已提交
45 46


E
ester.zhou 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
## KeySource Enums
| Name | Description |
| -------- | -------- |
| Unknown | Unknown input device. |
| [KeyCode](#common-keycode-enums) | The input device is a keyboard. |

## Common KeyCode Enums

| Value | Behavior | Physical Button |
| -------- | -------- | -------- |
| 19 | Upward | Up button. |
| 20 | Downward | Down button. |
| 21 | Leftward | Left button. |
| 22 | Rightward | Right button. |
| 23 | OK | **OK** key on a remote control. |
| 66 | OK | **Enter** key on a keyboard. |
| 160 | OK | **Enter** button on the numeric keypad. |
Z
zengyawen 已提交
64 65 66 67


## Example

Z
zengyawen 已提交
68

E
ester.zhou 已提交
69 70
```ts
// xxx.ets
Z
zengyawen 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
@Entry
@Component
struct KeyEventExample {
  @State text: string = ''
  @State eventType: string = ''

  build() {
    Column() {
      Button('KeyEvent').backgroundColor(0x2788D9)
        .onKeyEvent((event: KeyEvent) => {
          if (event.type === KeyType.Down) {
            this.eventType = 'Down'
          }
          if (event.type === KeyType.Up) {
            this.eventType = 'Up'
          }
          console.info(this.text = 'KeyType:' + this.eventType + '\nkeyCode:' + event.keyCode + '\nkeyText:' + event.keyText)
        })
      Text(this.text).padding(15)
    }.height(300).width('100%').padding(35)
  }
}
```

Z
zengyawen 已提交
95
![en-us_image_0000001257058433](figures/en-us_image_0000001257058433.gif)