Global Shortcut(グローバル・ショートカット)
Plugin 説明内容の英語表記部分について Plugin の各章は、原文データからページ内容の一部が自動生成されているため、英語表記のままの部分があります。
グローバルショートカットを登録します。
This plugin requires a Rust version of at least 1.77.2
| Platform | Level | Notes |
|---|---|---|
| windows | ||
| linux | ||
| macos | ||
| android | | |
| ios | |
はじめに、「Global Shortcut グローバル・ショートカット」プラグインをインストールしてください。
自分のプロジェクトのパッケージ・マネージャーを使用して依存関係を追加します:
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
「グローバル・ショートカット global-shortcut」プラグインは JavaScript と Rust の両方で利用可能です。
import { register } from '@tauri-apps/plugin-global-shortcut';// `"withGlobalTauri": true` を使用する場合は、// 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");}デフォルトでは、潜在的に危険なプラグイン・コマンドとそのスコープ(有効範囲)はすべてブロックされており、アクセスできません。これらを有効にするには、capabilities 設定でアクセス権限を変更する必要があります。
詳細については「セキュリティ・レベル Capabilities」の章を参照してください。また、プラグインのアクセス権限を設定するには「プライグン・アクセス権の使用」の章のステップ・バイ・ステップ・ガイドを参照してください。
{ "$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. |
【※ この日本語版は、「Feb 22, 2025 英語版」に基づいています】
© 2025 Tauri Contributors. CC-BY / MIT