Gma.UserActivityMonitor 的 SendKeys 问题 (C#)

发布于 2024-11-16 00:08:18 字数 1391 浏览 6 评论 0原文

我正在创建一个自动化工具,用于使用 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);
}

I'm creating an automation tool for sending text, using SendKeys.Send(), to various other windows than the sending windows forms application. I'm setting the tool up to "type" with hotkey commands using the Gma.UserActivityMonitor library available here: http://www.codeproject.com/KB/cs/globalhook.aspx

My problem is that when I use the hotkey to handle the keypress to trigger the typing, sometimes it still allows the keypress to slip through.

I attempted to spin up a new thread and use sendkeys there, but I got an error that I should use SendKeys.SendWait due to the target application not accepting input.

So my question can be answered in one of two ways:

1) In which direction can I look for more information about multi-threading and using sendkeys?

2) How can I make sure that the trigger keypress is correctly inhibited by the hookmanager in the library?

I'm allowing the user to build a dictionary that's keyed by different letters, so different letters send different strings to the target application.
Relevant Code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

混吃等死 2024-11-23 00:08:18

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

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

Have you tried SendKeys.SendWait(string keys) instead of Thread.Sleep(10)?

SendWait waits until the sent keys are cleared before proceeding.

请叫√我孤独 2024-11-23 00:08:18

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

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

Here is a static thread class that I wrote that may be useful in this case:

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();
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文