How do I capture the Escape key?

You can process the escape key by overriding the form's ProcessCmdKey() method.

For example, in C#:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
	bool Result = false;

	if (keyData == Keys.Escape)
	{
		MessageBox.Show("Escape was pressed");
		Result = true;
	}

	return Result;
}

And in VB .NET:

Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
   Dim Result As Boolean = False
   
   If keyData = Keys.Escape Then
      MessageBox.Show("Escape was pressed")
      Result = True
   End If
   
   Return Result
End Function

© (c) 2026 Oak Leaf Enterprises, Inc., 1996-2026 • Updated: 09/22/06
Comment or report problem with topic