js-apis-inputmonitor.md 4.6 KB
Newer Older
Z
zengyawen 已提交
1 2 3
# Input Monitor


S
shawn_he 已提交
4
> **NOTE**<br>
Z
zengyawen 已提交
5
> - The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
S
shawn_he 已提交
6
>
Z
zengyawen 已提交
7 8 9 10 11 12
> - The APIs of this module are system APIs and cannot be called by third-party applications.


## Modules to Import


S
shawn_he 已提交
13
```js
Z
zengyawen 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26
import inputMonitor from '@ohos.multimodalInput.inputMonitor';
```


## Required Permissions

ohos.permission.INPUT_MONITORING


## inputMonitor.on

on(type: "touch", receiver: TouchEventReceiver): void

S
shawn_he 已提交
27
Enables listening for global touch events.
Z
zengyawen 已提交
28 29 30 31 32

**Required permissions**: ohos.permission.INPUT_MONITORING

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

S
shawn_he 已提交
33 34
**Parameters**

S
shawn_he 已提交
35 36 37 38
| Name    | Type                                     | Mandatory| Description                           |
| -------- | ----------------------------------------- | ---- | ------------------------------- |
| type     | string                                    | Yes  | Type of the input event to listen for. The value is **touch**.|
| receiver | [TouchEventReceiver](#toucheventreceiver) | Yes  | Callback used to return the touch event.         |
Z
zengyawen 已提交
39 40 41

  **Example**

S
shawn_he 已提交
42 43 44 45 46
```js
inputMonitor.off("touch", (event) => {
  // A touch event is consumed.
  return false;
});
Z
zengyawen 已提交
47
```
S
shawn_he 已提交
48 49 50 51 52 53 54 55 56

on(type: "mouse", receiver:Callback<MouseEvent>):void

Enables listening for global mouse events.

**Required permissions**: ohos.permission.INPUT_MONITORING

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

S
shawn_he 已提交
57
**Parameters**
S
shawn_he 已提交
58 59 60 61 62 63 64 65 66 67 68 69

| Name    | Type                | Mandatory| Description                           |
| -------- | -------------------- | ---- | ------------------------------- |
| type     | string               | Yes  | Type of the input event to listen for. The value is **mouse**.|
| receiver | Callback<MouseEvent> | Yes  | Callback used to return the mouse event.         |

  **Example**

```js
inputMonitor.off("mouse", (event) => {
  // A mouse event is consumed.
});
Z
zengyawen 已提交
70 71 72
```


S
shawn_he 已提交
73

Z
zengyawen 已提交
74 75
## inputMonitor.off

S
shawn_he 已提交
76
off(type: "touch", receiver?:TouchEventReceiver):void
Z
zengyawen 已提交
77

S
shawn_he 已提交
78
Stops listening for global touch events.
Z
zengyawen 已提交
79 80 81 82 83

**Required permissions**: ohos.permission.INPUT_MONITORING

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

S
shawn_he 已提交
84 85
**Parameters**

S
shawn_he 已提交
86 87 88 89
| Name    | Type                                     | Mandatory| Description                           |
| -------- | ----------------------------------------- | ---- | ------------------------------- |
| type     | string                                    | Yes  | Type of the input event to listen for. The value is **touch**.|
| receiver | [TouchEventReceiver](#toucheventreceiver) | No  | Callback used to return the touch event.         |
Z
zengyawen 已提交
90 91 92

  **Example**

S
shawn_he 已提交
93 94
```js
inputMonitor.off("touch");
Z
zengyawen 已提交
95
```
S
shawn_he 已提交
96 97 98 99 100 101 102 103 104

off(type: "mouse", receiver?:Callback<MouseEvent>):void

Stops listening for global mouse events.

**Required permissions**: ohos.permission.INPUT_MONITORING

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

S
shawn_he 已提交
105
**Parameters**
S
shawn_he 已提交
106 107 108 109 110 111 112 113 114 115

| Name    | Type                | Mandatory| Description                           |
| -------- | -------------------- | ---- | ------------------------------- |
| type     | string               | Yes  | Type of the input event to listen for. The value is **mouse**.|
| receiver | Callback<MouseEvent> | No  | Callback used to return the mouse event.         |

**Example**

```js
inputMonitor.off("mouse");
Z
zengyawen 已提交
116 117 118
```


S
shawn_he 已提交
119

Z
zengyawen 已提交
120 121
## TouchEventReceiver

S
shawn_he 已提交
122
This class provides the callback of touch events.
Z
zengyawen 已提交
123 124 125 126


### (touchEvent: TouchEvent): Boolean

S
shawn_he 已提交
127
Represents the callback used to return the touch event. You need to define the name of the callback function in the correct format. Ensure that the input parameter is of the **TouchEvent** type, and the return value is of the **Boolean** type.
Z
zengyawen 已提交
128 129 130

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

S
shawn_he 已提交
131 132
**Parameters**

E
ester.zhou 已提交
133 134 135
| Name        | Type                                      | Mandatory  | Description                                      |
| ---------- | ---------------------------------------- | ---- | ---------------------------------------- |
| touchEvent | [TouchEvent](../arkui-js/js-components-common-events.md) | Yes   | Callback used to return the touch event.|
Z
zengyawen 已提交
136

S
shawn_he 已提交
137 138
**Return value**

E
ester.zhou 已提交
139 140 141
| Type     | Description                                    |
| ------- | -------------------------------------- |
| Boolean | Result indicating whether the touch event has been consumed by the input monitor. The value **true** indicates that the touch event has been consumed, and the value **false** indicates the opposite.|
Z
zengyawen 已提交
142 143 144

  **Example**

S
shawn_he 已提交
145 146 147 148 149 150
```js
inputMonitor.on("touch", (event) => {
  // A touch event is consumed.
  return false;
});
inputMonitor.off("touch");
Z
zengyawen 已提交
151
```