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
#1
Hi,
I wish to catch mouse click (all buttons) in a pure text, non-graphic program.
Example: I have a While loop that prints a list. I wish to exit or do something in the loop on a mouse click.
I can use tkinter, uinput or whatever.
thx
Reply
#2
what OS?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
The OS is Rasbian on RaspberryPi 3, using Python
Reply
#4
Maybe check pynput
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Yes, pynput seems to be a simple mouse event-catcher.
I tested it out in a very simple program:
from pynput import mouse

def on_click(x, y, button, pressed):
    print(button, pressed, listener)

listener = mouse.Listener(on_click=on_click)
listener.start()
I will need more advice to understand how to embed the mouse click event inside my while loop.
While the loop normally runs continuously, I need the option to hold the loop by its run conditions and advance one round per mouse click.
Reply
#6
maybe something like this:
from pynput import mouse
from functools import partial


def foo():
    i = 0
    while True:
        i += 1
        print('click: {}'.format(i))
        yield


def on_click(x, y, button, pressed, foo):
    if pressed:
        next(foo)


bar = partial(on_click, foo=foo())

with mouse.Listener(on_click=bar) as listener:
    listener.join()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Thank you for your reply.
The suggested works perfectly, but unfortunately it does not fully resolve my problem. I need the click to be within the while loop.
I have a while loop that runs by reading records (time and data) from a file. In normal mode ('auto') I want it to run until file ends, with the option to run each record by mouse click.

Here is a simplified snippet of what I actually need, and am unsuccessful in achieving.

class Convert():

    def __init__(self):
        while dataCounter < 100:
         if auto == 0:
              runTime = 0  
         # runTime = 0 will hold the next if and while loops and will wait for mouse click 

         if runTime >= dataTime or MOUSE_CLICK:

             do something................(sending data by I2C to a slave).

             dataCounter += 1
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  mouse move event/cinfiguration ttk/python janeik 4 984 Jul-03-2023, 05:30 PM
Last Post: deanhystad
  Non repetitive mouse click Sartre 5 1,377 Apr-22-2023, 06:46 PM
Last Post: deanhystad
  try catch not working? korenron 2 811 Jan-15-2023, 01:54 PM
Last Post: korenron
  how to mouse click a specific item in pygame? Frankduc 5 1,665 May-03-2022, 06:22 PM
Last Post: Frankduc
  Multiprocessing queue catch get timeout Pythocodras 1 2,240 Apr-22-2022, 06:01 PM
Last Post: Pythocodras
  help with python mouse script z4rxxxx 0 1,104 Jan-15-2022, 04:39 PM
Last Post: z4rxxxx
  twisted: catch return from sql wardancer84 0 1,501 Sep-08-2021, 12:38 PM
Last Post: wardancer84
  Python graphics kaltenherz 1 1,681 Sep-05-2021, 05:19 PM
Last Post: jefsummers
  how to catch schema error? maiya 0 1,808 Jul-16-2021, 08:37 AM
Last Post: maiya
  is this a good way to catch exceptions? korenron 14 4,593 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