bash 脚本来检测和记录鼠标移动?

ano*_*ous 19 input devices mouse

我需要能够经常记录鼠标移动(例如每 0.2 秒),并将它们放在坐标表示中而不是差异中。

我找到了以下脚本:

#!/bin/bash
while :
do
cat /dev/input/mice | read -n 1
date
sleep 1
done
Run Code Online (Sandbox Code Playgroud)

但它似乎没有向终端打印任何内容(或者可能都是胡言乱语)。其他讨论表明 /dev/input/mice 已被弃用。最重要的是, /dev/input/mice 实际上不会以友好的格式保存数据。

我是否必须手动进行转换(从 /dev/input 文件中的格式),或者是否有用于此的 API?

Gil*_*not 22

尝试以下命令:

xdotool getmouselocation 2>&1 |
    sed -rn '${s/x:([0-9]+) y:([0-9]+) .*/\1 \2/p}'
Run Code Online (Sandbox Code Playgroud)

http://www.semicomplete.com/projects/xdotool/


Bor*_*kov 15

另一种选择是 xinput。例如,xinput test 8会写

motion a[0]=496 a[1]=830 
motion a[0]=496 a[1]=829 
motion a[0]=496 a[1]=832 
motion a[0]=496 a[1]=834 
Run Code Online (Sandbox Code Playgroud)

鼠标移动时,其中“8”是我的鼠标设备编号。使用xinput --list找出你的鼠标设备之间的数字。