C# Windows Form: How to capture Capture Function, Arrow & Navigation Keys
i am trying to capture Function keys F1 to F12 & 4 Arrow Ke开发者_运维百科ys & Home, Insert, Delete, End, Page Up & Down Keys. How To ????
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
}
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.
精彩评论