I have the following code which does not show the MessageBox when enter/return is pressed.
For any other key(i.e. letters/numbers) the MessageBox shows False.
private void textBox_PreviewKeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) { MessageBox.Show("Enter key pressed"); } else if (e.Key == Key.Space) { MessageBox.Show("Space key pressed"); } }
Use PreviewKeyDown event to detect any key before shown in textbox or input