【问题标题】:SendKeys Issue with Gma.UserActivityMonitor (C#)Gma.UserActivityMonitor (C#) 的 SendKeys 问题
【发布时间】:2011-06-15 04:43:16
【问题描述】:

我正在创建一个自动化工具,用于使用SendKeys.Send() 将文本发送到除发送窗口窗体应用程序之外的各种其他窗口。我正在使用此处提供的 Gma.UserActivityMonitor 库使用热键命令将工具设置为“类型”:http://www.codeproject.com/KB/cs/globalhook.aspx

我的问题是当我使用热键处理按键触发打字时,有时它仍然允许按键滑过。

我试图启动一个新线程并在那里使用 sendkeys,但由于目标应用程序不接受输入,我收到了一个错误,我应该使用 SendKeys.SendWait

所以我的问题可以通过以下两种方式之一来回答:

1) 我可以从哪个方向查找有关多线程和使用 sendkeys 的更多信息?

2) 如何确保库中的 hookmanager 正确禁止了触发按键?

我允许用户构建一个由不同字母作为键的字典,因此不同的字母会向目标应用程序发送不同的字符串。 相关代码:

private void HookManager_KeyPress(object sender, KeyPressEventArgs e)
{
    //code to generate tValue from the pressed key
    if (tAutoTyperDictionary.ContainsKey(tValue))
    {
        //Should prevent the key from being passed to the window
        //works sometimes
        e.Handled = true;
        AutoType();
    }
}

private void AutoType()
{
    int tCount = 0;
    string tLine = tAutoTyperDictionary[tCurrentAutotypeKey];

    //I remove the listener to prevent it calling itself
    HookManager.KeyPress -= new KeyPressEventHandler(HookManager_KeyPress);
    while (tCount < tLine.Length)
    {
        SendKeys.Send(tLine[tCount].ToString());
        Thread.Sleep(10);
        tCount++;
    }

    HookManager.KeyPress += new KeyPressEventHandler(HookManager_KeyPress);
}

【问题讨论】:

    标签: c# winforms multithreading sendkeys


    【解决方案1】:

    您是否尝试过 SendKeys.SendWait(string keys) 而不是 Thread.Sleep(10)?

    SendWait 等到发送的密钥被清除后再继续。

    【讨论】:

      【解决方案2】:

      这是我编写的一个静态线程类,在这种情况下可能有用:

      public static class threadClass
          {
              private static Thread _thread;
              public static Thread thread
              {
                  get { return _thread; }
              }
              public static void startThread()
              {
                  _thread = new Thread(delegate() { });
                  _thread.Start();
              }
          }
      

      【讨论】:

      • 我希望能在本周末查看您的建议。感谢您提供帮助。 =)
      猜你喜欢
      • 1970-01-01
      • 2018-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-09
      • 1970-01-01
      相关资源
      最近更新 更多