js-apis-pointer.md 15.1 KB
Newer Older
S
shawn_he 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Mouse Pointer

The mouse pointer module provides APIs related to pointer attribute management.

> **NOTE**
>
> 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 已提交
15
## pointer.setPointerVisible<sup>9+</sup>
S
shawn_he 已提交
16 17 18 19 20 21 22 23 24

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 已提交
25 26 27 28
| 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 已提交
29 30 31 32

**Example**

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

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

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 已提交
56 57 58
| Name     | Type     | Mandatory  | Description                                      |
| ------- | ------- | ---- | ---------------------------------------- |
| visible | boolean | Yes   | Whether the mouse pointer is visible.|
S
shawn_he 已提交
59 60 61

**Return value**

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

**Example**

```js
S
shawn_he 已提交
69 70 71 72 73 74 75
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 已提交
76 77
```

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

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

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

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

**Parameters**

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

**Example**

```js
S
shawn_he 已提交
95 96 97 98 99 100 101 102 103 104 105
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 已提交
106 107
```

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

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

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

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

**Return value**

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

**Example**

```js
S
shawn_he 已提交
125 126
pointer.isPointerVisible().then((visible) => {
  console.log(`Get pointer visible success, visible: ${JSON.stringify(visible)}`);
S
shawn_he 已提交
127 128
});
```
S
shawn_he 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 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 174 175 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 208 209 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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 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 325 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 355 356 357 358 359 360 361 362 363 364 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 402 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

## 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

**Parameters**

| Name      | Type                       | Mandatory  | Description                                   |
| -------- | ------------------------- | ---- | ------------------------------------- |
| speed    | number                    | Yes   | Mouse movement speed. The value ranges from **1** to **11**. The default value is **5**.  |
| callback | AysncCallback&lt;void&gt; | Yes   | Callback used to return the result.|

**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

**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

**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

**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) => {
    var windowId = properties.id;
    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) => {
    var windowId = properties.id;
    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.                            |
| callback     | AysncCallback&lt;void&gt;      | Yes   | Callback used to return the result.|

**Example**

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

window.getTopWindow((error, win) => {
  win.getProperties((error, properties) => {
    var windowId = properties.id;
    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) => {
    var windowId = properties.id;
    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|