Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Python catch mouse click in pure text, no graphics
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.
what OS?
The OS is Rasbian on RaspberryPi 3, using Python
Maybe check pynput
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)
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.
maybe something like this:
from pynput import mouse
from functools import partial

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

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

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

with mouse.Listener(on_click=bar) as listener:
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
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:
             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)

if __name__ == '__main__':

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Slide show with mouse click pausing aantono 1 178 Jan-28-2020, 04:25 AM
Last Post: Larz60+
  mouse 0.7.0 - mouse polling hate 125-1000hz penahuse 1 249 Dec-06-2019, 09:51 PM
Last Post: Larz60+
  Python 3+ kybd and mouse control keycodes ineuw 5 536 Aug-20-2019, 07:51 AM
Last Post: ineuw
  catch input type error mcmxl22 5 488 Aug-11-2019, 07:33 AM
Last Post: wavic
  Python Graphics Help With Entries BadenJaden 1 345 Jul-24-2019, 10:36 PM
Last Post: Yoriz
  Why is left mouse click not working? yeto 3 868 Jul-15-2019, 05:23 AM
Last Post: Yoriz
  How to catch string between two strings SriMekala 3 582 May-24-2019, 03:05 AM
Last Post: heiner55
  How to get mouse coordinates on click and release OhNoSegFaultAgain 1 547 May-17-2019, 06:56 PM
Last Post: buran
  from graphics import * jmaloney413 2 1,124 Oct-18-2018, 08:22 AM
Last Post: perfringo
  Code runs as pure python but not jupyter notebook miner_tom 3 914 Aug-22-2018, 04:00 PM
Last Post: miner_tom

Forum Jump:

Users browsing this thread: 1 Guest(s)