wxPython:全局热键循环切换
我正在尝试创建一个热键切换(f12),该热键切换将在按下一次时打开循环,然后在再次按下时关闭该循环。循环是打开时每 0.5 秒单击一次鼠标。我在 wxpython 网站上找到了热键的配方,我可以打开循环,但无法找到关闭它的方法。我尝试创建一个单独的密钥来将其关闭,但没有成功。 鼠标模块模拟 1 次鼠标左键单击。
这是我当前的代码:
import wx, win32con, mouse
from time import sleep
class Frameclass(wx.Frame):
def __init__(self, parent, title):
super(Frameclass, self).__init__(parent, title=title, size=(400, 200))
self.Centre()
self.Show()
self.regHotKey()
self.Bind(wx.EVT_HOTKEY, self.handleHotKey, id=self.hotKeyId)
self.regHotKey2()
self.Bind(wx.EVT_HOTKEY, self.handleHotKey2, id=self.hotKeyId2)
def regHotKey(self):
"""
This function registers the hotkey Alt+F12 with id=150
"""
self.hotKeyId = 150
self.RegisterHotKey(self.hotKeyId,win32con.MOD_ALT, win32con.VK_F12)#the key to watch for
def handleHotKey(self, evt):
loop=True
print('clicks on')
while loop==True:
#simulated left mouse click
mouse.click()
sleep(0.50)
x=self.regHotKey2()
print(x)
if x==False:
print('Did it work?')
break
else:
pass
----------------------第二个按键热键--- -----
def regHotKey2(self):
self.hotKeyId2 = 100
self.RegisterHotKey(self.hotKeyId2,win32con.MOD_ALT, win32con.VK_F11)
def handleHotKey2(self, evt):
return False
loop=False
print(loop)
如果 name=='main':
showytitleapp=wx.App()
#gotta have one of these in every wxpython program apparently
Frameclass(None, title='Rapid Clicks')
showytitleapp.MainLoop()
#infinite manloop for catching all the program's stuff
I'm trying to create a hotkey toggle(f12) that will turn on a loop when pressed once then turn that loop off when pressed again. The loop is a mouse click every .5 seconds when toggled on. I found a recipe for a hot keys on the wxpython site and I can get the loop to turn on but can't figure a way to get it to turn off. I tried created a separate key to turn it off without success.
The mouse module simulates 1 left mouse click.
Here's my current code:
import wx, win32con, mouse
from time import sleep
class Frameclass(wx.Frame):
def __init__(self, parent, title):
super(Frameclass, self).__init__(parent, title=title, size=(400, 200))
self.Centre()
self.Show()
self.regHotKey()
self.Bind(wx.EVT_HOTKEY, self.handleHotKey, id=self.hotKeyId)
self.regHotKey2()
self.Bind(wx.EVT_HOTKEY, self.handleHotKey2, id=self.hotKeyId2)
def regHotKey(self):
"""
This function registers the hotkey Alt+F12 with id=150
"""
self.hotKeyId = 150
self.RegisterHotKey(self.hotKeyId,win32con.MOD_ALT, win32con.VK_F12)#the key to watch for
def handleHotKey(self, evt):
loop=True
print('clicks on')
while loop==True:
#simulated left mouse click
mouse.click()
sleep(0.50)
x=self.regHotKey2()
print(x)
if x==False:
print('Did it work?')
break
else:
pass
---------------------second keypress hotkey--------
def regHotKey2(self):
self.hotKeyId2 = 100
self.RegisterHotKey(self.hotKeyId2,win32con.MOD_ALT, win32con.VK_F11)
def handleHotKey2(self, evt):
return False
loop=False
print(loop)
if name=='main':
showytitleapp=wx.App()
#gotta have one of these in every wxpython program apparently
Frameclass(None, title='Rapid Clicks')
showytitleapp.MainLoop()
#infinite manloop for catching all the program's stuff
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
loop
变量的局部作用域位于handleHotKey
内部。由于regHotKey2
绑定到handleHotKey2
(这是一个不同的侦听器),因此它生成的事件永远不会影响handleHotKey
内的循环。除此之外,handleHotKey2
的第一行是一个返回值,它会在执行下面两行之前退出函数。出于好奇, x=self.regHotKey2(); 的输出是什么? print(x) 产生什么?
尝试在类级别而不是函数级别定义循环变量 -
然后修改处理程序中的那个循环 -
请尝试这个并告诉我这是否有效。
也许这会从同一个热键切换循环......
Your
loop
variable is locally scoped inside ofhandleHotKey
. BecauseregHotKey2
is bound tohandleHotKey2
, which is a different listener, the event it generates will never affect the loop withinhandleHotKey
. Besides that, the first line ofhandleHotKey2
is a return value, which will quit the function before the following two lines are executed.Out of curiousity, what output does
x=self.regHotKey2(); print(x)
produce?Try defining your loop variable at the class level instead of the function level -
and then modifying that loop in your handlers -
Please try this and tell me if this works.
And maybe this will toggle the loop from the same hotkey...
我将这些模块添加到我的导入中:
模块
在我的类下添加了这些
I had to import sys and add sys.executable to my popen otherwise i got an error everytime i tried to open another python program.
我使用 self.PID.terminate() 终止被调用的循环进程,否则它会打开单独的 123.py 循环文件。
单独的 123.py 文件包含以下代码:
What this basically does is call upon a separate python file with the subprocess module when the hotkey is pressed, then kills that process when the hotkey is pressed again. Thanks for the help.
I added these modules to my import:
subprocess,signal,sys
under my class I added these
I had to import sys and add sys.executable to my popen otherwise i got an error everytime i tried to open another python program.
I kill the called loop process with self.PID.terminate(), otherwise it opens the seperate 123.py loop file.
The seperate 123.py file contains this code:
What this basically does is call upon a separate python file with the subprocess module when the hotkey is pressed, then kills that process when the hotkey is pressed again. Thanks for the help.