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

E
ester.zhou 已提交
3
A key event is triggered when a component interacts with a keyboard, remote control, or any other input device with keys.
Z
zengyawen 已提交
4

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


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

None

Z
zengyawen 已提交
14 15 16

## Events

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


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

- Attributes
E
ester.zhou 已提交
25 26
  | Name                                         | Type                   | Description                  |
  | ------------------------------------- | --------------------------- | -------------------------- |
E
esterzhou 已提交
27
  | type                                  | [KeyType](ts-appendix-enums.md#keytype)   | Type of a key.                 |
E
ester.zhou 已提交
28 29
  | [keyCode](../apis/js-apis-keycode.md) | number                      | Key code.                      |
  | keyText                               | string                      | Key value.                     |
E
esterzhou 已提交
30
  | keySource                             | [KeySource](ts-appendix-enums.md#keysource) | Type of the input device that triggers the key event. |
E
ester.zhou 已提交
31 32 33
  | 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 已提交
34

E
esterzhou 已提交
35

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



## Example

E
ester.zhou 已提交
45 46
```ts
// xxx.ets
Z
zengyawen 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
@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 已提交
71
![en-us_image_0000001257058433](figures/en-us_image_0000001257058433.gif)