Strange behavior of the CefBrowserHost::SendKeyEvent method

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.

Strange behavior of the CefBrowserHost::SendKeyEvent method

Postby senya » Mon Jul 06, 2020 1:11 pm

Strange behavior of the CefBrowserHost::SendKeyEvent method.

Hello.

After updating the cef framework from version 3865 to version 4103.
When a character input event is generated using the method CefBrowserHost::SendKeyEvent.
Instead of letters (a,b,c), numbers appear in the input field.

cefsimple, lubuntu linux 19 OS x64.

The code below displays '123123123' in the Google input field instead of the 'abcabcabc'.

Code: Select all
void SimpleHandler::OnLoadingStateChange(CefRefPtr<CefBrowser> browser,
                                         bool isLoading,
                                         bool canGoBack,
                                         bool canGoForward) {
    LOG(INFO) << "SimpleApp::OnLoadingStateChange";
    LOG(INFO) << isLoading;

    if (false == isLoading) {
        CefKeyEvent keyEvent;
        char16 chr = 'a';
        char16 chr2 = 'b';
        char16 chr3 = 'c';

        keyEvent.type = KEYEVENT_CHAR;
        keyEvent.modifiers = 0;
        keyEvent.unmodified_character = chr;
        keyEvent.is_system_key = false;
        keyEvent.focus_on_editable_field = true;
        CefRefPtr<CefBrowserHost> browserHost = browser->GetHost();

        for (int i = 0; i < 9; ++i) {
            if (0 == (i % 3)) {
                keyEvent.character = chr;
                int lKeyCode = static_cast<int>(keyEvent.character);
                keyEvent.windows_key_code = lKeyCode;
                keyEvent.native_key_code = lKeyCode;
            }
            else if (1 == (i % 3)) {
                keyEvent.character = chr2;
                int lKeyCode = static_cast<int>(keyEvent.character);
                keyEvent.windows_key_code = lKeyCode;
                keyEvent.native_key_code = lKeyCode;
            }
            else if (2 == (i % 3)) {
                keyEvent.character = chr3;
                int lKeyCode = static_cast<int>(keyEvent.character);
                keyEvent.windows_key_code = lKeyCode;
                keyEvent.native_key_code = lKeyCode;
            }
            browserHost->SendKeyEvent(keyEvent);
        }
    }
}
Attachments
KeyEvent error cefsimple.png
KeyEvent error cefsimple.png (33.46 KiB) Viewed 358 times
Sergey, developer from Moscow region.
It's hot here today
https://docs.zoho.eu/file/40henad156481 ... 29fb3a8ebe
senya
Techie
 
Posts: 47
Joined: Mon Sep 18, 2017 2:58 pm

Re: Strange behavior of the CefBrowserHost::SendKeyEvent met

Postby Czarek » Mon Jul 06, 2020 1:33 pm

You can hook up to key events using CefKeyboardHandler in a windowed application and compare events. Note that SendKeyEvent is for off-screen rendering, any other usage I guess is undefined.
Maintainer of the CEF Python, PHP Desktop and CEF C API projects. I'm available for contract work, see my resume.
User avatar
Czarek
Virtuoso
 
Posts: 1869
Joined: Sun Nov 06, 2011 2:12 am

Re: Strange behavior of the CefBrowserHost::SendKeyEvent met

Postby salvadordf » Tue Jul 07, 2020 7:14 am

Just to give some more background information about this, we have a demo in the CEF4Delphi project that uses SendKeyEvent in a windowed browser with CEF 83.4.2.

I just tested this feature again in Windows 7 and it works correctly.
Maintainer of the CEF4Delphi project.
User avatar
salvadordf
Mentor
 
Posts: 98
Joined: Sun Dec 18, 2016 8:39 am
Location: Spain

Re: Strange behavior of the CefBrowserHost::SendKeyEvent met

Postby senya » Wed Jul 08, 2020 2:31 pm

Hook up with CefKeyboardHandler::OnPreKeyEvent and CefKeyboardHandler::OnKeyEvent shows that strange character code conversions are occurring.


Code fragment from cefsimple.

Code: Select all
void SimpleHandler::OnLoadingStateChange(CefRefPtr<CefBrowser> browser,
                                         bool isLoading,
                                         bool canGoBack,
                                         bool canGoForward) {
    if (false == isLoading) {
        LOG(INFO) << "SimpleApp::OnLoadingStateChange";

        CefKeyEvent keyEvent;
        char16 chr = 'a';
        char16 chr2 = 'b';
        char16 chr3 = 'z';

        keyEvent.type = KEYEVENT_CHAR;
        keyEvent.modifiers = 0;
        keyEvent.unmodified_character = chr;
        keyEvent.is_system_key = false;
        keyEvent.focus_on_editable_field = true;
        CefRefPtr<CefBrowserHost> browserHost = browser->GetHost();

        LOG(INFO) << "keyEvent.type " << KEYEVENT_CHAR;
        LOG(INFO) << "keyEvent.modifiers " << keyEvent.modifiers;
        LOG(INFO) << "keyEvent.unmodified_character " << keyEvent.unmodified_character;
        LOG(INFO) << "keyEvent.is_system_key " << keyEvent.is_system_key;
        LOG(INFO) << "keyEvent.focus_on_editable_field " << keyEvent.focus_on_editable_field;

        for (int i = 0; i < 9; ++i) {
            if (0 == (i % 3)) {
                keyEvent.character = chr;
                int lKeyCode = static_cast<int>(keyEvent.character);
                keyEvent.windows_key_code = lKeyCode;
                keyEvent.native_key_code = lKeyCode;

                LOG(INFO) << "keyEvent.character " << keyEvent.character;
                LOG(INFO) << "char(keyEvent.character) " << static_cast<char>(keyEvent.character);
                LOG(INFO) << "keyEvent.windows_key_code " << keyEvent.windows_key_code;
                LOG(INFO) << "keyEvent.native_key_code " << keyEvent.native_key_code;
            }
            else if (1 == (i % 3)) {
                keyEvent.character = chr2;
                int lKeyCode = static_cast<int>(keyEvent.character);
                keyEvent.windows_key_code = lKeyCode;
                keyEvent.native_key_code = lKeyCode;

                LOG(INFO) << "keyEvent.character " << keyEvent.character;
                LOG(INFO) << "char(keyEvent.character) " << static_cast<char>(keyEvent.character);
                LOG(INFO) << "keyEvent.windows_key_code " << keyEvent.windows_key_code;
                LOG(INFO) << "keyEvent.native_key_code " << keyEvent.native_key_code;
            }
            else if (2 == (i % 3)) {
                keyEvent.character = chr3;
                int lKeyCode = static_cast<int>(keyEvent.character);
                keyEvent.windows_key_code = lKeyCode;
                keyEvent.native_key_code = lKeyCode;

                LOG(INFO) << "keyEvent.character " << keyEvent.character;
                LOG(INFO) << "char(keyEvent.character) " << static_cast<char>(keyEvent.character);
                LOG(INFO) << "keyEvent.windows_key_code " << keyEvent.windows_key_code;
                LOG(INFO) << "keyEvent.native_key_code " << keyEvent.native_key_code;
            }
            browserHost->SendKeyEvent(keyEvent);
        }
    }
}
//------------------

bool SimpleHandler::OnPreKeyEvent(CefRefPtr<CefBrowser> browser,
                                      const CefKeyEvent& event,
                                      CefEventHandle os_event,
                                      bool* is_keyboard_shortcut) {
    CEF_REQUIRE_UI_THREAD();
    LOG(INFO) << "SimpleHandler::OnPreKeyEvent";

    LOG(INFO) << "CefKeyEvent";
    LOG(INFO) << "type " << event.type;
    LOG(INFO) << "character " << event.character;
    LOG(INFO) << "char(character) " << static_cast<char>(event.character);
    LOG(INFO) << "modifiers " << event.modifiers;
    LOG(INFO) << "is_system_key " << event.is_system_key;
    LOG(INFO) << "native_key_code " << event.native_key_code;
    LOG(INFO) << "windows_key_code " << event.windows_key_code;
    LOG(INFO) << "unmodified_character " << event.unmodified_character;
    LOG(INFO) << "focus_on_editable_field " << event.focus_on_editable_field;

    bool result {false};
    return result;
}
//------------------

bool SimpleHandler::OnKeyEvent(CefRefPtr<CefBrowser> browser,
                                   const CefKeyEvent& event,
                                   CefEventHandle os_event) {
    CEF_REQUIRE_UI_THREAD();
    LOG(INFO) << "SimpleHandler::OnKeyEvent";

    LOG(INFO) << "CefKeyEvent";
    LOG(INFO) << "type " << event.type;
    LOG(INFO) << "character " << event.character;
    LOG(INFO) << "char(character) " << static_cast<char>(event.character);
    LOG(INFO) << "modifiers " << event.modifiers;
    LOG(INFO) << "is_system_key " << event.is_system_key;
    LOG(INFO) << "native_key_code " << event.native_key_code;
    LOG(INFO) << "windows_key_code " << event.windows_key_code;
    LOG(INFO) << "unmodified_character " << event.unmodified_character;
    LOG(INFO) << "focus_on_editable_field " << event.focus_on_editable_field;

    bool result {false};
    return result;
}
//------------------


The background is CEF 83.4.0+gfd6631b+chromium-83.0.4103.106, Lubuntu linux 19.10 x64, I don’t know what features of the system can affect anymore.

I think I’ll have to use javascript events generation for the keyboard.
Attachments
CefBrowserHost_SendKeyEvent error cefsimple.png
CefBrowserHost_SendKeyEvent error cefsimple.png (428.01 KiB) Viewed 331 times
Sergey, developer from Moscow region.
It's hot here today
https://docs.zoho.eu/file/40henad156481 ... 29fb3a8ebe
senya
Techie
 
Posts: 47
Joined: Mon Sep 18, 2017 2:58 pm

Re: Strange behavior of the CefBrowserHost::SendKeyEvent met

Postby senya » Fri Jul 17, 2020 10:05 am

Good day.
I decided to work around this strange problem by using the javascript property document.activeElement.value to fill in the selected text fields on the web page.

For example.

Code: Select all
CefString code {document.activeElement.value += "string1"};
browser->GetMainFrame()->ExecuteJavaScript(code,CefString(),0);
Sergey, developer from Moscow region.
It's hot here today
https://docs.zoho.eu/file/40henad156481 ... 29fb3a8ebe
senya
Techie
 
Posts: 47
Joined: Mon Sep 18, 2017 2:58 pm


Return to Support Forum

Who is online

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