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).
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 can only be used by certain components; currently there are no supported components.<br>**Atomic service API**: This API can be used in atomic services since API version 12.|
44
45
46### getHistoricalPoints<sup>10+</sup>
47
48getHistoricalPoints(): Array&lt;HistoricalPoint&gt;
49
50Obtains 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.
51
52**Atomic service API**: This API can be used in atomic services since API version 11.
53
54**System capability**: SystemCapability.ArkUI.ArkUI.Full
55
56**Return value**
57
58| Type    | Description                     |
59| ------ | ----------------------- |
60| Array&lt;[HistoricalPoint](#historicalpoint10)&gt;| Array of historical points.|
61
62
63## TouchObject
64
65**System capability**: SystemCapability.ArkUI.ArkUI.Full
66
67| Name   | Type                                       | Description                                 |
68| ------- | ------------------------------------------- | ------------------------------------- |
69| 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.                     |
70| id      | number                                      | Unique identifier of a finger.<br>**Atomic service API**: This API can be used in atomic services since API version 11.                     |
71| 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.|
72| 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.|
73| 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.  |
74| 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.  |
75| 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.  |
76| 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.  |
77| 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.  |
78| 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.  |
79
80## HistoricalPoint<sup>10+</sup>
81
82**Atomic service API**: This API can be used in atomic services since API version 11.
83
84**System capability**: SystemCapability.ArkUI.ArkUI.Full
85
86| Name        | Type                                | Description                                                                        |
87| ----------- | ----------------------------------- | ----------------------------------------------------------------------------- |
88| touchObject | [TouchObject](#touchobject)  | Basic information of the historical point.                                                  |
89| size        | number                              | Size of the contact area between the finger and screen for the historical point.<br>Default value: **0**                                    |
90| 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.|
91| 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          |
92## Example
93
94```ts
95// xxx.ets
96@Entry
97@Component
98struct TouchExample {
99  @State text: string = ''
100  @State eventType: string = ''
101
102  build() {
103    Column() {
104      Button('Touch').height(40).width(100)
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      Button('Touch').height(50).width(200).margin(20)
123        .onTouch((event?: TouchEvent) => {
124          if(event){
125            if (event.type === TouchType.Down) {
126              this.eventType = 'Down'
127            }
128            if (event.type === TouchType.Up) {
129              this.eventType = 'Up'
130            }
131            if (event.type === TouchType.Move) {
132              this.eventType = 'Move'
133            }
134            this.text = 'TouchType:' + this.eventType + '\nDistance between touch point and touch element:\nx: '
135            + event.touches[0].x + '\n' + 'y: ' + event.touches[0].y + '\nComponent globalPos:('
136            + event.target.area.globalPosition.x + ',' + event.target.area.globalPosition.y + ')\nwidth:'
137            + event.target.area.width + '\nheight:' + event.target.area.height
138          }
139        })
140      Text(this.text)
141    }.width('100%').padding(30)
142  }
143}
144```
145
146![en-us_image_0000001209874754](figures/en-us_image_0000001209874754.gif)
147