2

I am interested to see if there is a way to store mouse and keyboard movements to automate some repetitive tasks.

Right now, I am able to send mouse and keyboard movements using pyautogui like this:

pyautogui.moveTo(X, Y) # Move the mouse to XY coordinates.
pyautogui.write('Chrome', interval=0.25) # Write 'Chrome'
pyautogui.press('enter')  # Press the Enter key

I know the old saying goes

The computer will always do exactly what you TELL it to do.

However, in this case, I would like to SHOW the computer how do it, and have those actions be recorded into like a text file or something that can be called at some point in the script. If that makes sense.

Would it make more sense / be more practical to just call out each stroke/click like above?

1 Answer 1

4

If I understood correctly, you want a log of actions in a txt file to be then read and reproduced by pyautogui.

There are many libraries for that like pynput that offer simple listeners to actions like mouse clicks, keyboard inputs... like so:

from pynput.mouse import Listener
import logging

logging.basicConfig(filename="mouse_log.txt", level=logging.DEBUG, format='%(asctime)s: %(message)s')

def on_move(x, y):
    logging.info("Mouse moved to ({0}, {1})".format(x, y))

def on_click(x, y, button, pressed):
    if pressed:
        logging.info('Mouse clicked at ({0}, {1}) with {2}'.format(x, y, button))

def on_scroll(x, y, dx, dy):
    logging.info('Mouse scrolled at ({0}, {1})({2}, {3})'.format(x, y, dx, dy))

with Listener(on_move=on_move, on_click=on_click, on_scroll=on_scroll) as listener:
    listener.join()

You will then have a mouse_log.txt file to be parsed and to execute pyautogui actions accordingly. That introduces a further level of indirection and could be more error prone, so I'd go with the plain pyautogui script you have mentioned.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.