SendKeys 在线程中运行 2 次后失败

发布于 2024-08-16 08:51:01 字数 708 浏览 2 评论 0原文

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 技术交流群。

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

发布评论

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

评论(2

暮色兮凉城 2024-08-23 08:51:01

看来SendKeys是线程安全的。以下代码适用于 Vista - Python 2.6

class Auto(threading.Thread):
    def run(self):
        SendKeys.SendKeys("#",pause=0.1);
        print('Sent');
        exit();

for i in xrange(30):
    Auto().start()

也许问题来自于 PyHook 或 Windows PumpMessage 机制的一些干扰。您是否尝试过将 SendKeys 部分放在不同的进程中而不是放在不同的线程中?

我希望它有帮助

It seems that SendKeys is thread safe. The following code works on Vista - Python 2.6

class Auto(threading.Thread):
    def run(self):
        SendKeys.SendKeys("#",pause=0.1);
        print('Sent');
        exit();

for i in xrange(30):
    Auto().start()

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

李白 2024-08-23 08:51:01

我对这个程序不太确定,但是如果你把 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();?

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