How to make a macro that clicks in several places?

Asked

Viewed 133 times

-1

Hi, I’d like to know how to make one. exe that acts as a macro, whenever I press the ALT key it will perform mouse clicks at a certain position with shift pressed at the same time. It can be in any language, I wanted the program to run in the background but in python always closes.

most I could get in python was:

import pyautogui

pyautogui.keyDown('shift')
pyautogui.click(x=873, y=480, clicks=1, interval=0.01, button='left')
pyautogui.click(x=950, y=480, clicks=1, interval=0.01, button='left')
pyautogui.click(x=1030, y=480, clicks=1, interval=0.01, button='left')
pyautogui.keyUp('shift')

 

1 answer

1


Well, about doing it run a sequence of commands when you press a specific key or CTRL + Something for example you can do the following steps:

  • First I’ll import: from pynput import keyboard to mess with hotkeys and allow you to execute the commands just by pressing a key or a combination of keys.

Documentation of pynput: https://pynput.readthedocs.io/en/latest/

import pyautogui
from pynput import keyboard

Remember to give the pip install pynput

  • Then I’ll turn the code you passed into a function

This greatly facilitates the repetition process from within the script without spending too many lines

def clicar_personalizado():

    pyautogui.keyDown('shift')
    pyautogui.click(x=873, y=480, clicks=1, interval=0.01, button='left')
    pyautogui.click(x=950, y=480, clicks=1, interval=0.01, button='left')
    pyautogui.click(x=1030, y=480, clicks=1, interval=0.01, button='left')
    pyautogui.keyUp('shift')
  • Now I will add allow it to run functions from what you press on the keyboard using the method Keyboard.Globalhotkeys of pynput as follows:

Again follows the link from that specific part:

with keyboard.GlobalHotKeys({
    '<alt>': clicar_personalizado, # "sequência de teclas": função
    '<ctrl>+f': quit, # Opcional apenas para fechar o script
}) as h:
    h.join()

The pynput documentation teaches another way to do this through a Listener, but in your case I think Globalhotkeys is the best way to do this.

  • In place of alt you can put anything else like for example: "Ctrl+alt+q".

The keys Ctrl and alt need to be inside < >.

Complete code:

import pyautogui
from pynput import keyboard

def clicar_personalizado():
    pyautogui.keyDown('shift')
    pyautogui.click(x=873, y=480, clicks=1, interval=0.01, button='left')
    pyautogui.click(x=950, y=480, clicks=1, interval=0.01, button='left')
    pyautogui.click(x=1030, y=480, clicks=1, interval=0.01, button='left')
    pyautogui.keyUp('shift')

with keyboard.GlobalHotKeys({
    '<alt>': clicar_personalizado, # "sequência de teclas": função
    '<ctrl>+f': quit, # Opcional apenas para fechar o script
}) as h:
    h.join()

Now on how to make the program into one .exe I know two ways:

  • Using pyinstaller:

I recommend this video here explaining how to use: https://www.youtube.com/watch?v=RMkPGjGhzxg

Or you can follow for documentation: https://pyinstaller.readthedocs.io/en/stable/usage.html

  • Using Auto Py To Exe:

see this link here for the tutorial on how to use it: https://dev.to/eshleron/how-to-convert-py-to-exe-step-by-step-guide-3cfi

I hope I’ve helped!

  • Is everything all right? i managed to decrease the time in ms of each click, but the alt response time is very low after a second command, for example, has to leave the macro whenever the alt has pressed the code will loop until I release the alt ?

  • and on the executable, the . exe opens and closes even putting input at the end of the code

  • How bad is it? Mano on the issue ai of delay between clicks I have this problem recurring with pyautogui tb, sometimes it takes a little to execute the commands. About pynput allow the code to loop until you hit a cancel key or hit it dnv.

  • Check this link here: https://nitratine.net/blog/post/python-auto-clicker/. The guy here is talking about click for example but you can add any sequence of different commands in this tutorial he’s doing there (is the way to let in loop while you keep tight or just tighten it once dnv)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.