首页 > 解决方案 > Pynput mac 计算机未检测到密钥的释放

问题描述

我有以下代码:

# importing time and threading
import time
import threading
from pynput.mouse import Button, Controller
import random
  
# pynput.keyboard is used to watch events of 
# keyboard for start and stop of auto-clicker
from pynput.keyboard import KeyCode
from pynput.keyboard import Listener as KeyListener
from pynput.mouse import Listener as MouseListener
  
  
# four variables are created to 
# control the auto-clicker
delay = [20,30] # average cps for delay of [a,b] is a+b-10
button = Button.left
stop_key = KeyCode(char=']')
  
# threading.Thread is used 
# to control clicks
class ClickMouse(threading.Thread):
    
  # delay and button is passed in class 
  # to check execution of auto-clicker
    def __init__(self, delay, button):
        super(ClickMouse, self).__init__()
        self.delay = delay
        self.button = button
        self.running = False
        self.program_running = True
  
    def start_clicking(self):
        self.running = True
  
    def stop_clicking(self):
        self.running = False
  
    def exit(self):
        self.stop_clicking()
        self.program_running = False
  
    # method to check and run loop until 
    # it is true another loop will check 
    # if it is set to true or not, 
    # for mouse click it set to button 
    # and delay.
    def run(self):
        while self.program_running:
            p=time.time()
            k=1/random.randrange(delay[0],delay[1])
            v=(delay[0]+delay[1])/2
            while self.running:
                if time.time()-p>=0.5:
                    p=time.time()
                    k=1/random.randrange(delay[0],delay[1])
                mouse.click(self.button)
                time.sleep(k)
            time.sleep(0.1)
  
  
# instance of mouse controller is created
mouse = Controller()
click_thread = ClickMouse(delay, button)
click_thread.start()
  
  
# on_press method takes 
# key as argument
def on_press(key):
    if key == stop_key:
        click_thread.exit()
        keyListener.stop()
        mouseListener.stop()
isPressed=0
mBut=button
def on_click(x, y, button, pressed):
    global isPressed
    if (button==mBut and pressed):
        isPressed=isPressed+1
        if (click_thread.running):
            pass
        else:
            #click_thread.start_clicking()
            pass
    elif (button==mBut and not pressed):
        print("rel: "+str(isPressed))
        isPressed=isPressed-1
        if isPressed<=0:
            isPressed=0
            click_thread.stop_clicking()
        
    
# Collect events until released
mouseListener=MouseListener(on_click=on_click)
print("DONT PUT MOUSE ON IDE")
print(f"Starting in 3 seconds, press {stop_key} to stop.")
time.sleep(3)
mouseListener.start()
keyListener=KeyListener(on_press=on_press)
keyListener.start()

keyListener.join()

当我在 Windows 上按住左键单击时,它会像预期的那样不断地左键单击。但是,当我在 mac 上使用这个程序时,它无法检测到我的触摸板的释放并不断点击。我尝试降低 cps 的速度,但是当我松开鼠标时计算机没有检测到。有什么解决办法吗?也许要使用另一个模块?

标签: pythonmacosmousepynput

解决方案


推荐阅读