更新时间:2023-12-03 08:05:58
忽略msg.Msg,只看看KEYDATA。而如果没有,如果你使用的按键呼叫基地返回true。它简化为:
保护覆盖布尔ProcessCmdKey(参考消息味精,钥匙KEYDATA)
{
如果(KEYDATA ==(Keys.Control | Keys.Alt | Keys.Q)){
this.Parent.Text =<&CTRL GT + Alt键+ Q俘虏;
返回真;
}
返回base.ProcessCmdKey(REF味精,KEYDATA);
}
这或许应该是一个覆盖了的格式的的方法让你不依赖于具有聚焦的控制。你会使用this.Text来代替。
What's the 'correct/best' way to pick up a keystroke combination? The keys in question are Ctrl+Alt+Q, I want the user to press them all at the same time, at which point I am going to open a window. I'm currently doing it by have an array and then catching each keystroke individually, but my results are inconsistent, especially on a particular make of Dell laptop/windows 7 combination, but that's another story.
So after spending five minutes with google, this is just a rough version after looking at the msdn, but as I stated earlier is this version (untested) the correct/best way of doing it?
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
const int WM_KEYDOWN = 0x100;
const int WM_SYSKEYDOWN = 0x104;
if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
{
switch(keyData)
{
case Keys.Control | Keys.Alt | Keys.Q:
this.Parent.Text="<CTRL> + Alt + Q Captured";
break;
}
}
return base.ProcessCmdKey(ref msg,keyData);
}
Ignore msg.Msg, only look at keyData. And return true without calling base if you use the keystroke. Which simplifies it to:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Control | Keys.Alt | Keys.Q)) {
this.Parent.Text="<CTRL> + Alt + Q Captured";
return true;
}
return base.ProcessCmdKey(ref msg,keyData);
}
This should probably be an override of the form's method so you don't depend on the control having the focus. You'd use this.Text instead.