9

This might be a simple question, but I have a winforms app that is loading a ChromiumWebBrowser control (CefSharp) and I can't figure out how to capture key preview events as they are all being swallowed by the control.

The standard attaching a handler to the PreviewKeyDown event of the browser control isn't working. Is there a known workaround?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Jared Wilkin
  • 231
  • 3
  • 12

1 Answers1

9

CEF is run in it's own message loop, so the standard events don't work.

The first an easiest option is to implement IKeyboardHandler, you can check the CefSharp source for a more detailed example (there's one that forwards messages to the parent window if required).

Second run with settings.MultiThreadedMessageLoop = false, and call Cef.DoMessageLoopWork() on application idle, this will integrate CEF into the same message loop as your main application. Again, the source contains examples see https://github.com/cefsharp/CefSharp/blob/cefsharp/49/CefSharp.WinForms.Example/Program.cs#L63

The third option is to hook into the CEF message loop, see https://github.com/cefsharp/CefSharp/blob/cefsharp/49/CefSharp.WinForms.Example/ChromeWidgetMessageInterceptor.cs for an example

CEF = Chromium Embedded Framework - CefSharp is just a wrapper.

amaitland
  • 4,073
  • 3
  • 25
  • 63
  • I tried both options 1 and 3 and while I was able to get the mouse events from the interceptor I wasn't able to capture any keyboard events for some reason. That being said option 1 worked great, thanks – Jared Wilkin Apr 15 '16 at 15:47
  • Option 1 works for me - thanks (chromiumWebBrowser.KeyboardHandler = new MyKeyboardHandler();) – A.J.Bauer Apr 02 '18 at 17:02