10

I'm looking for a solution that would display the current mouse cursor coordinates in realtime (i.e. NOT xdotool and NOT xev).

I need to move the mouse to a certain position, then press Alt-Tab to flip to another window and record the coordinates there. (This would not move the mouse, so the coordinate display would stay the same).

There's a Windows program that works BEAUTIFULLY for this purpose - http://download.cnet.com/Cursor-Position/3000-2383_4-75449858.html?tag=mncol;1

...but it doesn't even start up in Wine.

Alternately, instead of displaying the coordinates, if this solution could copy the coordinates (in XXX,YYY format) to the clipboard, upon pressing a hotkey, that would be even better.

Any suggestions would be much appreciated!

P.S. I'm running Ubuntu 12.04 LTS.

1
  • 1
    I don't know a program that do this specifically in Ubuntu. I would try luck into Unix & Linux instead (if you do that, please delete this question, since they don't like crossposting).
    – Braiam
    Sep 18, 2013 at 2:56

3 Answers 3

8

Spartan solution: you can show the coordinates in real-time with xdotool if you create a bash script. Just execute this in a new Terminal:

while true; do xdotool getmouselocation; sleep 0.2; clear; done

Change the value after sleep to make it more or less "real-time". This requires bash, the default user shell in Ubuntu.

Better solution: if you have admin rights, install watch (sudo apt-get install watch), and then execute this in a new Terminal:

watch -ptn 0 "xdotool getmouselocation"

It uses xdotool but does not require bash.


Thank you b_laoshi for your suggestion!

2
  • 1
    You can do this without a while loop if desired... watch -n 0.1 xdotool getmouselocation
    – b_laoshi
    Jun 5, 2017 at 6:03
  • Thank you! :-) You can obtain a faster and precise result with: watch -p -t -n 0 xdotool getmouselocation. This has the advantage of being locale-agnostic and removes the unnecessary information. Jun 6, 2017 at 16:47
2

Get coordinates and copy to clipboard

Displaying the coordinates in real-time has already been addressed, so I won't repeat that answer here. By creating a 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
    
  2. Create a new script file with the following contents. Save the script and make it executable.

    #!/bin/bash
    xdotool getmouselocation | grep -oP "[0-9]+ y:[0-9]+" | sed 's/ y:/,/' | tr -d '\n' | xsel --clipboard
    
  3. Create a custom keyboard shortcut that calls your script for the desired key combination.

0

A complete solution to show the mouse pointer location, based on xdotool getmouselocation like the other answers, to display the location in its own xterm terminal:

xterm -geometry 9x1+0+0 +sb -sl 0 -T 'Mouse location' -e '
  set -e; tput civis; stty quit "^[";
  while true; do
    eval $(xdotool getmouselocation --shell) && printf "%4d %4d\r" $X $Y;
    sleep .05;
  done'

(this can be written on one line if need be).

Some explanations:

  • The -geometry 9x1+0+0 puts the terminal at the top left, with the expected size.
  • The +sb tells xterm not to display a scrollbar.
  • The -sl 0 avoids an unnecessary scrolling buffer.
  • The tput civis makes the cursor invisible (it would be annoying).
  • The stty quit "^[" allows one to quit with the Esc key instead of Ctrl-\.
  • Concerning the sleep value, I find 0.1 too large; 0.05 seems OK.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .