MicrosoftEdge / WebView2Feedback

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

Add support for sending keyboard input to WebView2 - SendKeyEvent #3671

Open 53b4 opened 1 year ago

53b4 commented 1 year ago

Hi, From some reasons inside CoreWebView2.AddHostObjectToScript(....) method I need to catch pressed keys and programmatically send its to webview2. Previously, in Cefsharp I used : ChromiumWebBrowser.GetHost().SendKeyEvent() .
Do we have some equivalent in webview2 ? I tried to use JS script, but here is problem because Monaco Editor for some subpages is used.

Please help Regards,

AB#45786265

novac42 commented 1 year ago

Hi @53b4, thanks for reaching out and right now we don't have an equivalent API for this, but we can take it as a feature request. Would you like to shed more light on why you need this - for example, what kind of app is this, and what use cases you are handling here to catch pressed keys?

53b4 commented 1 year ago

Hi, thank you for response. We have Webview2 as embed control in some desktop application. When focus is on this window we have to capture some keys combinations that already used as a shortkeys in native application using custom IKeyboardHandler and send it to displayed page.

novac42 commented 1 year ago

Thanks, I've added it to our feature backlog. Is this feature critical for your app? Any workaround available yet?

53b4 commented 1 year ago

Thanks; we are working to found out workaround; let you know results

53b4 commented 1 year ago

Sorry, I missed let you know. It is not critical.

darbid commented 1 year ago

Sending keys to a page is something I'm missing too. Some input elements require "real events" to recognise the text value. In JavaScript the document.execommand method currently works but that was deprecated years ago. Other JavaScript events and event sequences to emulate key down and up are not reliable to work all the time.

novac42 commented 1 year ago

@darbid Thanks for the input. May you give some examples in your app's scenarios where input elements require real events to recognize the text value?

darbid commented 1 year ago

This is not my app but an example of the input element.

https://research.wolterskluwer-online.de/document/55f290ba-966d-3e48-b8c6-69b6b63b890a

image

Do note if you inspect that example you can give the input element a value but it will not be recognised.

ajtruckle commented 5 months ago

@victorhuangwq @champnic

I have the same issue and it seems a basic one really. We can tell WebView2 to listen for hotkeys to give us a chance to handle them, since we use them on our parent window, and when the browser has focus it would eat it up or handle it itself. So this direction is catered for with API.

But parent window to WebView2. Simple example:

So we need a simple, uncomplicated way, when we use our Accelerator map and OnHotKey event, that we can just pass it, eg CTRL + U over to WebView2.

I really don't want to mess with JavaScript and other event handlers for what sounds like a simple SendKeyInput call. For me this adds a lot of confusion for users, even me, because wonder why it is not working sometimes.

It is why it was a godsend to have the API implemented for us to decide to let WebView2 handle hotkeys or do custom action.

I would really like to see this implemented.

ajtruckle commented 5 months ago

https://github.com/MicrosoftEdge/WebView2Feedback/issues/4483