Gma.UserActivityMonitor 的 SendKeys 问题 (C#)
我正在创建一个自动化工具,用于使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过 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.
这是我编写的一个静态线程类,在这种情况下可能有用:
Here is a static thread class that I wrote that may be useful in this case: