简体   繁体   中英

send keys in WndProc method in C#

I override WndProc method in windows form and use SendKeys.

I wanna send "CTRL + N" when the bool value if true and wanna send just "n" when bool value is false.

My problem is here when i send "n" send unlimited "n".

Is the any way to fix this ?

   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");
  }

Boolean value is added in your class. If the key is sent I am changing it to true. So that the keypress event is called only once.

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;

   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM