Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BASH XDOTool Mouse click, repeat and stop

Tags:

bash

xdotool

I am trying to integrate a simple infinite click script using XDOToool with another piece of script to detect keyboard input; to end the running clicking script when a key is pressed but unsure how to match them up.

This script runs infinitely repetitively clicking at screen cursor point XXX, YYY determined by XDOTool

 #!/bin/bash
 while true [ 1 ]; do
 xdotool mousemove XXX YYY click 1 &
 sleep 1.5
 done

Next I wish to use something like:

#!/bin/bash
if [ -t 0 ]; then stty -echo -icanon -icrnl time 0 min 0; fi
count=0
keypress=''
while [ "x$keypress" = "x" ]; do
let count+=1
echo -ne $count'\r'
keypress="`cat -v`"
done
if [ -t 0 ]; then stty sane; fi
echo "You pressed '$keypress' after $count loop iterations"
echo "Thanks for using this script."
exit 0

I don't understand how I take the:

 xdotool mousemove XXX YYY click 1 &
 sleep 1.5

And where to put it in the script above, BASH confusion and MAN BASH doesn't help so anyone who could assist would be appreciated. THANKS

like image 536
Robert Williams Avatar asked Jun 04 '26 12:06

Robert Williams


1 Answers

Improved (and commented) script:

#!/bin/bash

x_pos="0"   # Position of the mouse pointer in X.
y_pos="0"   # Position of the mouse pointer in Y.
delay="1.5" # Delay between clicks.

# Exit if not running from a terminal.
test -t 0 || exit 1

# When killed, run stty sane.
trap 'stty sane; exit' SIGINT SIGKILL SIGTERM

# On exit, kill this script and it's child processes (the loop).
trap 'kill 0' EXIT

# Do not show ^Key when pressing Ctrl+Key.
stty -echo -icanon -icrnl time 0 min 0

# Infinite loop...
while true; do
    xdotool mousemove "$x_pos" "$y_pos" click 1 &
    sleep "$delay"
done & # Note the &: We are running the loop in the background to let read to act.

# Pause until reads a character.
read -n 1

# Exit.
exit 0
like image 185
Helio Avatar answered Jun 06 '26 07:06

Helio