Page 1 of 2

Cefframe Copy / Paste not working

PostPosted: Fri Jun 11, 2021 10:42 am
by JCEFBeginner
Hello,

I have a problem with copying something programmatically.

I have the following code in my application:

Code: Select all
client.addKeyboardHandler(new CefKeyboardHandlerAdapter() {
            @Override
            public boolean onKeyEvent(CefBrowser browser, CefKeyEvent event) {
                if (event.type == CefKeyEvent.EventType.KEYEVENT_CHAR && event.modifiers == KeyEvent.BUTTON3_MASK) {
                    if (Character.toLowerCase(event.native_key_code) == 'c') {
                        browser.getFocusedFrame().copy();
                    }
                    if (Character.toLowerCase(event.native_key_code) == 'v') {
                        browser.getFocusedFrame().paste();
                    }
                    if (Character.toLowerCase(event.native_key_code) == 'x') {
                        browser.getFocusedFrame().cut();
                    }
                }
                return true;
            }
        });


I tested the code with a debugger. After the copy call, the clipboard is cleared.

Re: Cefframe Copy / Paste not working

PostPosted: Fri Jul 16, 2021 9:38 am
by JCEFBeginner
Hello?
Is no one reading this thread, or does no one have an idea?

Re: Cefframe Copy / Paste not working

PostPosted: Sat Jul 17, 2021 10:16 am
by Czarek
What if you execute javascript code to interact with clipboard? See https://developer.mozilla.org/en-US/doc ... _clipboard

Re: Cefframe Copy / Paste not working

PostPosted: Mon Jul 19, 2021 1:41 am
by Phylanx
Hi!

We had the problem that in some PDFs the context menu copy action didn't work.
We fixed it with our own ContextMenuHandler and using Java AWTs Toolkit.

Hope it helps...

Code: Select all
    class CopyContextMenuHandler
            implements CefContextMenuHandler
    {
        @Override
        public void onBeforeContextMenu(CefBrowser P_browser,
                                        CefFrame P_frame,
                                        CefContextMenuParams P_params,
                                        CefMenuModel P_model)
        {
            logger.debug("CopyContextMenuHandler.onBeforeContextMenu called!");
        }

        @Override
        public boolean onContextMenuCommand(CefBrowser P_browser,
                                            CefFrame P_frame,
                                            CefContextMenuParams P_params,
                                            int Pi_commandId,
                                            int Pi_eventFlags)
        {
            logger.debug("CopyContextMenuHandler.onContextMenuCommand called!");
            if (Pi_commandId == CefMenuModel.MenuId.MENU_ID_COPY)
            {
                logger.info("CopyContextMenuHandler copy command was overwritten!");
                Clipboard F_clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                StringSelection F_stringContent = new StringSelection(P_params.getSelectionText());
                F_clipboard.setContents(F_stringContent, F_stringContent);
                logger.debug("Handling copying text with our copy fix!");
                return true;
            }
            return false;
        }

        @Override
        public void onContextMenuDismissed(CefBrowser P_browser, CefFrame P_frame)
        {
            logger.debug("CopyContextMenuHandler.onContextMenuDismissed called!");
        }
    }

Re: Cefframe Copy / Paste not working

PostPosted: Tue Jul 20, 2021 11:12 am
by JCEFBeginner
Hello,

Thank you for your answers @Phylanx developers and @Czarek! ;)

@Phylanx, I currently disabled the Context Menu of JCEF, because of problems with it. (If I trigger the Context Menu, I can't click with the mouse anymore.)

@Czarek, It is not working I tested it with https://permission.site/ and all copy commands failed.

Re: Cefframe Copy / Paste not working

PostPosted: Tue Jul 20, 2021 12:20 pm
by Czarek
Does it occur on specific websites or all of them? Does it reproduce with jcef original examples?

Re: Cefframe Copy / Paste not working

PostPosted: Tue Jul 20, 2021 1:43 pm
by JCEFBeginner
I found out that the problem only persists, if off-screen-rendering is enabled.
I think the pages are not the cause of the problem. Furthermore, I tested it out on permission.site, Google and a local HTML File.

Re: Cefframe Copy / Paste not working

PostPosted: Tue Jul 20, 2021 4:33 pm
by Czarek
I've found an issue in the tracker that says enabling multi threaded message loop resolved clipboard issue, you could try that. You could also test with C++ CEF sample app to see if it reproduces.

Re: Cefframe Copy / Paste not working

PostPosted: Thu Jul 22, 2021 10:14 am
by JCEFBeginner
Sorry, but I don't know how to enable multithreaded message loop. :?:

Re: Cefframe Copy / Paste not working

PostPosted: Thu Jul 22, 2021 12:15 pm
by Czarek
Looks like it is enabled by default in java-cef: https://github.com/chromiumembedded/jav ... t.cpp#L206