mouse1=$( xinput query-state <your device name> )
# Give a short delay.
sleep 1
mouse2=$( xinput query-state <your device name> )
if [ "$mouse1" == "$mouse2" ]
......OR......
if [ "$mouse1" != "$mouse2" ]
Depending upon how you want to use the conditionals.
NOTE: UNTESTED as I am back in OSX 10.14.3 mode.
EDIT:
IMPORTANT! I forgot to add that there IS a flaw in this method, it is not a bug however.
As these things are learning curves when wanting to _interrupt_ something then see if you can realise what that flaw is from those few lines.
mouse1=$( xinput query-state 9 )
# Give a short delay.
sleep 2
mouse2=$( xinput query-state 9 )
if [ "$mouse1" != "$mouse2" ]
then
echo "Mouse moved."
fi
Now I need to learn how to poll (Hope its the right word) say every 30 seconds.
You are jumping the gun at the moment; UNDERSTAND what is going on first!
I mentioned that there is a FLAW. Try to understand that when messing with HW you need to know what you are doing.
Read the URL and understand to get a grasp of what is going on; although the code will work 99.999% of the time there is a condition known as a "race condition". Race condition - Wikipedia
Although not strictly a race condition it is a _pseudo_race condition.
An explanation:
If you move the mouse a tiny fraction of a second AFTER the 'if' line and before the condition is finalised then the two variables will be the same.
Assuming you have this _polling_ as you put it in a loop then this will be missed on the second variable and the next time in the loop the two values will be the same again and so it will seem no-one has moved the mouse. This, although NOT strictly a race condition per-se is close enough.
AFAIAC there is no easy cure using sequential code like a shell script so you HAVE to be aware of such situations.
So be aware; it is these subtleties that can cause headaches in the future.
As for _polling_ you could put the code inside a 'while' loop, while true and exit the loop via a break command using the conditional statement if that is what you want.
test1.sh
/home/andy/bin/test1.sh: line 24: syntax error near unexpected token `done'
/home/andy/bin/test1.sh: line 24: `done'
mouse1=$( xinput query-state 9 )
# Give a short delay.
sleep 2
mouse2=$( xinput query-state 9 )
while [ "$mouse1" != "$mouse2" ]
do
echo "Mouse moved."
if [ "$mouse1" == "$mouse2" ]
then
break
done
#while [ condition ]
#do
#statements1 #Executed as long as condition is true and/or, up to a disaster-condition if any.
#statements2
#if (disaster-condition)
#then
#break #Abandon the while lopp.
#fi
#statements3 #While good and, no disaster-condition.
#done
#!/bin/bash
# Initialise a log file if you want one.
: > /tmp/my_mouse_log.txt
while true
do
MOUSE1=$( xinput query-state 9 )
# Use read's timer as the 2 second delay to allow quitting the program by pressing 'q' or 'Q'.
read -r -n1 -t2 QUIT
MOUSE2=$( xinput query-state 9 )
# Remember a possible _race_condition here!
if [ "$MOUSE1" != "$MOUSE2" ]
then
echo "Mouse moved!"
# The line below will create a logfile as an option. It goes the the /tmp directory but could be anywhere inside your $HOME directory.
echo "$( date ), mouse moved!" >> /tmp/my_mouse_log.txt
fi
if [ "$QUIT" == "q" ] || [ "$QUIT" == "Q" ]
then
break
fi
done
# Exit to here when 'q' or 'Q' is pressed.
printf "\rYou pressed $QUIT to quit.\n"
cat /tmp/my_mouse_log.txt
This is a basic _poll_ of your mouse capturing mouse movement.
NO!
It means you are moving the mouse which you have been doing and it is logging the fact. As you wanted mouse movement detection then that is exactly what the code does.
It was supposed to be part of your detection of mouse movement for your project and although works as expected it is not a finished piece of code as it was meant to be added to your original code, but you have changed the goalposts since then.
Your original requirements were that you wanted to check if there was no mouse movement and if there was it was to tell you.
Thus, this does exactly 'what it says on the tin', detect mouse movement!