MicrosoftEdge / WebView2Feedback

Feedback and discussions about Microsoft Edge WebView2
https://aka.ms/webview2
454 stars 55 forks source link

WebView2_PreviewKeyDown not work, WebView2_KeyDown some keys #1196

Open michalh2020 opened 3 years ago

michalh2020 commented 3 years ago

Description

WebView2_PreviewKeyDown not work. WebView2_KeyDown only keys: ControlKey/Apps/Menu/Capital/Escape/F1-F12/Home/End/Next/PageUp/Delete/Insert/Right/Left/Up/Down

Version SDK: 1.0.774.44 or 1.0.824-prerelease Runtime: 89.0.774.77 or 90.0.818.41 Framework: 4.6.1 or 4.8 OS: Win10 Pro [Version 10.0.18363.1440]

Repro Steps

    private void webView2_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
    {
        //NOT WORK
        MessageBox.Show("previewkeydown:\n" + e.Alt + " " + e.Control + " " + e.KeyData, sender.ToString());
    }
    private void webView2r_KeyDown(object sender, KeyEventArgs e)
    {
       //ONLY ControlKey/Apps/Menu/Capital/Escape/F1-F12/Home/End/Next/PageUp/Delete/Insert/Right/Left/Up/Down
        MessageBox.Show("keydown:\n "+e.Alt + " " + e.Control + " " + e.KeyData, sender.ToString());
    }

Screenshots

Additional context

AB#32770120

champnic commented 3 years ago

Hey @michalh2020 - thanks for the feedback. Are you looking for all keypresses to be handled by the Preview/KeyDown events? If so I believe that request is tracked in #112.

michalh2020 commented 3 years ago

@champnic Thank you for your response. I need to capture some keys using PreviewKeyPress in WebView2 (eg Shift + D) and pass them to the MainWindow (these are shortcut keys and work when focus is not in WebView2).

For example, in Delphi 10.3, PreKeyEvent was running with dCEF4-Chromium and there I was able to capture keys and cancel Chromium.

I dream of the same in Visual Studio 2019 + WebView2 :)

Or another similar use in WebView2, I have two WebView2s and I want to pass a Uri to another WebView2 from the html menu:

_private void webView2LeftNavigationStarting (object sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationStartingEventArgs e) { if (LeftLinkToCentrer == true) { webView2Center.Source = new Uri (e.Uri); e.Cancel = true; } }

I thought PreviewKewDown would do the same as NavigationStarting - it will be able to interrupt and forward the button action like NavigationStarting cancel opening Uri.

champnic commented 3 years ago

I believe you should be able to achieve something similar with the KeyDown event. If you mark the event as handled (e.Handled = true;) that should stop the WebView2 from handling that shortcut itself. Can you give that a try? Thanks!

michalh2020 commented 3 years ago

@champnic Unfortunately, KeyDown from WebView2 does not transfer all keys, for example from another System.Windows.Forms.TextBox control, Shift + D works, Microsoft.Web.WebView2.WinForms.WebView2 Shift + D does not. Only some keys in WebView2, eg Shift + Ctrl, but not everything like TextBox.

    _private void webView2Center_KeyPress(object sender, KeyPressEventArgs e)
    {
        Debug.Write("\n webView2Center_KeyPress " + e.KeyChar.ToString() + " " + sender.ToString());
    }

    private void webView2Center_KeyUp(object sender, KeyEventArgs e)
    {
        Debug.Write("\n webView2Center_KeyUp "+e.Alt + " " + e.Control + " " + e.KeyData + " "+ sender.ToString());
    }

    private void textBoxwww_KeyPress(object sender, KeyPressEventArgs e)
    {
        Debug.Write("\n textBoxwww_KeyPress " + e.KeyChar + " "+ sender.ToString());
    }

    private void textBoxwww_KeyUp(object sender, KeyEventArgs e)
    {
        Debug.Write("\n textBoxwww_KeyUp " + e.Alt + " " + e.Control + " " + e.KeyData +" " + sender.ToString());
    }_
champnic commented 3 years ago

Thanks for the added info - I've added this bug to our backlog.