触觉反馈
Android 和 iOS 上的触觉反馈和振动。
Android 上的振动支持没有标准/要求,因此反馈 API 在更实惠的手机(包括最近发布的手机)上可能无法正常工作。
This plugin requires a Rust version of at least 1.77.2
| Platform | Level | Notes |
|---|---|---|
| windows | | |
| linux | | |
| macos | | |
| android | ||
| ios |
安装触觉反馈插件以开始使用。
使用项目的包管理器添加依赖项:
npm run tauri add hapticsyarn run tauri add hapticspnpm tauri add hapticsdeno task tauri add hapticsbun tauri add hapticscargo tauri add haptics-
在
src-tauri文件夹中运行以下命令,将插件添加到Cargo.toml中的项目依赖项中:cargo add tauri-plugin-haptics --target 'cfg(any(target_os = "android", target_os = "ios"))' -
修改
lib.rs以初始化插件:src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().setup(|app| {#[cfg(mobile)]app.handle().plugin(tauri_plugin_haptics::init());Ok(())}).run(tauri::generate_context!()).expect("error while running tauri application");} -
使用你喜欢的 JavaScript 包管理器安装 JavaScript Guest 绑定:
npm install @tauri-apps/plugin-hapticsyarn add @tauri-apps/plugin-hapticspnpm add @tauri-apps/plugin-hapticsdeno add npm:@tauri-apps/plugin-hapticsbun add @tauri-apps/plugin-haptics
触觉反馈插件在 JavaScript 中可用。
import { vibrate, impactFeedback, notificationFeedback, selectionFeedback,} from '@tauri-apps/plugin-haptics';
await vibrate(1);await impactFeedback('medium');await notificationFeedback('warning');await selectionFeedback();默认情况下,所有潜在危险的插件命令和范围都被阻止,无法访问。你必须修改 capabilities 配置中的权限以启用这些功能。
有关更多信息,请参阅 功能概述 和 使用插件权限的步骤指南。
{ "$schema": "../gen/schemas/mobile-schema.json", "identifier": "mobile-capability", "windows": ["main"], "platforms": ["iOS", "android"], "permissions": [ "haptics:allow-impact-feedback", "haptics:allow-notification-feedback", "haptics:allow-selection-feedback", "haptics:allow-vibrate" ]}Permission Table
| Identifier | Description |
|---|---|
|
|
Enables the impact_feedback command without any pre-configured scope. |
|
|
Denies the impact_feedback command without any pre-configured scope. |
|
|
Enables the notification_feedback command without any pre-configured scope. |
|
|
Denies the notification_feedback command without any pre-configured scope. |
|
|
Enables the selection_feedback command without any pre-configured scope. |
|
|
Denies the selection_feedback command without any pre-configured scope. |
|
|
Enables the vibrate command without any pre-configured scope. |
|
|
Denies the vibrate command without any pre-configured scope. |
© 2025 Tauri Contributors. CC-BY / MIT