ts-universal-mouse-key.md 3.3 KB
Newer Older
Z
zengyawen 已提交
1 2 3
# Mouse Event


E
esterzhou 已提交
4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
Z
zengyawen 已提交
5 6 7 8 9 10 11 12 13 14
> This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.


## Required Permissions

None


## Events

E
esterzhou 已提交
15
  | Name | Bubble Supported | Description | 
Z
zengyawen 已提交
16
| -------- | -------- | -------- |
E
esterzhou 已提交
17 18
| onHover(callback: (isHover: boolean) =&gt; void) | No | Triggered when the mouse cursor enters or leaves the component.<br/>**isHover**: whether the mouse cursor hovers over the component. The value **true** means that the mouse cursor enters the component, and the value **false** means that the mouse cursor leaves the component. | 
| onMouse(callback: (event?: MouseEvent) =&gt; void) | Yes | Triggered when the component is clicked by a mouse button or the mouse cursor moves on the component. The **event** parameter indicates the timestamp, mouse button, action, coordinates of the clicked point on the entire screen, and coordinates of the clicked point relative to the component when the event is triggered. | 
Z
zengyawen 已提交
19 20 21 22 23 24 25


### MouseEvent

- Attributes
    | Name | Type | Description | 
  | -------- | -------- | -------- |
E
esterzhou 已提交
26 27 28 29 30 31 32
  | timestamp | number | Timestamp when the event is triggered. | 
  | screenX | number | X-coordinate of the clicked point relative to the upper left corner of the screen. | 
  | screenY | number | Y-coordinate of the clicked point relative to the upper left corner of the screen. | 
  | x | number | X-coordinate of the clicked point relative to the upper left corner of the component. | 
  | y | number | Y-coordinate of the clicked point relative to the upper left corner of the component. | 
  | button | MouseButton | Mouse button. | 
  | action | MouseAction | Event action. | 
Z
zengyawen 已提交
33 34 35 36 37


- MouseButton attributes
    | Name | Type | Description | 
  | -------- | -------- | -------- |
E
esterzhou 已提交
38 39 40 41 42 43
  | Left | number | Left mouse button. | 
  | Right | number | Right mouse button. | 
  | Middle | number | Middle mouse button. | 
  | Back | number | Back button on the left of the mouse. | 
  | Forward | number | Forward button on the left of the mouse. | 
  | None | number | No button. | 
Z
zengyawen 已提交
44 45 46 47

- MouseAction attributes
    | Name | Type | Description | 
  | -------- | -------- | -------- |
E
esterzhou 已提交
48 49 50
  | Press | number | The mouse button is pressed. | 
  | Release | number | The mouse button is released. | 
  | Move | number | The mouse cursor moves. | 
Z
zengyawen 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87


## Example

  
```
@Entry
@Component
struct MouseEventExample {
  @State hoverText: string = 'no hover'
  @State mouseText: string = 'MouseText'
  @State color: Color = Color.Blue

  build() {
    Column({ space:20 }) {
      Button(this.hoverText)
        .onHover((isHover: boolean) => {
          if (isHover) {
            this.hoverText = 'on hover'
            this.color = Color.Pink
          } else {
            this.hoverText = 'no hover'
            this.color = Color.Blue
          }
        })
        .backgroundColor(this.color)
      Button('onMouse')
        .onMouse((event: MouseEvent) => {
          console.log(this.mouseText = 'onMouse:\nButton = ' + event.button + 
          '\nAction = ' + event.action + '\nlocalXY=(' + event.x + ',' + event.y + ')' + 
          '\nscreenXY=(' + event.screenX + ',' + event.screenY + ')')
        })
      Text(this.mouseText)
    }.padding({ top: 20 }).width('100%')
  }
}
```