Hi, welcome to Microsoft Q&A.
You can use the PreviewKeyDown event to make the form get a hotkey and perform the corresponding operation.
The following code implements the function of pressing the Esc key to close all forms.
private void Form2_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
List<Form> formsToClose = new List<Form>(Application.OpenForms.Cast<Form>());
foreach (Form form in formsToClose)
{
form.Close();
}
}
}
Best Regards.
Jiachen Li
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.