Search code examples
pythonbashpython-3.xx11xdotool

The way to detect the current mouse cursor type from bash or python


I know that I can get the current location of the mouse cursor by executing "xdotool getmouselocation".

I would like to detect the current mouse cursor type such as pointer, beam, or hand cursor from bash terminal or python code. Would this be possible?

Thank you. June


Solution

  • You can use xdotool to continuously click where the link would be until the program notices the window title changes. When the window title changes, that means the link has been clicked, and the new page is loading.

    Clicking function:

    ff_window=$(xdotool search --all --onlyvisible --pid "$(pgrep firefox)" --name ".+")
    
    click-at-coords() {
        title_before=$(xdotool getwindowname $ff_window)
        while true; do
            sleep 1
            title_now=$(xdotool getwindowname $ff_window)
            if [[ $title_now != $title_before]]; then
                break
            else
                xdotool windowfocus --sync "$ff_window" mousemove --sync "$1" "$2" click 1
            fi
        done
    }
    

    Assuming that you're using xdotool to click using coordinates:

    # replace each x and y with the coordinates of each link
    # example with 2 sets of coordinates: all_coords=("67 129" "811 364")
    all_coords=("x y" "x y")
    
    for sub in "${all_coords[@]}"; do
        coords=($sub)
        click-at-coords "${coords[@]}"
    done