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?
However, pressing the arrow keys for a Button does raise the PreviewKeyDown event. By handling the PreviewKeyDown event for a Button and setting the IsInputKey property to true, you can raise the KeyDown event when the arrow keys are pressed. However, if you handle the arrow keys, the focus will no longer move to the previous or next control.
The PreviewKeyDown event handler detects when the UP ARROW or DOWN ARROW keys are pressed and sets the IsInputKey property to true. This raises the KeyDown event so that you can display the ContextMenuStrip. You should not put any logic in the PreviewKeyDown event handler, other than to set the IsInputKey property.
You should not put any logic in the PreviewKeyDown event handler, other than to set the IsInputKey property. Instead, you should put your logic in the KeyDown event handler.
C# PreviewKeyDown Event PreviewKeyDown fixes a problem with keyboard input on DataGridView. It helps correct an issue with keyboard navigation. If the user focuses a cell and presses enter, the selection might not work properly.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With