- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在用 python 2.7 编写一个工具,记录用户按下键盘或鼠标按钮的次数。点击量将显示在屏幕左上角的小黑框中。即使另一个应用程序处于事件状态,该程序也会记录点击次数。
它工作正常,除非我将鼠标移到框上。然后鼠标会卡住几秒钟,然后程序再次运行。如果我再次将鼠标移到该框上,鼠标将再次卡住,但这次程序崩溃了。
我尝试注释掉 pumpMessages() 然后程序运行。这个问题看起来很像这个问题 pyhook+tkinter=crash , 但那里没有给出解决方案。
其他答案表明,在 python 2.6 中同时使用 wx 和 pyhook 时,dll 文件存在错误。我不知道这在这里是否相关。
我自己的想法是,这可能与并行运行的两个事件循环有关。我读到 tkinter 不是线程安全的,但我看不出如何让这个程序在单个线程中运行,因为我需要同时运行 pumpmessages() 和 mainloop()。
总结一下:为什么我的程序在鼠标悬停时卡住?
import pythoncom, pyHook, time, ctypes, sys
from Tkinter import *
from threading import Thread
print 'Welcome to APMtool. To exit the program press delete'
## Creating input hooks
#the function called when a MouseAllButtonsUp event is called
def OnMouseUpEvent(event):
global clicks
clicks+=1
updateCounter()
return True
#the function called when a KeyUp event is called
def OnKeyUpEvent(event):
global clicks
clicks+=1
updateCounter()
if (event.KeyID == 46):
killProgram()
return True
hm = pyHook.HookManager()# create a hook manager
# watch for mouseUp and keyUp events
hm.SubscribeMouseAllButtonsUp(OnMouseUpEvent)
hm.SubscribeKeyUp(OnKeyUpEvent)
clicks = 0
hm.HookMouse()# set the hook
hm.HookKeyboard()
## Creating the window
root = Tk()
label = Label(root,text='something',background='black',foreground='grey')
label.pack(pady=0) #no space around the label
root.wm_attributes("-topmost", 1) #alway the top window
root.overrideredirect(1) #removes the 'Windows 7' box around the label
## starting a new thread to run pumMessages() and mainloop() simultaniusly
def startRootThread():
root.mainloop()
def updateCounter():
label.configure(text=clicks)
def killProgram():
ctypes.windll.user32.PostQuitMessage(0) # stops pumpMessages
root.destroy() #stops the root widget
rootThread.join()
print 'rootThread stopped'
rootThread = Thread(target=startRootThread)
rootThread.start()
pythoncom.PumpMessages() #pump messages is a infinite loop waiting for events
print 'PumpMessages stopped'
最佳答案
我已经用多处理解决了这个问题:
主进程处理 GUI (MainThread) 和一个线程,该线程使用来自第二个进程的消息
子进程 Hook 所有鼠标/键盘事件并将它们推送到主进程(通过队列对象)
关于multithreading - 使用 tkinter + pyhook 时卡住。两个事件循环和多线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12278570/
我正在尝试在 pycharm 中安装 pyHook 包,但出现标题错误。我已经使用 pip install pyHook-1.5.1-cp37-cp37m-win_amd64.whl 在 cmd 中成
我正在使用 pyhook 和 pyhk 来映射 Windows XP 机器上的击键,它工作正常,除非击键(例如,ctrl+z)已经存在于应用程序中。在这种情况下,ctrl+z 将传递给应用程序并触发已
我运行了这个 PyHook 示例代码: import pythoncom, pyHook def OnKeyboardEvent(event): print 'MessageName:
这是我在这个网站上的第一个问题,所以我希望我的格式正确。我对编程也很陌生,所以请原谅我缺乏知识,谢谢! 我正在尝试像这样安装 pyHook,但我不断收到此错误消息: C:\Python27>py -2
我是 cx_Freeze 的新手,但是当我尝试使用 pyHooks 时,它不起作用。这是我的 setup.py 代码: from cx_Freeze import setup, Executable
我为键盘记录器创建了一个线程,该线程与另一个产生一些声音的线程并行记录(我想捕捉 react 时间)。 不幸的是,尽管我调用了 killKey() 并打印了“invoked killkey()”,但线
我安装了pyHook并成功将处理程序附加到键盘事件,但现在我需要查明用户是否正在使用英文布局或其他布局进行键入。我在事件对象中找不到此信息。 如何在 Windows 上找到焦点窗口中的输入语言?我尝试
我正在尝试制作一个简单的程序来记录所有击键,但我很难弄清楚如何让它工作。我似乎无法在任何地方找到这个问题的解决方案。 我正在使用 pyHook 来获取击键,并且创建了一个名为 OnKeyboardEv
我正在尝试在 python 中使用 pyhook 创建一个全局热键,它应该只在按下 alt 键时工作。 这是来源: import pyHook import pythoncom hm = pyHook
我有一个程序可以使用 pyHook 捕获所有按键,然后运行一些函数。 我注意到一段时间后(随机持续时间),程序停止接收按键触发,即使我正在按键? pyHook不稳定吗? 我不会更改按下的键或过早按下它
这里确实有更多问题。基于pyHook's tutorial ,函数中的 .HookManager().OnMouseEvent 事件变量具有 .Injected 属性。我没找到相关信息,有谁知道这是什
import win32api import win32console import win32gui import pythoncom, pyHook , sys, time , os , thre
我正在尝试下载 pyHook 模块以完成学校作业。我尝试寻找问题的答案,但还没有找到。当我尝试 pip install python-pyHook 时,我得到以下信息: pip install pyt
我正在使用 teamplayer ,它可以让您将更多鼠标连接到您的计算机以同时使用。我还使用 pyHook 来捕获鼠标事件,代码如下: import pyHook import pythoncom d
我一直在用 Python 编写一个基本的键盘记录器。在检查我的日志时,我发现我使用 event.Time 记录的时间戳不是 Unix 时间戳。它是什么类型的时间戳,如何将其转换为可读格式? impor
我编写了一个脚本,使用 pyHook 按下按键时移动鼠标。问题是,在 6 个按键事件之后,脚本停止拾取按键,需要从任务管理器结束。 我在 Windows 7 机器上使用 python 2.7。我还没有
我想检查我写入 key 的文件是否超过 2Kb 。如果它超过 2KB,那么我需要创建另一个文件并停止在这个文件中写入。我是 python 初学者请帮助我。请指出在哪里可以使用代码来检查文件是否超过 2
我有一个 Tkinter GUI 应用程序,需要在按下按钮时隐藏它。我不能假设应用程序将具有焦点,因此我实现了键盘记录器样式的 pyHook。但是,每当我从 pyHook 启动的函数调用withdra
我希望我的 (Python/Windows) GUI GTK 窗口在按键时关闭。然而,没有任何反应。我是初学者,我在谷歌中寻找答案。我的英语也不是很专业。请耐心等待我。 import pygtk im
这是我的脚本: import pyHook import pythoncom hookManager = pyHook.HookManager() def onKeyboardEvent(event)
我是一名优秀的程序员,十分优秀!