在ProcessCmdKey中处理Ctrl + AnyKey的技术上正确的方法是什么? [英] What is the technically correct way to handle Ctrl+AnyKey in ProcessCmdKey?
问题描述
我创建了一个派生的RichTextBox类,如下所示:
I have created a derived RichTextBox class as follows:
public partial class FACTConsole : RichTextBox {
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if ((keyData == (Keys.Control | Keys.A)) ||
(keyData == (Keys.Control | Keys.Z)) ||
(keyData == (Keys.Control | Keys.V))) {
return true;
}
//:
//: Other keys and combinations
//:
}
}
按预期工作(禁用那些特定的Ctrl键组合)。
但我的要求是禁用Ctrl- AnyKey组合,而不是明确提及每个键。
我尝试更改我的代码如下:
This works as expected (disables those specific Ctrl-key combinations).
But my requirement is to disable Ctrl-AnyKey combination, rather than mention each key explicitly.
I tried changing my code as follows:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (keyData == Keys.Control) {
return true;
}
//:
//: Other keys and combinations
//:
}
但这不能按预期工作。我正在使用VS .Net 2008(.Net Framework 3.5)
But that doesn't work as expected. I'm using VS .Net 2008 (.Net Framework 3.5)
处理此要求的技术上正确的方法是什么?
顺便说一句,我不想仅仅为了处理Ctrl键而使用KeyDown事件,因为我的ProcessCmdKey处理了许多其他情况和密钥,如果我行为不端使用e.SuppressKeyPress = true进行此操作;
What is the technically correct way to handle this requirement?
By the way, I do not want to use the KeyDown event just to handle the Ctrl key, because my ProcessCmdKey handles many other cases and keys, which misbehave if I do so with e.SuppressKeyPress = true;
推荐答案
< p style ="margin:0px 0px 10.66px"> 试试这个条件:
如果 ((keyData& Keys.Control)! = 0)
{
{
return
< span style ="margin:0px; color:blue; font-family:Consolas; font-size:9.5pt"> true ;
}
}
这篇关于在ProcessCmdKey中处理Ctrl + AnyKey的技术上正确的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!