Ubuntu – Unity keyboard shortcut to move mouse (and focus) from one screen to another screen

keyboardmousemultiple-monitorsshortcut-keysunity

Enjoying multi-monitor setup at home and work, but wondering how to move focus between individual monitors (ie "screens") without a mouse?

Keyboards shortcuts are excellent for switching virtual desktops, and I looked at various options in ccsm but nothing came to mind.

I also look at other questions such as focus switch between separate X screens or links to dualscreenmouseutils and switchscreen but all of these seem to be concerned with separate screens per xorg.conf. These days, Unity "just works" with multiple monitors (via display port) so it is a bit of an embarrassment of riches.

But any hints how to navigate multiple (physical) screens within a single (virtual) Unity display would be very welcome.

Best Answer

Toggle between screens and (optionally) set focus on the (full screen) window

The script below will toggle (and "focus") between the left and the right screen if both screens are more or less center- or top aligned, and more or less of the same vertical resolution.
I assume in practically all situations of a left/right screen setup it will work.

The script

#!/usr/bin/env python3
import subprocess
# just a helper function
get = lambda cmd: subprocess.check_output(cmd).decode("utf-8")
# get the current mouse position
current = [int(n) for n in [it.split(":")[1] for it in get(["xdotool", "getmouselocation"]).split()[:2]]]
# get the x/y size of the left screen
screendata = [(s.split("x")[0], s.split("x")[1].split("+")[0]) for s in get(["xrandr"]).split() if "+0+0" in s ][0]
xy = [int(n) for n in screendata]
# see if the mouse is on the left- or right screen
if current[0] < xy[0]:
    # if the mouse currently is on the left screen, move it to the right (from the middle of the left screen)
    command = ["xdotool", "mousemove", "--sync", str(current[0]+xy[0]), str(xy[1]/2)]
else:
    # if the mouse currently is on the left screen, move it to the right (from the middle of the left screen)
    command = ["xdotool", "mousemove", "--sync", str(current[0]-xy[0]), str(xy[1]/2)]

subprocess.Popen(command)
# optional: click after the mouse move: comment out if not needed / wanted
subprocess.Popen(["xdotool", "click", "1"])

How to use

  1. The script need xdotool to be installed (!)

    sudo apt-get install xdotool
    
  2. Copy the script into an empty file, save it as toggle_screenloc.py

  3. Test-run it by the command:

    python3 /path/to/toggle_screenloc.py
    
  4. If all works fine, add it to a shortcut key: Choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command:

    python3 /path/to/toggle_screenloc.py
    

What it does exactly

If the script is run, it:

  1. derives the (left) screen's size (x/y) from the output of the xrandr command.
  2. it sees if the mouse is on the left- or right screen, by checking the (xdotool) command:

    xdotool getmouselocation
    

If the mouse pointer is located on the left screen:

  • it is moved to the middle (vertically) of the left screen, and horizontally moved to a position, equal to the current position + the width of the left screen.

If the mouse pointer is on the right screen:

  • actions are the opposite.

Subsequently, the mouse clicks a single time to set focus on the (possible) full screen application (optional).

Related Question