A user's mouse click or finger touch on a touch screen device is the most common form of interface interaction. In these scenarios, the Mini Program Base Library provides a set of events to describe the user's touch and click states.
List of Touch and Click Events
All touch and click events are bubbling events. Specifically, they can traverse component boundaries during bubbling and have a capture phase (for related concepts, refer to the Event System section).
Touch Events
Touch events describe the touch state of a single finger on the screen.
| Type | Trigger Condition | Minimum Version |
|---|---|---|
| touchstart | A finger touch action starts | |
| touchmove | A finger moves after touching | |
| touchcancel | A finger touch action is interrupted, e.g., by an incoming call alert or popup | |
| touchend | A finger touch action ends | |
| touchforcechange | On devices supporting 3D Touch, triggered when pressing hard | 1.9.90 |
Touch events have two array fields on the event object: touches and changedTouches. They represent the touch information of fingers currently in a touch state and the touch information of fingers whose state has changed, respectively.
Each touch information object contains the following main fields.
| Field Name | Description |
|---|---|
| identifier | Finger identifier, used to indicate which finger it corresponds to |
| clientX | Horizontal coordinate of the touch position (relative to the viewport) |
| clientY | Vertical coordinate of the touch position (relative to the viewport) |
| pageX | Horizontal coordinate of the touch position (relative to the page, accounting for page scroll position) |
| pageY | Vertical coordinate of the touch position (relative to the page, accounting for page scroll position) |
Tap Events
Tap events are used to represent a "click." They can be triggered by touch or mouse clicks.
| Type | Trigger Condition | Minimum Version |
|---|---|---|
| tap | Finger touches and leaves immediately, or single mouse click | |
| longpress | Finger touches (or mouse holds) for more than 350ms before leaving. If an event callback function is specified and this event is triggered, the tap event will not be triggered | 1.5.0 |
| longtap | Finger touches (or mouse holds) for more than 350ms before leaving (it is recommended to use the longpress event instead) |
The detail of Tap events contains the following fields.
| Field Name | Description |
|---|---|
| x | Horizontal coordinate (relative to the viewport) |
| y | Vertical coordinate (relative to the viewport) |
Pointer Events
On PC, pointer events are also triggered. You can use pointer events to implement custom features such as swipe-in and right-click actions (similar to web standard pointer events).
Example:
<view
bind:pointerdown="pointerdown"
bind:pointerup="pointerup"
bind:pointermove="pointermove"
>
Test code
</view>
Example of right-click response: 在开发者工具中预览效果
Tap State
To prevent sensitive APIs from being abused, certain APIs verify "whether the current call originates from a user tap operation" when triggered. This verification relies on the Tap State—the API will execute normally only if the call occurs within the valid window following a user tap.
If such an API is invoked without a Tap State, it will throw an error:
can only be invoked by user TAP gesture
APIs that require a Tap State:
Get Click State
On User Click
Each time the user clicks, the corresponding tap event callback function receives the click state.
Within the Callbacks of Specific APIs
In the callbacks of the following APIs, the callback function is triggered upon user click, thus possessing the click state.
| API | Description |
|---|---|
| wx.requestPayment / wx.requestOrderPayment | The obtained click state cannot be used to trigger wx.openEmbeddedMiniProgram. See Notification on Relevant Adjustments for Opening Half-Screen Mini Program Capabilities After Payment for details. |
| wx.requestAutomaticRepayment | |
| wx.showModal / wx.showActionSheet | The click state can only be obtained in the success callback, or in the complete callback after the API call succeeds. |
Validity of the Click State
The click state is only valid within the macro task containing the current event callback. The core rules are:
-
✅ Synchronous code, as well as microtasks that remain within the same macro task, are valid.
// <button bind:tap="handleTap">tap</button> handleTap: async () => { wx.openSetting({}); // ✅ Valid (synchronous call, within the macro task of the tap event callback) Promise.resolve().then(() => { wx.openSetting({}); // ✅ Valid (microtask, still within the current macro task) }); await Promise.resolve(); // Awaiting a resolved value does not create a new macro task wx.openSetting({}); // ✅ Valid (still within the same macro task) }; -
❌ Once a new macro task is entered, the click state becomes invalid. Typical scenarios include: callbacks for
setTimeout/setInterval, and code execution afterawaiting a truly asynchronous taskawaitcompletes. In this case, the code runs in a different macro task and no longer possesses the click state.// <button bind:tap="handleTap">tap</button> handleTap: async () => { wx.openSetting({}); // ✅ Valid (synchronous call, within the macro task of the tap event callback) setTimeout(() => { wx.openSetting({}); // ❌ Error: can only be invoked by user TAP gesture }, 0); await new Promise((resolve) => setTimeout(resolve, 1000)); // Enters a new macro task during the wait wx.openSetting({}); // ❌ Error: can only be invoked by user TAP gesture };
Persistence of Tap State During Asynchronous Requests
Initiating asynchronous network requests within a tap event handler is a common scenario. Therefore, when APIs such as network requests are invoked while the tap state is active, the tap state is propagated to their callbacks:
// <button bind:tap="handleTap">tap</button>
handleTap: async () => {
// ✅ Tap state is active
wx.request({
url: "https://www.thissitedoesnotexist.com/step1",
complete: () => {
// ✅ Tap state remains active, propagated by request
wx.request({
url: "https://www.thissitedoesnotexist.com/step2",
complete: () => {
// ✅ Tap state remains active, propagated by request
},
});
},
});
};
The following APIs can inherit the propagated tap state: