Detect mouse move or keystroke Is it possible to make a program execute when the mouse is moved or any key is pressed?
You can do the opposite. You can check for inactivity.

So then it is just a matter of doing nothing as long as inactivity increases...

#!/bin/sh

# Pause/Interval between checks
checkInt=5

# Keep track of the idle timer
lastIdle=0

while true ; do
   ioresp=`ioreg -w 0 -c IOHIDSystem | sed -e '/HIDIdleTime/ !{ d' -e 't' -e '}' -e 's/.* = //g' -e 'q'`
   idleNow=`echo $(( ${ioresp} / 1000000000 ))`

   re='^[0-9]+$'
   if ! [[ $idleNow =~ $re ]] ; then
      echo "error: ioreg did not return a number" &gt;&amp;2; exit 1
   fi

   echo "Idle now is $idleNow secs, was $lastIdle secs"

   # if idle decreased then action occurred so we do something...
   if [ $idleNow -lt $lastIdle ] ; then
      osascript -e 'tell application "System Events" to display dialog "Hello World"'
      lastIdle=0
   else
      lastIdle=$idleNow
   fi

   sleep $checkInt
done


It is crude but may be it will do the job.... !?!