'Simulate real click python

As I can simulate a click as if it were made from the mouse, a game has detection of click events or simulated keys, I would like to know if it is possible to simulate a real click. Translated.

My code:

import time
import keyboard
import pyautogui
from playsound import playsound

pyautogui.PAUSE = 0.2
sW, sH = pyautogui.size()
down = False

while True:
    if keyboard.is_pressed('f4'):
        if not down:
            down = True
            playsound('on.mp3')
            pyautogui.moveTo(sW*0.67, sH*0.5)
            pyautogui.click()
            pyautogui.moveTo(sW*0.67, sH*0.6)
            pyautogui.click()
            pyautogui.moveTo(sW*0.67, sH*0.64)
    else:
        down = False


Solution 1:[1]

This code simulates a real click of the mouse.

import win32api, win32con
from time import sleep

def click(x,y, duration=30, delay=0.3): 
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
sleep(duration / 1000)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
sleep(delay)

Why? Because the lib win32api is directly connected with the windows commands. When u click with the mouse, you actualy triggers a function inside the windows. win32api does de same. Also...win32api is the fastest comunication with the windows for this stuff, like image rendering

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1