Focus Issue

Having problems with building or using the JCEF Java binding? Ask your questions here.

Focus Issue

Postby redpillar » Wed Jan 08, 2020 8:34 am

grettings, it's redpillar here.
i would like to ask your help in order to fix a strange behaviour about focusing.

when the browser load a webpage, all my Swing/AWT components will lose focus.
in particular, main menu's shortcuts (JMenuBar) won't work anymore (ie: CTRL+F4 -> quit the app).

I already read some topics related to this specific problem with no success.
indeed, i added the following code in _cefclient.addLoadHandler() => onLoadingStateChange()

Code: Select all
KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwner();
_jframe.requestFocus();


this trick works sometimes only. in my personal opinion this is why:
when the user clicks inside the webview to scroll the webpage, the focus will still stand attached to the webview itself.

that said, you might want to trigger when the user stop interacting with the webview in order to redirect the focus somewhere else.
unfortunatelly i dunno how to catch/implement this type of event/s.

since i tried to figure something out by myself, i wrote this code:

Code: Select all
Thread t = new Thread(){
   @Override
   public void run(){
      while( true ){
         try{
            String obj = "" + (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner().toString());
            System.out.println( "focus:" + obj );
            Thread.sleep(1000);
         }catch(Exception idk){
            //TODO
         }
      }
   }
};
t.start();


that gives me an important clue: the webview/browserUI doesn't seem to get any focus when the user interact with it. why??? i mean, the component (browserUI) is effectively there and i can attach any Listener to it with no effect. at this point i'm seriously confused.

just to be clear, i don't ask you to code for me.
i would appreciate some suggestions or criticisms (maybe i'm doing something wrong).
obviously if you could also provide some examples, it would be awesome.

thanks ahead time for your help and availability.
-redpillar
Last edited by redpillar on Wed Jan 08, 2020 9:54 am, edited 1 time in total.
redpillar
Newbie
 
Posts: 6
Joined: Wed Jan 08, 2020 7:28 am

Re: Focus Issue

Postby magreenblatt » Wed Jan 08, 2020 8:59 am

What OS? Have you tried implementing CefKeyboardHandler callbacks?
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: Focus Issue

Postby Czarek » Wed Jan 08, 2020 9:18 am

On Linux there are issues with keyboard focus reported in Issue 2026. A patch is available.
Maintainer of the CEF Python, PHP Desktop and CEF C API projects. My LinkedIn.
User avatar
Czarek
Virtuoso
 
Posts: 1927
Joined: Sun Nov 06, 2011 2:12 am

Re: Focus Issue

Postby redpillar » Wed Jan 08, 2020 10:22 am

thanks for your dealing.

magreenblatt wrote:What OS? [...]


my computer is still running Windows 7 64Bit, and Java Version installed is 1.8.0_231 (JDK).

currently my app uses the following JCEF version downloaded from here:
https://mvnrepository.com/artifact/com.github.doraig/jcef/78.1-win64

in order to make things easy, i moved the "lib" folder outside the JAR, by setting a custom java.library.path.
that's my setup.

magreenblatt wrote:[...] Have you tried implementing CefKeyboardHandler callbacks?


no, i didn't try to implement CefKeyboardHandler yet. i am looking at KeyboardHandler.java example.
so far that's what i tried:

Code: Select all
import JFrame parent;
CefClient client;
//
client.addKeyboardHandler(new CefKeyboardHandlerAdapter() {
   @Override
   public boolean onKeyEvent(CefBrowser thisBrowserInstance, CefKeyEvent event) {
      thisBrowserInstance.setFocus(false);
      KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwner();
      parent.requestFocus();
      return false;
   }
});


it seems to work (just tested), but i wonder if i am achieving my objective properly. i don't want to copy-and-paste portion of codes only, i would like to understand why/how this work now. at this point, should i avoid KeyboardFocusManager usage?

i would be grateful if you could lead me more further. thanks.
redpillar
Newbie
 
Posts: 6
Joined: Wed Jan 08, 2020 7:28 am

Re: Focus Issue

Postby magreenblatt » Wed Jan 08, 2020 10:37 am

The underlying problem is that light-weight (Swing) and heavy-weight (AWT/browser) components don't play nice together. There are also OS considerations (how native event queues are implemented) which make this more complicated. Ideally, if the AWT/browser has focus you should get key events via the CefKeyboardHandler callbacks. You could then inject synthetic events into the Java event queue, but that logic is not implemented (and I'm not sure how useful it would be in practice).
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: Focus Issue

Postby redpillar » Wed Jan 08, 2020 11:28 am

magreenblatt wrote:The underlying problem is that light-weight (Swing) and heavy-weight (AWT/browser) components don't play nice together. There are also OS considerations (how native event queues are implemented) which make this more complicated. Ideally, if the AWT/browser has focus you should get key events via the CefKeyboardHandler callbacks. You could then inject synthetic events into the Java event queue, but that logic is not implemented (and I'm not sure how useful it would be in practice).


indeed, as you suggested i used CefKeyboardHandler callbacks. and it works, now.

could a pure app-awt-based help to run the application smoothly with no needed to trigger CefKeyboardHandler callbacks?
what would you suggest to me to do? just asking.

again, thanks for your time.
redpillar
Newbie
 
Posts: 6
Joined: Wed Jan 08, 2020 7:28 am

Re: Focus Issue

Postby magreenblatt » Wed Jan 08, 2020 1:14 pm

I think using pure AWT might work better, but that would be very 1990’s :) You would still need CefKeyboardHandler but other focus and activation-related issues might disappear.
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: Focus Issue

Postby redpillar » Thu Jan 09, 2020 5:24 am

@magreenblatt

thanks for everything. i will update my app in order to run/use AWT components only.
redpillar
Newbie
 
Posts: 6
Joined: Wed Jan 08, 2020 7:28 am

Re: Focus Issue

Postby redpillar » Fri Jan 10, 2020 2:07 pm

@magreenblatt

i recoded my app in order to use only AWT components.
the only exception was/is a JSlider just because i couldn't find a proper alternative.

( my app overlays other windows app. so i needed to take control of java.awt.Frame translucency. at the beginning i wanted to use a MouseWheelListener combined with some keyboard shorcuts: maximum efficiency but poor viability for a non-experienced user, in my opinion ).

i have been testing such "new" app on Windows 10 and it's been impossible for me to understand the cause of some memory leaks. on Windows 7 the app run perfectly. however on WIndows 10 the same app tends to slow down the entire enviroment and RAM usage significantly increases. -- i see, it's difficult to come to a conclusion with no code. i can just confirm that that strange behaviour happens (on Windows 10) when running "your" [tests\detailed\MainFrame.java].

just for instance, why could you see two "jcef_helper.exe" processes running at the same time (TaksManager,Windows)?
surely i am ignoring some important information, and maybe this is why i cannot optimize the app for Windows 10 yet. dunno, just saying.

as always, thanks ahead time.
redpillar
Newbie
 
Posts: 6
Joined: Wed Jan 08, 2020 7:28 am

Re: Focus Issue

Postby redpillar » Sun Jan 12, 2020 6:05 am

i just compiled the lastest version of JCEF. the slow-thing about Windows 10 seems to be gone.

thanks anyway. bbye!
redpillar
Newbie
 
Posts: 6
Joined: Wed Jan 08, 2020 7:28 am


Return to JCEF Forum

Who is online

Users browsing this forum: No registered users and 14 guests