openharmony 鸿蒙 ts-universal-events-axis

2025-06-12 浏览 (1)

Axis Event

An axis event is triggered when a user interacts with a component by scrolling the mouse wheel or swiping two fingers on a touchpad along a specific direction (axis). The term "axis" here refers to the direction in a 2D coordinate system, which includes the horizontal direction (x-axis) and the vertical direction (y-axis).

NOTE

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

onAxisEvent

onAxisEvent(event: (event: AxisEvent) => void): T

Called when the mouse wheel is scrolled or two fingers are swiped on a touchpad.

Widget capability: This API can be used in ArkTS widgets since API version 18.

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

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

NameTypeMandatoryDescription
eventAxisEventYesAxisEvent object.

Return value

TypeDescription
TCurrent component.

AxisEvent

Defines an axis event object that inherits from BaseEvent.

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

System capability: SystemCapability.ArkUI.ArkUI.Full

Properties

NameTypeRead-OnlyOptionalDescription
actionAxisActionYesNoAction type of the axis event.
xnumberYesNoX coordinate of the cursor relative to the left edge of the element.
Unit: vp
ynumberYesNoY coordinate of the cursor relative to the top edge of the element.
Unit: vp
windowXnumberYesNoX coordinate of the cursor relative to the upper left corner of the current window.
Unit: vp
windowYnumberYesNoY coordinate of the cursor relative to the upper left corner of the current window.
Unit: vp
displayXnumberYesNoX coordinate of the cursor relative to the upper left corner of the screen.
Unit: vp
displayYnumberYesNoY coordinate of the cursor relative to the upper left corner of the current screen.
Unit: vp
scrollStepnumberYesNoScroll step length for the mouse wheel.
NOTE
Only the mouse wheel is supported. The value ranges from 0 to 65535.
propagationCallback<void>YesNoCallback to activate event bubbling.

getHorizontalAxisValue

getHorizontalAxisValue(): number

Obtains the horizontal axis value of this axis event.

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

System capability: SystemCapability.ArkUI.ArkUI.Full

Return value

TypeDescription
numberHorizontal axis value.

getVerticalAxisValue

getVerticalAxisValue(): number

Obtains the vertical axis value of this axis event.

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

System capability: SystemCapability.ArkUI.ArkUI.Full

Return value

TypeDescription
numberVertical axis value.

Example

This example shows how to set up an axis event on a button. When the user scrolls the mouse wheel over the button, the event parameters are captured and displayed.

// xxx.ets
@Entry
@Component
struct AxisEventExample {
  @State text: string = ''

  build() {
    Column() {
      Row({ space: 20 }) {
        Button('AxisEvent').width(100).height(40)
          .onAxisEvent((event?: AxisEvent) => {
            if (event) {
              this.text =
                'AxisEvent:' + '\n  action:' + event.action + '\n  displayX:' + event.displayX + '\n  displayY:' +
                event.displayY + '\n  windowX:' + event.windowX + '\n  windowY:' + event.windowY + '\n  x:' + event.x +
                  '\n  y:' + event.y + '\n VerticalAxisValue:' + event.getVerticalAxisValue() +
                  '\n HorizontalAxisValue:' + event.getHorizontalAxisValue()
            }
          })
      }.margin(20)

      Text(this.text).margin(15)
    }.width('100%')
  }
}

The figure below shows the event parameters captured when the user scrolls the mouse wheel over the button.

onAxisEvent

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArcButton

harmony 鸿蒙ArcSlider

harmony 鸿蒙Chip

harmony 鸿蒙ChipGroup

harmony 鸿蒙ComposeListItem

harmony 鸿蒙ComposeTitleBar

harmony 鸿蒙advanced.Counter

harmony 鸿蒙Dialog Box (Dialog)

harmony 鸿蒙DialogV2

harmony 鸿蒙DownloadFileButton

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/rdn2vi