Question

i am trying to capture Function keys F1 to F12 & 4 Arrow Keys & Home, Insert, Delete, End, Page Up & Down Keys. How To ????

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
}
Was it helpful?

Solution

Override the ProcessCmdKey() method of the form. It gets called straight from the message loop, before the keyboard message is dispatched to the control with the focus. Which is why overriding WndProc() doesn't work.

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
        if (keyData == (Keys.Control | Keys.F)) {
            MessageBox.Show("What the Ctrl+F?");
            return true;
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }

Technically, you can also override the form's OnKeyDown method with KeyPreview = true, but that's an ugly VB6 anachronism.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top