• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Touch Event
2
3A touch event is triggered when a finger is pressed against, swipes on, or is lifted from a component.
4
5> **NOTE**
6>
7> This event is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
8
9## onTouch
10
11onTouch(event: (event: TouchEvent) => void)
12
13Invoked when a touch event is triggered.
14
15**Widget capability**: Since API version 9, this feature is supported in ArkTS widgets.
16
17**System capability**: SystemCapability.ArkUI.ArkUI.Full
18
19**Parameters**
20
21| Name| Type                             | Mandatory| Description                |
22| ------ | --------------------------------- | ---- | -------------------- |
23| event  | [TouchEvent](#touchevent) | Yes  | **TouchEvent** object.|
24
25## TouchEvent
26
27| Name               | Type                                      | Description          |
28| ------------------- | ---------------------------------------- | ------------ |
29| type                | [TouchType](ts-appendix-enums.md#touchtype)      | Type of the touch event.    |
30| touches             | Array<[TouchObject](#touchobject)> | All finger information.     |
31| changedTouches      | Array<[TouchObject](#touchobject)> | Finger information changed.|
32| stopPropagation      | () => void | Stops the event from bubbling upwards or downwards.|
33| timestamp<sup>8+</sup> | number | Timestamp of the event. It is the interval between the time when the event is triggered and the time when the system starts.<br>For example, if the system starts at 2023/10/12 11:33 and the touch event is triggered at 2023/10/12 11:34, then the returned timestamp value is 60,000,000,000 ns.<br>Unit: ns|
34| target<sup>8+</sup> | [EventTarget](ts-universal-events-click.md#eventtarget8) | Display area of the element that triggers the gesture event.|
35| source<sup>8+</sup> | [SourceType](ts-gesture-settings.md#sourcetype)| Event input device.|
36
37
38### getHistoricalPoints<sup>10+</sup>
39
40getHistoricalPoints(): Array&lt;HistoricalPoint&gt;
41
42Obtains all historical points of the current frame. The touch event frequency of a frame varies by device, and all touch events of the current frame are referred to as its historical points. This API can be called only in [TouchEvent](#touchevent). You can use this API to obtain the historical points of the current frame when [onTouch](#ontouch) is invoked. [onTouch](#ontouch) is invoked only once for a frame. If the value of [TouchEvent](#touchevent) received by the current frame is greater than 1, the last point of the frame is returned through [onTouch](#ontouch). The remaining points are regarded as historical points.
43
44**Return value**
45
46| Type    | Description                     |
47| ------ | ----------------------- |
48| Array&lt;[HistoricalPoint](#historicalpoint10)&gt;| Array of historical points.|
49
50
51## TouchObject
52
53| Name   | Type                                       | Description                                 |
54| ------- | ------------------------------------------- | ------------------------------------- |
55| type    | [TouchType](ts-appendix-enums.md#touchtype) | Type of the touch event.                     |
56| id      | number                                      | Unique identifier of a finger.                     |
57| x       | number                                      | X coordinate of the touch point relative to the upper left corner of the original area of the touched element.|
58| y       | number                                      | Y coordinate of the touch point relative to the upper left corner of the original area of the touched element.|
59| windowX<sup>10+</sup>  | number                       | X coordinate of the touch point relative to the upper left corner of the application window.  |
60| windowY<sup>10+</sup>  | number                       | Y coordinate of the touch point relative to the upper left corner of the application window.  |
61| displayX<sup>10+</sup> | number                       | X coordinate of the touch point relative to the upper left corner of the application screen.  |
62| displayY<sup>10+</sup> | number                       | Y coordinate of the touch point relative to the upper left corner of the application screen.  |
63| screenX<sup>(deprecated)</sup> | number               | X coordinate of the touch point relative to the upper left corner of the application window.<br>This API is deprecated since API version 10. You are advised to use **windowX** instead.  |
64| screenY<sup>(deprecated)</sup> | number               | Y coordinate of the touch point relative to the upper left corner of the application window.<br>This API is deprecated since API version 10. You are advised to use **windowX** instead.  |
65
66## HistoricalPoint<sup>10+</sup>
67
68| Name        | Type                                | Description                                                                        |
69| ----------- | ----------------------------------- | ----------------------------------------------------------------------------- |
70| touchObject | [TouchObject](#touchobject)  | Basic information of the historical point.                                                  |
71| size        | number                              | Size of the contact area between the finger and screen for the historical point.<br>Default value: **0**                                    |
72| force       | number                              | Touch force of the historical point.<br>Default value: **0**<br>Value range: [0,1]. A larger value indicates a greater touch force.<br>The support for this API varies by device. Currently, it is only available on tablets.|
73| timestamp   | number                              | Timestamp of the historical point. It is the interval between the time when the event is triggered and the time when the system starts.<br>Unit: ns          |
74## Example
75
76```ts
77// xxx.ets
78@Entry
79@Component
80struct TouchExample {
81  @State text: string = ''
82  @State eventType: string = ''
83
84  build() {
85    Column() {
86      Button('Touch').height(40).width(100)
87        .onTouch((event?: TouchEvent) => {
88          if(event){
89            if (event.type === TouchType.Down) {
90              this.eventType = 'Down'
91            }
92            if (event.type === TouchType.Up) {
93              this.eventType = 'Up'
94            }
95            if (event.type === TouchType.Move) {
96              this.eventType = 'Move'
97            }
98            this.text = 'TouchType:' + this.eventType + '\nDistance between touch point and touch element:\nx: '
99            + event.touches[0].x + '\n' + 'y: ' + event.touches[0].y + '\nComponent globalPos:('
100            + event.target.area.globalPosition.x + ',' + event.target.area.globalPosition.y + ')\nwidth:'
101            + event.target.area.width + '\nheight:' + event.target.area.height
102          }
103        })
104      Button('Touch').height(50).width(200).margin(20)
105        .onTouch((event?: TouchEvent) => {
106          if(event){
107            if (event.type === TouchType.Down) {
108              this.eventType = 'Down'
109            }
110            if (event.type === TouchType.Up) {
111              this.eventType = 'Up'
112            }
113            if (event.type === TouchType.Move) {
114              this.eventType = 'Move'
115            }
116            this.text = 'TouchType:' + this.eventType + '\nDistance between touch point and touch element:\nx: '
117            + event.touches[0].x + '\n' + 'y: ' + event.touches[0].y + '\nComponent globalPos:('
118            + event.target.area.globalPosition.x + ',' + event.target.area.globalPosition.y + ')\nwidth:'
119            + event.target.area.width + '\nheight:' + event.target.area.height
120          }
121        })
122      Text(this.text)
123    }.width('100%').padding(30)
124  }
125}
126```
127
128![en-us_image_0000001209874754](figures/en-us_image_0000001209874754.gif)
129