C# 在C中的WndProc方法中发送密钥#
我在windows窗体中重写WndProc方法并使用SendKeys 如果bool值为true,我想发送“CTRL+N”,如果bool值为false,我只想发送“N” 我的问题是当我发送“n”时,发送无限的“n” 有办法解决这个问题吗C# 在C中的WndProc方法中发送密钥#,c#,winforms,C#,Winforms,我在windows窗体中重写WndProc方法并使用SendKeys 如果bool值为true,我想发送“CTRL+N”,如果bool值为false,我只想发送“N” 我的问题是当我发送“n”时,发送无限的“n” 有办法解决这个问题吗 public ShiledMaker() { InitializeComponent(); this.KeyPreview = true; RegisterHotKey(Handle, (int)Keys
public ShiledMaker()
{
InitializeComponent();
this.KeyPreview = true;
RegisterHotKey(Handle, (int)Keys.N, 0, (int)Keys.N);
}
protected override void WndProc(ref Message xMessage)
{
base.WndProc(ref xMessage);
if (bool value)
SendKeys.Send("n");
else
SendKeys.SendWait("^n");
}
在类中添加布尔值。如果密钥已发送,我将其更改为true。这样按键事件只调用一次
class MyClass {
private bool keySent = false;
protected override void WndProc(ref Message xMessage)
{
if (keySent)
return;
base.WndProc(ref xMessage);
if (bool value)
SendKeys.Send("n");
else
SendKeys.SendWait("^n");
keySent = true;
}
}
为什么要从
WndProc
调用所有消息?因为我声明了很多类似于这些的快捷键。我为自己更改了一些快捷键,那么您应该仅在收到所需消息时调用该快捷键,而不是所有消息。若要处理快捷键,请覆盖ProcessCmdKey
。