1

I have a WPF MainWindow and try to react to a certain key combination (CTRL + F4). I registered the following methods for testing purposes:

private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
    Log.AsInfo("PreviewKeyDown");
}

private void Window_KeyDown(object sender, KeyEventArgs e)
{
    Log.AsInfo("KeyDown");
}

private void Window_KeyUp(object sender, KeyEventArgs e)
{
    Log.AsInfo("KeyUp");
}

private void Window_PreviewKeyUp(object sender, KeyEventArgs e)
{
    Log.AsInfo("PreviewKeyUp");
}

Crazy thing is, those methods are only triggered after I interact with the application for the first time:

How it does not work:

  1. I start the application
  2. I enter CTRL + F4 on the keyboard
  3. Nothing happens

How it works:

  1. I start the application
  2. I click on a random menu item with no functionality
  3. I enter CTRL + F4 on the keyboard
  4. Everything works, log messages are written

Any ideas? I am not able to even debug the situation, because none of the handler methods is called in the first place. I even tried this.Focus() in the MainWindow constructor, but this did not help either.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • Can you post your xaml code? I tested with a window and an empty grid inside. The handlers are called after I start the application without clicking on anything. – bernd_rausch Dec 01 '14 at 20:17
  • Yes, sure. I don't know what's the best way to post a larger code sample, but here it is: [http://pastebin.com/im4vi528](http://pastebin.com/im4vi528). I left some elements out, but it's in the comments. –  Dec 01 '14 at 20:36
  • Possible duplicate of http://stackoverflow.com/questions/1361350/keyboard-shortcuts-in-wpf – Bizhan Dec 01 '14 at 20:47
  • I deleted a little code, because it referenced non-existing stuff. But this xaml works on my machine in VS2010 / C#4: http://pastebin.com/Xrwh8CPZ – bernd_rausch Dec 01 '14 at 21:01
  • @bernd_rausch, I also created a test WPF app. Everything works as expected. I reduced the code in my MainWindow constructor so that only `InitializeComponent()` is left there, but it's still not working. Might be a problem with an external control I am using. I keep trying and answer my own question, if I found a solution. –  Dec 03 '14 at 12:41

1 Answers1

0

Found the solution:

The browser control I used is the EO.WebBrowser. It seems, that this browser control swallows every first time key interaction. I implemented the shortcut combination by binding the functionality I needed to a hotkey of the WebBrowser control as mentioned here. So it was more of a third party than a WPF problem.