2

I have a question related to key handling in CefSharp for WPF. I implemented the IKeyboardHandler interface and my ChromiumWebBrowser is using it as KeyboardHandler. The main logic is in the OnPreKeyEvent method where I return true if my key wass handled, false otherwise.

The issue is that if the F10 key is pressed, the screen loses focus as F10 is a system key and toggles the Windows Menu. How could I let CefSharp know not to relay the F10 to the OS for handling so my screen keeps focus?

In standard WPF there would be a e.Handled that I could set to true, but in this case there is none (based on documentation the return value does this though).

ZekeMarsh
  • 122
  • 10
  • Just a question: but is `OnPreKeyEvent` called at all when pressing F10? – Stefan Jan 11 '19 at 13:38
  • (if not you might need a hook.... arrgghh!: https://stackoverflow.com/questions/35749505/how-to-implement-keyboard-handler-cefsharp-for-shortcut-keys) – Stefan Jan 11 '19 at 13:39
  • @Stefan yes, it gets called and the implementation actually executes, it's only the even number of F10 presses that are not working due to this toggle. I've come across this SO link before, but I am not certain that the global hook is really what is needed for this problem. In this case it is not about shortcuts, but is about system keys. – ZekeMarsh Jan 11 '19 at 13:41
  • @Stefan just to clarify: OnPreKeyEvent does not get called on even presses (i.e. when the toggle is on) – ZekeMarsh Jan 11 '19 at 13:52
  • 1
    See https://github.com/cefsharp/CefSharp/wiki/General-Usage#wpf-1 for background. Inherit from `ChromiumWebBrowser` and override `OnPreviewKey[Up|Down]`, and make the key press as handled as you would any standard `WPF` event. https://github.com/cefsharp/CefSharp/blob/cefsharp/71/CefSharp.Wpf/ChromiumWebBrowser.cs#L2043 – amaitland Jan 11 '19 at 19:30

1 Answers1

1

As Alex Maitland said, the way to do this is to inherit from ChromiumWebBrowser and override OnPreviewKeyUp and OnPreviewKeyDown methods. In there, you can mark the KeyEventArgs as handled. In this case inheriting from IKeyboardHandler is not required.

ZekeMarsh
  • 122
  • 10