MicrosoftEdge / WebView2Feedback

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

[WinForms] Focus and KeyDown bugs. #721

Open kirsan31 opened 3 years ago

kirsan31 commented 3 years ago

Description

WVHK

Version SDK: 1.0.664.37 Runtime: Evergreen WebView2 Runtime Framework: .Net core 3.1 OS: Win10,2004

Repro Steps

Code ```cs public partial class Form1 : Form { private string _LastText; public Form1() { InitializeComponent(); webView21.KeyDown += WebView21_KeyDown; } private void WebView21_KeyDown(object sender, KeyEventArgs e) { string info = $"Key: {e.KeyCode}; KeyData: {e.KeyData}; Control: {e.Control}.\n"; if (_LastText != info) { _LastText = info; richTextBox1.Text += info; } if (e.KeyData == (Keys.Control | Keys.G)) e.Handled = true; } private async void btnInit_Click(object sender, EventArgs e) { await webView21.EnsureCoreWebView2Async().ConfigureAwait(true); } } ```

WebView2Test.zip

Additional context Related issue (was closed): https://github.com/MicrosoftEdge/WebView2Feedback/issues/288. Issues probably related to focus bugs: https://github.com/MicrosoftEdge/WebView2Feedback/issues/397, https://github.com/MicrosoftEdge/WebView2Feedback/issues/686, https://github.com/MicrosoftEdge/WebView2Feedback/issues/718.

AB#31029009

champnic commented 3 years ago

Thanks for the detailed report!

Do you have a strong use-case for using mouse to focus the non-initialized WebView2 control? I think this would be low enough priority that we probably wouldn't fix it (unless it is a dupe of #397, which we are planning to fix).

For the second keyboarding issue, if I'm understanding correctly, when focus is only put into the WebView2 using the mouse, the keydown event you receive for Ctrl+G only comes through as 'G' (control == false). This seems like something we should fix - I've added this bug to our backlog.

kirsan31 commented 3 years ago

@champnic Thanks for you answer.

Do you have a strong use-case for using mouse to focus the non-initialized WebView2 control? I think this would be low enough priority that we probably wouldn't fix it (unless it is a dupe of #397, which we are planning to fix).

Not focusable (with mouse) control is low priority in WinForms? 😱 😱 😱 Use case is simple - hot keys that not working due to unable to focus control. We have several hot keys for WebView2. Most important:

Also about hotkeys, Here you mentioned:

We're removing exposing the AcceleratorKeyPressed event and have updated our documentation to try and make this more clear.

But in 1.0.721 release notes (December 8) I see:

Added AcceleratorKeyPressed event to support AcceleratorKey press in WebView2. (#288).

Where the truth?

wethegreenpeople commented 3 years ago

Yeah I think this is the issue I'm facing right now. We had an old IE browser control that we're trying to replace in our WinForms app. The previous control performed some actions when it lost focus, which we hooked into via the "Validated" event. Unfortunately the webview2 control never triggers that event unless we tab into the control first. At that point we can click out, tab out, whatever and the Validated event will trigger.

It also seems to only be an issue (for me) when it exists inside of a tab control/page. When I tried it out in it's own test app, the events trigger how I would expect.

Not sure of the solution yet.

champnic commented 3 years ago

@kirsan31 To clarify, I mean that using the WebView2 control in a non-initialized state is not a general use case and not a high priority for us. The vast majority of WebView2 control usage is when it is initialized. Once initialized and displaying web content, of course mouse focus and input is incredibly important 😊 If the control is not initialized yet, I would suggest hiding it until it's ready to use. It's definitely a bug that it doesn't work properly today, but we have higher priority items we will be working on first.

The addition of the AcceleratorKeyPressed event to the control in the most recent package and release notes was a mistake. It is being removed.

kirsan31 commented 2 years ago

1 year have passed, as for now 1.0.1072.54 all bugs not fixed, moreover mouse and tab focus changing now behave the same - KeyDown not working.

So we still can't manage hotkeys without reflection 😭

champnic commented 2 years ago

Hey @kirsan31 - Yes it's been a while. The good news is we are now actively working on a change that would make all input go through the UI frameworks regularly before being handled by the WebView2 processes (it currently goes to the WebView2 processes first) which should fix most of the keyboarding and mouse issues. I don't have a timeline on that yet, as we're still putting together the spec for that. Thanks!

kirsan31 commented 2 years ago

Thanks, Looking forward to it. Pls don't forget to add XButton1 and XButton2 events too ;)

gaetandezeiraud commented 2 years ago

Hey @kirsan31 - Yes it's been a while. The good news is we are now actively working on a change that would make all input go through the UI frameworks regularly before being handled by the WebView2 processes (it currently goes to the WebView2 processes first) which should fix most of the keyboarding and mouse issues. I don't have a timeline on that yet, as we're still putting together the spec for that. Thanks!

Any news on it? Can be very usefull! (Even indispensable)

champnic commented 2 years ago

We have a design doc that we are reviewing and a prototype that looks promising. No specific timeline though.

dhess83 commented 1 year ago

Sorry to bump this, but as it is still in an "Open" state I assume that's OK. I was wondering if there are any work-arounds present for these issues with FormsApps handling events with an initialized WebView2 control.

I am attempting to make a simple, for-home-only use app that displays a small web-view control on a Windows Form, but I want the application to end when Form1 loses focus. The problem is, once WebView initializes, form events such as LostFocus and KeyPress do not seem to work. I would even settle for pressing ESC to unload the form if LostFocus is not an option. It seems that the way in which WebView2 runs, it kinda steals all the power from the form to determine that events are happening.

champnic commented 1 year ago

Hey @dhess83 - Do you have a sample app that repros the issue? Where do you expect focus to be moving to that would cause LostFocus to fire? Thanks!