如何使用 bash 获取鼠标按下和释放的位置
How to get mouse press and release positions using bash
我想在单击鼠标时获取鼠标位置,然后在 bash 中将鼠标释放为 2 个变量。
像
这样的东西
press=( X Y )
release= ( X Y )
或
pressX
pressY
releaseX
releaseY
我该怎么做?
我已经尝试过
xinput list | grep -Po 'id=\K\d+(?=.*slave\s*pointer)' | xargs -P0 -n1 xinput test | awk '{print }'
但我不知道如何正确处理输出...
谢谢@gst 的回答!
这是我为以后其他人编辑的版本
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\[' | sort)
while true; do
sleep 0.2
STATE2=$(xinput --query-state $MOUSE_ID | grep 'button\[' | sort)
button=$(comm -13 <(echo "$STATE1") <(echo "$STATE2") | grep -Po "(?<=\=).*$")
STATE1=$STATE2
# if the mouse is clicked down - save the x and y coordinates into downX and downY
if [ "$button" = "down" ]; then eval $(xdotool getmouselocation --shell); downX=$X;downY=$Y; fi
# when the mouse is unclicked - save the x and y coordinates into upX and upY
# then exit the while loop
if [ "$button" = "up" ]; then eval $(xdotool getmouselocation --shell); upX=$X;upY=$Y; break; fi
done
编辑:
我放弃了所有这些并使用了
slop=$(slop --padding=-1 --bordersize=2 -f "%x %y %w %h %g %i") || exit 1
read -r X Y W H G ID < <(echo $slop)
相反,因为 slop
使您的光标在鼠标按下时不能 select 文本等!
我想在单击鼠标时获取鼠标位置,然后在 bash 中将鼠标释放为 2 个变量。 像
这样的东西press=( X Y )
release= ( X Y )
或
pressX
pressY
releaseX
releaseY
我该怎么做?
我已经尝试过
xinput list | grep -Po 'id=\K\d+(?=.*slave\s*pointer)' | xargs -P0 -n1 xinput test | awk '{print }'
但我不知道如何正确处理输出...
谢谢@gst 的回答!
这是我为以后其他人编辑的版本
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\[' | sort)
while true; do
sleep 0.2
STATE2=$(xinput --query-state $MOUSE_ID | grep 'button\[' | sort)
button=$(comm -13 <(echo "$STATE1") <(echo "$STATE2") | grep -Po "(?<=\=).*$")
STATE1=$STATE2
# if the mouse is clicked down - save the x and y coordinates into downX and downY
if [ "$button" = "down" ]; then eval $(xdotool getmouselocation --shell); downX=$X;downY=$Y; fi
# when the mouse is unclicked - save the x and y coordinates into upX and upY
# then exit the while loop
if [ "$button" = "up" ]; then eval $(xdotool getmouselocation --shell); upX=$X;upY=$Y; break; fi
done
编辑: 我放弃了所有这些并使用了
slop=$(slop --padding=-1 --bordersize=2 -f "%x %y %w %h %g %i") || exit 1
read -r X Y W H G ID < <(echo $slop)
相反,因为 slop
使您的光标在鼠标按下时不能 select 文本等!