簡體   English   中英

在 C# 中的 WndProc 方法中發送密鑰

[英]send keys in WndProc method in C#

我在 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;

   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM