C++ CEF3 Keyboard Input From GLFW

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.

C++ CEF3 Keyboard Input From GLFW

Postby KKlouzal » Wed Aug 24, 2022 9:46 pm

Hello, I'm using CEF3 (104.4.25) setup for offscreen rendering using Vulkan as my applications graphics library. I also use GLFW as my applications window manager.

I am struggling to get keyboard input working properly. I have some input currently, standard keypresses come through (A-Z, 0-9, spacebar).
Unfortunately, all characters come through as capital, backspace does not work, nor does the shift key do anything yet. Attempting to press any other key comes through as a seemingly random character with an accent above it.
Obviously I need to account for the user pressing these special/system keys but am unsure how to properly use the CefKeyEvent to handle these special cases.
I am also not sure why every standard character comes through as capital..

int scancode: https://www.glfw.org/docs/3.3/group__keys.html
int mods: https://www.glfw.org/docs/3.3/group__mods.html


Code: Select all
      void Keyboard(int scancode, int action, int mods)
      {
         CefKeyEvent KeyEvent;
         
         KeyEvent.windows_key_code = scancode;
         KeyEvent.type = cef_key_event_type_t::KEYEVENT_CHAR;
         KeyEvent.focus_on_editable_field = true;
         KeyEvent.modifiers = cef_event_flags_t::EVENTFLAG_NONE;
         KeyEvent.is_system_key = false;
         browser->GetHost()->SendKeyEvent(KeyEvent);
      }

(int scancode) appears to line up with what CefKeyEvent expects to see for windows_key_code, thankfully.
I'm not sure which approach to take moving forward, for example, I can check (int mods) to see if shift is currently being held, or capslock is turned on, but what do I need to change inside CefKeyEvent to let CEF know one of these keys are pressed?
I can also check (int scancode) to see if the key is non-standard (A-Z, 1-9, etc..) or the reverse and check to see if backspace was pressed, but for example, say backspace was pressed, what do I need to change inside CefKeyEvent to let CEF know?

Do I need to make a map of all the scancodes, and if its a standard key then use KEYEVENT_CHAR and if it's a non-standard key then use KEYEVENT_KEYDOWN and then set is_system_key=true? Perhaps something else..?

I have looked at cefclient project here as suggested in some other posts:
https://bitbucket.org/chromiumembedded/ ... win.cc-755
Unfortunately, I do not have access to the lparam or wparam from raw system events, nor does their usage of CefKeyEvent help me to understand what is actually happening..

I appreciate any input here.
Thank you.
KKlouzal
Newbie
 
Posts: 9
Joined: Fri Nov 08, 2019 5:30 am

Re: C++ CEF3 Keyboard Input From GLFW

Postby magreenblatt » Wed Aug 24, 2022 10:02 pm

I suggest adding logging of the CefKeyEvent contents when running with “cefclient --off-screen-rendering-enabled” to better understand what is expected.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: C++ CEF3 Keyboard Input From GLFW

Postby KKlouzal » Wed Aug 24, 2022 11:06 pm

magreenblatt wrote:I suggest adding logging of the CefKeyEvent contents when running with “cefclient --off-screen-rendering-enabled” to better understand what is expected.

Thank you, I will add some debugging output there to help understand what's happening.

I have gotten all the alphanumeric characters working, upper/lowercase, symbols (capslock/numlock works correctly)
I used glfwSetCharCallback() which returns the unicode codepoint of the character being pressed, so depending on if you hold shift or have capslock the codepoint returned is different:
https://www.glfw.org/docs/3.3/input_gui ... input_char
Code: Select all
void EventReceiver::char_callback(GLFWwindow* window, unsigned int codepoint)
{
   EventReceiver* Rcvr = static_cast<EventReceiver*>(glfwGetWindowUserPointer(window));

   if (Rcvr->isCursorActive)
   {
      WorldEngine::CEF::KeyboardCharacter(codepoint);
   }
}

Code: Select all
      void KeyboardCharacter(unsigned int scancode)
      {
         CefKeyEvent KeyEvent;
         
         KeyEvent.windows_key_code = scancode;
         KeyEvent.native_key_code = scancode;
         KeyEvent.type = cef_key_event_type_t::KEYEVENT_CHAR;
         KeyEvent.focus_on_editable_field = true;
         KeyEvent.modifiers = 0;
         KeyEvent.is_system_key = false;
         browser->GetHost()->SendKeyEvent(KeyEvent);
         printf("SCANCODE: %i\n", scancode);
      }


Backspace/delete, however, is still not functioning. No codepoint is returned when pressing either of these keys. I will need to handle these events manually through the glfwSetKeyCallback function in my original post.

Now the question is, how to properly formulate the CefKeyEvent to fire a backspace or delete off into CEF..
KKlouzal
Newbie
 
Posts: 9
Joined: Fri Nov 08, 2019 5:30 am

Re: C++ CEF3 Keyboard Input From GLFW

Postby KKlouzal » Wed Aug 24, 2022 11:54 pm

Not sure if this is proper or portable but..

glfw keycallback
Code: Select all
      if (key == GLFW_KEY_BACKSPACE || key == GLFW_KEY_DELETE || key == GLFW_KEY_LEFT || key == GLFW_KEY_RIGHT || key == GLFW_KEY_UP || key == GLFW_KEY_DOWN || key == GLFW_KEY_ENTER)
      {
         WorldEngine::CEF::KeyboardKey(key, bDown);
      }


Code: Select all
      void KeyboardKey(unsigned int scancode, bool bDown)
      {
         CefKeyEvent KeyEvent;
         if (scancode == GLFW_KEY_BACKSPACE)
            KeyEvent.windows_key_code = VK_BACK;
         if (scancode == GLFW_KEY_DELETE)
            KeyEvent.windows_key_code = VK_DELETE;
         if (scancode == GLFW_KEY_LEFT)
            KeyEvent.windows_key_code = VK_LEFT;
         if (scancode == GLFW_KEY_RIGHT)
            KeyEvent.windows_key_code = VK_RIGHT;
         if (scancode == GLFW_KEY_UP)
            KeyEvent.windows_key_code = VK_UP;
         if (scancode == GLFW_KEY_DOWN)
            KeyEvent.windows_key_code = VK_DOWN;
         if (scancode == GLFW_KEY_ENTER)
            KeyEvent.windows_key_code = VK_RETURN;

         if (bDown)
         {
            KeyEvent.type = cef_key_event_type_t::KEYEVENT_RAWKEYDOWN;
         }
         else {
            KeyEvent.type = cef_key_event_type_t::KEYEVENT_KEYUP;
         }
         KeyEvent.focus_on_editable_field = true;
         KeyEvent.modifiers = 0;
         KeyEvent.is_system_key = false;
         browser->GetHost()->SendKeyEvent(KeyEvent);
         printf("KEY SCANCODE: %i\n", scancode);
      }
KKlouzal
Newbie
 
Posts: 9
Joined: Fri Nov 08, 2019 5:30 am


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 96 guests