Currently, I am attempting to write a program that takes a screenshot on mouse-click. The basic functionality works as expected, but, it seems that Pynput is sending two triggers for every one click? (I am getting two screenshots created for every one mouse click).
The end goal is to have a program that takes one screenshot for every mouse-click.
Here is the current code that I am using.
import numpy as np
import cv2
import pyautogui
from pynput import mouse
COUNT = 0
def take_screenshot(x,y,button,pressed):
COUNT+=1
if button == mouse.Button.left:
image = pyautogui.screenshot()
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
cv2.imwrite(f"image{COUNT}.png", image)
with mouse.Listener(on_click=take_screenshot) as listener:
listener.join()
I have tried a couple things, but it has either made the mouse react oddly, or didn't improve anything.
- Tried to use booleans values and used "AND" addition to "if" statement:
TAKEN = False
...
if button == mouse.Button.left and TAKEN == False:
image = pyautogui.screenshot()
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
cv2.imwrite(f"image{COUNT}.png", image)
TAKEN = True
- Tried wrapping the listener in different functionality (while loops, etc.)
- Tried to put the listener in its on function, but this caused the mouse to react oddly.
Attached screenshot, I don't know if it helps any in this situation.
From other StackOverflow items, like this one --> Why IF judgement in PYNPUT mouse listener will be operated twice?
It feels like the press/release on mouse-click is the culprit. But reviewing pynput documentation, the mouse listener doesn't have an on_release argument like the keyboard listener does. Does anybody know:
- If this what is happening? If so, is there an option to disable this that isn't documented?
- Alternative tooling for mouse-clicks that does offer a single call per mouse-click event?