October 22, 2024
Chicago 12, Melborne City, USA
python

PYTHON Execute code only while Mouse right is held


Is there any way to execute python code only while Mouse button right is held? If Mouse right becomes up code should interrupt immediately.

I tried to use pynput library but pressed parameter does not work on while loop. Whithout while it works right: prints correct message on Mouse right down and Mouse right up events. But whith while loop it starts endless loop and does not stop when Mouse button up becomes Up.

        from pynput import mouse

        def on_click(x, y, button, pressed):
            #keyboard = Controller()
            #while button == mouse.Button.right and pressed:
            if button == mouse.Button.right:
                if pressed:
                    print(pressed)
                    while pressed:  # Makes the problem
                        # Place your code here
                        print("Executing code...") # Endless Print here
              
                else:
                    print(pressed)
        



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video