Issue with WH_KEYBOARD_LL Hook and getUserMedia Interaction

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.

Issue with WH_KEYBOARD_LL Hook and getUserMedia Interaction

Postby leeroy » Fri Nov 03, 2023 7:11 pm

We have implemented a low-level keyboard hook in our CEF application to monitor specific keyboard input events, especially to intercept and process Windows key events.

Hook Setup:
Code: Select all
SetWindowsHookExA(WH_KEYBOARD_LL, KeyManager::LowLevelKeyboardProc, m_hinstance, 0);

LRESULT CALLBACK KeyManager::LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
  LOG(INFO) << "LowLevelKeyboardProc: nCode: " << nCode << ", wParam: " << wParam << ", lParam: " << lParam;
  // Logic to block windows key and other processing...
}


This approach has been working seamlessly until we encountered web pages that make a call to getUserMedia:
Code: Select all
navigator.mediaDevices.getUserMedia({ audio: true });


Upon invoking getUserMedia, we observe that the LowLevelKeyboardProc callback is not triggered as long as CEF retains focus. However, when the focus is shifted away from the CEF window, the callback works as expected.

I've been considering alternatives like OnPreKeyEvent in CEF, but I'm not fully versed with its implementation, especially for trapping specific keys like the Windows key.

Environment:

Windows Versions: 10/11
Observed in CEF Versions: 5672, 5845 (There might be more affected versions)

Any insights or workarounds to address this interaction would be greatly appreciated.
leeroy
Expert
 
Posts: 103
Joined: Mon Jan 06, 2020 6:27 pm

Re: Issue with WH_KEYBOARD_LL Hook and getUserMedia Interact

Postby amaitland » Fri Nov 03, 2023 7:34 pm

Maintainer of the CefSharp project.
amaitland
Virtuoso
 
Posts: 1292
Joined: Wed Jan 14, 2015 2:35 am

Re: Issue with WH_KEYBOARD_LL Hook and getUserMedia Interact

Postby leeroy » Fri Nov 03, 2023 10:16 pm

Thank you amaitland, that certainly looks like the issue! Is there no other way to achieve this functionality without having to remove this from the chromium source?
leeroy
Expert
 
Posts: 103
Joined: Mon Jan 06, 2020 6:27 pm

Re: Issue with WH_KEYBOARD_LL Hook and getUserMedia Interact

Postby HarmlessDave » Sat Nov 04, 2023 4:53 pm

When we looked at the chromium source a couple of years ago it seemed to be grabbing raw input, so you would need to patch the source to prevent that and deal with any side effects. We chose not to do that, but I would not be surprised if it breaks something like the player controls for a video widget.
HarmlessDave
Expert
 
Posts: 370
Joined: Fri Jul 11, 2014 2:02 pm

Re: Issue with WH_KEYBOARD_LL Hook and getUserMedia Interact

Postby amaitland » Sun Nov 05, 2023 4:22 pm

CefKeyboardHandler is the supported option for key press interception in CEF.
Maintainer of the CefSharp project.
amaitland
Virtuoso
 
Posts: 1292
Joined: Wed Jan 14, 2015 2:35 am

Re: Issue with WH_KEYBOARD_LL Hook and getUserMedia Interact

Postby leeroy » Mon Nov 06, 2023 6:20 pm

Thanks amaitland and HarmlessDave. For whatever reason I can seem to get OnPreKeyEvent working and identify the windows key, but I cannot seem to consume it by returning true. Is there more to this formula I'm missing?
leeroy
Expert
 
Posts: 103
Joined: Mon Jan 06, 2020 6:27 pm

Re: Issue with WH_KEYBOARD_LL Hook and getUserMedia Interact

Postby leeroy » Tue Jan 09, 2024 7:43 pm

In the OnPreKeyEvent handler, I've implemented code to intercept the Windows key. While it successfully logs the keystroke, it fails to block the key's default action. I gather I'm missing a critical step somewhere?

Code: Select all
if (event.type == KEYEVENT_RAWKEYDOWN || event.type == KEYEVENT_KEYDOWN) {
    if (event.windows_key_code == VK_LWIN  || // 0x5B
        event.windows_key_code == VK_RWIN) { // 0x5C
      LOG(INFO) << "!!!!! WINDOW KEY PRESSED";
      return true;
    }
  }
leeroy
Expert
 
Posts: 103
Joined: Mon Jan 06, 2020 6:27 pm

Re: Issue with WH_KEYBOARD_LL Hook and getUserMedia Interact

Postby KatrinaS » Tue Jan 09, 2024 8:31 pm

I think it may count as a 'is_keyboard_shortcut', so set bool* is_keyboard_shortcut to true, return false, and handle it (return true) in OnKeyEvent instead,, it's worth a go anyway :D
KatrinaS
Mentor
 
Posts: 83
Joined: Tue Jul 04, 2023 6:30 pm

Re: Issue with WH_KEYBOARD_LL Hook and getUserMedia Interact

Postby leeroy » Wed Jan 10, 2024 12:09 pm

Thank you for your suggestion, KatrinaS. Unfortunately, it didn't resolve the issue either.
leeroy
Expert
 
Posts: 103
Joined: Mon Jan 06, 2020 6:27 pm

Re: Issue with WH_KEYBOARD_LL Hook and getUserMedia Interact

Postby KatrinaS » Wed Jan 10, 2024 1:07 pm

It's hard to know if this problem is a bug or a feature, the *.h says bing but the outcome is actually bong, very strange :)
KatrinaS
Mentor
 
Posts: 83
Joined: Tue Jul 04, 2023 6:30 pm

Next

Return to Support Forum

Who is online

Users browsing this forum: No registered users and 213 guests