In KMK there is a module called Mouse Jiggler which allows you to "periodically jiggle the mouse cursor, keeping the host system from idling or sleeping."

It works really fine, since the keyboard sends HID commands to move the mouse and all apps in Windows recognize the movement. But what if I do not have my keyboard around me all the time? can't I just make an script to jiggle the mouse?

Well yes, there are libraries like PyAutoGUI and pynput that allow you to control mouse and keyboard. However, some apps in windows do not recognize this simulated mouse movement as an actual movement and therefore the app idles. But why?

Apparently PyAutoGUI and pynput do not interact with the windows API for mouse events (Not really sure this is my guess).

A PyAutoGUI script like this will move the mouse but still some apps will idle.

1
2
3
4
5
6
7
8
import pyautogui, time, random

whilte True:
    x = random.randint(-1, 1)
    y = random.randint(-1, 1)
    pyautgui.move(x, y)
    print(rf"Mouse moved x = {x}, y = {y}")
    time.sleep(5)

So, I found another way, use pywin32:

1
2
3
4
5
6
7
8
import time, random, win32api, win32con

while True:
    x = random.randint(-1,1)
    y = random.randint(-1,1)
    win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, int(x), int(y))
    print(rf"Mouse moved x = {x}, y = {y}")
    time.sleep(5)

Now mouse movement is recognized by all apps in windows and never idle.

The script was adapted from this stackoverflow discussion.

Final thoughts

  • While PyAutoGUI lets you control mouse, keyboard, identify images in screen and more. It does not interact directly with the app commands.
  • pywin32 allows you to interact directly with the app commands in windows. This lets you use apps like Outlook to automate sending emails and so on, all from a script without using PyAutoGUI to click, write, etc.
  • You don't even have to know how to code. In Windows there is Power Automate, it allows you to create a work flow by clicking and dragging commands or use templates.
  • Be creative!
If you found this content useful, please support me :
BTC: 1E2YjL6ysiPxRF4AEdXChpzpesRuyzgE1y