The problem:
- KeyUp is handled by the grid in main window (
Grid_KeyUp
) - Main window shows a child window (e.g.
MessageBox.Show(...)
) - User presses [Return] (
Keys.Return
) to close theMessageBox
- Main window actually GETS the KeyUp event from that keypress in MessageBox
I have a very simple isolated sample that shows the problem I am having. Just create an empty WPF project and use this XAML:
<Grid KeyUp="Grid_KeyUp">
<Button Click="Button_Click"></Button>
</Grid>
And the code-behind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Grid_KeyUp(object sender, KeyEventArgs e)
{
Console.WriteLine("KEYUP: " + e.Key);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(this, "TEST");
}
}
If you click the button and press [Return] to close the MessageBox the output window will print:
KEYUP: Return
Weird facts:
- I get the same behaviour when pressing [Escape] (
KEYUP: Escape
gets printed), but when I press [Space] the console does not print out anything! - e.Source and e.OriginalSource point to the button in the main window (which is, obviously, wrong).
- If you put a breakpoint right after
MessageBox.Show(...)
the event does not get handled by out KeyUp handler (i.e. the output is empty).
Could anyone explain to me what is happening?
PS Target framework is: .NET 4 Client