wxPython:全局热键循环切换

发布于 2024-12-07 02:51:41 字数 2073 浏览 1 评论 0原文

我正在尝试创建一个热键切换(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=='ma​​in':

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

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

发布评论

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

评论(2

那请放手 2024-12-14 02:51:41

您的 loop 变量的局部作用域位于 handleHotKey 内部。由于 regHotKey2 绑定到 handleHotKey2(这是一个不同的侦听器),因此它生成的事件永远不会影响 handleHotKey 内的循环。除此之外,handleHotKey2的第一行是一个返回值,它会在执行下面两行之前退出函数。

出于好奇, x=self.regHotKey2(); 的输出是什么? print(x) 产生什么?

尝试在类级别而不是函数级别定义循环变量 -

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

然后修改处理程序中的那个循环 -

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

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

请尝试这个并告诉我这是否有效。

也许这会从同一个热键切换循环......

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

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
还在原地等你 2024-12-14 02:51:41

我将这些模块添加到我的导入中:

模块

在我的类下添加了这些


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

def openClicker(自身,事件): 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)

我使用 self.PID.terminate() 终止被调用的循环进程,否则它会打开单独的 123.py 循环文件。

单独的 123.py 文件包含以下代码:


import mouse
from time import 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.

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.

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