-
Notifications
You must be signed in to change notification settings - Fork 55
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
WebView2 WinForms control in Excel VSTO Task pane steals and holds on to keyboard focus #951
Comments
Thanks for finding this issue @ShaunLoganOracle - we have some other bugs around VSTO Task Panes and I think they probably share a common root cause. I've created a bug on our backlog and we'll take a look. |
I have that same behaviour with a project using Custom Task Panes from ExcelDNA.
FYI, a demo project to reproduce can be found there: https://github.com/alaincao/ExcelDNASamples/tree/webview2_issue_933/CustomTaskPane |
@champnic |
@champnic |
Hey @ShaunLoganOracle - Can you try this potential workaround? Before initializing your WebView2, call: If that resolves the issue, then we are currently working on a better solution which should fix this automatically. That workaround also won't work on Win 7/8/8.1. |
Thanks. My add-in supports Win 10 & 11 only so I'll try this shortly and report back. |
I tried this by setting a System Environment variable (as spec'd above) in the Settings app and then running Excel and trying my use case. The stolen keyboard focus issue seems be resolved! |
Observation: by setting this environment variable programmatically in my add-in (running in the Excel process), any other usages of WebView2 in that process will presumably have their behavior changed too. For example if there was another add-in using WebView2, it would be affected. |
@champnic |
@ShaunLoganOracle Thanks for trying it out. The fact that it fixes the original issue does suggest that the other work we are doing will fix this when it's completed. It's unfortunate the side effects mean you can't use the workaround yet though, sorry about that. |
@champnic I have some questions around the environment variable that I hope you can help me with.
|
Hey @sbolofsson!
|
Ok, many thanks for the clarifications and details @champnic. For us, setting this environment variable solves a focus/mouse related issue in a VSTO task pane. So please don't remove this "feature" 😊 We understand that it can be hard to make this hosting mode the default, due to possibly breaking existing compat. However, looking at the release notes history and also currently open issues for WebView2, it's quite clear that there have been (and still are) quite a number of issues related to focus and/or VSTO. Now, I obviously don't know if this environment variable solves all (probably not) - and it may even introduce new issues, as also mentioned in this issue. Therefore, I think it would very much make sense to make this officially supported (when it's ready), and then opt-in instead of default. I think that even the limited OS support could be fine for us. 2018 is somewhat old and VSTO is Windows only anyway. |
Hello @champnic , we also added this workaround to address a mouse issue that appeared when running our add-in in PowerPoint. Is there any alternative workaround we can pursue? |
@champnic @ShaunLoganOracle @alaincao Is there anything else we could do with the issue? |
@champnic |
Hey @ShaunLoganOracle - I believe @zhuhaichao518 has been working on a change that's in experimental that can alleviate some of this issue. @zhuhaichao518 can you share the latest on your investigations using the hit-test transparent mode here, and the remaining issues? |
We are still having this issue. |
Hi @ShaunLoganOracle @andrewkittredge does the solution mentioned in #1254 work for you? Meanwhile @zhuhaichao518 is working on adding an API into ControllerOptions, you can check the detailed spec here: #3596 |
@novac42 I observe now that clicking with the mouse in the HTML rendered in the WV2 control sends the mouse click to the browser (eg. HTML element gets onClick), but leaves keyboard focus where it was (in my case, the worksheet grid). If I explicitly give focus to the WV2 via the keyboard using a combination of F6 and Tab, then subsequent keystrokes are seen by the WV2 (and HTML). I also quickly tried other cases where we host a WebView2 control in a modal WinForms Dialog: focus and keyboard access seem to behave as expected. We will have our accessibility experts review the changes and report back if this approach introduces any issues in that domain. |
There is one non-intuitive behavior: clicking the mouse in the HTML content rendered in the WV2 control does not give keyboard focus to that control. The keyboard focus stays in the worksheet grid. Previously, clicking in the HTML gave keyboard focus to the WV2 (which then did not give back the focus when clicking back on the grid). |
@novac42 @zhuhaichao518 |
@ShaunLoganOracle Thanks for the feedback. We will take a look into it. |
same scenario, same problems. |
Is there any update? This is a showstopper bug, preventing any use in Microsoft Office applications like Excel. First there was the failure to render issue, dating back to May 2020 (#187), where the successful workaround is to set the user data folder to a writeable location. So far so good. This current issue (#951) dates to Feb 2021. The workaround above, unfortunately, shifts the focus from the WebView2 100% to the Excel worksheet 100%. Is there a setting or another workaround that allows for focus to shift smoothly between the Excel sheet and WebView2 in a task pane? Thanks a lot in advance. |
I found a solution. In order to return keyboard focus back to Excel after a WebView2 control steals keyboard focus, you can use the Windows API to re-set the foreground window back to Excel. Here is how you do it: Here it is in VB.NetAdd this module to your code:
When you want to return focus back to Excel, just call the method
Here it is in C#
I have tested this to work with WebView2 version 1.0.2592.51 in a VSTO Excel add in where the WebView2 is embedded in a Custom Task Pane Note: You might not actually need to focus the desktop hWnd before focusing Excel. This is just overkill to ensure it works. I ran into a situation where it was necessary, so I thought I'd include it. |
Description
I have a Winforms WebView2 hosted in an Excel VSTO Task pane, There is no
System.Windows.Forms.Form
hosting the WebView2 control, instead, aSystem.Windows.Forms.UserControl
is the owner/host of the control. After giving the WebView2 focus, it holds on to it after I've clicked back into the Excel worksheet.Version
SDK: 1.0.705.50
Runtime: Evergreen RT 88.0.705.68
Framework: WinForms
OS: Win10 1909
Repro Steps
Observed: selected cell is changed as expected, however keystrokes are still getting captured by WebView2 (ie. Enter toggles the HTML widget instead of moving the cell selection).
AB#36775741
The text was updated successfully, but these errors were encountered: