Python Global Capturing and Disposing Mouse Click Event - python

I want to create a tool, which allows me to send keys to active window using my mouse.
Let's say I want to send the key "A" when I click the left mouse button. But I also want to suppress / dispose that particular click event. So the target app will only feel the keyboard input "A", but not the left click of the mouse event.
With the following code I am able to see the mouse clicks. But I want to dispose / stop the event and not processed by the system.
from pynput import mouse
def do_something():
print("something")
def on_click(x, y, button, pressed):
print('{0} {1} at {2}'.format(button, 'Pressed' if pressed else 'Released', (x, y)))
if (button.value == 1):
do_something()
#suppress / dispose the click event...
# Collect events until released
with mouse.Listener( on_click=on_click ) as listener:
listener.join()
By the way I am using Ubuntu 20.04.
Thanks in advance.

I found a way that suppress all the click events.
from pynput import mouse
def do_something():
print("something")
def on_click(x, y, button, pressed):
print('{0} {1} at {2}'.format(button, 'Pressed' if pressed else 'Released', (x, y)))
if (button.value == 1):
do_something()
#suppress / dispose the click event...
# Collect events until released
with mouse.Listener( on_click=on_click, suppress=True ) as listener:
listener.join()
I am still looking for a solution to suppress a certain button click.

Related

How to stop triggering the key while holding the key in pynput?

I am using the pynput and winsound modules to make a program that makes the pc play the sound of the keys and mouse. The problem is that when I press a key and hold it down the key is triggered repeatedly, which causes the sound to play repeatedly in a loop until I release the key.
I followed this solution to create the program: Play Sound whenever key is pressed in Python
Then as I wanted to play the mouse sound as well, I found this article: How to Use pynput's Mouse and Keyboard Listener at the Same Time
So, I ended up with this code:
from pynput.mouse import Listener as MouseListener
from pynput.keyboard import Listener as KeyboardListener
import winsound
def on_press(key):
winsound.PlaySound("sound.wav", winsound.SND_ASYNC)
print("Key pressed: {0}".format(key))
def on_release(key):
print("Key released: {0}".format(key))
def on_click(x, y, button, pressed):
if pressed:
winsound.PlaySound("mouse_click.wav", winsound.SND_ASYNC)
print('Mouse clicked at ({0}, {1}) with {2}'.format(x, y, button))
else:
print('Mouse released at ({0}, {1}) with {2}'.format(x, y, button))
keyboard_listener = KeyboardListener(on_press=on_press, on_release=on_release)
mouse_listener = MouseListener(on_click=on_click)
keyboard_listener.start()
mouse_listener.start()
keyboard_listener.join()
mouse_listener.join()
Now, the mouse click does exactly what I want the keyboard to do! It plays the sound once while it is being held until I release it!
I just don't know how to make the keyboard play the sound only once until the key is released, like the mouse!!!
Here's an example that stores the pressed keys in a set so that a single pressed key will not be able to play a sound until it is released. If you press another key whilst holding the first, the sound will play again.
I've modified your code to use the sound aliases so you can test without having your sound.wav and mouse_click.wav files. I changed to use f-strings as they're more intuitive and I added support for Esc to exit the handlers. Please comment if you'd like any further elaboration.
from pynput.mouse import Listener as MouseListener
from pynput.keyboard import Listener as KeyboardListener
from pynput.keyboard import Key
import winsound
keys_pressed = set() # store which keys are pressed
def on_press(key):
# winsound.PlaySound("sound.wav", winsound.SND_ASYNC)
if key not in keys_pressed:
winsound.PlaySound("SystemAsterisk", winsound.SND_ALIAS | winsound.SND_ASYNC)
keys_pressed.add(key)
print(f"Key pressed: {key}")
if key == Key.esc:
mouse_listener.stop() # stop the mouse listener as well
winsound.PlaySound("SystemExit", winsound.SND_ALIAS | winsound.SND_ASYNC)
return False # Stop listener
def on_release(key):
print(f"Key released: {key}")
try:
keys_pressed.remove(key)
except KeyError:
pass # started with key pressed?
def on_click(x, y, button, pressed):
if pressed:
# winsound.PlaySound("mouse_click.wav", winsound.SND_ASYNC)
winsound.PlaySound("SystemHand", winsound.SND_ALIAS | winsound.SND_ASYNC)
print(f"Mouse clicked at ({x}, {y}) with {button}")
else:
print(f"Mouse released at ({x}, {y}) with {button}")
keyboard_listener = KeyboardListener(on_press=on_press, on_release=on_release)
mouse_listener = MouseListener(on_click=on_click)
keyboard_listener.start()
mouse_listener.start()
keyboard_listener.join()
mouse_listener.join()
You may instead want to try the flag winsound.SND_NOSTOP but that will only stop repeated sounds while any other sound is playing so the sounds won't overlap. You'll also need to catch the RuntimeError that is thrown if SND_NOSTOP is set and PlaySound() is called when a sound is already playing.
could possibly set up a variable that would store whether you have played the sound on the click, and would prevent playing another sound until you have released, and the variable is reset.
canPlaySoundFromPress = True
def onPress(...):
if canPlaySoundFromPress:
winsound.PlaySound("sound.wav", winsound.SND_ASYNC)
canPlaySoundFromPress = False
print("Key pressed: {0}".format(key))
def onRelease(...):
canPlaySoundFromPress = True
print("Key released: {0}".format(key))

Why do I always have the error message Controller has no attribute is_pressed?

