How to map virtual key codes to javascript key names?

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.

How to map virtual key codes to javascript key names?

Postby rkcef » Wed Jun 01, 2022 3:42 am

I'm building a Windows/C++ desktop application with CEF as embedded browser engine.

I need to send send keyboard shortcuts from Windows (i.e. ALT + A) to the render engine (javascript) to trigger some javascript functions.
In javascript I create a new event of the type "KeyboardEvent" with the key information from CEF/C++ and dispatch it to the event listeners (https://developer.mozilla.org/en-US/doc ... boardEvent).

The issue I'm facing is that in javascript, the utilization of traditional key codes like "65" for the keyboard key "A" is deprecated (https://developer.mozilla.org/en-US/doc ... nt/keyCode).
Instead now each key has a string representation. For example the key "A" has the code "KeyA". The code for the key "1" is "Digit1". I'm using this tool to look them up: https://keyjs.dev.

To make it work I have to manually map the virtual key codes I get from Windows API (65 for "A", 49 for "1", etc.) to their corresponding string representation ("KeyA", "Digit1").

Example:

Code: Select all
switch (vkCode) // <-- virtual key codes from windows WM_KEYDOWN and WM_KEYUP events
{
  case 49:
  case VK_NUMPAD1:
    key = "1";
    code = "Digit1";
    break;

  case 65:
    key = "A";
    code = "KeyA";
    break;
...
}


I was wondering if CEF has any built-in functionality to "translate" theses values automatically?
Last edited by rkcef on Wed Jun 01, 2022 4:27 am, edited 1 time in total.
rkcef
Techie
 
Posts: 27
Joined: Fri Feb 26, 2021 9:11 am

Re: How to map virtual key codes to javascript key names?

Postby magreenblatt » Wed Jun 01, 2022 4:10 am

If the browser window has focus, and you don't implement CefKeyboardHandler::OnPreKeyEvent, then the keyboard shortcuts should go to the JavaScript code already.
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: How to map virtual key codes to javascript key names?

Postby rkcef » Wed Jun 01, 2022 4:22 am

Thanks for your reply.

I've another use case where I need to send keyboard shortcuts to javascript even if the application window has no focus. I've implemented a low-level hook like so:

Code: Select all
::SetWindowsHookEx(
    WH_KEYBOARD_LL,
    (HOOKPROC)&KeyboardEventsHook,
    NULL,
    0);


The important part is the hook type "WH_KEYBOARD_LL" (https://docs.microsoft.com/en-us/window ... eyboard_ll).

I collect all needed information in a CefDictionary and send is as part of the CefProcessMessage via "browser->GetMainFrame()->SendProcessMessage(PID_RENDERER, message);" to the render engine (javascript).

This works just fine. The issue is the manual translation of Windows keyboard event data to a javascript-compatible format.
rkcef
Techie
 
Posts: 27
Joined: Fri Feb 26, 2021 9:11 am

Re: How to map virtual key codes to javascript key names?

Postby magreenblatt » Wed Jun 01, 2022 4:42 am

How many keyboard shortcuts do you need to handle? Do you control the JavaScript code? One option might be calling a JavaScript function directly from the native code (like "doShortcutAction('action')") instead of mapping to a KeyboardEvent.
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: How to map virtual key codes to javascript key names?

Postby rkcef » Wed Jun 01, 2022 4:52 am

For now there are 20 keyboard shortcuts. I need to keep it flexible so I can't call a javascript function directly.

It's kind of ugly to manually hardcode key code mappings (65 => "KeyA", 49 => "Digit1", etc.).

Because CEF is all about the browser/render engine I thought that there could be some functions to simplify the communication between C++ and Javascript.

Maybe this is a topic for the future to include such a mapping function in the CEF core :)
rkcef
Techie
 
Posts: 27
Joined: Fri Feb 26, 2021 9:11 am

Re: How to map virtual key codes to javascript key names?

Postby magreenblatt » Wed Jun 01, 2022 5:35 am

There may be such a function in Chromium/Blink, but nothing is exposed via CEF currently.
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm


Return to Support Forum

Who is online

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