Welcome Guest Search | Active Topics | Sign In | Register

How to set keyboard focus to a webview? Options
GrooveXT
Posted: Thursday, August 27, 2015 8:13:16 AM
Rank: Newbie
Groups: Member

Joined: 8/27/2015
Posts: 4
Hello,

I'm testing the EO.Webbrowser in wpf for our company. We use it to execute java applets with its own JVM as a custom plugin.
Now our java applet has to be quit via F12-Key before the browser is closed (to close database connection etc.).
So I hooked up to the window closing event and try to send the F12-key by program.
Code: C#
void BrowserWindow_Closing(object sender, CancelEventArgs e)
{
  SendKeys.SendWait("{F12}");
}


That works...but only if the webview is focused. When I try to close the window via taskbar or even when I first switch to another app and come back to the browser, the webview isn't focused and can't be closed. The user has to click into the webpage to able to close the program. That is not what I wanted. So I try to focus the webview beforce I send the F12-Key

Code: C#
void BrowserWindow_Closing(object sender, CancelEventArgs e)
{
  _webView.SetFocus();
  SendKeys.SendWait("{F12}");
}

But the webview isn't focused or at least not keyboard focused. Any other element, like for example the webcontrol can be focused with Keyboard.Focus(webcontrol) (and works flawless), but this isn't available to the webview either.

To cut a long story short, how do I set the keyboard focus to a webview? Or is there any other way to pass/send keypresses to a webview?

Thanks
eo_support
Posted: Thursday, August 27, 2015 1:42:18 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,071
Hi,

WebView.SetFocus is the correct method. However that method is not synchronous. So if you immediately send keyboards event to the browser, then it won't work.

Since focus is notoriously unreliable, you might want to use some other methods to unload your plug-ins. For example, you can consider exposing a JavaScript method to close the plug-in and then close the window. Or you can navigate the WebView to a different blank page first (and unload your plug-in in the process) and then close the WebView.

Thanks!
GrooveXT
Posted: Friday, August 28, 2015 3:06:35 AM
Rank: Newbie
Groups: Member

Joined: 8/27/2015
Posts: 4
Thanks for your response.

I tried to inject SetFocus via Dispatcher and different sleep and wait delays of more then 1 second. Immediately after using SetFocus() the property HasFocus() is true, but Keyboard.FocusedItem says something different. Also after several seconds the webview is not focused. I tried it with different other controls, like webcontrol, textboxes, listboxes etc. every control is focused immediately.

I'm truly not a genius about c# and may miss something but it seems to me, that SetFocus() either malfunctions or doesn't do what it says in the docs (sets keyboard focus). Maybe I'm using it the wrong way?

I also tried to use javascript to close the window...this works, but its the same effect as if I directly close the browser window...it seems that the applet doesn't listen to any close event, so I have explicitly to trigger the F12-Key before close.
eo_support
Posted: Friday, August 28, 2015 11:19:21 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,071
Hi,

Keyboard.FocusedElement will not work for this case because the WebView itself is not a WPF element (the wrapper control is, but the wrapper control is not the control that has focus).

Focus is notoriously not reliable. Sometimes after you set the focus either the code or additional user input would bring the focus to somewhere else. Sometimes Windows simply won't set the focus to the window you want to (for example, the window that currently has focus may refuse to give up focus). So you really should not rely your application logic on the focus.

You may want to rely on NPN_ReleaseObject in your plug-in to properly close your plug-in. The browser engine will call NPN_ReleaseObject in your plug-in to decrease the reference count of your objects. If the ref-count reaches zero, then it will call the deallocator you provided. You probably can clean up the resource there. Here is some information that you might find helpful:

https://developer.mozilla.org/en-US/Add-ons/Plugins/Reference/NPN_ReleaseObject

Thanks!
GrooveXT
Posted: Wednesday, September 2, 2015 7:30:08 AM
Rank: Newbie
Groups: Member

Joined: 8/27/2015
Posts: 4
Ok, let say this could work if I can edit the applet...so I have another problem.
Our users often work with two or more instances of our program and have to switch between them. As you may already know the work is all done by keyboard. So when you switch between our program instance (for example by pressing ALT+TAB), the windows is activated, but the webview isn't focused again. So you have to use the mouse to click into the window. That is very annoying and at some workstation we even don't have a mouse to do so.

May you have any advice how we can work around this problem?

Thanks for you support.
GrooveXT
Posted: Wednesday, September 2, 2015 11:00:13 AM
Rank: Newbie
Groups: Member

Joined: 8/27/2015
Posts: 4
I hooked to my Window.Activated-Event and add webview.LoadUrl. After loading, the website is in focus...regardless whether I tab in by ALT+TAB, via taskbar or by minimizing/maximizing. So there seems to be something LoadUrl does to reliable get the keyboard focus to the browser content.
What is it?
Reload doesn't have this effect nor any other navigation method. To fully load the page everytime you leave the application isn't the way to go, but maybe it will help you to help me. :)
eo_support
Posted: Monday, September 7, 2015 8:01:38 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,071
We are not aware of LoadUrl would automatically set focus. Our TabbedBrowser sample project does demonstrate this behavior --- but that's because the code in IsLoadingChanged event handler explicitly calls WebControl.Focus() when isLoading changed from true to false.


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.