a-bad-js-developer
a-bad-js-developer

Reputation: 85

run a loop while mouse is down

i have this code:

import pyautogui
from pynput.mouse import Listener
# import threading
import time

mouseIsDown = False

def on_click(*args):
    if args[-1]:
        if args[-2].name == "left":
            mouseIsDown = True
            print("down")

    elif not args[-1]:
        if args[-2].name == "left":
            mouseIsDown = False
            print("up")

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

while mouseIsDown:
    print("hello!")
    time.sleep(0.001)

its supposed to run the loop every .001 seccond while a mouse button is down, yet its doing absolutely nothing.

any suggestions?

Upvotes: 0

Views: 56

Answers (1)

jizhihaoSAMA
jizhihaoSAMA

Reputation: 12672

Run this threading in a non-blocking way, You may need something like this(Read it in document):

import pyautogui
from pynput.mouse import Listener
# import threading
import time

mouseIsDown = False

def on_click(*args):
    global mouseIsDown
    if args[-1]:
        if args[-2].name == "left":
            mouseIsDown = True
            print("down")

    elif not args[-1]:
        if args[-2].name == "left":
            mouseIsDown = False
            print("up")

listner = Listener(on_click=on_click)
listner.start()

while True:
    if mouseIsDown:
        time.sleep(0.5) # time to sleep
        print("hello!")

Upvotes: 1

Related Questions