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): T
12
13Invoked when a touch event is triggered.
14
15**Atomic service API**: This API can be used in atomic services since API version 11.
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**Return value**
26
27| Type| Description|
28| -------- | -------- |
29| T | Current component.|
30
31## TouchEvent
32
33Inherits from [BaseEvent](ts-gesture-customize-judge.md#baseevent). In non-injected event scenarios, **changedTouches** indicates points resampled according to the screen's refresh rate, while **touches** indicates points based on the device's refresh rate. As such, the data in **changedTouches** may differ from that in **touches**.
34
35**System capability**: SystemCapability.ArkUI.ArkUI.Full
36
37| Name               | Type                                      | Description          |
38| ------------------- | ---------------------------------------- | ------------ |
39| type                | [TouchType](ts-appendix-enums.md#touchtype)      | Type of the touch event.<br>**Atomic service API**: This API can be used in atomic services since API version 11.    |
40| touches             | Array&lt;[TouchObject](#touchobject)&gt; | All finger information.<br>**Atomic service API**: This API can be used in atomic services since API version 11.     |
41| changedTouches      | Array&lt;[TouchObject](#touchobject)&gt; | Finger information changed.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
42| stopPropagation      | () => void | Stops the event from bubbling upwards or downwards.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
43| preventDefault<sup>12+</sup>      | () => void |  Blocks the default event.<br> **NOTE**<br>This API is only supported by the following components: **Hyperlink**. Asynchronous calls and the **Modifier** API are not yet supported.<br> **Atomic service API**: This API can be used in atomic services since API version 12.|
44
45### getHistoricalPoints<sup>10+</sup>
46
47getHistoricalPoints(): Array&lt;HistoricalPoint&gt;
48
49Obtains 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.
50
51**Atomic service API**: This API can be used in atomic services since API version 11.
52
53**System capability**: SystemCapability.ArkUI.ArkUI.Full
54
55**Return value**
56
57| Type    | Description                     |
58| ------ | ----------------------- |
59| Array&lt;[HistoricalPoint](#historicalpoint10)&gt;| Array of historical points.|
60
61
62## TouchObject
63
64**System capability**: SystemCapability.ArkUI.ArkUI.Full
65
66| Name   | Type                                       | Description                                 |
67| ------- | ------------------------------------------- | ------------------------------------- |
68| type    | [TouchType](ts-appendix-enums.md#touchtype) | Type of the touch event.<br>**Atomic service API**: This API can be used in atomic services since API version 11.                     |
69| id      | number                                      | Unique identifier of a finger.<br>**Atomic service API**: This API can be used in atomic services since API version 11.                     |
70| x       | number                                      | X coordinate of the touch point relative to the upper left corner of the event responding component.<br>Unit: vp<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
71| y       | number                                      | Y coordinate of the touch point relative to the upper left corner of the event responding component.<br>Unit: vp<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
72| windowX<sup>10+</sup>  | number                       | X coordinate of the touch point relative to the upper left corner of the application window.<br>Unit: vp<br>**Atomic service API**: This API can be used in atomic services since API version 11.  |
73| windowY<sup>10+</sup>  | number                       | Y coordinate of the touch point relative to the upper left corner of the application window.<br>Unit: vp<br>**Atomic service API**: This API can be used in atomic services since API version 11.  |
74| displayX<sup>10+</sup> | number                       | X coordinate of the touch point relative to the upper left corner of the application screen.<br>Unit: vp<br>**Atomic service API**: This API can be used in atomic services since API version 11.  |
75| displayY<sup>10+</sup> | number                       | Y coordinate of the touch point relative to the upper left corner of the application screen.<br>Unit: vp<br>**Atomic service API**: This API can be used in atomic services since API version 11.  |
76| screenX<sup>(deprecated)</sup> | number               | X coordinate of the touch point relative to the upper left corner of the application window.<br>Unit: vp<br>This API is deprecated since API version 10. You are advised to use **windowX** instead.  |
77| screenY<sup>(deprecated)</sup> | number               | Y coordinate of the touch point relative to the upper left corner of the application window.<br>Unit: vp<br>This API is deprecated since API version 10. You are advised to use **windowY** instead.  |
78
79## HistoricalPoint<sup>10+</sup>
80
81**Atomic service API**: This API can be used in atomic services since API version 11.
82
83**System capability**: SystemCapability.ArkUI.ArkUI.Full
84
85| Name        | Type                                | Description                                                                        |
86| ----------- | ----------------------------------- | ----------------------------------------------------------------------------- |
87| touchObject | [TouchObject](#touchobject)  | Basic information of the historical point.                                                  |
88| size        | number                              | Size of the contact area between the finger and screen for the historical point.<br>Default value: **0**                                    |
89| force       | number                              | Touch force of the historical point.<br>Default value: **0**<br>Value range: [0, 65535). The greater the pressure, the larger the value.|
90| 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          |
91## Example
92
93This example configures a touch event for a button. When the button is touched, it obtains relevant parameters of the touch event.
94
95```ts
96// xxx.ets
97@Entry
98@Component
99struct TouchExample {
100  @State text: string = ''
101  @State eventType: string = ''
102
103  build() {
104    Column() {
105      Button('Touch').height(40).width(100)
106        .onTouch((event?: TouchEvent) => {
107          if(event){
108            if (event.type === TouchType.Down) {
109              this.eventType = 'Down'
110            }
111            if (event.type === TouchType.Up) {
112              this.eventType = 'Up'
113            }
114            if (event.type === TouchType.Move) {
115              this.eventType = 'Move'
116            }
117            this.text = 'TouchType:' + this.eventType + '\nDistance between touch point and touch element:\nx: '
118            + event.touches[0].x + '\n' + 'y: ' + event.touches[0].y + '\nComponent globalPos:('
119            + event.target.area.globalPosition.x + ',' + event.target.area.globalPosition.y + ')\nwidth:'
120            + event.target.area.width + '\nheight:' + event.target.area.height
121          }
122        })
123      Button('Touch').height(50).width(200).margin(20)
124        .onTouch((event?: TouchEvent) => {
125          if(event){
126            if (event.type === TouchType.Down) {
127              this.eventType = 'Down'
128            }
129            if (event.type === TouchType.Up) {
130              this.eventType = 'Up'
131            }
132            if (event.type === TouchType.Move) {
133              this.eventType = 'Move'
134            }
135            this.text = 'TouchType:' + this.eventType + '\nDistance between touch point and touch element:\nx: '
136            + event.touches[0].x + '\n' + 'y: ' + event.touches[0].y + '\nComponent globalPos:('
137            + event.target.area.globalPosition.x + ',' + event.target.area.globalPosition.y + ')\nwidth:'
138            + event.target.area.width + '\nheight:' + event.target.area.height
139          }
140        })
141      Text(this.text)
142    }.width('100%').padding(30)
143  }
144}
145```
146
147![en-us_image_0000001209874754](figures/en-us_image_0000001209874754.gif)
148