I've been coding a very simple autoclicker. My autoclicker works just fine, but the only way to kill it is forcefully shut down my computer because it clicks so fast I can't access my taskbar. I could make it slower, but I'd prefer a way for the user to close the autoclicker with the press of a button. I've tried if keyboard.is_presssed('q'): break but I always get the error message AttributeError: 'Controller' object has no attribute 'is_pressed'. Did you mean: 'alt_pressed'? I expected my code to break the loop when I press q, but instead I get an error message. The error message will also pop up without the pressing of q. My code as of now is:
from pynput.mouse import Button, Controller
from pynput.keyboard import Key, Controller
import time
keyboard = Controller()
mouse = Controller()
while True:
time.sleep(10)
mouse.click(Button.left)
if keyboard.is_pressed('q'):
break
pynput doesn't have is_pressed() - I don't know where you find it.
You should rather use pynput.keyboard.Listener to run code when q is pressed and set some variable - q_is_pressed = True - or run code which ends program.
You can't have two objects with the same name Controller.
One Controller replaces other Controller and later you use the same Controller to create mouse and keyboard - and this makes problem.
You have to use pynput.mouse.Controller and pynput.keyboard.pynput.Controller
import pynput
from pynput.mouse import Button
from pynput.keyboard import Key
import time
keyboard = pynput.keyboard.Controller()
mouse = pynput.mouse.Controller()
while True:
time.sleep(10)
mouse.click(Button.left)
#if keyboard.is_pressed('q'): # <-- this will need Listener()
# break
EDIT:
To end code when q was pressed you have to use Listener
For example:
import pynput
from pynput.mouse import Button
import time
mouse_controller = pynput.mouse.Controller()
def on_press(key):
print('pressed:', key)
if str(key) == "'q'": # it has to be with `' '` inside `" "`
# Stop listener
print("exit listener")
return False # `False` ends listener
with pynput.keyboard.Listener(on_press=on_press) as keyboard_listener:
while keyboard_listener.is_alive():
time.sleep(10)
mouse_controller.click(Button.left)
print('clicked')

Question about mouse and keyboard events in python

from pynput.keyboard import Key, Controller as KeyboardController
from pynput.mouse import Button, Controller as MouseController
from pynput.keyboard import Key, Listener
keyboard = KeyboardController()
mouse = MouseController()
def on_press(key):
if key.char == 'q':
print(mouse.position)
return True
# Collect events until released
with Listener(
on_press=on_press) as listener:
listener.join()
I made this simple script to return the mouse's position if I press the letter q. It works fine until I press something that is not a char, for example, the Enter key. I've been searching around but couldn't find good ways of implementing an if "certain button clicked" to do that. How can I fix this?
Evidently, the key object returned to on_press does not have a char attribute unless you have actually hit a character key. So you have to check for the existence of that attribute. You can do that using hasattr:
from pynput.keyboard import Key, Controller as KeyboardController
from pynput.mouse import Button, Controller as MouseController
from pynput.keyboard import Key, Listener
keyboard = KeyboardController()
mouse = MouseController()
def on_press(key):
if hasattr(key, 'char'):
if key.char == 'q':
print(mouse.position)
return True
# Collect events until released
with Listener(
on_press=on_press) as listener:
listener.join()

Using pynput to get mouse coordinates

The program runs, but onclick I do not get a print statement of my x, and y.
I built a simple MoustInput class like so
from pynput import mouse
class MouseInput:
def __init__(self):
with mouse.Listener(on_click=self.on_click) as listener:
listener.join()
def on_click(self, x, y, button, pressed):
print('{0} at {1}'.format(
'Pressed' if pressed else 'Released',
(x, y)))
if not pressed:
# Stop listener
return False
and then in app.py
I have something like this to call the class
from MouseInputs.mouse_input import MouseInput
if __name__ == "__main__":
MouseInput()
Fixed the issue.. It was a Mac related problem not allowing inputs to be detected..
I fixed this by allowing VSCode, and the terminal in accessibility in the Security & Privacy section of system preferences.

How do I bind an event to the left mouse button being held down?

I need a command to be executed as long as the left mouse button is being held down.
If you want "something to happen" without any intervening events (ie: without the user moving the mouse or pressing any other buttons) your only choice is to poll. Set a flag when the button is pressed, unset it when released. While polling, check the flag and run your code if its set.
Here's something to illustrate the point:
import Tkinter
class App:
def __init__(self, root):
self.root = root
self.mouse_pressed = False
f = Tkinter.Frame(width=100, height=100, background="bisque")
f.pack(padx=100, pady=100)
f.bind("<ButtonPress-1>", self.OnMouseDown)
f.bind("<ButtonRelease-1>", self.OnMouseUp)
def do_work(self):
x = self.root.winfo_pointerx()
y = self.root.winfo_pointery()
print "button is being pressed... %s/%s" % (x, y)
def OnMouseDown(self, event):
self.mouse_pressed = True
self.poll()
def OnMouseUp(self, event):
self.root.after_cancel(self.after_id)
def poll(self):
if self.mouse_pressed:
self.do_work()
self.after_id = self.root.after(250, self.poll)
root=Tkinter.Tk()
app = App(root)
root.mainloop()
However, polling is generally not necessary in a GUI app. You probably only care about what happens while the mouse is pressed and is moving. In that case, instead of the poll function simply bind do_work to a <B1-Motion> event.
Look at table 7-1 of the docs. There are events that specify motion while the button is pressed, <B1-Motion>, <B2-Motion> etc.
If you're not talking about a press-and-move event, then you can start doing your activity on <Button-1> and stop doing it when you receive <B1-Release>.
Use the mouse move/motion events and check the modifier flags. The mouse buttons will show up there.

Categories