21

Is there any working utility that can simulate mouse movements on a ubuntu?

Important: I only want that utility to control the mouse if the machine is idle for X minutes.

Of course I could use xdotool to simulate some movement, but how would I detect if the pc is idle for x minutes? I could not find any working tool for linux...

membersound
  • 1,410
  • Does this help? https://unix.stackexchange.com/questions/508427/how-to-detect-if-the-computer-is-idle I haven't tested... – FedKad Mar 16 '20 at 15:59
  • Please see https://help.ubuntu.com/stable/ubuntu-help/session-screenlocks.html.en which eliminates the need for a mouse movement emulator. – K7AAY Mar 16 '20 at 17:15
  • @K7AAY Preventing screen lock isn't the only use for this. There is plenty of other software which reacts in some way to being idle. – Jon Bentley Feb 16 '21 at 16:22

4 Answers4

31

There's a program that moves the mouse when it detects you are away, or idle.

https://github.com/carrot69/keep-presence/

You can install it from snap:

sudo snap install keep-presence

Then run it:

keep-presence --seconds 30

It will move the mouse after 30 seconds if it detects that you are idle.

mousanony
  • 311
19

Meanwhile I created my own script:

#requires:
# 'xprintidle' for inactivity check (in ms)
# 'rand' for generating random number (screen resolution)
# 'xdotool' to move the mouse pointer

#parameters:
# 100000 idle time in ms before executing the mousemove
# 800 / 600: your screen resolution, at at least the moving range for the mouse pointer

while :; do
    if  [ $(xprintidle) -gt 100000 ]
    then
        xdotool mousemove `rand -M 800` `rand -M 600`;
    fi

    sleep 30
done
membersound
  • 1,410
  • 2
    It seems there is no "rand" binary in Debian Bullseye.

    One can manage to replace it with something like:

    xdotool mousemove $(echo $(( ${RANDOM} % 800 ))) $(echo $(( ${RANDOM} % 600 )))
    
    – Stéphane Jul 25 '22 at 08:26
0

For Wayland and X11 compatibility, I did not bother with checking activity as you do not even notice it is running. Needs evemu-tools and needs to be run as root.

#!/bin/bash
_dev=$(ls -1 /dev/input/by-id/*event-mouse | tail -1)
[ -z "${_dev}" ] && echo "Cannot find mouse event device" && exit 1
echo "Using ${_dev}"
while [ true ]
do
        sleep 60
        /usr/bin/evemu-event ${_dev} --type EV_REL --code REL_X --value 1 --sync
        /usr/bin/evemu-event ${_dev} --type EV_REL --code REL_X --value -1 --sync
        echo -n "#"
done
0

I was able to get a script working based off of Howard's answer but I found the movement to be noticable. So, I wait to detect mouse input and will jiggle it dependent on that.

This script depends on evemu

#!/bin/bash

uncomment two lines below to help select your mouse event number

grep -E 'Name|Handlers' /proc/bus/input/devices | grep -B1 'mouse'

printf "\nFind your mouse's event number, it should be in this format 'eventX' where X is your event number\nWrite your event number into the script"

end of find mouse event number code

EVENT_NUMBER=24 # use above code to get your mouse event number

SLEEP_TIME=10 output=$(timeout $SLEEP_TIME evemu-record /dev/input/event$EVENT_NUMBER)

if ! grep -q "EV_REL / REL_Y" <<< "$output"; then mouse=$(ls -1 /dev/input/by-id/*event-mouse | tail -1) # evemu-event must be ran as sudo evemu-event $mouse --type EV_REL --code REL_X --value 1 --sync evemu-event $mouse --type EV_REL --code REL_X --value -1 --sync fi

I then run this script every few minutes using a cron

EDIT: I found that the event number of my mouse changes on restart. I modified the script to just capture the ouput from /dev/input/mice. This worked fine for me as an alternative. I did notice a bash warning for a null byte but it seems harmless.

#!/bin/bash

SCRIPT VERSION 2

SLEEP_TIME=4 output=$(timeout $SLEEP_TIME cat /dev/input/mice)

if [[ -z "$output" ]]; then mouse=$(ls -1 /dev/input/by-id/*event-mouse | tail -1) # evemu-event must be ran as sudo evemu-event $mouse --type EV_REL --code REL_X --value 1 --sync evemu-event $mouse --type EV_REL --code REL_X --value -1 --sync fi

Coda Bool
  • 1
  • 1