SendKeys 在线程中运行 2 次后失败
Python 和 SendKeys
import SendKeys, threading, pyHook, pythoncom
class Auto(threading.Thread):
def run(self):
SendKeys.SendKeys("{ENTER}",pause=0.1);
print('Sent');
exit();
def OnKeyboardEvent(event):
if event.Ascii == 22:
Auto().start();
return True
hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()
pythoncom.PumpMessages()
由于某种原因,该程序在运行两次后失败,我不知道原因是什么。当您注释掉 SendKeys 部分时,程序运行正常,因此肯定是发送键有问题。
[编辑]另外,为了澄清,在 for i in range(0,100) 中运行 SendKeys.SendKeys(...) 是有效的,所以我认为这与线程有关。我以前从未编写过线程。另外,这只是一个复制问题的模型示例。
我在 windows 7、python2.6 上运行
[编辑]此外,程序不会“失败”,它只是冻结(该函数根本不运行,它只是坐在那里)
Python and SendKeys
import SendKeys, threading, pyHook, pythoncom
class Auto(threading.Thread):
def run(self):
SendKeys.SendKeys("{ENTER}",pause=0.1);
print('Sent');
exit();
def OnKeyboardEvent(event):
if event.Ascii == 22:
Auto().start();
return True
hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()
pythoncom.PumpMessages()
For some reason this program fails after running it exactly two times, I have no idea what the cause for this is. When you comment out the SendKeys part the program runs fine, so it has to be a problem with send keys.
[edit] Also, to clarify, running SendKeys.SendKeys(...) in a for i in range(0,100) works, so I assume it's something to do with the thread. I've never programmed threads before. Also this is just a mockup example to replicate the problem.
I'm running on windows 7, python2.6
[edit]Also, the program doesn't 'fail' it simply freezes (the function isn't run at all, it just sits there)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来SendKeys是线程安全的。以下代码适用于 Vista - Python 2.6
也许问题来自于 PyHook 或 Windows PumpMessage 机制的一些干扰。您是否尝试过将 SendKeys 部分放在不同的进程中而不是放在不同的线程中?
我希望它有帮助
It seems that SendKeys is thread safe. The following code works on Vista - Python 2.6
Maybe the problem comes from some interferences with PyHook or the Windows PumpMessage mechanism. Have you tried to put the SendKeys part in a different process rather than in a different thread?
I hope it helps
我对这个程序不太确定,但是如果你把
exit();
放在程序中间,它将完全退出程序。那么你可以尝试不使用
exit();
吗?I am not so sure with the program but if you put
exit();
in the middle of the program, It will quit program completely.So Could you try without
exit();
?