fix: hotkey status not accurately processed, resulting in two triggers

This commit is contained in:
huzibaca 2024-09-17 10:59:39 +08:00
parent 10cd0a1f30
commit f31fe1440d

9
src-tauri/src/core/hotkey.rs Normal file → Executable file
View File

@ -4,8 +4,7 @@ use once_cell::sync::OnceCell;
use parking_lot::Mutex;
use std::{collections::HashMap, sync::Arc};
use tauri::AppHandle;
use tauri_plugin_global_shortcut::GlobalShortcutExt;
use tauri_plugin_global_shortcut::{GlobalShortcutExt, ShortcutState};
pub struct Hotkey {
current: Arc<Mutex<Vec<String>>>, // 保存当前的热键设置
app_handle: Arc<Mutex<Option<AppHandle>>>,
@ -69,7 +68,11 @@ impl Hotkey {
_ => bail!("invalid function \"{func}\""),
};
let _ = manager.on_shortcut(hotkey, move |_, _, _| f());
let _ = manager.on_shortcut(hotkey, move |_app_handle, _hotkey, event| {
if event.state == ShortcutState::Pressed {
f()
}
});
log::info!(target: "app", "register hotkey {hotkey} {func}");
Ok(())
}