WebView2 shortcuts not working when focus is inside WebView2 #5661
Replies: 1 comment 3 replies
|
Handle the shortcut at the WebView2 controller level, not only with WinUI When focus is inside the web content, WebView2 owns that input path first. The WebView2 API surface for this is The pattern is: webView.CoreWebView2InitializationCompleted += (s, e) =>
{
if (!e.IsSuccess)
return;
webView.CoreWebView2.Settings.AreBrowserAcceleratorKeysEnabled = false;
webView.CoreWebView2Controller.AcceleratorKeyPressed += (_, args) =>
{
if (args.KeyEventKind != CoreWebView2KeyEventKind.KeyDown &&
args.KeyEventKind != CoreWebView2KeyEventKind.SystemKeyDown)
return;
// Check your real shortcut/modifier combination here. In WinUI apps,
// modifier state is usually read with InputKeyboardSource.GetKeyStateForCurrentThread(...).
if (IsMyAppShortcut(args.VirtualKey))
{
args.Handled = true;
DispatcherQueue.TryEnqueue(() => RunMyAppShortcut(args.VirtualKey));
}
};
};Two details matter:
So the practical answer is: keep your normal WinUI accelerators for native focus, and mirror the same command routing from If this solves the WebView focus case, please mark it as answered so others know to use the WebView2 controller accelerator event rather than only WinUI keyboard APIs. |
Uh oh!
There was an error while loading. Please reload this page.
When the focus is inside a WebView2 control, WinUI's keyboard accelerator/hotkey APIs stop working. The shortcut messages appear to be swallowed by WebView2 and never reach the application.
How can I make shortcut keys work globally, regardless of whether the focus is on WinUI or inside WebView2, without having the key events consumed by WebView2?
All reactions