Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cefsharp and previewkeydown event not working

Tags:

c#

cefsharp

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?

like image 659
Jared Wilkin Avatar asked Apr 14 '16 23:04

Jared Wilkin


People also ask

Does pressing the arrow keys raise the previewkeydown event?

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.

How do I display the contextmenustrip using the previewkeydown event?

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.

Should I put any logic in the previewkeydown event handler?

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.

What is previewkeydown event in datagridview?

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.


1 Answers

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.

like image 144
amaitland Avatar answered Oct 17 '22 07:10

amaitland