快捷键(globalShortcut)
注册全局快捷键。
当 tauri.conf.json 中的 build.withGlobalTauri 设置为 true 时,也可以通过 window.__TAURI__.globalShortcut 访问此包。
API 必须添加到 tauri.conf.json 中的 tauri.allowlist.globalShortcut:
{
"tauri": {
"allowlist": {
"globalShortcut": {
"all": true // enable all global shortcut APIs
}
}
}
}
建议仅将使用的 API 加入允许列表,以优化捆绑包大小和安全性。
Type Aliases
ShortcutHandler
-
ShortcutHandler: (
shortcut:string) ⇒void
类型声明
-
(
shortcut:string):void
参数
名称 |
类型 |
|
|
返回: void
定义在: globalShortcut.ts:29
Functions
isRegistered
-
isRegistered(
shortcut:string):Promise<boolean>
判断此应用程序是否注册了给定的快捷键。
示例
import { isRegistered } from '@tauri-apps/api/globalShortcut';
const isRegistered = await isRegistered('CommandOrControl+P');
Since: 1.0.0
参数
名称 |
类型 |
描述 |
|
|
快捷键定义数组,修饰符和键用“+”分隔,例如 CmdOrControl+Q |
返回: Promise<boolean>
register
-
register(
shortcut:string,handler:ShortcutHandler):Promise<void>
注册一个全局快捷键。
示例
import { register } from '@tauri-apps/api/globalShortcut';
await register('CommandOrControl+Shift+C', () => {
console.log('Shortcut triggered');
});
Since: 1.0.0
参数
名称 |
类型 |
描述 |
|
|
快捷键定义,修饰符和键用“+”分隔,例如 CmdOrControl+Q |
|
快捷键处理回调函数 - 以触发的快捷键为参数 |
返回: Promise<void>
registerAll
-
registerAll(
shortcuts:string[],handler:ShortcutHandler):Promise<void>
注册一组全局快捷键。
示例
import { registerAll } from '@tauri-apps/api/globalShortcut';
await registerAll(['CommandOrControl+Shift+C', 'Ctrl+Alt+F12'], (shortcut) => {
console.log(`Shortcut ${shortcut} triggered`);
});
Since: 1.0.0
参数
名称 |
类型 |
描述 |
|
|
快捷键定义数组,修饰符和键用“+”分隔,例如 CmdOrControl+Q |
|
快捷键处理回调函数 - 以触发的快捷键为参数 |
返回: Promise<void>