.Net Winform checking which keys are pressed

I wanted to be able to check for which key(s) were pressed on a form.

KeyPreview = true for the form was not working.

I was able to use the following:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == (Keys.Control | Keys.S))     // Ctrl-S
    {
        return true;    // indicate that you handled this keystroke
    }
    else if (keyData == (Keys.Control | Keys.N))    // Ctrl-N
    {
        return true;    // indicate that you handled this keystroke
    }
    else if (keyData == Keys.F9)    // F9 Key
    {
        return true;    // indicate that you handled this keystroke
    }
    // Call the base class
    return base.ProcessCmdKey(ref msg, keyData);
}

Comments are closed.