How to intercept mouse events?

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 intercept mouse events?

Postby digory » Mon Oct 02, 2017 10:54 am

I need to track user activity (keyboard input, left and right mouse buttons, mouse wheel). For the keyboard, I can install a CefKeyboardHandler and implement OnKeyEvent. There does not seem to be a similar handler for mouse events, though. How can I intercept them?
digory
Expert
 
Posts: 118
Joined: Wed Oct 26, 2016 3:13 am

Re: How to intercept mouse events?

Postby magreenblatt » Mon Oct 02, 2017 3:49 pm

You can inject JavaScript into the page or use OS-specific hooks.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: How to intercept mouse events?

Postby digory » Tue Oct 03, 2017 2:21 am

Thanks for the reply. I can't use JavaScript, because a mouse event handler on the body tag does not see any mouse events whose propagation has been cancelled. I need to see *all* mouse events. Instead, I tried SetWindowsHookEx, but this is no good either, because it sees mouse events outside the browser window as well, even those in a different application. Finally, I tried SetWindowSubclass, which seems the way to go:

Code: Select all
LRESULT CALLBACK SubclassProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass,
   DWORD_PTR dwRefData)
   {
   // Intercept events here.
   return ::DefSubclassProc (hwnd, uMsg, wParam, lParam);
   }

void KisCefClient::OnAfterCreated (CefRefPtr<CefBrowser> browser)
   {
   CEF_REQUIRE_UI_THREAD();

   HWND hwnd = browser->GetHost()->GetWindowHandle();
   HWND child = ::GetWindow (hwnd, GW_CHILD);

   if (child)
      {
      ::SetWindowSubclass (child, &SubclassProc, 0, 0);
      }
   }

The SubclassProc sees all kinds of events, but unfortunately, WM_LBUTTONDOWN is not seen. :(
digory
Expert
 
Posts: 118
Joined: Wed Oct 26, 2016 3:13 am

Re: How to intercept mouse events?

Postby magreenblatt » Tue Oct 03, 2017 2:40 am

Use SetWindowHooksEx and compare the point against the browser bounding box.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 60 guests