1e41f4b71Sopenharmony_ci# Universal Events
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci>  **NOTE**
4e41f4b71Sopenharmony_ci>
5e41f4b71Sopenharmony_ci>  Universal events are supported since API version 4. Updates will be marked with a superscript to indicate their earliest API version.
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci## Event Description
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci- Events are bound to components. When a component meets the event triggering condition, the corresponding event callback in the JS is executed to implement the interaction between the UI and the JS logic layer of the page.
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci- The event callback can carry additional information through parameters, such as the dataset on the component and event-specific callback parameters.
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ciDifferent from private events, universal events can be bound to most components.
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci| Name                    | Parameter      | Description                                                        | Bubbling Supported                                         | Capturing Supported   |
18e41f4b71Sopenharmony_ci| ------------------------ | ---------- | ------------------------------------------------------------ | ----------------------------------------------------- | --------------- |
19e41f4b71Sopenharmony_ci| touchstart               | TouchEvent | Triggered when the tapping starts. For details about **TouchEvent**, see Table 2.      | Yes<sup>5+</sup>                                      | Yes<sup>5+</sup>|
20e41f4b71Sopenharmony_ci| touchmove                | TouchEvent | Triggered when the tapping moves.                            | Yes<sup>5+</sup>                                      | Yes<sup>5+</sup>|
21e41f4b71Sopenharmony_ci| touchcancel              | TouchEvent | Triggered when the tapping is interrupted.                      | Yes<sup>5+</sup>                                      | Yes<sup>5+</sup>|
22e41f4b71Sopenharmony_ci| touchend                 | TouchEvent | Triggered when the tapping ends.                          | Yes<sup>5+</sup>                                      | Yes<sup>5+</sup>|
23e41f4b71Sopenharmony_ci| click                    | BaseEvent  | Triggered when a component is clicked.                                        | Yes<sup>6+</sup>                                      | No             |
24e41f4b71Sopenharmony_ci| doubleclick<sup>7+</sup> | BaseEvent  | Triggered when a component is double-clicked.                                          | No<br> Bubbling is supported since API version 9.| No             |
25e41f4b71Sopenharmony_ci| longpress                | BaseEvent  | Triggered when a component is long pressed.                                        | No<br>Bubbling is supported since API version 9. | No             |
26e41f4b71Sopenharmony_ci| swipe<sup>5+</sup>       | SwipeEvent | Triggered when a user quickly swipes on a component. For details about **SwipeEvent**, see Table 4.     | No<br>Bubbling is supported since API version 9. | No             |
27e41f4b71Sopenharmony_ci| attached<sup>6+</sup>    | -          | Triggered after the current component node is mounted to the render tree.                            | No                                                   | No             |
28e41f4b71Sopenharmony_ci| detached<sup>6+</sup>    | -          | Triggered when the current component node is removed from the render tree.                          | No                                                   | No             |
29e41f4b71Sopenharmony_ci| pinchstart<sup>7+</sup>  | PinchEvent | Triggered when a pinch operation is started.<br>For details about **PinchEvent**, see Table 5.| No                                                   | No             |
30e41f4b71Sopenharmony_ci| pinchupdate<sup>7+</sup> | PinchEvent | Triggered when a pinch operation is in progress.                          | No                                                   | No             |
31e41f4b71Sopenharmony_ci| pinchend<sup>7+</sup>    | PinchEvent | Triggered when a pinch operation is ended.                      | No                                                   | No             |
32e41f4b71Sopenharmony_ci| pinchcancel<sup>7+</sup> | PinchEvent | Triggered when a pinch operation is interrupted.                            | No                                                   | No             |
33e41f4b71Sopenharmony_ci| dragstart<sup>7+</sup>   | DragEvent  | Triggered when dragging starts.<br>For details about **DragEvent**, see Table 6.     | No                                                   | No             |
34e41f4b71Sopenharmony_ci| drag<sup>7+</sup>        | DragEvent  | Triggered when dragging is in progress.                                      | No                                                   | No             |
35e41f4b71Sopenharmony_ci| dragend<sup>7+</sup>     | DragEvent  | Triggered when dragging is ended.                                        | No                                                   | No             |
36e41f4b71Sopenharmony_ci| dragenter<sup>7+</sup>   | DragEvent  | Triggered when the dragged component enters a drop target.                                  | No                                                   | No             |
37e41f4b71Sopenharmony_ci| dragover<sup>7+</sup>    | DragEvent  | Triggered when the dragged component is being dragged over a drop target.                                    | No                                                   | No             |
38e41f4b71Sopenharmony_ci| dragleave<sup>7+</sup>   | DragEvent  | Triggered when the dragged component leaves a drop target.                                    | No                                                   | No             |
39e41f4b71Sopenharmony_ci| drop<sup>7+</sup>        | DragEvent  | Triggered when the dragged component is dropped on a drop target.                              | No                                                   | No             |
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ci>  **NOTE**
43e41f4b71Sopenharmony_ci>
44e41f4b71Sopenharmony_ci>  Events not listed in the preceding table do not support bubbling, such as the [change event](js-components-basic-input.md#events) of the **<input\>** component. For details, see the description of the specific component.
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci**Table 1** BaseEvent
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci| Attribute                   | Type                  | Description                          |
49e41f4b71Sopenharmony_ci| --------------------- | -------------------- | --------------------------- |
50e41f4b71Sopenharmony_ci| type                  | string               | Event type, such as **click** and **longpress**.|
51e41f4b71Sopenharmony_ci| timestamp             | number               | Timestamp when the event is triggered.                |
52e41f4b71Sopenharmony_ci| deviceId<sup>6+</sup> | number               | ID of the device that triggers the event.              |
53e41f4b71Sopenharmony_ci| target<sup>6+</sup>   | [Target](#target6)| Target object that triggers the event.                |
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci**Table 2** TouchEvent (inherited from BaseEvent)
56e41f4b71Sopenharmony_ci
57e41f4b71Sopenharmony_ci| Attribute            | Type                    | Description                                       |
58e41f4b71Sopenharmony_ci| -------------- | ---------------------- | ---------------------------------------- |
59e41f4b71Sopenharmony_ci| touches        | Array&lt;TouchInfo&gt; | Attribute set of the touch event, including the information array of the touch points on the screen.                |
60e41f4b71Sopenharmony_ci| changedTouches | Array&lt;TouchInfo&gt; | Attribute set when a touch event occurs, including the information array of changed touch points on the screen. **changedTouches** has the same data format as **touches** and indicates touch point changes, including changes in the number and location of touch points. For example, when the user's finger leaves the screen, which means that the number of touch points changes from 1 to 0, **changedTouches** has the relevant data generated, but not **touches**.|
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ci**Table 3** TouchInfo
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci| Attribute                | Type    | Description                            |
65e41f4b71Sopenharmony_ci| ------------------ | ------ | ------------------------------ |
66e41f4b71Sopenharmony_ci| globalX            | number | Horizontal distance from the upper left corner of the screen (excluding the status bar), which acts as the origin of coordinates.|
67e41f4b71Sopenharmony_ci| globalY            | number | Vertical distance from the upper left corner of the screen (excluding the status bar), which acts as the origin of coordinates.|
68e41f4b71Sopenharmony_ci| localX             | number | Horizontal distance from the upper left corner of the touched component, which acts as the origin of coordinates.     |
69e41f4b71Sopenharmony_ci| localY             | number | Vertical distance from the upper left corner of the touched component, which acts as the origin of coordinates.     |
70e41f4b71Sopenharmony_ci| size               | number | Touch area.                       |
71e41f4b71Sopenharmony_ci| force<sup>6+</sup> | number | Touch force.                        |
72e41f4b71Sopenharmony_ci
73e41f4b71Sopenharmony_ci**Table 4** SwipeEvent (inherited from BaseEvent)
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ci| Attribute                   | Type    | Description                                      |
76e41f4b71Sopenharmony_ci| --------------------- | ------ | ---------------------------------------- |
77e41f4b71Sopenharmony_ci| direction             | string | Swiping direction. The value can be one of the following:<br>- **left**: Swipe left.<br>- **right**: Swipe right.<br>- **up**: Swipe up.<br>- **down**: Swipe down.|
78e41f4b71Sopenharmony_ci| distance<sup>6+</sup> | number | Swiping distance in the swiping direction.                            |
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ci**Table 5** PinchEvent<sup>7+</sup>
81e41f4b71Sopenharmony_ci
82e41f4b71Sopenharmony_ci| Attribute          | Type    | Description            |
83e41f4b71Sopenharmony_ci| ------------ | ------ | -------------- |
84e41f4b71Sopenharmony_ci| scale        | number | Scale factor.          |
85e41f4b71Sopenharmony_ci| pinchCenterX | number | X-coordinate of the pinch center, in px.|
86e41f4b71Sopenharmony_ci| pinchCenterY | number | Y-coordinate of the pinch center, in px.|
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ci**Table 6** DragEvent<sup>7+</sup> (inherited from BaseEvent)
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ci| Attribute                       | Type                              | Description               |
91e41f4b71Sopenharmony_ci| ------------------------- | -------------------------------- | ---------------- |
92e41f4b71Sopenharmony_ci| type                      | string                           | Event name.           |
93e41f4b71Sopenharmony_ci| globalX                   | number                           | Horizontal distance from the upper left corner of the screen, which acts as the origin of coordinates.|
94e41f4b71Sopenharmony_ci| globalY                   | number                           | Vertical distance from the upper left corner of the screen, which acts as the origin of coordinates.|
95e41f4b71Sopenharmony_ci| timestamp                 | number                           | Timestamp.            |
96e41f4b71Sopenharmony_ci| dataTransfer<sup>9+</sup> | [DataTransfer](#datatransfer9)| Object for data transfer.         |
97e41f4b71Sopenharmony_ci
98e41f4b71Sopenharmony_ci## Target<sup>6+</sup>
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ciWhen a component triggers an event, the event callback receives an event object by default. You can obtain the corresponding information through the event object.
101e41f4b71Sopenharmony_ci
102e41f4b71Sopenharmony_ci
103e41f4b71Sopenharmony_ci
104e41f4b71Sopenharmony_ci| Attribute                  | Type    | Description                                      |
105e41f4b71Sopenharmony_ci| -------------------- | ------ | ---------------------------------------- |
106e41f4b71Sopenharmony_ci| dataSet<sup>6+</sup> | Object | Custom attribute set defined through [data-*](js-components-common-attributes.md).|
107e41f4b71Sopenharmony_ci
108e41f4b71Sopenharmony_ci**Example**
109e41f4b71Sopenharmony_ci
110e41f4b71Sopenharmony_ci```html
111e41f4b71Sopenharmony_ci<!-- xxx.hml -->
112e41f4b71Sopenharmony_ci<div>
113e41f4b71Sopenharmony_ci  <div data-a="dataA" data-b="dataB" 
114e41f4b71Sopenharmony_ci    style="width: 100%; height: 50%; background-color: saddlebrown;"@touchstart='touchstartfunc'></div>
115e41f4b71Sopenharmony_ci</div>
116e41f4b71Sopenharmony_ci```
117e41f4b71Sopenharmony_ci
118e41f4b71Sopenharmony_ci```js
119e41f4b71Sopenharmony_ci// xxx.js
120e41f4b71Sopenharmony_ciexport default {
121e41f4b71Sopenharmony_ci  touchstartfunc(msg) {
122e41f4b71Sopenharmony_ci    console.info(`on touch start, point is: ${msg.touches[0].globalX}`);
123e41f4b71Sopenharmony_ci    console.info(`on touch start, data is: ${msg.target.dataSet.a}`);
124e41f4b71Sopenharmony_ci  }
125e41f4b71Sopenharmony_ci}
126e41f4b71Sopenharmony_ci```
127e41f4b71Sopenharmony_ci
128e41f4b71Sopenharmony_ci## DataTransfer<sup>9+</sup>
129e41f4b71Sopenharmony_ci
130e41f4b71Sopenharmony_ciUse **dataTransfer** to transfer data during the drag operation, so you can perform operations on the data when the drag operation is complete.
131e41f4b71Sopenharmony_ci
132e41f4b71Sopenharmony_ci### setData<sup>9+</sup>
133e41f4b71Sopenharmony_ci
134e41f4b71Sopenharmony_cisetData(key: string, value: object): boolean
135e41f4b71Sopenharmony_ci
136e41f4b71Sopenharmony_ciSets the data associated with the specified key. If there is no data associated with the key, the data will be appended. If there is already data associated with the key, the data will replace the existing data in the same position.
137e41f4b71Sopenharmony_ci
138e41f4b71Sopenharmony_ci**Parameters**
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_ci| Name  | Type  | Mandatory  | Description     |
141e41f4b71Sopenharmony_ci| ----- | ------ | ---- | ------- |
142e41f4b71Sopenharmony_ci| key   | string | Yes   | Data key.  |
143e41f4b71Sopenharmony_ci| value | object | Yes   | Data to be stored.|
144e41f4b71Sopenharmony_ci
145e41f4b71Sopenharmony_ci**Return value**
146e41f4b71Sopenharmony_ci
147e41f4b71Sopenharmony_ci| Type     | Description                       |
148e41f4b71Sopenharmony_ci| ------- | ------------------------ |
149e41f4b71Sopenharmony_ci| boolean | Operation result. The value **true** means that the operation is successful, and **false** means the opposite.|
150e41f4b71Sopenharmony_ci
151e41f4b71Sopenharmony_ci**Example**
152e41f4b71Sopenharmony_ci
153e41f4b71Sopenharmony_ci```js
154e41f4b71Sopenharmony_ci// value in setData can be of a primitive type.
155e41f4b71Sopenharmony_cidragStart(e) {
156e41f4b71Sopenharmony_ci	var isSetOk = e.dataTransfer.setData('name', 1);
157e41f4b71Sopenharmony_ci},
158e41f4b71Sopenharmony_ci// value in setData can be of the object type.
159e41f4b71Sopenharmony_cidragStart(e) {
160e41f4b71Sopenharmony_ci	var person = new Object();
161e41f4b71Sopenharmony_ci	person.name = "tom";
162e41f4b71Sopenharmony_ci	person.age = 21;
163e41f4b71Sopenharmony_ci	var isSetOk = e.dataTransfer.setData('person', person);
164e41f4b71Sopenharmony_ci}
165e41f4b71Sopenharmony_ci```
166e41f4b71Sopenharmony_ci### getData<sup>9+</sup>
167e41f4b71Sopenharmony_ci
168e41f4b71Sopenharmony_cigetData(key: string): object
169e41f4b71Sopenharmony_ci
170e41f4b71Sopenharmony_ciObtains the data associated with the specified key. If no data is associated with the key, an empty string will be returned.
171e41f4b71Sopenharmony_ci
172e41f4b71Sopenharmony_ci**Parameters**
173e41f4b71Sopenharmony_ci
174e41f4b71Sopenharmony_ci| Name | Type  | Mandatory  | Description   |
175e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ----- |
176e41f4b71Sopenharmony_ci| key  | string | Yes   | Data key.|
177e41f4b71Sopenharmony_ci
178e41f4b71Sopenharmony_ci**Return value**
179e41f4b71Sopenharmony_ci
180e41f4b71Sopenharmony_ci| Type    | Description     |
181e41f4b71Sopenharmony_ci| ------ | ------ |
182e41f4b71Sopenharmony_ci| object | Obtained data.|
183e41f4b71Sopenharmony_ci
184e41f4b71Sopenharmony_ci**Example**
185e41f4b71Sopenharmony_ci
186e41f4b71Sopenharmony_ci```js
187e41f4b71Sopenharmony_cidragStart(e) {
188e41f4b71Sopenharmony_ci	var person = new Object();
189e41f4b71Sopenharmony_ci	person.name = "tom";
190e41f4b71Sopenharmony_ci	person.age = 21;
191e41f4b71Sopenharmony_ci	e.dataTransfer.setData('person', person);
192e41f4b71Sopenharmony_ci},
193e41f4b71Sopenharmony_cidragEnd(e){
194e41f4b71Sopenharmony_ci	var person = e.dataTransfer.getData('person');
195e41f4b71Sopenharmony_ci},
196e41f4b71Sopenharmony_ci```
197e41f4b71Sopenharmony_ci### clearData<sup>9+</sup>
198e41f4b71Sopenharmony_ci
199e41f4b71Sopenharmony_ciclearData(key?: string): boolean
200e41f4b71Sopenharmony_ci
201e41f4b71Sopenharmony_ciDeletes data associated with the specified key. If there is no data associated with the key, this API will not have any effect.
202e41f4b71Sopenharmony_ciIf the key is null, all data will be deleted.
203e41f4b71Sopenharmony_ci
204e41f4b71Sopenharmony_ci**Parameters**
205e41f4b71Sopenharmony_ci
206e41f4b71Sopenharmony_ci| Name | Type  | Mandatory  | Description   |
207e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ----- |
208e41f4b71Sopenharmony_ci| key  | string | No   | Data key.|
209e41f4b71Sopenharmony_ci
210e41f4b71Sopenharmony_ci**Return value**
211e41f4b71Sopenharmony_ci
212e41f4b71Sopenharmony_ci| Type     | Description                       |
213e41f4b71Sopenharmony_ci| ------- | ------------------------ |
214e41f4b71Sopenharmony_ci| boolean | Operation result. The value **true** means that the operation is successful, and **false** means the opposite.|
215e41f4b71Sopenharmony_ci
216e41f4b71Sopenharmony_ci**Example**
217e41f4b71Sopenharmony_ci
218e41f4b71Sopenharmony_ci```js
219e41f4b71Sopenharmony_cidragEnd(e) {
220e41f4b71Sopenharmony_ci	var isSuccess = e.dataTransfer.clearData('name');
221e41f4b71Sopenharmony_ci}
222e41f4b71Sopenharmony_ci```
223e41f4b71Sopenharmony_ci### setDragImage<sup>9+</sup>
224e41f4b71Sopenharmony_ci
225e41f4b71Sopenharmony_cisetDragImage(pixelmap: PixelMap, offsetX: number,offsetY: number): boolean
226e41f4b71Sopenharmony_ci
227e41f4b71Sopenharmony_ciSets a custom drag image.
228e41f4b71Sopenharmony_ci
229e41f4b71Sopenharmony_ci**Parameters**
230e41f4b71Sopenharmony_ci
231e41f4b71Sopenharmony_ci| Name     | Type    | Mandatory  | Description                                      |
232e41f4b71Sopenharmony_ci| -------- | -------- | ---- | ---------------------------------------- |
233e41f4b71Sopenharmony_ci| pixelmap | [PixelMap](../../apis-image-kit/js-apis-image.md#pixelmap7) | Yes   | Image transferred from the frontend.|
234e41f4b71Sopenharmony_ci| offsetX  | number   | Yes   | Horizontal offset relative to the image.                            |
235e41f4b71Sopenharmony_ci| offsetY  | number   | Yes   | Vertical offset relative to the image.                           |
236e41f4b71Sopenharmony_ci
237e41f4b71Sopenharmony_ci**Return value**
238e41f4b71Sopenharmony_ci
239e41f4b71Sopenharmony_ci| Type  | Description                       |
240e41f4b71Sopenharmony_ci| ---- | ------------------------ |
241e41f4b71Sopenharmony_ci| boolean | Operation result. The value **true** means that the operation is successful, and **false** means the opposite.|
242e41f4b71Sopenharmony_ci
243e41f4b71Sopenharmony_ci**Example**
244e41f4b71Sopenharmony_ci
245e41f4b71Sopenharmony_ci```js
246e41f4b71Sopenharmony_ciimport image from '@ohos.multimedia.image';
247e41f4b71Sopenharmony_ci
248e41f4b71Sopenharmony_ciexport default {
249e41f4b71Sopenharmony_ci    createPixelMap() {
250e41f4b71Sopenharmony_ci        let color = new ArrayBuffer(4 * 96 * 96);
251e41f4b71Sopenharmony_ci        var buffer = new Uint8Array(color);
252e41f4b71Sopenharmony_ci        for (var i = 0; i < buffer.length; i++) {
253e41f4b71Sopenharmony_ci            buffer[i] = (i + 1) % 255;
254e41f4b71Sopenharmony_ci        }
255e41f4b71Sopenharmony_ci        let opts = {
256e41f4b71Sopenharmony_ci            alphaType: 0,
257e41f4b71Sopenharmony_ci            editable: true,
258e41f4b71Sopenharmony_ci            pixelFormat: 4,
259e41f4b71Sopenharmony_ci            scaleMode: 1,
260e41f4b71Sopenharmony_ci            size: {
261e41f4b71Sopenharmony_ci                height: 96, width: 96
262e41f4b71Sopenharmony_ci            }
263e41f4b71Sopenharmony_ci        }
264e41f4b71Sopenharmony_ci        const promise = image.createPixelMap(color, opts);
265e41f4b71Sopenharmony_ci        promise.then((data) => {
266e41f4b71Sopenharmony_ci            console.error('-create pixmap has info message:' + JSON.stringify(data));
267e41f4b71Sopenharmony_ci            this.pixelMap = data;
268e41f4b71Sopenharmony_ci            this.pixelMapReader = data;
269e41f4b71Sopenharmony_ci        })
270e41f4b71Sopenharmony_ci    },
271e41f4b71Sopenharmony_ci
272e41f4b71Sopenharmony_ci    onInit() {
273e41f4b71Sopenharmony_ci        this.createPixelMap
274e41f4b71Sopenharmony_ci    },
275e41f4b71Sopenharmony_ci
276e41f4b71Sopenharmony_ci    dragStart(e) {
277e41f4b71Sopenharmony_ci        e.dataTransfer.setDragImage(this.pixelMapReader, 50, 50);
278e41f4b71Sopenharmony_ci    }
279e41f4b71Sopenharmony_ci}
280e41f4b71Sopenharmony_ci```
281