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.