alexcepoi
alexcepoi

Reputation: 708

WPF windows forms host keyboard events

This is probably a simple question but i've been unable to find a quick answer.

I have a WPF application which has a Windows Forms Control hosting a GeckoFX component (doesn't really matter).

What i want to do is capture key down events inside the Windows Forms Control and grab focus of a WPF control for some particular key combination. And what if i want to capture events from the entire WPF application window (even inside the Windows Forms Control)? I tried handling the KeyDown event and PreviewKeyDown event but to no avail.

What i want to know is if this is possible and how this should be done. I can post some code if required.

Upvotes: 3

Views: 3272

Answers (1)

Sire
Sire

Reputation: 4358

The KeyDown even on the Form should work for non special keys (like arrow keys). Use PreviewKeyDown to capture those, or use this solution.

For GeckoWebBrowser specifically, I had to use PreviewKeyDown. Also, I added a line so it doesn't break in design mode:

private void webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
  if (DesignMode) return;
  if (!e.IsInputKey && e.Control && e.KeyCode == Keys.S) {
    DoStuff();
    return;
  }
}

Upvotes: 1

Related Questions