Script for mouse-click is activating on mouse-over [SOLVED]

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Post Reply
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Script for mouse-click is activating on mouse-over [SOLVED]

Post by Logansfury »

Hello all,

I am having difficulty with a bash script on my Linux Box. It is supposed to define a square area on my mainscreen (which it does) and then wait for the mouse to click within that square. The script launches, then waits, but as soon as the mouse enters the area of defined screenspace, the script reacts instead of waiting for a mouse click in the square. Can anyone help?

Script:

Code: Select all

#!/bin/bash

# Define the coordinates of the clickable box
box_x=780
box_y=385
box_width=300
box_height=260

# Function to check if the mouse is inside the box
mouse_inside_box() {
    mouse_x=$(xdotool getmouselocation --shell | grep 'X' | awk -F'=' '{print $2}')
    mouse_y=$(xdotool getmouselocation --shell | grep 'Y' | awk -F'=' '{print $2}')

    if [ "$mouse_x" -ge "$box_x" ] && [ "$mouse_x" -le "$((box_x + box_width))" ] && \
       [ "$mouse_y" -ge "$box_y" ] && [ "$mouse_y" -le "$((box_y + box_height))" ]; then
        return 0  # Mouse is inside the box
    else
        return 1  # Mouse is outside the box
    fi
}

# Wait for a mouse click event within the specified box
while true; do
    if mouse_inside_box && xdotool click 1; then
    # Play a beep sound at max volume
    paplay /usr/share/sounds/freedesktop/stereo/complete.oga
        # Run your script after a mouse click
        pkill -f "conky -c /home/logansfury/.conky/Biohazzard/conkyrc"
        break  # Exit the loop after the script is executed
    fi
    sleep 0.1  # Adjust the sleep duration as needed
done
Thank you for reading,

Logan
Last edited by Logansfury on Tue Jan 30, 2024 9:13 pm, edited 1 time in total.
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
vimes666
Level 6
Level 6
Posts: 1241
Joined: Tue Jan 19, 2016 6:08 pm

Re: Script for mouse-click is activating on mouse-over

Post by vimes666 »

Afaik you cannot detect a mouse click with xdotool. What you are doing is simulating a mouse click.
Have a look here: https://unix.stackexchange.com/question ... or-command
If you think the issue is solved, edit your original post and add the word solved to the title.
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Script for mouse-click is activating on mouse-over

Post by Logansfury »

vimes666 wrote: Tue Jan 30, 2024 8:34 pm Afaik you cannot detect a mouse click with xdotool. What you are doing is simulating a mouse click.
Have a look here: https://unix.stackexchange.com/question ... or-command
Hello Vimes,

Thank you so much for joining the thread :)

I have followed your link to another link that provided this script:

Code: Select all

Here is a little mouse button watcher script:
#!/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\[' | sort)
while true; do
    sleep 0.2
    STATE2=$(xinput --query-state $MOUSE_ID | grep 'button\[' | sort)
    comm -13 <(echo "$STATE1") <(echo "$STATE2")
    STATE1=$STATE2
done
I have made a file on my desktop named click.sh and pasted this, performed a chmod +x and launched it from terminal with

Code: Select all

./click.sh
I got a pause space in the terminal, as I usually get when a script is waiting for an action, and then as soon as I clicked the mouse, I got a return of

button[1]=down
button[1]=up

Can this script above please be edited to perform the

# Play a beep sound at max volume
paplay /usr/share/sounds/freedesktop/stereo/complete.oga
# Run your script after a mouse click
pkill -f "conky -c /home/logansfury/.conky/Biohazzard/conkyrc"

when mouse button is clicked instead of printing mouse button state in teh terminal?
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Script for mouse-click is activating on mouse-over

Post by Logansfury »

I got it!!!!

I asked chatgpt3.5 to combine scripts for me and it did it correctly in just 2 attempts! Here is the working script:

Code: Select all

#!/bin/bash

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

# Define the coordinates of the clickable box
box_x=780
box_y=385
box_width=300
box_height=260

# Function to check if the mouse is inside the box
mouse_inside_box() {
    mouse_x=$(xdotool getmouselocation --shell | grep 'X' | awk -F'=' '{print $2}')
    mouse_y=$(xdotool getmouselocation --shell | grep 'Y' | awk -F'=' '{print $2}')

    if [ "$mouse_x" -ge "$box_x" ] && [ "$mouse_x" -le "$((box_x + box_width))" ] && \
       [ "$mouse_y" -ge "$box_y" ] && [ "$mouse_y" -le "$((box_y + box_height))" ]; then
        return 0  # Mouse is inside the box
    else
        return 1  # Mouse is outside the box
    fi
}

# Wait for a mouse click event within the specified box
while true; do
    # Get the current state of the mouse buttons
    STATE1=$(xinput --query-state $MOUSE_ID | grep 'button\[' | sort)

    if mouse_inside_box; then
        # Wait for a change in mouse button state
        while true; do
            sleep 0.2
            STATE2=$(xinput --query-state $MOUSE_ID | grep 'button\[' | sort)
            
            # Check for a change in mouse button states
            if [ "$(comm -13 <(echo "$STATE1") <(echo "$STATE2"))" != "" ]; then
                # Play a beep sound at max volume
                paplay /usr/share/sounds/freedesktop/stereo/complete.oga
                
                # Run your script after a mouse click
                pkill -f "conky -c /home/logansfury/.conky/Biohazzard/conkyrc"
                
                # Exit the loop after the script is executed
                break 2  # Break out of both loops
            fi
            STATE1=$STATE2
        done
    fi
    
    sleep 0.1  # Adjust the sleep duration as needed
done
THIS IS AWESOME :D

Couldn't have done this without the piece of mouse watch code that came from the link in the reply. Thank you so much :)
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
Post Reply

Return to “Scripts & Bash”