我正在 try catch 功能键 F1 到 F12 和 4 个箭头键以及主页、插入、删除、结束、向上翻页和向下翻页键。如何????
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
}
最佳答案
覆盖表单的 ProcessCmdKey() 方法。在将键盘消息发送到具有焦点的控件之前,它直接从消息循环中调用。这就是覆盖 WndProc() 不起作用的原因。
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);
}
从技术上讲,您还可以使用 KeyPreview = true 覆盖窗体的 OnKeyDown 方法,但这是一个丑陋的 VB6 时代错误。
关于C# Windows 窗体 : How to capture Capture Function, 箭头和导航键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5361486/