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...

3

4 Answers 4

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.

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
1
  • 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, 2022 at 8:26
1

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
1
  • Great answer! Add the following after the first line to switch to root automatically: if [ $UID -ne 0 ]; then exec sudo "$0" fi echo "This will be run from user $USER"
    – mcarans
    yesterday
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .