Characters in send_key_event not inputting to forms

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Characters in send_key_event not inputting to forms

Postby aleitner » Tue Jun 20, 2023 11:12 am

OS: ubuntu 22.04.1
CEF: cef_binary_112.3.0+gb09c4ca+chromium-112.0.5615.165_linux64.tar.bz2

Hey, I am trying to send key events using the C API running a headless browser. I am rendering the headless browser using the data from the on_paint callback. I have been testing to make sure the key events are properly sent by loading the https://keyboard-test.space/ webpage and when I press keys the webpage indicates they are correctly being pressed as seen in the screenshot below. The problem is that when I click on a form and try to input keys no letters/symbols are being input into forms. I can however Ctrl-V paste, backspace, and enter. Enter will submit whatever form I am typing into, and backspace will delete any characters copied into the form. I can't figure out why no characters are being input into forms when I send key. Is there anything obviously wrong with my key_event struct that I am building?

keyboard.png
keyboard.png (490.09 KiB) Viewed 3533 times


function for sending the key event:
Code: Select all
void send_keys(custom_client_t* client, int pressed, int keysym, keyboard_state* kbd_state) {

        /* Get previous Keyboard state to check for modifiers and what is pressed */
        pthread_mutex_lock(&kbd_state->mutex);
        int previous_key_state = kbd_state->key_state[keysym];
        int key_modifiers = get_modifiers(kbd_state);
        kbd_state->key_state[keysym] = pressed;
        pthread_mutex_unlock(&kbd_state->mutex);

        /* Convert keysym to native key code */
        int native_key_code = guac_keysym_to_native_key_code(keysym);

        /* Set the key event type based on the current and previous key state */
        cef_key_event_t key_event = {};
        if (pressed) {
            if (previous_key_state == 1) {
                key_event.type = KEYEVENT_KEYDOWN;
            } else {
                key_event.type = KEYEVENT_RAWKEYDOWN;
            }
        } else {
            key_event.type = KEYEVENT_KEYUP;
        }
       
        key_event.native_key_code = native_key_code;
        key_event.windows_key_code = native_key_code;

        key_event.character = keysym_to_unicode_char(keysym);
        key_event.unmodified_character = keysym_to_unicode_char(keysym);

        key_event.modifiers = key_modifiers;

            /* Send the key event to the browser */
        cef_browser_host_t *browser_host = client->life_span_handler->browser->get_host(client->life_span_handler->browser);
        browser_host->send_key_event(browser_host, &key_event);

        fprintf(stderr, "CEF: [Key Event] type: %d, keysym: %d, native_key_code: %d, character: %c, modifiers: %d\n",
                key_event.type, keysym, native_key_code, key_event.character, key_modifiers);
}


Logs when I type Hello, World!:
Code: Select all
CEF: [Key Event] type: 0, keysym: 72, native_key_code: 72, character: H, modifiers: 2
CEF: [Key Event] type: 2, keysym: 72, native_key_code: 72, character: H, modifiers: 2
CEF: [Key Event] type: 2, keysym: 65505, native_key_code: 16, character: 0, modifiers: 2
CEF: [Key Event] type: 0, keysym: 101, native_key_code: 69, character: e, modifiers: 0
CEF: [Key Event] type: 2, keysym: 101, native_key_code: 69, character: e, modifiers: 0
CEF: [Key Event] type: 0, keysym: 108, native_key_code: 76, character: l, modifiers: 0
CEF: [Key Event] type: 2, keysym: 108, native_key_code: 76, character: l, modifiers: 0
CEF: [Key Event] type: 0, keysym: 108, native_key_code: 76, character: l, modifiers: 0
CEF: [Key Event] type: 2, keysym: 108, native_key_code: 76, character: l, modifiers: 0
CEF: [Key Event] type: 0, keysym: 111, native_key_code: 79, character: o, modifiers: 0
CEF: [Key Event] type: 2, keysym: 111, native_key_code: 79, character: o, modifiers: 0
CEF: [Key Event] type: 0, keysym: 44, native_key_code: 188, character: ,, modifiers: 0
CEF: [Key Event] type: 2, keysym: 44, native_key_code: 188, character: ,, modifiers: 0
CEF: [Key Event] type: 0, keysym: 32, native_key_code: 32, character:  , modifiers: 0
CEF: [Key Event] type: 2, keysym: 32, native_key_code: 32, character:  , modifiers: 0
CEF: [Key Event] type: 0, keysym: 65505, native_key_code: 16, character: 0, modifiers: 0
CEF: [Key Event] type: 0, keysym: 87, native_key_code: 87, character: W, modifiers: 2
CEF: [Key Event] type: 2, keysym: 65505, native_key_code: 16, character: 0, modifiers: 2
CEF: [Key Event] type: 0, keysym: 111, native_key_code: 79, character: o, modifiers: 0
CEF: [Key Event] type: 2, keysym: 111, native_key_code: 79, character: o, modifiers: 0
CEF: [Key Event] type: 0, keysym: 114, native_key_code: 82, character: r, modifiers: 0
CEF: [Key Event] type: 2, keysym: 114, native_key_code: 82, character: r, modifiers: 0
CEF: [Key Event] type: 0, keysym: 108, native_key_code: 76, character: l, modifiers: 0
CEF: [Key Event] type: 0, keysym: 100, native_key_code: 68, character: d, modifiers: 0
CEF: [Key Event] type: 2, keysym: 108, native_key_code: 76, character: l, modifiers: 0
CEF: [Key Event] type: 2, keysym: 100, native_key_code: 68, character: d, modifiers: 0
CEF: [Key Event] type: 0, keysym: 65505, native_key_code: 16, character: 0, modifiers: 0
CEF: [Key Event] type: 0, keysym: 33, native_key_code: 49, character: !, modifiers: 2
CEF: [Key Event] type: 2, keysym: 33, native_key_code: 49, character: !, modifiers: 2
CEF: [Key Event] type: 2, keysym: 65505, native_key_code: 16, character: 0, modifiers: 2
aleitner
Techie
 
