js-apis-pointer.md 15.5 KB
Newer Older
S
shawn_he 已提交
1
# @ohos.multimodalInput.pointer (Mouse Pointer)
S
shawn_he 已提交
2

S
shawn_he 已提交
3
The **pointer** module provides APIs related to pointer attribute management.
S
shawn_he 已提交
4

S
shawn_he 已提交
5
> **NOTE**<br>
S
shawn_he 已提交
6 7 8 9 10 11 12 13
> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.

## Modules to Import

```js
import pointer from '@ohos.multimodalInput.pointer';
```

S
shawn_he 已提交
14
## pointer.setPointerVisible<sup>9+</sup>
S
shawn_he 已提交
15 16 17 18 19 20 21 22 23

setPointerVisible(visible: boolean, callback: AsyncCallback&lt;void&gt;): void

Sets the visible status of the mouse pointer. This API uses an asynchronous callback to return the result.

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

**Parameters**

S
shawn_he 已提交
24 25 26 27
| Name      | Type                       | Mandatory  | Description                                      |
| -------- | ------------------------- | ---- | ---------------------------------------- |
| visible  | boolean                   | Yes   | Whether the mouse pointer is visible.|
| callback | AsyncCallback&lt;void&gt; | Yes   | Callback used to return the result.|
S
shawn_he 已提交
28 29 30 31

**Example**

```js
S
shawn_he 已提交
32 33 34 35 36
try {
  pointer.setPointerVisible(true, (error) => {
    if (error) {
      console.log(`Set pointer visible failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
      return;
S
shawn_he 已提交
37
    }
