#tauri-plugin #tauri-app #global #applications #shortcut #hotkeys #listener

sys tauri-plugin-global-shortcut

在您的 Tauri 应用程序上注册全局快捷键监听器

19 个版本

2.0.0-rc.1 2024 年 8 月 17 日
2.0.0-beta.92024 年 7 月 31 日
2.0.0-beta.32024 年 3 月 21 日
2.0.0-alpha.62023 年 12 月 20 日
2.0.0-alpha.02023 年 5 月 24 日

#1036GUI

Download history 325/week @ 2024-04-27 274/week @ 2024-05-04 329/week @ 2024-05-11 173/week @ 2024-05-18 830/week @ 2024-05-25 713/week @ 2024-06-01 206/week @ 2024-06-08 285/week @ 2024-06-15 233/week @ 2024-06-22 440/week @ 2024-06-29 223/week @ 2024-07-06 318/week @ 2024-07-13 339/week @ 2024-07-20 578/week @ 2024-07-27 746/week @ 2024-08-03 374/week @ 2024-08-10

2,073 每月下载
用于 qwit-desktop

Apache-2.0 OR MIT

83KB
408 代码行

plugin-global-shortcut

注册全局快捷键。

  • 支持的平台:Windows、Linux 和 macOS。

安装

此插件需要至少 1.75 版本的 Rust

我们推荐三种安装方法。

  1. 使用 crates.io 和 npm(最简单,需要您信任我们的发布管道)
  2. 直接从 Github 使用 git 标签/版本哈希拉取源代码(最安全)
  3. 使用 Git 子模块在此 tauri 项目中安装此存储库,然后使用文件协议导入源代码(最安全,但使用不便)

通过将以下内容添加到您的 Cargo.toml 文件中安装核心插件

src-tauri/Cargo.toml

# you can add the dependencies on the `[dependencies]` section if you do not target mobile
[target."cfg(not(any(target_os = \"android\", target_os = \"ios\")))".dependencies]
tauri-plugin-global-shortcut = "2.0.0-rc"
# alternatively with Git:
tauri-plugin-global-shortcut = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" }

您可以使用您首选的 JavaScript 包管理器安装 JavaScript 客户端绑定

注意:由于大多数 JavaScript 包管理器无法从 git monorepos 安装包,我们为每个插件提供了只读镜像。这使得安装选项 2 更易于使用。

pnpm add @tauri-apps/plugin-global-shortcut
# or
npm add @tauri-apps/plugin-global-shortcut
# or
yarn add @tauri-apps/plugin-global-shortcut

# alternatively with Git:
pnpm add https://github.com/tauri-apps/tauri-plugin-global-shortcut#v2
# or
npm add https://github.com/tauri-apps/tauri-plugin-global-shortcut#v2
# or
yarn add https://github.com/tauri-apps/tauri-plugin-global-shortcut#v2

用法

首先,您需要将核心插件注册到 Tauri 中

src-tauri/src/main.rs

fn main() {
    tauri::Builder::default()
        .setup(|app| {
            #[cfg(desktop)]
            {
                use tauri::Manager;
                use tauri_plugin_global_shortcut::{Code, Modifiers, ShortcutState};

                app.handle().plugin(
                    tauri_plugin_global_shortcut::Builder::new()
                        .with_shortcuts(["ctrl+d", "alt+space"])?
                        .with_handler(|app, shortcut, event| {
                            if event.state == ShortcutState::Pressed  {
                                if shortcut.matches(Modifiers::CONTROL, Code::KeyD) {
                                    let _ = app.emit("shortcut-event", "Ctrl+D triggered");
                                }
                                if shortcut.matches(Modifiers::ALT, Code::Space) {
                                    let _ = app.emit("shortcut-event", "Alt+Space triggered");
                                }
                            }
                        })
                        .build(),
                )?;
            }

            Ok(())
        })
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

之后,所有插件 API 都可通过 JavaScript 绑定访问

import { register } from "@tauri-apps/plugin-global-shortcut";
await register("CommandOrControl+Shift+C", (event) => {
  if (event.state === "Pressed") {
    console.log("Shortcut triggered");
  }
});

贡献

接受 PR。在提交拉取请求之前,请务必阅读贡献指南。

合作伙伴

CrabNebula

有关赞助商的完整列表,请访问我们的 网站Open Collective

许可证

代码:© 2015 - 现在 - The Commons Conservancy 的 Tauri 程序。

适用时为 MIT 或 MIT/Apache 2.0。

依赖关系

~17–57MB
~886K SLoC