From 942e6cb237f4d1c890da783efa16787af3a5edc1 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Sat, 21 Nov 2026 19:57:55 +0800 Subject: [PATCH] fix(windows): stop WebView2 browser shortcuts from taking unhandled IDE keys (#5007) A key the keymap leaves alone reached WebView2's browser accelerators: Ctrl+F in a view without its own find opened the page's Find bar over the whole window, and F3, F12 or Alt+Left ran browser features. Turn the accelerators off for every webview; the page still gets every key. --- src-tauri/src/lib.rs | 4 ++++ src-tauri/src/windows_webview_keys.rs | 32 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src-tauri/src/windows_webview_keys.rs diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 59047f673..2845b39c0 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -44,6 +44,8 @@ mod windows; #[cfg(windows)] mod windows_error_mode; #[cfg(windows)] +mod windows_webview_keys; +#[cfg(windows)] mod windows_webview_recovery; use parking_lot::Mutex; @@ -140,6 +142,8 @@ pub fn run() { .plugin(tauri_plugin_notification::init()) .plugin(tauri_plugin_dialog::init()); #[cfg(windows)] + let app = app.plugin(windows_webview_keys::init()); + #[cfg(windows)] let app = app.plugin(windows_webview_recovery::init()); #[cfg(target_os = "linux")] let app = app.plugin(linux_window_state::plugin()); diff --git a/src-tauri/src/windows_webview_keys.rs b/src-tauri/src/windows_webview_keys.rs new file mode 100644 index 000000000..c55bd4c50 --- /dev/null +++ b/src-tauri/src/windows_webview_keys.rs @@ -0,0 +1,32 @@ +//! WebView2's browser accelerator keys. A key the IDE keymap leaves alone otherwise reaches +//! WebView2, which runs its browser feature: Ctrl+F in a view without its own find opens the +//! page's Find bar over the whole window, F3 finds the next match there, Ctrl+P prints, F12 opens +//! DevTools. Turning them off leaves every key to the page; editing keys stay with the webview. + +use tauri::{Webview, Wry, plugin}; +use webview2_com::Microsoft::Web::WebView2::Win32::ICoreWebView2Settings3; +use windows::core::Interface; + +pub fn init() -> plugin::TauriPlugin { + plugin::Builder::new("webview-keys").on_webview_ready(disable_browser_keys).build() +} + +fn disable_browser_keys(webview: Webview) { + let label = webview.label().to_owned(); + if let Err(error) = webview.with_webview(move |platform| { + // SAFETY: with_webview runs on the controller's owning STA thread. + let result = unsafe { + platform + .controller() + .CoreWebView2() + .and_then(|core| core.Settings()) + .and_then(|settings| settings.cast::()) + .and_then(|settings| settings.SetAreBrowserAcceleratorKeysEnabled(false)) + }; + if let Err(error) = result { + tracing::warn!(window = %label, %error, "cannot turn off WebView2 browser keys"); + } + }) { + tracing::warn!(window = webview.label(), %error, "cannot access WebView2 controller"); + } +}