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

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).


Jacob Vlijm's answer has the right idea, but there are other ways. Here's my take:

#!/bin/bash

eval $(xdotool getmouselocation --shell)

if [ $Y -gt 1080 ]
then
    theta=0
else
    theta=180
fi

xdotool mousemove_relative --polar $theta 1080

eval $(xdotool getmouselocation --shell)

xdotool windowfocus $WINDOW

The simplification comes from using xdotool getmouselocation --shell, which conveniently dumps the variables into the running script. This also lets us focus the window without clicking, which could have side effects we don't want.

Note that in my case, my displays are stacked vertically, so I move the mouse up (theta = 0) or down (theta = 180). I also choose 1080px as the dividing line.


this repository may help you

https://github.com/Eitol/screen_focus_changer

You place the focus_changer.py left script in a fixed place (/ opt for example) And then add the keybinding / shortcut / hotkey in your settings

python3 /opt/focus_changer.py left # Focus to left

python3 /opt/focus_changer.py right # Focus to right