将带有时间戳的鼠标坐标和鼠标点击写入文件?

Write mouse coordinates and mouse clicks with timestamps to file?

我正在尝试创建一个 bash 脚本,每 5 毫秒记录一次鼠标的位置。我还想记录鼠标点击的时间戳和位置。

使用 xdotool getmouselocation 可以轻松记录鼠标位置。我已经能够使用此处的一些建议来记录鼠标点击:https://unix.stackexchange.com/questions/106736/detect-if-mouse-button-is-pressed-then-invoke-a-script-or-command 但是,我无法将两者结合起来。

有什么办法可以实现吗?提前致谢!

https://unix.stackexchange.com/questions/106736/detect-if-mouse-button-is-pressed-then-invoke-a-script-or-command 的已接受答案中有一个获取鼠标状态更改的示例。只需稍作修改,您就可以在按下鼠标按钮时打印鼠标位置。

@Gem Taylor 提到为此使用脚本语言不是一种可选方式。

在测试 运行 期间,我遇到了无法捕获点击的情况。

#!/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