JOURNAL.ROBBI.MY
← Back to Posts

Python 🐍 - active screen cursor movement

article 1 min read

Sometimes, I need to let the computer running so I can monitor logs or make some application not goes to sleep. So I use this python script to make random mouse movement and pressing some keyboard stroke on some interval of time.

python
import time as t
import random as rand
from pynput.mouse import Controller as mouseController
from pynput.keyboard import Key, Controller, Listener
import keyboard as keyb

# This function will move the cursor and 
# press 'space' every couple of seconds

def active_cursor_mover():
    start = t.time()
    mouse = mouseController()
    keyboard = Controller()

    while True:
        # Random movements
        movedirX = rand.randint(-100, 100)
        movedirY = rand.randint(-100, 100)

        mouse.move(movedirX, movedirY)
        keyboard.press(Key.space)

        # Every _ seconds
        t.sleep(2.0 - ((t.time() - start) % 2))

if __name__ == '__main__':
    active_cursor_mover()

You need 2 python package to make it run

bash
pip install pynput keyboard

so use ‘venv’ or any python virtual enviroment and run the script. She will do the job…

Have some thoughts, discussion or feedback on this post?

▸ What is webmention? How to send interactions!
Below you can find all of webmention with this page. You can also mention this URL on any website that supports WebMention. Have you written a response to this post? Let me know the URL:

Don't have WebMention? Use Comment Parade!

No webmentions yet.

Comments section coming soon. For now, send a webmention or email.