Page 1 of 1

Trying to get Action Keys (backspace, arrow, etc.) in JCEF

PostPosted: Mon Jul 04, 2022 1:56 am
by Hauntedpasta1
I'm working on a minecraft mod that uses JCEF, but I ran into a problem, it requires me to use sendKeyEvent(KeyEvent).
Is there any way to call action keys, like backspaces, arrow keys, insert, etc. This is for windows 11 and macOS (intel)

This is the code for it currently this is in the CefBrowserOsr class in java

Code: Select all
@Override
    public void injectKeyTyped(int key, int mods) {
        KeyEvent ev = new KeyEvent(dc_, KeyEvent.KEY_TYPED, 0, mods, 0, (char) key);
        sendKeyEvent(ev);


here is the code for calling the native code.
Code: Select all
/**
     * Send a key event.
     * @param e The event to send.
     */
    protected final void sendKeyEvent(KeyEvent e) {
        try {
            N_SendKeyEvent(e);
        } catch (UnsatisfiedLinkError ule) {
            ule.printStackTrace();
        }
    }

Re: Trying to get Action Keys (backspace, arrow, etc.) in JC

PostPosted: Mon Jul 04, 2022 6:24 am
by Hauntedpasta1
Can someone anyone help me pls?

Re: Trying to get Action Keys (backspace, arrow, etc.) in JC

PostPosted: Tue Jul 05, 2022 3:07 am
by FriwiDev
Code: Select all
KeyEvent ev = new KeyEvent(dc_, KeyEvent.KEY_TYPED, System.currentTimeMillis(), mods, KeyEvent.VK_UNDEFINED, (char) key);

...could work better. Make sure you have the browser on focus before trying to send key presses! (Please note that focus magic with CEF browsers has been a difficult task in the past and will most likely be buggy).

Re: Trying to get Action Keys (backspace, arrow, etc.) in JC

PostPosted: Tue Jul 05, 2022 7:28 pm
by Hauntedpasta1
FriwiDev wrote:
Code: Select all
KeyEvent ev = new KeyEvent(dc_, KeyEvent.KEY_TYPED, System.currentTimeMillis(), mods, KeyEvent.VK_UNDEFINED, (char) key);

...could work better. Make sure you have the browser on focus before trying to send key presses! (Please note that focus magic with CEF browsers has been a difficult task in the past and will most likely be buggy).


Let me see if this works with arrow keys :D

Re: Trying to get Action Keys (backspace, arrow, etc.) in JC

PostPosted: Tue Jul 05, 2022 7:35 pm
by Hauntedpasta1
backspace send an a with a ~ on top, arrow keys and every other action key isn't doing its thing. A-Z, a-z, and, 0-9 + some symbols are working

Re: Trying to get Action Keys (backspace, arrow, etc.) in JC

PostPosted: Thu Jul 07, 2022 2:30 pm
by Hauntedpasta1
Can someone help me again pls!

Re: Trying to get Action Keys (backspace, arrow, etc.) in JC

PostPosted: Fri Jul 08, 2022 4:36 pm
by FriwiDev
You need to figure out the correct key codes for the arrow keys. You can find them by either searching them online, or debug printing them when receiving key presses on some awt component. Keycodes for the arrows are 37 to 40 (left, up, right, down). They can also be found in the constants KeyEvent.VK_LEFT, ...

Re: Trying to get Action Keys (backspace, arrow, etc.) in JC

PostPosted: Fri Jul 08, 2022 7:41 pm
by Hauntedpasta1
ah ok but how do I send the action keys to the browser

Re: Trying to get Action Keys (backspace, arrow, etc.) in JC

PostPosted: Sun Jul 10, 2022 5:51 am
by FriwiDev
Please use your favorite search engine to find this out way faster in the future. Your question is not really dependent on JCEF, as it applies to all swing components. You need to set the char value to KeyEvent.CHAR_UNDEFINED and change the KeyEvent.VK_UNSPECIFIED to the special key you want. For example KeyEvent.VK_LEFT.