Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there any way for WebView2 to detect the KeyDown the same as the MainWindow? #536

Closed
dorofino opened this issue Oct 14, 2020 · 4 comments

Comments

@dorofino
Copy link

Hi,

I have a function on the MainWindow that makes the window Maximized when I press the Keyword "T".
Everything works fine until I click with my mouse or set focus on the WebView2, then it stopped working altogether.

Is there any way for WebView2 to detect the KeyDown the same as the MainWindow?

        bool _isFullScreen = false;
        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.Key == Key.T) && _isFullScreen == false)
            {
                WindowStyle = WindowStyle.None;
                WindowState = WindowState.Maximized;
                ResizeMode = ResizeMode.NoResize;
                Topmost = true;
                _isFullScreen = true;
            }
            else
            {
                WindowStyle = WindowStyle.SingleBorderWindow;
                WindowState = WindowState.Normal;
                ResizeMode = ResizeMode.NoResize;
                Topmost = false;
                _isFullScreen = false;
            }
        }

@DjSt3rios
Copy link

You could use the power of Javascript, although it might not be reliable at all times (e.g when a page is still loading). You can use the function AddScriptToExecuteOnDocumentCreatedAsync to inject the code to listen for "keydown" and then listen for this message using the WebMessageReceived event.

Try this:
in "Loaded" event:
Browser.CoreWebView2Ready += (_, args) => { Browser.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync("document.body.addEventListener(\"keydown\",event => { if (event.isComposing || event.keyCode === 229) return; if(event.code === \"KeyT\") window.chrome.webview.postMessage(\"PressedT\"); });"); Browser.CoreWebView2.WebMessageReceived += (_, args) => { if (args.WebMessageAsJson.Contains("PressedT")) MaximizeFunction(); }; };

and create a function called "MaximizeFunction", or simply rename it, with your maximize/restore code you already have. I can't try the code at the moment as there is a problem with Edge Canary. There are more reliable ways like a keyboard hook, but then you'd also need to check if edge is focused, and if you have multiple windows you'd need to check the handles for each window, it gets rather complicated.

@G33kDude
Copy link

See also #112 and #468

@champnic
Copy link
Member

As others have mentioned, please try the javascript workaround for now. We are tracking better keyboard support as @G33kDude mentioned, so I'm going to close this issue. Please reply if those issues don't cover your scenario, the workaround isn't working for you, or you'd like me to reopen for another reason. Thanks!

@BenSeymourODB
Copy link

Noting for others that end up here via search: as of version 1.0.1264.42, the WPF version of WebView2 has support for Key events (via methods like OnKeyUp, OnKeyDown, etc.), but the WinForms version does not. The JavaScript workaround from above is still the way to go for WinForms.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants