I'm trying to make a bash script that requires the ability to probe the cursor state, e.g. if it's hovering over a link or a document I need it to behave differently than if it's neutral or hovering text.

How could I go about doing this?

  • xdotool can get you the cursor’s coordinates (using xdotool getmouselocation), but I am unsure how you would check whether that’s above a link. What do you mean ‘above a folder’? That could be a directory icon in a GUI file manager, a word in a terminal, or a dozen other things, as I see it. – bp99 Jan 6 '20 at 8:34
  • 1
    @bertalanp99 I meant a document in a gui file manager/wm for that, I wouldn't be surprised if detecting that is impossible though, but when hovering over a link or text the cursor icon actually changes, just detecting these icon changes (state changes...) would be enough. – Cestarian Jan 6 '20 at 18:16

Getting the coordinates of mouse pointer.

Below is the script and configuring a keyboard shortcut to run it, we can copy mouse coordinates to the clipboard in x,y format. Here's how:

  1. Install xdotool for grabbing coordinates and xsel for managing the clipboard.

sudo apt-get install xdotool xsel

  1. Create a new script file with the following contents. Save the script and make it executable.
xdotool getmouselocation | grep -oP "[0-9]+ y:[0-9]+" | sed 's/ y:/,/' | tr -d '\n' | xsel --clipboard
  1. Create a custom keyboard shortcut that calls your script for the desired key combination
  • 1
    See also the xmouspos package from xautomation – Stéphane Chazelas Jan 6 '20 at 11:18
  • Hey man, I'm sorry but this isn't what I was asking. I was asking about a way to get the cursor state, not location (e.g. if it's hovering over a link or text or neutral). There's also a ebtter way to use this in a script, if you use eval $(xdotool getmouselocation --shell) it will set $Y and $X as the current coordinates of the mouse which you can use in whatever variables you wish. – Cestarian Jan 6 '20 at 17:19
  • 1
    @Cestarian, there's no such thing as "a cursor state", or if there's one it is one in the application whose visible window the cursor is positioned in so you'd have to query that application. – Stéphane Chazelas Jan 6 '20 at 18:31
  • @StéphaneChazelas sure there is, otherwise how would we have so much variation in cursor sets? If you hover your cursor over text it usually changes to an I shape, how would it do that without various states? Of course I'm not saying you're wrong that you'd have to query the focused application, maybe that is the only way (and if you can give me a satisfactory answer proving it I could end up accepting it too), I don't know (if I did I wouldn't be asking) but surely there has to be another way right? Like querying X11 for example? – Cestarian Jan 6 '20 at 19:24
  • 1
    @Cestarian the xfixes x11 extensions has support for "cursor image monitoring" but I don't think that it's supported in any way by xdotool, so if you want to use it from the shell you're out of luck I'm afraid. – pizdelect Jan 6 '20 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.