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:
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
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 ?
– Azathoth
and on the executable, the . exe opens and closes even putting input at the end of the code
– Azathoth
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.– LucasV75
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)
– LucasV75