0

I am using pynput based listener to detect and record mouse clicks using python.

def on_click(x, y, button, pressed):
    if pressed:
        on_release("clicked")
        button = str(button).replace("Button.","")
        inverted_comma = "'"
        button = f"{inverted_comma}{button}{inverted_comma}"
        mouse_values = [x, y, button]
        macro_writer('click',mouse_values)
        
    #image based click
    time.sleep(1)
    pyautogui.moveTo(1,1)
    time.sleep(2)
    
    x = x-50
    y = y-50
    im3 = pyautogui.screenshot(r"D:\Library\Project\Project\theautomater\src\macro\prett2.png",region=(x,y, 100 , 100))
    

I am able to record the coordinates of the mouse. Problem is I want to record the image/icon where the mouse clicks and as you can see the last line, I can do that but it happens AFTER the click. This creates the problem that the icon is in "clicked" or "hover" state.

The solution I am thinking of implementing is pausing the click function taking screenshot then clicking.

For this, I need to figure out how to delay the mouse click using python. Can anyone suggest something?

The other question on SO, does not work as intended (Delay mouse click 0.5 second), please do not mark as duplicate.

Shreyash Sharma
  • 310
  • 2
  • 11
  • There's no way to win. If the button happens to be a dialog OK button, the dialog will be gone by the time you delay. BTW, why do you have the `inverted_comma` nonsense instead of just writing `f"'{button}'"`? – Tim Roberts Mar 30 '21 at 03:24
  • If there is a possibility to delay, I can record the intended click coordinates, take screenshot then perform the click myself. That's a win. – Shreyash Sharma Mar 30 '21 at 03:40

0 Answers0