Programming a virtual keyboard for JCEF

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

Programming a virtual keyboard for JCEF

Postby alex9849 » Thu Apr 21, 2022 10:22 am

Hi

first, I really like CEF and JCEF it is exactly what I searched for.
I want to use JCEF for an application that is controlled by a touchscreen.
I want to include a virtual keyboard that can be used instead of the non existing physical keyboard.

I want that the keyboard is only displayed if it is needed. That means I need some way to listen for focus and unfocus events on input fields. Is this somehow possible. I couldn't find a way.
Another thing is that I want to send keyboard inputs from the virtual keyboard to the browser so I can write letters into the currently selected input field. I tried to do that testwise with a single button that sends a single letter to the browser. The problem is that everytime I click on the button the input field in the bowser loses focus. I used `setFocusable(false);` for the button, but that didn't help.

This is my code so far:
Code: Select all
getContentPane().add(browerUI_, BorderLayout.CENTER);

        Button button = new Button();
        button.setLabel("Write letter");
        button.setFocusable(false);

        button.addActionListener((e) -> {
            /*
            KeyEvent keyEvent = new KeyEvent(button, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_D, 'd');
            KeyboardFocusManager.getCurrentKeyboardFocusManager().dispatchKeyEvent(keyEvent);
             */
            try {
                Robot robot = new Robot();
                robot.keyPress(KeyEvent.VK_D);
                robot.keyRelease(KeyEvent.VK_D);
            } catch (AWTException ex) {
                ex.printStackTrace();
            }
        });
        getContentPane().add(button, BorderLayout.SOUTH);
        pack();
        setSize(800, 600);
        getContentPane().requestFocusInWindow();


Thanks for your help :)
alex9849
Newbie
 
Posts: 9
Joined: Thu Apr 21, 2022 9:45 am

Re: Programming a virtual keyboard for JCEF

Postby FriwiDev » Sat Apr 23, 2022 7:02 am

You could try to stop cef from receiving focus updates by removing/modifying this focus listener [0]. Then you can use the native sendkey/mouse/... methods to enter your desired inputs [1].

[0] https://bitbucket.org/chromiumembedded/ ... #lines-261
[1] https://bitbucket.org/chromiumembedded/ ... #lines-598
Maintainer of jcefmaven on GitHub.
FriwiDev
Techie
 
Posts: 41
Joined: Sun Jul 09, 2017 4:18 am

Re: Programming a virtual keyboard for JCEF

Postby alex9849 » Sun Apr 24, 2022 2:31 am

Sadly this didn't work for me. I removed all FocusListeners. If I click on the button outside of the browser component the focus gets lost. Any ideas what could be the problem?
I noticed that the focuslistener doesn't get executed anyway. Even if I don't remove it. The listener only gets executed if I minimize/maximize the whole window.
alex9849
Newbie
 
Posts: 9
Joined: Thu Apr 21, 2022 9:45 am

Re: Programming a virtual keyboard for JCEF

Postby alex9849 » Sun Apr 24, 2022 3:11 am

Ok I found out the following:
I can re-select the text field within the browser by using the setFocus-Method from CefBrowserWr.
But this doesn't prevent the browser from loosing the focus in first place. Is there a solution for that?
Last edited by alex9849 on Sun Apr 24, 2022 3:53 am, edited 1 time in total.
alex9849
Newbie
 
Posts: 9
Joined: Thu Apr 21, 2022 9:45 am

Re: Programming a virtual keyboard for JCEF

Postby FriwiDev » Sun Apr 24, 2022 3:41 am

If there is, it would need to be a normal swing approach. Sadly, I do not know swing enough to really help with this problem.

Edit: You could try playing with InputVerifiers: https://docs.oracle.com/javase/7/docs/a ... ifier.html
Maintainer of jcefmaven on GitHub.
FriwiDev
Techie
 
Posts: 41
Joined: Sun Jul 09, 2017 4:18 am

Re: Programming a virtual keyboard for JCEF

Postby alex9849 » Sun Apr 24, 2022 4:11 am

I'm not sure if it is possible to prevent the component from loosing focus in general. But my problem would also be solved if I could prevent JCEF from removing the courser from the input field if the focus gets lost.
alex9849
Newbie
 
Posts: 9
Joined: Thu Apr 21, 2022 9:45 am

Re: Programming a virtual keyboard for JCEF

Postby FriwiDev » Sun Apr 24, 2022 4:14 am

You should be able to mitigate the focus loosing by using an inputverifier, as specified in the docs :)
Maintainer of jcefmaven on GitHub.
FriwiDev
Techie
 
Posts: 41
Joined: Sun Jul 09, 2017 4:18 am

Re: Programming a virtual keyboard for JCEF

Postby alex9849 » Sun Apr 24, 2022 4:18 am

The problem is that the CefBrowser and CefBrowser-component don't inherit the JComponent-class and therefore don't have an inputverifier method. :cry:
alex9849
Newbie
 
Posts: 9
Joined: Thu Apr 21, 2022 9:45 am

Re: Programming a virtual keyboard for JCEF

Postby FriwiDev » Sun Apr 24, 2022 4:36 am

But they do provide their JComponent: https://bitbucket.org/chromiumembedded/ ... #lines-309
Maintainer of jcefmaven on GitHub.
FriwiDev
Techie
 
Posts: 41
Joined: Sun Jul 09, 2017 4:18 am

Re: Programming a virtual keyboard for JCEF

Postby alex9849 » Sun Apr 24, 2022 5:03 am

Yes but that Component is just a "Component" and not a "JComponent". The component-Object doesn't provide the InputVerifier functionality. :cry:
alex9849
Newbie
 
Posts: 9
Joined: Thu Apr 21, 2022 9:45 am

Next

Return to JCEF Forum

Who is online

Users browsing this forum: Google [Bot] and 34 guests