S
shawn_he 已提交
38 39 40 41 42
    console.log(`Set pointer visible success`);
  });
} catch (error) {
  console.log(`Set pointer visible failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
S
shawn_he 已提交
43 44
```

S
shawn_he 已提交
45
## pointer.setPointerVisible<sup>9+</sup>
S
shawn_he 已提交
46 47 48 49 50 51 52 53 54

setPointerVisible(visible: boolean): Promise&lt;void&gt;

Sets the visible status of the mouse pointer. This API uses a promise to return the result.

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

**Parameters**

S
shawn_he 已提交
55 56 57
| Name     | Type     | Mandatory  | Description                                      |
| ------- | ------- | ---- | ---------------------------------------- |
| visible | boolean | Yes   | Whether the mouse pointer is visible.|
S
shawn_he 已提交
58 59 60

**Return value**

S
shawn_he 已提交
61 62
| Name                 | Description                 |
| ------------------- | ------------------- |
S
shawn_he 已提交
63 64 65 66 67
| Promise&lt;void&gt; | Promise used to return the result.|

**Example**

```js
S
shawn_he 已提交
68 69 70 71 72 73 74
try {
  pointer.setPointerVisible(false).then(() => {
    console.log(`Set pointer visible success`);
  });
} catch (error) {
  console.log(`Set pointer visible failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
S
shawn_he 已提交
75 76
```

S
shawn_he 已提交
77
## pointer.isPointerVisible<sup>9+</sup>
S
shawn_he 已提交
78 79 80

isPointerVisible(callback: AsyncCallback&lt;boolean&gt;): void

S
shawn_he 已提交
81
Checks the visible status of the mouse pointer. This API uses an asynchronous callback to return the result. 
S
shawn_he 已提交
82 83 84 85 86

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

**Parameters**

S
shawn_he 已提交
87 88 89
| Name      | Type                          | Mandatory  | Description            |
| -------- | ---------------------------- | ---- | -------------- |
| callback | AsyncCallback&lt;boolean&gt; | Yes   | Callback used to return the result.|
S
shawn_he 已提交
90 91 92 93

**Example**

```js
S
shawn_he 已提交
94 95 96 97 98 99 100 101 102 103 104
try {
  pointer.isPointerVisible((error, visible) => {
    if (error) {
      console.log(`Get pointer visible failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
      return;
    }
    console.log(`Get pointer visible success, visible: ${JSON.stringify(visible)}`);
  });
} catch (error) {
  console.log(`Get pointer visible failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
S
shawn_he 已提交
105 106
```

S
shawn_he 已提交
107
## pointer.isPointerVisible<sup>9+</sup>
S
shawn_he 已提交
108 109 110

isPointerVisible(): Promise&lt;boolean&gt;

S
shawn_he 已提交
111
Checks the visible status of the mouse pointer. This API uses a promise to return the result. 
S
shawn_he 已提交
112 113 114 115 116

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

**Return value**

S
shawn_he 已提交
117 118
| Name                    | Description                 |
| ---------------------- | ------------------- |
S
shawn_he 已提交
119 120 121 122 123
| Promise&lt;boolean&gt; | Promise used to return the result.|

**Example**

```js
S
shawn_he 已提交
124 125 126 127 128 129 130
try {
  pointer.isPointerVisible().then((visible) => {
    console.log(`Get pointer visible success, visible: ${JSON.stringify(visible)}`);
  });
} catch (error) {
  console.log(`Get pointer visible failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
S
shawn_he 已提交
131
```
S
shawn_he 已提交
132 133 134 135 136 137 138 139 140

## pointer.setPointerSpeed<sup>9+</sup>

setPointerSpeed(speed: number, callback: AsyncCallback&lt;void&gt;): void

Sets the mouse movement speed. This API uses an asynchronous callback to return the result.

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

S
shawn_he 已提交
141 142
**System API**: This is a system API.

S
shawn_he 已提交
143 144 145 146 147
**Parameters**

| Name      | Type                       | Mandatory  | Description                                   |
| -------- | ------------------------- | ---- | ------------------------------------- |
| speed    | number                    | Yes   | Mouse movement speed. The value ranges from **1** to **11**. The default value is **5**.  |
S
shawn_he 已提交
148
| callback | AsyncCallback&lt;void&gt; | Yes   | Callback used to return the result.|
S
shawn_he 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

**Example**

```js
try {
  pointer.setPointerSpeed(5, (error) => {
    if (error) {
      console.log(`Set pointer speed failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
      return;
    }
    console.log(`Set pointer speed success`);
  });
} catch (error) {
  console.log(`Set pointer speed failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
```

## pointer.setPointerSpeed<sup>9+</sup>

setPointerSpeed(speed: number): Promise&lt;void&gt;

Sets the mouse movement speed. This API uses a promise to return the result.

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

S
shawn_he 已提交
174 175
**System API**: This is a system API.

S
shawn_he 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
**Parameters**

| Name   | Type    | Mandatory  | Description                                 |
| ----- | ------ | ---- | ----------------------------------- |
| speed | number | Yes   | Mouse movement speed. The value ranges from **1** to **11**. The default value is **5**.|

**Return value**

| Name                 | Description              |
| ------------------- | ---------------- |
| Promise&lt;void&gt; | Promise used to return the result.|

**Example**

```js
try {
  pointer.setPointerSpeed(5).then(() => {
    console.log(`Set pointer speed success`);
  });
} catch (error) {
  console.log(`Set pointer speed failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
```

## pointer.getPointerSpeed<sup>9+</sup>

getPointerSpeed(callback: AsyncCallback&lt;number&gt;): void

Obtains the mouse movement speed. This API uses an asynchronous callback to return the result.

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

S
shawn_he 已提交
208 209
**System API**: This is a system API.

S
shawn_he 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
**Parameters**

| Name      | Type                         | Mandatory  | Description            |
| -------- | --------------------------- | ---- | -------------- |
| callback | AsyncCallback&lt;number&gt; | Yes   | Callback used to return the result.|

**Example**

```js
try {
  pointer.getPointerSpeed((error, speed) => {
    if (error) {
      console.log(`Get pointer speed failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
      return;
    }
    console.log(`Get pointer speed success, speed: ${JSON.stringify(speed)}`);
  });
} catch (error) {
  console.log(`Get pointer speed failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
```

## pointer.getPointerSpeed<sup>9+</sup>

getPointerSpeed(): Promise&lt;number&gt;

Obtains the mouse movement speed. This API uses a promise to return the result.

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

**Return value**

| Name                   | Description                 |
| --------------------- | ------------------- |
| Promise&lt;number&gt; | Promise used to return the result.|

**Example**

```js
try {
  pointer.getPointerSpeed().then(speed => {
    console.log(`Get pointer speed success, speed: ${JSON.stringify(speed)}`);
  });
} catch (error) {
  console.log(`Get pointer speed failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
```

## pointer.getPointerStyle<sup>9+</sup>

getPointerStyle(windowId: number, callback: AsyncCallback&lt;PointerStyle&gt;): void

Obtains the mouse pointer style. This API uses an asynchronous callback to return the result.

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

S
shawn_he 已提交
266 267
**System API**: This is a system API.

S
shawn_he 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280 281
**Parameters**

| Name      | Type                                      | Mandatory  | Description            |
| -------- | ---------------------------------------- | ---- | -------------- |
| windowId | number                                   | Yes   | Window ID.   |
| callback | AsyncCallback&lt;[PointerStyle](#pointerstyle9)&gt; | Yes   | Callback used to return the result.|

**Example**

```js
import window from '@ohos.window';

window.getTopWindow((error, win) => {
  win.getProperties((error, properties) => {
S
shawn_he 已提交
282
    let windowId = properties.id;
S
shawn_he 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
    if (windowId < 0) {
      console.log(`Invalid windowId`);
      return;
    }
    try {
      pointer.getPointerStyle(windowId, (error, style) => {
        console.log(`Get pointer style success, style: ${JSON.stringify(style)}`);
      });
    } catch (error) {
      console.log(`Get pointer style failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
    }
  });
});
```

## pointer.getPointerStyle<sup>9+</sup>

getPointerStyle(windowId: number): Promise&lt;PointerStyle&gt;

Obtains the mouse pointer style. This API uses a promise to return the result.

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

**Parameters**

| Name    | Type  | Mandatory| Description    |
| -------- | ------ | ---- | -------- |
| windowId | number | Yes  | Window ID.|

**Return value**

| Name                                      | Description                 |
| ---------------------------------------- | ------------------- |
| Promise&lt;[PointerStyle](#pointerstyle9)&gt; | Promise used to return the result.|

**Example**

```js
import window from '@ohos.window';

window.getTopWindow((error, win) => {
  win.getProperties((error, properties) => {
S
shawn_he 已提交
325
    let windowId = properties.id;
S
shawn_he 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
    if (windowId < 0) {
      console.log(`Invalid windowId`);
      return;
    }
    try {
      pointer.getPointerStyle(windowId).then((style) => {
        console.log(`Get pointer style success, style: ${JSON.stringify(style)}`);
      });
    } catch (error) {
      console.log(`Get pointer style failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
    }
  });
});
```

## pointer.setPointerStyle<sup>9+</sup>

setPointerStyle(windowId: number, pointerStyle: PointerStyle, callback: AsyncCallback&lt;void&gt;): void

Sets the mouse pointer style. This API uses an asynchronous callback to return the result.

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

**Parameters**

| Name          | Type                            | Mandatory  | Description                                 |
| ------------ | ------------------------------ | ---- | ----------------------------------- |
| windowId     | number                         | Yes   | Window ID.                         |
| pointerStyle | [PointerStyle](#pointerstyle9) | Yes   | Mouse pointer style ID.                            |
S
shawn_he 已提交
355
| callback     | AsyncCallback&lt;void&gt;      | Yes   | Callback used to return the result.|
S
shawn_he 已提交
356 357 358 359 360 361 362 363

**Example**

```js
import window from '@ohos.window';

window.getTopWindow((error, win) => {
  win.getProperties((error, properties) => {
S
shawn_he 已提交
364
    let windowId = properties.id;
S
shawn_he 已提交
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
    if (windowId < 0) {
      console.log(`Invalid windowId`);
      return;
    }
    try {
      pointer.setPointerStyle(windowId, pointer.PointerStyle.CROSS, error => {
        console.log(`Set pointer style success`);
      });
    } catch (error) {
      console.log(`Set pointer style failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
    }
  });
});
```
## pointer.setPointerStyle<sup>9+</sup>

setPointerStyle(windowId: number, pointerStyle: PointerStyle): Promise&lt;void&gt;

Sets the mouse pointer style. This API uses a promise to return the result.

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

**Parameters**

| Name                 | Type                            | Mandatory  | Description              |
| ------------------- | ------------------------------ | ---- | ---------------- |
| windowId            | number                         | Yes   | Window ID.      |
| pointerStyle        | [PointerStyle](#pointerstyle9) | Yes   | Mouse pointer style ID.         |
| Promise&lt;void&gt; | void                           | Yes   | Promise used to return the result.|

**Example**

```js
import window from '@ohos.window';

window.getTopWindow((error, win) => {
  win.getProperties((error, properties) => {
S
shawn_he 已提交
402
    let windowId = properties.id;
S
shawn_he 已提交
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
    if (windowId < 0) {
      console.log(`Invalid windowId`);
      return;
    }
    try {
      pointer.setPointerStyle(windowId, pointer.PointerStyle.CROSS).then(() => {
        console.log(`Set pointer style success`);
      });
    } catch (error) {
      console.log(`Set pointer style failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
    }
  });
});
```
## PointerStyle<sup>9+</sup>

Enumerates mouse pointer styles.

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

| Name                              | Value   | Description    |
| -------------------------------- | ---- | ------ |
| DEFAULT                          | 0    | Default     |
| EAST                             | 1    | East arrow  |
| WEST                             | 2    | West arrow  |
| SOUTH                            | 3    | South arrow  |
| NORTH                            | 4    | North arrow  |
| WEST_EAST                        | 5    | West-east arrow |
| NORTH_SOUTH                      | 6    | North-south arrow |
| NORTH_EAST                       | 7    | North-east arrow |
| NORTH_WEST                       | 8    | North-west arrow |
| SOUTH_EAST                       | 9    | South-east arrow |
| SOUTH_WEST                       | 10   | South-west arrow |
| NORTH_EAST_SOUTH_WEST            | 11   | North-east and south-west adjustment|
| NORTH_WEST_SOUTH_EAST            | 12   | North-west and south-east adjustment|
| CROSS                            | 13   | Cross (accurate selection)  |
| CURSOR_COPY                      | 14   | Copy cursor    |
| CURSOR_FORBID                    | 15   | Forbid cursor   |
| COLOR_SUCKER                     | 16   | Sucker    |
| HAND_GRABBING                    | 17   | Grabbing hand  |
| HAND_OPEN                        | 18   | Opening hand  |
| HAND_POINTING                    | 19   | Hand-shaped pointer  |
| HELP                             | 20   | Help   |
| MOVE                             | 21   | Move    |
| RESIZE_LEFT_RIGHT                | 22   | Left and right resizing|
| RESIZE_UP_DOWN                   | 23   | Up and down resizing|
| SCREENSHOT_CHOOSE                | 24   | Screenshot crosshair|
| SCREENSHOT_CURSOR                | 25   | Screenshot cursor    |
| TEXT_CURSOR                      | 26   | Text cursor  |
| ZOOM_IN                          | 27   | Zoom in    |
| ZOOM_OUT                         | 28   | Zoom out    |
| MIDDLE_BTN_EAST                  | 29   | Scrolling east  |
| MIDDLE_BTN_WEST                  | 30   | Scrolling west  |
| MIDDLE_BTN_SOUTH                 | 31   | Scrolling south  |
| MIDDLE_BTN_NORTH                 | 32   | Scrolling north  |
| MIDDLE_BTN_NORTH_SOUTH           | 33   | Scrolling north-south |
| MIDDLE_BTN_NORTH_EAST            | 34   | Scrolling north-east |
| MIDDLE_BTN_NORTH_WEST            | 35   | Scrolling north-west |
| MIDDLE_BTN_SOUTH_EAST            | 36   | Scrolling south-east |
| MIDDLE_BTN_SOUTH_WEST            | 37   | Scrolling south-west |
| MIDDLE_BTN_NORTH_SOUTH_WEST_EAST | 38   | Moving as a cone in four directions|