开发者

wxPython: global hotkey loop toggle

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开发者_如何学JAVA()
            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 loop variable is locally scoped inside of handleHotKey. Because regHotKey2 is bound to handleHotKey2, which is a different listener, the event it generates will never affect the loop within handleHotKey. Besides that, the first line of handleHotKey2 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 -

def __init__(self, parent, title):
    ... your original stuff ...
    self.clicker_loop = False

and then modifying that loop in your handlers -

def handleHotKey(self, evt):
    self.clicker_loop = True
    while self.clicker_loop:
        ... do the thing ...

def handleHotKey2(self, evt):
    self.clicker_loop = False

Please try this and tell me if this works.

And maybe this will toggle the loop from the same hotkey...

def handleHotKey(self, evt):
    if self.clicker_loop:
        self.clicker_loop = False
    else:
        self.clicker_loop = True


I added these modules to my import:

subprocess,signal,sys

under my class I added these


def init(self, parent, title):
    self.clicker_loop = False
    self.PID=1

def openClicker(self,event): self.PID=subprocess.Popen([sys.executable, "123.py"])

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.


    def handleHotKey(self, evt):
        if self.clicker_loop:
            self.clicker_loop = False
            print(self.clicker_loop)
            self.PID.terminate()
        else:
            self.clicker_loop = True
            print(self.clicker_loop)
            self.openClicker(self)

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:


import mouse
from time import sleep

while True: mouse.click() sleep(1)

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