1e41f4b71Sopenharmony_ci# Touchscreen Event
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ciTouchscreen events are events triggered when a finger or stylus is placed on, moved along, or lifted from a component. They can be classified as [click event](#click-event), [drag event](arkts-common-events-drag-event.md), or [touch event](#touch-event).
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci**Figure 1** Touchscreen event principles
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci![en-us_image_0000001562700461](figures/en-us_image_0000001562700461.png)
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci## Click Event
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ciA click event is triggered when a complete press and lift action is performed by using a finger or a stylus. When a click event occurs, the following callback is triggered:
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci```ts
20e41f4b71Sopenharmony_cionClick(event: (event?: ClickEvent) => void)
21e41f4b71Sopenharmony_ci```
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ciThe **event** parameter provides the coordinates of the click relative to the window or component as well as the event source where the click occurs, for example, a button, a click on which shows or hides an image.
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci```ts
27e41f4b71Sopenharmony_ci@Entry
28e41f4b71Sopenharmony_ci@Component
29e41f4b71Sopenharmony_cistruct IfElseTransition {
30e41f4b71Sopenharmony_ci  @State flag: boolean = true;
31e41f4b71Sopenharmony_ci  @State btnMsg: string = 'show';
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci  build() {
34e41f4b71Sopenharmony_ci    Column() {
35e41f4b71Sopenharmony_ci      Button(this.btnMsg).width(80).height(30).margin(30)
36e41f4b71Sopenharmony_ci        .onClick(() => {
37e41f4b71Sopenharmony_ci          if (this.flag) {
38e41f4b71Sopenharmony_ci            this.btnMsg = 'hide';
39e41f4b71Sopenharmony_ci          } else {
40e41f4b71Sopenharmony_ci            this.btnMsg = 'show';
41e41f4b71Sopenharmony_ci          }
42e41f4b71Sopenharmony_ci          // Click the button to show or hide the image.
43e41f4b71Sopenharmony_ci          this.flag = !this.flag;
44e41f4b71Sopenharmony_ci        })
45e41f4b71Sopenharmony_ci      if (this.flag) {
46e41f4b71Sopenharmony_ci        Image($r('app.media.icon')).width(200).height(200)
47e41f4b71Sopenharmony_ci      }
48e41f4b71Sopenharmony_ci    }.height('100%').width('100%')
49e41f4b71Sopenharmony_ci  }
50e41f4b71Sopenharmony_ci}
51e41f4b71Sopenharmony_ci```
52e41f4b71Sopenharmony_ci**Figure 2** Showing or hiding an image through the click event of a button
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci![ClickEventControl.gif](figures/ClickEventControl.gif)
56e41f4b71Sopenharmony_ci
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ci
59e41f4b71Sopenharmony_ci## Touch Event
60e41f4b71Sopenharmony_ci
61e41f4b71Sopenharmony_ciA touch event is triggered when a finger or stylus is placed on, moved along, or lifted from a component.
62e41f4b71Sopenharmony_ci
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci```ts
65e41f4b71Sopenharmony_cionTouch(event: (event?: TouchEvent) => void)
66e41f4b71Sopenharmony_ci```
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ci- If **event.type** is **TouchType.Down**, the finger or stylus is placed on the component.
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ci- If **event.type** is **TouchType.Up**, the finger or stylus is lifted from the component.
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci- If **event.type** is **TouchType.Move**, the finger or stylus is moved along the component.
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ci- If **event.type** is **TouchType.Cancel**, the current finger operation is interrupted and canceled.
75e41f4b71Sopenharmony_ci
76e41f4b71Sopenharmony_ciThe touch event supports single and multi-touch interactions. Information about the touch event can be obtained using the **event** parameter, such as the location of the finger that triggers the event, unique identifier of the finger, finger information changed, and the input device source.
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ci```ts
80e41f4b71Sopenharmony_ci// xxx.ets
81e41f4b71Sopenharmony_ci@Entry
82e41f4b71Sopenharmony_ci@Component
83e41f4b71Sopenharmony_cistruct TouchExample {
84e41f4b71Sopenharmony_ci  @State text: string = '';
85e41f4b71Sopenharmony_ci  @State eventType: string = '';
86e41f4b71Sopenharmony_ci
87e41f4b71Sopenharmony_ci  build() {
88e41f4b71Sopenharmony_ci    Column() {
89e41f4b71Sopenharmony_ci      Button('Touch').height(40).width(100)
90e41f4b71Sopenharmony_ci        .onTouch((event?: TouchEvent) => {
91e41f4b71Sopenharmony_ci          if(event){
92e41f4b71Sopenharmony_ci            if (event.type === TouchType.Down) {
93e41f4b71Sopenharmony_ci              this.eventType = 'Down';
94e41f4b71Sopenharmony_ci            }
95e41f4b71Sopenharmony_ci            if (event.type === TouchType.Up) {
96e41f4b71Sopenharmony_ci              this.eventType = 'Up';
97e41f4b71Sopenharmony_ci            }
98e41f4b71Sopenharmony_ci            if (event.type === TouchType.Move) {
99e41f4b71Sopenharmony_ci              this.eventType = 'Move';
100e41f4b71Sopenharmony_ci            }
101e41f4b71Sopenharmony_ci            this.text = 'TouchType:' + this.eventType + '\nDistance between touch point and touch element:\nx: '
102e41f4b71Sopenharmony_ci            + event.touches[0].x + '\n' + 'y: ' + event.touches[0].y + '\nComponent globalPos:('
103e41f4b71Sopenharmony_ci            + event.target.area.globalPosition.x + ',' + event.target.area.globalPosition.y + ')\nwidth:'
104e41f4b71Sopenharmony_ci            + event.target.area.width + '\nheight:' + event.target.area.height
105e41f4b71Sopenharmony_ci          }
106e41f4b71Sopenharmony_ci        })
107e41f4b71Sopenharmony_ci      Button('Touch').height(50).width(200).margin(20)
108e41f4b71Sopenharmony_ci        .onTouch((event?: TouchEvent) => {
109e41f4b71Sopenharmony_ci          if(event){
110e41f4b71Sopenharmony_ci            if (event.type === TouchType.Down) {
111e41f4b71Sopenharmony_ci              this.eventType = 'Down';
112e41f4b71Sopenharmony_ci            }
113e41f4b71Sopenharmony_ci            if (event.type === TouchType.Up) {
114e41f4b71Sopenharmony_ci              this.eventType = 'Up';
115e41f4b71Sopenharmony_ci            }
116e41f4b71Sopenharmony_ci            if (event.type === TouchType.Move) {
117e41f4b71Sopenharmony_ci              this.eventType = 'Move';
118e41f4b71Sopenharmony_ci            }
119e41f4b71Sopenharmony_ci            this.text = 'TouchType:' + this.eventType + '\nDistance between touch point and touch element:\nx: '
120e41f4b71Sopenharmony_ci            + event.touches[0].x + '\n' + 'y: ' + event.touches[0].y + '\nComponent globalPos:('
121e41f4b71Sopenharmony_ci            + event.target.area.globalPosition.x + ',' + event.target.area.globalPosition.y + ')\nwidth:'
122e41f4b71Sopenharmony_ci            + event.target.area.width + '\nheight:' + event.target.area.height
123e41f4b71Sopenharmony_ci          }
124e41f4b71Sopenharmony_ci        })
125e41f4b71Sopenharmony_ci      Text(this.text)
126e41f4b71Sopenharmony_ci    }.width('100%').padding(30)
127e41f4b71Sopenharmony_ci  }
128e41f4b71Sopenharmony_ci}
129e41f4b71Sopenharmony_ci```
130e41f4b71Sopenharmony_ci
131e41f4b71Sopenharmony_ci
132e41f4b71Sopenharmony_ci![en-us_image_0000001511900468](figures/en-us_image_0000001511900468.gif)
133