js-apis-shortKey.md 3.3 KB
Newer Older
S
shawn_he 已提交
1 2 3 4 5 6 7
#  @ohos.multimodalInput.shortKey (Shortcut Key)

The **shortKey** module provides APIs to set the delay for starting an ability using a shortcut key. For example, you can set the delay to 3 seconds so that a screenshot is taken when you press and hold the shortcut key for 3 seconds.

> **NOTE**
>
> - The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.
S
shawn_he 已提交
8
>
S
shawn_he 已提交
9 10 11 12 13 14 15 16 17 18
> - The APIs provided by this module are system APIs.

##  Modules to Import

```
import shortKey from '@ohos.multimodalInput.shortKey';
```

##  shortKey.setKeyDownDuration

S
shawn_he 已提交
19
setKeyDownDuration(businessKey: string, delay: number, callback: AsyncCallback<void>): void
S
shawn_he 已提交
20

S
shawn_he 已提交
21
Sets the delay for starting an ability using shortcut keys. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
22 23 24 25 26 27 28

**System capability**: SystemCapability.MultimodalInput.Input.ShortKey

**Parameters**

| Name    | Type               | Mandatory| Description                                                        |
| ---------- | ------------------- | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
29
| businessKey| string              | Yes  | Unique service ID registered on the multimodal side. It corresponds to **businessId** in the **ability_launch_config.json** file.|
S
shawn_he 已提交
30 31
| delay      | number              | Yes  | Delay for starting an ability using shortcut keys, in milliseconds. This field is invalid only when shortcut keys are used.|
| callback   | AsyncCallback<void> | Yes  | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. |                                               
S
shawn_he 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

**Example**

```
try {
  shortKey.setKeyDownDuration("screenshot", 500, (error) => {
    if (error) {
      console.log(`Set key down duration failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
      return;
    }
    console.log(`Set key down duration success`);
  });
} catch (error) {
  console.log(`Set key down duration failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
```

## shortKey.setKeyDownDuration

S
shawn_he 已提交
51
setKeyDownDuration(businessKey: string, delay: number): Promise<void>
S
shawn_he 已提交
52

S
shawn_he 已提交
53
Sets the delay for starting an ability using shortcut keys. This API uses a promise to return the result.
S
shawn_he 已提交
54 55 56 57 58 59 60

**System capability**: SystemCapability.MultimodalInput.Input.ShortKey

**Parameters**

| Name    | Type  | Mandatory| Description                                                        |
| ---------- | ------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
61
| businessKey| string | Yes  | Unique service ID registered on the multimodal side. It corresponds to **businessId** in the **ability_launch_config.json** file.|
S
shawn_he 已提交
62
| delay      | number | Yes  | Delay for starting an ability using shortcut keys, in milliseconds. This field is invalid only when shortcut keys are used.|
S
shawn_he 已提交
63 64 65 66 67

**Return value**

| Parameters         | Description         |
| ------------- | ------------- |
S
shawn_he 已提交
68
| Promise<void> | Promise that returns no value.|
S
shawn_he 已提交
69 70 71 72 73 74 75 76 77 78 79 80

**Example**

```
try {
  shortKey.setKeyDownDuration("screenshot", 500).then(() => {
    console.log(`Set key down duration success`);
  });
} catch (error) {
  console.log(`Set key down duration failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
```