简体   繁体   中英

How to get mouse press and release positions using bash

I want to get the mouses position when it clicks down, and then when its released in bash as 2 variables. some thing like

press=( X Y )
release= ( X Y ) 

or

pressX
pressY
releaseX
releaseY 

How can I do this?

I've already tried doing

xinput list | grep -Po 'id=\K\d+(?=.*slave\s*pointer)' | xargs -P0 -n1 xinput test | awk '{print $2}'

but I don't know how to process the output correctly…

thank you @gst for the answer!

heres my edited version for other ppl in the future

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

EDIT: I scrapped all this and used

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)

instead, since slop makes it so that your cursor cant select text etc while the mouse is down!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM