DEV Community 👩‍💻👨‍💻

Cover image for Using xdotool and shell scripts to automate keystrokes and randomise GatherTown avatar movements
Lena
Lena

Posted on • Updated on

Using xdotool and shell scripts to automate keystrokes and randomise GatherTown avatar movements

Have you ever wanted to automate your movements on places like GatherTown? You can use xdotool combined with shell scripting to do so, as this allows you to emulate keystrokes.

On Ubuntu, you can install xdotool using,
$ sudo apt install xdotool

For example, executing

$ xdotool keydown a

will emulate the "a" key being pressed down, and will type "a" continuously.
Image description

On GatherTown, there are four direction of movements, which are up (using "w"), down (using "s"), left (using "a"), right (using "d"). In addition to the movements, you can dance (using "z") and go through people (using "g").

We will be using a shell script to randomise the movements on GatherTown.

We want to randomise the direction of the movement, and we will use the following to generate a number between 0-3 randomly:
randdirec=$((RANDOM%4))

If $randdirec is 0, xdotool key w is executed, which causes the avatar to go up.
If $randdirec is 1, xdotool key s is executed, which causes the avatar to go down.
If $randdirec is 2, xdotool key a is executed, which causes the avatar to go left.
If $randdirec is 3, xdotool key d is executed, which causes the avatar to go right.

We also want to randomise the amount of steps we take in that particular direction, and will use the following to generate a number between 1-7 randomly.

x=$(((RANDOM%7)+1))

The minimum amount of steps we take in that direction will be 1, and the maximum amount of steps we take in that direction will be 7.

The direction and the number of steps will be randomised at every iteration of the infinite loop.

The code until this point looks like this:

#!/bin/bash

while true
do
    i=0
    #generate random number between 1-7
    x=$(((RANDOM%7)+1))

    #generate random number between 0-3
    randdirec=$((RANDOM%4))

    while [ $i -lt $x ] 
    do
            case $randdirec in
                0)
                    #go up
                    xdotool key w
                ;;

                1)
                    #go down
                    xdotool key s
                ;;

                2)
                    #go left
                    xdotool key a
                ;;


                3)
                    #go right
                    xdotool key d
                ;;

                *)
                ;;
            esac

            #wait 0.01 seconds until the next iteration 
            sleep 0.01
            i=$((i+1))
    done

done
Enter fullscreen mode Exit fullscreen mode

To execute the script, we first give execution permission,
$ chmod +x auto.sh

Then we can execute the script,
$ ./auto.sh

Next, we can add random dance movements. If we want the avatar to dance with a probability of 3/200 at every iteration, we can use the following,

    #generate random number between 0-199
    dance=$((RANDOM%200))
    #3/200 chance of dancing
    if [ $dance -gt 196 ]
    then
        #dance
        xdotool key z
        #dance for 2 seconds
        sleep 2
    fi
Enter fullscreen mode Exit fullscreen mode

Finally, we can randomly activate the "go through people" key, namely the "g" key, which can be useful when our avatar gets stuck. If we want the avatar to activate the "g" key with a probability of 5/100, we can use the following,

    #generate random number between 0-99
    g=$((RANDOM%100))
    #5/100 chance of activating go through people key
    if [ $g -gt 94 ]
    then
        #hold down the go through people key
        xdotool keydown g
        flag=1
    fi
Enter fullscreen mode Exit fullscreen mode

Then at the end of the iteration, we will release the "g" key using the following,

    if [ $flag == 1 ]
    then
        #release the go through people key
        xdotool keyup g
        flag=0
    fi
Enter fullscreen mode Exit fullscreen mode

This script can be used to pretend that you're online on GatherTown, or play a game of tag which will require multiple people to work together and surround you (and hope the "g" key won't be activated)!

The whole code can be found on my Github here: https://github.com/LambdaMamba/KeystrokeInjection/blob/main/shellscript_gathertown/auto.sh

Top comments (0)

🌚 Browsing with dark mode makes you a better developer by a factor of exactly 40.

It's a scientific fact.