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
Your
loopvariable is locally scoped inside ofhandleHotKey. BecauseregHotKey2is bound tohandleHotKey2, which is a different listener, the event it generates will never affect the loop withinhandleHotKey. Besides that, the first line ofhandleHotKey2is 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…