You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tauri窗口获焦时无法覆盖Windows键的问题排查

问题解答:Tauri窗口获焦时Windows键钩子失效

我想要安装一个Windows钩子来覆盖Windows键,编写了如下代码,但当Tauri应用窗口处于焦点状态时无法正常工作,其他场景均生效。请问可能的原因是什么?是否是Tauri窗口本身捕获了Windows键?

// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use mki::{bind_key, Action, InhibitEvent, Keyboard};

fn main() {

  bind_key(Keyboard::LeftWindows, Action {
    callback: Box::new(|_event, _state| {
      println!("Left Windows key pressed");
    }),
    inhibit: InhibitEvent::Yes,
    sequencer: false,
    defer: false,
  });

  bind_key(Keyboard::RightWindows, Action {
    callback: Box::new(|_event, _state| {
      println!("Right Windows key pressed");
    }),
    inhibit: InhibitEvent::Yes,
    sequencer: false,
    defer: false,
  });


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

原因分析

是的,Tauri窗口在获得焦点时会优先捕获Windows键事件,导致你的全局钩子无法正常触发,核心原因有两点:

  1. WebView2默认拦截:Tauri在Windows平台默认使用WebView2作为渲染引擎,WebView2会自动拦截并处理部分系统级键盘事件(包括单独的Windows键),防止网页误触发系统操作。
  2. Windows消息传递机制:当窗口处于焦点状态时,键盘事件会优先发送到该窗口的消息队列,Tauri的窗口消息循环会先处理这个事件,若事件被消费就不会继续传递到你的全局钩子。

解决办法

方法1:禁用WebView2的系统快捷键拦截

通过Tauri的WebView2控制器修改设置,关闭系统快捷键拦截:

tauri::Builder::default()
  .setup(|app| {
    let window = app.get_window("main").unwrap();
    #[cfg(windows)]
    {
      use tauri::WindowExt;
      let webview_controller = window.webview().controller()?;
      let settings = webview_controller.settings()?;
      // 禁用WebView2的系统菜单和快捷键处理
      settings.set_is_system_menu_enabled(false)?;
    }
    Ok(())
  })
  .run(tauri::generate_context!())
  .expect("error while running tauri application");

方法2:在Tauri窗口内直接监听键盘事件

绕过全局钩子,直接在窗口事件循环中捕获Windows键:

tauri::Builder::default()
  .on_window_event(|event| {
    if let tauri::WindowEvent::KeyboardInput { input, .. } = event {
      #[cfg(windows)]
      {
        use tauri::keyboard::Key;
        if input.key == Key::MetaLeft || input.key == Key::MetaRight {
          if input.state == tauri::keyboard::KeyState::Pressed {
            println!("Windows key pressed in Tauri window");
            // 阻止事件传递给系统
            event.prevent_default();
          }
        }
      }
    }
  })
  .run(tauri::generate_context!())
  .expect("error while running tauri application");

方法3:升级全局钩子优先级

如果坚持使用全局钩子,可改用Windows低级键盘钩子(LowLevelKeyboardProc),这类钩子会比窗口级钩子更早捕获事件,避免被Tauri窗口拦截。不过这需要直接调用Windows原生API,替代mki库的现有实现。


内容的提问来源于stack exchange,提问作者Advait

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 23:52:44