Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling F10 key from moving focus to menu bar in C# Winforms program

Tags:

c#

focus

winforms

When I press F10 in my program, the focus is lost from my main program window, and moves to the menu bar. It turns out this strange behavior is common across many Windows apps.

I think it's ugly because the Alt key does the same thing, and we lose a precious function key. Why is Windows doing this, and how can I solve the case for my C# app?

like image 570
Dan W Avatar asked Nov 14 '11 01:11

Dan W


1 Answers

Use the KeyDown Event for your form, and handle the Keystroke:

private void form_KeyDown(object sender, KeyEventArgs e)
{ if(e.KeyData == Keys.F10)
    {
        // Do what you want with the F10 key
        e.SuppressKeyPress = true;
    }
}

Also make sure that your forms KeyPreview is set to true.

like image 101
Mark Hall Avatar answered Oct 18 '22 12:10

Mark Hall