Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中的WndProc方法中发送密钥#_C#_Winforms - Fatal编程技术网

C# 在C中的WndProc方法中发送密钥#

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

我在windows窗体中重写WndProc方法并使用SendKeys

如果bool值为true,我想发送“CTRL+N”,如果bool值为false,我只想发送“N”

我的问题是当我发送“n”时,发送无限的“n”

有办法解决这个问题吗

   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