Python catch mouse click in pure text, no graphics - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Python catch mouse click in pure text, no graphics (/thread-12617.html) |
Python catch mouse click in pure text, no graphics - samtal - Sep-03-2018 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 RE: Python catch mouse click in pure text, no graphics - buran - Sep-03-2018 what OS? RE: Python catch mouse click in pure text, no graphics - samtal - Sep-03-2018 The OS is Rasbian on RaspberryPi 3, using Python RE: Python catch mouse click in pure text, no graphics - buran - Sep-03-2018 Maybe check pynput RE: Python catch mouse click in pure text, no graphics - samtal - Sep-06-2018 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. RE: Python catch mouse click in pure text, no graphics - buran - Sep-06-2018 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() RE: Python catch mouse click in pure text, no graphics - samtal - Sep-08-2018 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 RE: Python catch mouse click in pure text, no graphics - samtal - Sep-10-2018 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() |