I want to have the Esc key undo any changes to a textbox since it got focus.
I have the text, but can't seem to figure out how to capture the Esc key. Both KeyUp
and KeyPressed
don't seem to get it.
This should work. How are you handling the event?
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
MessageBox.Show("Escape Pressed");
}
}
Edit in reply to comment - Try overriding ProcessCmdKey
instead:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Escape && myTextBox.Focused)
{
MessageBox.Show("Escape Pressed");
}
return base.ProcessCmdKey(ref msg, keyData);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With