【发布时间】: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