onKey
Start from API_LEVEL
2.0. Please refer to API_LEVEL.
Listen to key events, only one event is allowed to be registered, if multiple registrations will cause the last registered event to fail.
Type
function onKey(option: Option): void
Simplified calling method
function onKey(callback: (key: Key, event: KeyEvent) => PreventDefault): void
Parameters
Option
| Property | Type | Required | DefaultValue | Description | API_LEVEL |
|---|---|---|---|---|---|
| callback | (key: Key, event: KeyEvent) => PreventDefault | Y | - | Key event callback function | 2.0 |
Key
| Type | Description |
|---|---|
number | Key name, value reference key name constants |
KeyEvent
| Type | Description |
|---|---|
number | Key event name, value reference key event constants |
PreventDefault
| Type | Description |
|---|---|
boolean | Whether to skip the default key behavior, true - skip, false - don't skip |
Constants
Key name constants
| Constant | Description | API_LEVEL |
|---|---|---|
KEY_BACK | BACK KEY | 2.0 |
KEY_SELECT | SELECT KEY | 2.0 |
KEY_HOME | HOME KEY | 2.0 |
KEY_UP | UP KEY | 2.0 |
KEY_DOWN | SHORTCUT KEY | 2.0 |
KEY_SHORTCUT | SHORTCUT KEY | 2.0 |
Key event constants
| Constant | Description | API_LEVEL |
|---|---|---|
KEY_EVENT_CLICK | Key click event | 2.0 |
KEY_EVENT_LONG_PRESS | Key long-press event | 2.0 |
KEY_EVENT_DOUBLE_CLICK | Key double-click event | 2.0 |
KEY_EVENT_PRESS | Key press event | 2.0 |
KEY_EVENT_RELEASE | Key release event | 2.0 |
Example
import { onKey, KEY_UP, KEY_EVENT_CLICK } from '@zos/interaction'
onKey({
callback: (key, keyEvent) => {
if (key === KEY_UP && keyEvent === KEY_EVENT_CLICK) {
console.log('up click')
}
return true
},
})