Python Forum
Python catch mouse click in pure text, no graphics
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python catch mouse click in pure text, no graphics
#8
With buran's blessed directives I could easily solve the last question by myself. Sorry to have bothered others.

Coming from C, C# and new to Python, I totally ignored (forgot) the Python's Global declaration, and could not make a global variable to run inside function.
Once I recognized this, it was easy to solve.
In the code snippet, the while loop will normally (auto = 1) run by time, but if set auto = 0 it will run by mouse clicks.
#!/usr/bin/env python
from pynput import mouse
clicked = False

def foo():
        counter = 0
        auto = 0
        global clicked

        while counter < 10:
         if auto == 0:
              runTime = 0  # runTime = 0 will hold the next while loop waiting for mouse click 
 
         if runTime > counter or clicked:
 
             print(counter,"clicked")
             clicked = False
 
             counter += 1
             runTime = counter  #Time progress (example)

def on_click(x, y, button, pressed):
    global clicked
    if pressed:
       clicked = pressed

listener = mouse.Listener(on_click=on_click)
listener.start()

if __name__ == '__main__':
    foo()
Reply


Messages In This Thread
RE: Python catch mouse click in pure text, no graphics - by samtal - Sep-10-2018, 03:02 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  mouse move event/cinfiguration ttk/python janeik 4 1,056 Jul-03-2023, 05:30 PM
Last Post: deanhystad
  Non repetitive mouse click Sartre 5 1,415 Apr-22-2023, 06:46 PM
Last Post: deanhystad
  try catch not working? korenron 2 840 Jan-15-2023, 01:54 PM
Last Post: korenron
  how to mouse click a specific item in pygame? Frankduc 5 1,706 May-03-2022, 06:22 PM
Last Post: Frankduc
  Multiprocessing queue catch get timeout Pythocodras 1 2,305 Apr-22-2022, 06:01 PM
Last Post: Pythocodras
  help with python mouse script z4rxxxx 0 1,130 Jan-15-2022, 04:39 PM
Last Post: z4rxxxx
  twisted: catch return from sql wardancer84 0 1,524 Sep-08-2021, 12:38 PM
Last Post: wardancer84
  Python graphics kaltenherz 1 1,713 Sep-05-2021, 05:19 PM
Last Post: jefsummers
  how to catch schema error? maiya 0 1,856 Jul-16-2021, 08:37 AM
Last Post: maiya
  is this a good way to catch exceptions? korenron 14 4,706 Jul-05-2021, 06:20 PM
Last Post: hussaind

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020