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.
This commit is contained in:
l0ng-ai
2026-11-21 19:57:55 +08:00
committed by GitHub
parent 674cdd3925
commit 942e6cb237
2 changed files with 36 additions and 0 deletions
+4
View File
@@ -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());
+32
View File
@@ -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<Wry> {
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::<ICoreWebView2Settings3>())
.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");
}
}