We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
ReferencesApplication FrameworkArkUIArkTS ComponentsUniversal AttributesInteraction AttributesCursor Control

Cursor Control

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

You can control the display style of the mouse cursor.

NOTE

This feature is supported since API version 11. Updates will be marked with a superscript to indicate their earliest API version.

cursorControl

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

setCursor

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

setCursor(value: PointerStyle): void

Sets the current mouse cursor style. This API can be used globally in method statements.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

Expand
Name Type Mandatory Description
value PointerStyle All consistent Cursor style.

restoreDefault

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

restoreDefault(): void

Restores the mouse cursor to the default arrow style. This API can be used globally in method statements.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

PointerStyle

type PointerStyle = pointer.PointerStyle

Defines the pointer style.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Expand
Type Description
pointer.PointerStyle Pointer style.
NOTE

Directly using cursorControl can lead to the issue of ambiguous UI context. To avoid this, obtain the UIContext object using the getUIContext() API and then obtain the cursorControl bound to the instance using the getCursorController API.

Example

This example demonstrates how to change the mouse cursor style using setCursor.

Collapse
Word wrap
Dark theme
Copy code
  1. // xxx.ets
  2. import { pointer } from '@kit.InputKit';
  3. @Entry
  4. @Component
  5. struct CursorControlExample {
  6. @State text: string = '';
  7. controller: TextInputController = new TextInputController()
  8. build() {
  9. Column() {
  10. Row()
  11. .height(200)
  12. .width(200)
  13. .backgroundColor(Color.Green)
  14. .position({ x: 60, y: 70 })
  15. .onHover((flag) => {
  16. if (flag) {
  17. // You are advised to use this.getUIContext().getCursorController().setCursor().
  18. cursorControl.setCursor(pointer.PointerStyle.EAST)
  19. } else {
  20. // You are advised to use this.getUIContext().getCursorController().restoreDefault().
  21. cursorControl.restoreDefault()
  22. }
  23. })
  24. Row()
  25. .height(200)
  26. .width(200)
  27. .backgroundColor(Color.Blue)
  28. .position({ x: 130, y: 120 })
  29. .onHover((flag) => {
  30. if (flag) {
  31. // You are advised to use this.getUIContext().getCursorController().setCursor().
  32. cursorControl.setCursor(pointer.PointerStyle.WEST)
  33. } else {
  34. // You are advised to use this.getUIContext().getCursorController().restoreDefault().
  35. cursorControl.restoreDefault()
  36. }
  37. })
  38. }.width('100%')
  39. }
  40. }

Diagrams:

When the mouse hovers over the blue area, it displays a west-pointing arrow cursor style.

When the mouse hovers over the green area, it displays an east-pointing arrow cursor style.

Search in References
Enter a keyword.