简体   繁体   中英

Double click when clicking once pynput

Trying to do a program that double clicks for you when you click the left mouse button once with pynput. I have the following code, but if I run the code, my mouse glitches out and stops working.

from pynput.mouse import Listener, Button, Controller

mouse = Controller()

def on_click(x, y, button, pressed):
    if pressed == True:
        mouse.click(Button.left, 2)
    else:
        pass

with Listener(on_click=on_click) as listener:
    listener.join()

Also in addition of this, how would the implementation of pressing "F10" enables that 1 click acts like double click and pressing "F10" again would disable it, so 1 click would act like 1 click be possible?

Oh,I maybe find your problem, Two probable causes:

  1. In your script,when your press the mouse button.It will call function on_click .Then it will mouse.click(Button.left, 2) .But this code will also call on_click .So it will be a endless loop.Finally,you will find your mouse will be not responding.So I think you should use another way to do that.
  2. In pynput official document,It seems it can be used in macOS(maybe windows couldn't use it.And I also found if I only use mouse.click(Button.left, 2) in my PC,my python will be not responding.(It couldn't be stopped).Maybe you should just use .press and .release directly): 在此处输入图像描述

Also in addition of this, how would the implementation of pressing "F10" enables that 1 click acts like double click and pressing "F10" again would disable it.

So this seems a switch,you can use a global variable to do that.There is a minimal example of use pynput to do a switch.(This will don't print Mode is on if you don't press F10,and it won't print it after you press F10 again).

# import win32api,win32con
from pynput.mouse import Controller
from pynput import keyboard
from pynput.keyboard import Key

mouse = Controller()
Mode = False

def on_press(key):
    global Mode
    if key == Key.f10:
        if Mode:
            Mode = False
        else:
            Mode = True

listener = keyboard.Listener(on_press=on_press)
listener.start()
while True:
    if Mode:
        print("Mode is on")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM