Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KeyPress F1 does not work C#

Tags:

c#

keypress

I'm designing a device app. Compact Framework 2.0

I want the user to press F1 to navigate to the next screen, but it does not work.

Can't seem to find a solution.

Is it possible?

This is how I normally use Keypress:

This works:

        if (e.KeyChar == (char)Keys.M)
        {
            MessageBox.Show("M pressed");
            e.Handled = true;
        }

This dos NOT work:

        if (e.KeyChar == (char)Keys.F1)
        {
            MessageBox.Show("F1 pressed");
            e.Handled = true;
        }
like image 892
Werner van den Heever Avatar asked Jul 30 '13 13:07

Werner van den Heever


1 Answers

Refer This

You can override the ProcessCmdKey method of your form class and use keyData == Keys.F1 to check whether F1 is pressed. Above link has example as follows.

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == Keys.F1)
    {
        MessageBox.Show("You pressed the F1 key");
        return true;    // indicate that you handled this keystroke
    }

    // Call the base class
    return base.ProcessCmdKey(ref msg, keyData)
}
like image 103
Microsoft DN Avatar answered Sep 24 '22 22:09

Microsoft DN