Ubuntu – Automatically move mouse to focused window


Is it possible to automatically move the mouse to anywhere in the window that has gained focus by a keystroke?

For example:

  1. There are two monitors.
  2. Mouse is near the left edge of the monitor on the left.
  3. Using Alt+Tab, a window residing at the monitor on the right is switched and you have to move the mouse to this window.
  4. It will be great to move the mouse into this window automatically or at least with a keyboard shortcut.

Best Answer

  • I just had the same need and ended up here looking for a solution to the problem.

    As it doesn't seem like something that anybody else solved anywhere else, I used my basic shell skills to create the following script, which does the job using xdotool:

    # Get geometry information of the currently active window.
    GEOMETRY=`xdotool getwindowgeometry $(xdotool getactivewindow)`                 
    # Extract information about the dimensions of the window and divide
    # both of them by 2.
    DIMENSIONS=$(echo "$GEOMETRY" | grep -Po "[0-9]+x[0-9]+")                       
    X=$(echo $DIMENSIONS | sed 's/x[0-9]\+//g')                                     
    Y=$(echo $DIMENSIONS | sed 's/[0-9]\+x//g')                                     
    X=$(expr $X / 2)                                                                
    Y=$(expr $Y / 2)                                                                
    # Move the mouse cursor to the middle of the active window.
    xdotool mousemove -w $(xdotool getactivewindow) $X $Y

    I've put this in a file and added a custom keyboard shortcut that runs it.