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

S
shawn_he 已提交
3
The Input Monitor module implements listening for events of input devices (namely, touchscreen and mouse). 
Z
zengyawen 已提交
4

S
shawn_he 已提交
5
>  **NOTE**
S
shawn_he 已提交
6
>
S
shawn_he 已提交
7 8 9
>  - 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.
>
>  - The APIs provided by this module are system APIs.
Z
zengyawen 已提交
10 11 12 13 14


## Modules to Import


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


## inputMonitor.on

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

S
shawn_he 已提交
24
Enables listening for global touch events.
Z
zengyawen 已提交
25 26 27 28 29

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

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

S
shawn_he 已提交
30 31
**Parameters**

S
shawn_he 已提交
32 33 34 35
| Name      | Type                                      | Mandatory  | Description                 |
| -------- | ---------------------------------------- | ---- | ------------------- |
| type     | string                                   | Yes   | Type of the input device event. The value is **touch**.|
| receiver | [TouchEventReceiver](#toucheventreceiver) | Yes   | Callback used to return the touch event.|
Z
zengyawen 已提交
36

S
shawn_he 已提交
37
**Example**
Z
zengyawen 已提交
38

S
shawn_he 已提交
39
```js
S
shawn_he 已提交
40
try {
S
shawn_he 已提交
41 42 43 44
  inputMonitor.on("touch", (touchEvent) => {
    console.log(`Monitor on success ${JSON.stringify(touchEvent)}`);
    return false;
  });
S
shawn_he 已提交
45
} catch (error) {
S
shawn_he 已提交
46
  console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
S
shawn_he 已提交
47
}
Z
zengyawen 已提交
48
```
S
shawn_he 已提交
49

S
shawn_he 已提交
50 51
## inputMonitor.on<sup>9+</sup>

S
shawn_he 已提交
52
on(type: "mouse", receiver: Callback&lt;MouseEvent&gt;): void
S
shawn_he 已提交
53 54 55 56 57 58 59

Enables listening for global mouse events.

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

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

S
shawn_he 已提交
60
**Parameters**
S
shawn_he 已提交
61

S
shawn_he 已提交
62 63 64 65
| Name      | Type                        | Mandatory  | Description                 |
| -------- | -------------------------- | ---- | ------------------- |
| type     | string                     | Yes   | Type of the input device event. The value is **mouse**.|
| receiver | Callback&lt;MouseEvent&gt; | Yes   | Callback used to return the mouse event. |
S
shawn_he 已提交
66 67 68 69

  **Example**

```js
S
shawn_he 已提交
70
try {
S
shawn_he 已提交
71 72 73 74
  inputMonitor.on("mouse", (mouseEvent) => {
    console.log(`Monitor on success ${JSON.stringify(mouseEvent)}`);
    return false;
  });
S
shawn_he 已提交
75
} catch (error) {
S
shawn_he 已提交
76
  console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
S
shawn_he 已提交
77
}
Z
zengyawen 已提交
78 79
```

S
shawn_he 已提交
80 81


Z
zengyawen 已提交
82 83
## inputMonitor.off

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

S
shawn_he 已提交
86
Disables listening for global touch events.
S
shawn_he 已提交
87

Z
zengyawen 已提交
88 89 90 91
**Required permissions**: ohos.permission.INPUT_MONITORING

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

S
shawn_he 已提交
92 93
**Parameters**

S
shawn_he 已提交
94 95 96 97
| Name      | Type                                      | Mandatory  | Description                 |
| -------- | ---------------------------------------- | ---- | ------------------- |
| type     | string                                   | Yes   | Type of the input device event. The value is **touch**.|
| receiver | [TouchEventReceiver](#toucheventreceiver) | No   | Callback for which listening is disabled. If this parameter is not specified, listening will be disabled for all callbacks registered by the current application. |
Z
zengyawen 已提交
98

S
shawn_he 已提交
99
**Example**
Z
zengyawen 已提交
100

S
shawn_he 已提交
101
```js
S
shawn_he 已提交
102 103 104 105 106
// Disable listening for a single callback function.
function callback(touchEvent) {
  console.log(`Monitor on success ${JSON.stringify(touchEvent)}`);
  return false;
};
S
shawn_he 已提交
107
try {
S
shawn_he 已提交
108 109 110
  inputMonitor.on("touch", callback);
  inputMonitor.off("touch", callback);
  console.log(`Monitor off success`);
S
shawn_he 已提交
111
} catch (error) {
S
shawn_he 已提交
112
  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
S
shawn_he 已提交
113
}
S
shawn_he 已提交
114 115 116 117 118 119 120 121
```

```js
// Cancel listening for all callback functions.
function callback(touchEvent) {
  console.log(`Monitor on success ${JSON.stringify(touchEvent)}`);
  return false;
};
S
shawn_he 已提交
122
try {
S
shawn_he 已提交
123 124 125
  inputMonitor.on("touch", callback);
  inputMonitor.off("touch");
  console.log(`Monitor off success`);
S
shawn_he 已提交
126
} catch (error) {
S
shawn_he 已提交
127
  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
S
shawn_he 已提交
128
}
Z
zengyawen 已提交
129
```
S
shawn_he 已提交
130

S
shawn_he 已提交
131
## inputMonitor.off<sup>9+</sup>
S
shawn_he 已提交
132

S
shawn_he 已提交
133
off(type: "mouse", receiver?: Callback&lt;MouseEvent&gt;): void
S
shawn_he 已提交
134

S
shawn_he 已提交
135
Stops listening for global mouse events.
S
shawn_he 已提交
136

S
shawn_he 已提交
137 138 139 140
**Required permissions**: ohos.permission.INPUT_MONITORING

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

S
shawn_he 已提交
141
**Parameters**
S
shawn_he 已提交
142

S
shawn_he 已提交
143 144 145 146
| Name      | Type                        | Mandatory  | Description                 |
| -------- | -------------------------- | ---- | ------------------- |
| type     | string                     | Yes   | Type of the input device event. The value is **mouse**.|
| receiver | Callback&lt;MouseEvent&gt; | No   | Callback for which listening is disabled. If this parameter is not specified, listening will be disabled for all callbacks registered by the current application.|
S
shawn_he 已提交
147 148 149 150

**Example**

```js
S
shawn_he 已提交
151 152 153 154 155
// Disable listening for a single callback.
function callback(mouseEvent) {
  console.log(`Monitor on success ${JSON.stringify(mouseEvent)}`);
  return false;
};
S
shawn_he 已提交
156
try {
S
shawn_he 已提交
157 158 159
  inputMonitor.on("mouse", callback);
  inputMonitor.off("mouse", callback);
  console.log(`Monitor off success`);
S
shawn_he 已提交
160
} catch (error) {
S
shawn_he 已提交
161
  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
S
shawn_he 已提交
162
}
S
shawn_he 已提交
163 164 165 166 167 168 169 170
```

```js
// Disable listening for all callback functions.
function callback(mouseEvent) {
  console.log(`Monitor on success ${JSON.stringify(mouseEvent)}`);
  return false;
};
S
shawn_he 已提交
171
try {
S
shawn_he 已提交
172 173 174
  inputMonitor.on("mouse", callback);
  inputMonitor.off("mouse");
  console.log(`Monitor off success`);
S
shawn_he 已提交
175
} catch (error) {
S
shawn_he 已提交
176
  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
S
shawn_he 已提交
177
}
Z
zengyawen 已提交
178 179 180 181
```

## TouchEventReceiver

S
shawn_he 已提交
182
Represents the callback used to return the touch event.
Z
zengyawen 已提交
183

S
shawn_he 已提交
184
**Required permissions**: ohos.permission.INPUT_MONITORING
Z
zengyawen 已提交
185 186 187

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

S
shawn_he 已提交
188 189
**Parameters**

E
ester.zhou 已提交
190 191
| Name        | Type                                      | Mandatory  | Description                                      |
| ---------- | ---------------------------------------- | ---- | ---------------------------------------- |
S
shawn_he 已提交
192
| touchEvent | [TouchEvent](../arkui-js/js-components-common-events.md) | Yes   | Touch event.|
Z
zengyawen 已提交
193

S
shawn_he 已提交
194 195
**Return value**

S
shawn_he 已提交
196 197 198
| Type     | Description                                      |
| ------- | ---------------------------------------- |
| Boolean | Result indicating whether the touch event will be dispatched to the window. The value **true** indicates that the touch event will be dispatched to the window, and the value **false** indicates the opposite.|
Z
zengyawen 已提交
199

S
shawn_he 已提交
200
**Example**
Z
zengyawen 已提交
201

S
shawn_he 已提交
202
```js
S
shawn_he 已提交
203
try {
S
shawn_he 已提交
204 205 206 207 208 209
  inputMonitor.on("touch", touchEvent => {
    if (touchEvent.touches.length == 3) {// Three fingers are pressed.
      return true;
    } else {
      return false;
    }
S
shawn_he 已提交
210 211
  });
} catch (error) {
S
shawn_he 已提交
212
    console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
S
shawn_he 已提交
213
}
Z
zengyawen 已提交
214
```