0

I'm trying to create a bash script that records the location of the mouse every 5 milliseconds. I also want to record the timestamps and locations of mouse-clicks.

Recording the mouse location has been easy with xdotool getmouselocation. I've been able to record mouse clicks using some of the suggestions here: https://unix.stackexchange.com/questions/106736/detect-if-mouse-button-is-pressed-then-invoke-a-script-or-command However, I've not been able to combine the two.

Is there any way to accomplish this? Thank you in advance!

David Papp
  • 119
  • 1
  • 10
  • Can you post the code you wrote, so that we can see where you get stuck? – Dario Apr 30 '19 at 14:06
  • 2
    Doing this 200 times a second in a scripting language is going to trash performance – Gem Taylor Apr 30 '19 at 14:11
  • What would be a better alternative to a scripting language? I stayed away from higher lever languages like Java because I wasn't sure if these languages would have access to mouse information if running in the background. – David Papp Apr 30 '19 at 15:21

1 Answers1

1

In accepted answer of https://unix.stackexchange.com/questions/106736/detect-if-mouse-button-is-pressed-then-invoke-a-script-or-command have an example to get mouse status change. With little modification you get print the mouse location upon mouse button down.

@Gem Taylor mentioned using script language for this is not an optional way.

During test run, I experienced cases when clicks are not get captured.

#!/bin/bash

MOUSE_ID=$(xinput --list | grep -i -m 1 'mouse' | grep -o 'id=[0-9]\+' | grep -o '[0-9]\+')

STATE1=$(xinput --query-state $MOUSE_ID | grep 'button\['"."'\]=down' | sort)
while true; do
        sleep 0.005
        STATE2=$(xinput --query-state $MOUSE_ID | grep 'button\['"."'\]=down' | sort)
        CLICK=$(comm -13 <(echo "$STATE1") <(echo "$STATE2"))
        if [[ -n $CLICK ]]; then
                echo "$CLICK"
                xinput --query-state $MOUSE_ID | grep 'valuator\['
        fi
        STATE1=$STATE2
done
lw0v0wl
  • 664
  • 4
  • 12
  • Thank you! I couldn't get this code to work earlier because the correct MOUSE_ID did not have 'mouse' in the name. – David Papp Apr 30 '19 at 19:00