Posts: 49
Joined: Fri Jun 16, 2023 12:05 pm

Re: Characters in send_key_event not inputting to forms

Postby magreenblatt » Tue Jun 20, 2023 11:28 am

How does it behave in cefclient with --off-screen-rendering-enabled ? You should compare your key events to those generated by cefclient.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: Characters in send_key_event not inputting to forms

Postby aleitner » Tue Jun 20, 2023 12:04 pm

magreenblatt wrote:How does it behave in cefclient with --off-screen-rendering-enabled ? You should compare your key events to those generated by cefclient.


I am currently running with windowless rendering. I appended --off-screen-rendering-enabled to the commands but that didn't change anything.

Code: Select all
    /* Create a browser instance */
    cef_window_info_t window_info = {0};
    window_info.windowless_rendering_enabled = 1;


Code: Select all
void on_before_command_line_processing(struct _cef_app_t* self,
                                       const cef_string_t* process_type,
                                       struct _cef_command_line_t* command_line) {   
    /* Append the --disable-gpu switch to the command line arguments */
    cef_string_t disable_gpu = {};
    cef_string_from_ascii("--disable-gpu", 13, &disable_gpu);
    command_line->append_switch(command_line, &disable_gpu);
   
    /* Append the --disable-software-rasterizer switch to the command line argument */
    cef_string_t disable_software_rasterizer = {};
    cef_string_from_ascii("--disable-software-rasterizer", 29, &disable_software_rasterizer);
    command_line->append_switch(command_line, &disable_software_rasterizer);

    /* Append the --off-screen-rendering-enabled switch to the command line argument */
    cef_string_t off_screen_rendering_enabled = {};
    cef_string_from_ascii("--off-screen-rendering-enabled", 30, &off_screen_rendering_enabled);
    command_line->append_switch(command_line, &off_screen_rendering_enabled);
}


CEF is a compiled binary so I wouldn't be able to add a print statement to see any inputs.
aleitner
Techie
 
Posts: 49
Joined: Fri Jun 16, 2023 12:05 pm

Re: Characters in send_key_event not inputting to forms

Postby magreenblatt » Tue Jun 20, 2023 12:17 pm

The cefclient application is provided as source code with the CEF binary distribution. You should build and run it, and add debug print statements there.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: Characters in send_key_event not inputting to forms

Postby aleitner » Fri Jun 23, 2023 11:20 am

After looking through the source code I found out that I needed to send a followup CHAR event after a DOWN event.

Code: Select all
            key_event.type = KEYEVENT_CHAR;
            browser_host->send_key_event(browser_host, &key_event);


With this added characters were now being input into text fields
aleitner
Techie
 
Posts: 49
Joined: Fri Jun 16, 2023 12:05 pm


Return to Support Forum

Who is online

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