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

2

5

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

Depend answered 14/7, 2017 at 7:36 Comment(8)
What OS are you using?Lawmaker
If you mean which "state" the cursor is in (the different cursors that are shown when mousing over a text-box or link or the desktop), I feel like a better option would be for you to explain why you want this. I could offer a better alternative.Lawmaker
@Lawmaker I am using Xubuntu 17.04.Depend
@Lawmaker I would like to make a kind of macro using bash script or python code which works over Firefox. I actually made it and it works looping some hyperlinks, but sometimes it clicks too ealier than the page is loaded.Depend
So you want to wait until the cursor changes, indicating there is a link under it?Lawmaker
@Lawmaker That's exactly what I am looking for!Depend
You can use Selenium's WebDriverWait in conjunction with expected_conditions to wait for your element to be clickable for example. See an example here #26567299Inwards
Please check the answer is here as well with example, <#59609398>Malfunction
L
0

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
Lawmaker answered 14/7, 2017 at 15:26 Comment(0)
R
9

On MS Windows:

import win32gui    
win32gui.GetCursorInfo()

Will return something like

(1, 65539, (1920, 1080))

2nd number is ID of cursor type

On Windows 10 I get:

  • 65539 - normal

  • 65567 - pointer

  • 65541 - insert

Roderic answered 28/7, 2019 at 23:30 Comment(3)
Thank you!! Needed this for a GUI automation- Program I am controlling goes unresponsive during processing so its hard to tell when its done without continuous try and timeout loops so I was using hard coded excessive delays. I get 65547 for the spinning/wait cursor and 65543 for my normal pointer.Hydrotaxis
I'm also sometimes getting 65569 for pointer for the situations where one minute ago it was 65567. Probably 65567-65569 are pointers.Metamerism
The win32gui is only compatible with window computer. Would you have another package for macOs in order to do the same thing ?Darrickdarrill
L
0

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
Lawmaker answered 14/7, 2017 at 15:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.