全局快捷方式
注册全局快捷方式。
🌐 Register global shortcuts.
🌐 Supported Platforms
This plugin requires a Rust version of at least 1.77.2
| Platform | Level | Notes |
|---|---|---|
| windows | ||
| linux | ||
| macos | ||
| android | | |
| ios | |
🌐 Setup
安装全局快捷方式插件即可开始使用。
🌐 Install the global-shortcut plugin to get started.
使用项目的包管理器添加依赖:
npm run tauri add global-shortcutyarn run tauri add global-shortcutpnpm tauri add global-shortcutdeno task tauri add global-shortcutbun tauri add global-shortcutcargo tauri add global-shortcut-
在
src-tauri文件夹中运行以下命令,将插件添加到Cargo.toml中的项目依赖:cargo add tauri-plugin-global-shortcut --target 'cfg(any(target_os = "macos", windows, target_os = "linux"))' -
修改
lib.rs以初始化插件:src-tauri/src/lib.rs pub fn run() {tauri::Builder::default().setup(|app| {#[cfg(desktop)]app.handle().plugin(tauri_plugin_global_shortcut::Builder::new().build());Ok(())}).run(tauri::generate_context!()).expect("error while running tauri application");} -
使用你偏好的 JavaScript 包管理器安装 JavaScript Guest 绑定:
npm install @tauri-apps/plugin-global-shortcutyarn add @tauri-apps/plugin-global-shortcutpnpm add @tauri-apps/plugin-global-shortcutdeno add npm:@tauri-apps/plugin-global-shortcutbun add @tauri-apps/plugin-global-shortcut
🌐 Usage
global-shortcut 插件在 JavaScript 和 Rust 中均可用。
🌐 The global-shortcut plugin is available in both JavaScript and Rust.
import { register } from '@tauri-apps/plugin-global-shortcut';// when using `"withGlobalTauri": true`, you may use// const { register } = window.__TAURI__.globalShortcut;
await register('CommandOrControl+Shift+C', () => { console.log('Shortcut triggered');});pub fn run() { tauri::Builder::default() .setup(|app| { #[cfg(desktop)] { use tauri_plugin_global_shortcut::{Code, GlobalShortcutExt, Modifiers, Shortcut, ShortcutState};
let ctrl_n_shortcut = Shortcut::new(Some(Modifiers::CONTROL), Code::KeyN); app.handle().plugin( tauri_plugin_global_shortcut::Builder::new().with_handler(move |_app, shortcut, event| { println!("{:?}", shortcut); if shortcut == &ctrl_n_shortcut { match event.state() { ShortcutState::Pressed => { println!("Ctrl-N Pressed!"); } ShortcutState::Released => { println!("Ctrl-N Released!"); } } } }) .build(), )?;
app.global_shortcut().register(ctrl_n_shortcut)?; } Ok(()) }) .run(tauri::generate_context!()) .expect("error while running tauri application");}🌐 Permissions
默认情况下,所有潜在危险的插件命令和作用域都会被阻止,无法访问。你必须在 capabilities 配置中修改权限以启用这些功能。
🌐 By default all potentially dangerous plugin commands and scopes are blocked and cannot be accessed. You must modify the permissions in your capabilities configuration to enable these.
有关更多信息,请参见功能概览,有关使用插件权限的分步指南,请参见分步指南。
🌐 See the Capabilities Overview for more information and the step by step guide to use plugin permissions.
{ "$schema": "../gen/schemas/desktop-schema.json", "identifier": "main-capability", "description": "Capability for the main window", "windows": ["main"], "permissions": [ "global-shortcut:allow-is-registered", "global-shortcut:allow-register", "global-shortcut:allow-unregister" ]}Default Permission
No features are enabled by default, as we believe the shortcuts can be inherently dangerous and it is application specific if specific shortcuts should be registered or unregistered.
Permission Table
| Identifier | Description |
|---|---|
|
|
Enables the is_registered command without any pre-configured scope. |
|
|
Denies the is_registered command without any pre-configured scope. |
|
|
Enables the register command without any pre-configured scope. |
|
|
Denies the register command without any pre-configured scope. |
|
|
Enables the register_all command without any pre-configured scope. |
|
|
Denies the register_all command without any pre-configured scope. |
|
|
Enables the unregister command without any pre-configured scope. |
|
|
Denies the unregister command without any pre-configured scope. |
|
|
Enables the unregister_all command without any pre-configured scope. |
|
|
Denies the unregister_all command without any pre-configured scope. |
Tauri 中文网 - 粤ICP备13048890号
Nodejs.cn 旗下网站