13

I use Gnome3 as my desktop interface with two monitors. I want to be able to move my mouse cursor from one monitor to the other using my keyboard, say with Ctrl + Alt + Right-key. How do I set this up?

8
  • @JacobVlijm Hey jacob! yes I notices your answer and I thank you! It looks to be compelling but the thing is that I am extremely busy finishing one project so I haven't checked it in detail yet, which is why I haven't accpedted it yet. When I get the time I will Dec 20, 2016 at 13:15
  • @JacobVlijm does it matter where I save the .py file? also, in the test-run the script you have written the same command twice Dec 22, 2016 at 19:16
  • @JacobVlijm I have two monitors at work, but right now I have only one, I assume that if I run the script in my current situation it should be a fuck up right? Dec 22, 2016 at 19:20
  • @JacobVlijm I run python3 /path/to/move_mouse.py left and it says :python3: can't open file '/path/to/move_mouse.py': [Errno 2] No such file or directory Dec 22, 2016 at 19:22
  • ok, I run python3 move_mouse.py in the same folder and it returns Traceback (most recent call last): File "move_mouse.py", line 5, in <module> arg = sys.argv[1] IndexError: list index out of range Dec 22, 2016 at 19:26

4 Answers 4

10

The script below can be run with two arguments: left and right.

In a two monitor- setup (left/right, in any configuration), the script will move the mouse to the center of either the left- or right monitor, depending on the argument.

enter image description here

The script

#!/usr/bin/env python3
import subprocess
import sys

arg = sys.argv[1]

screeninfo = [
    s for s in subprocess.check_output("xrandr").decode("utf-8").split()\
    if s.count("+") == 2
    ]

if arg == "left":
    match = [s for s in screeninfo if s.endswith("+0+0")][0]
elif arg == "right":
    match = [s for s in screeninfo if not s.endswith("+0+0")][0]

data = [item.split("x") for item in match.split("+")]
numbers = [int(n) for n in [item for sublist in data for item in sublist]]
coord = [str(int(n)) for n in [(numbers[0]/2)+numbers[2], (numbers[1]/2)+numbers[3]]]

subprocess.Popen(["xdotool", "mousemove", coord[0], coord[1]])

How to use

  1. The script needs xdotool:

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

  3. Test-run the script. Run from a terminal the commands:

    python3 /path/to/move_mouse.py left
    

    and

    python3 /path/to/move_mouse.py right
    
  4. If all works fine, add the commands to two available shortcuts: Choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the commands above.

Note

The script is not gnome -specific; it should run fine on all Ubuntu distro's.

Explanation

  • We can move the mouse to specific position by the command (e.g.):

    xdotool mousemove 300 500
    
  • To calculate the targeted positions (centered) in both screens, we need to understand connected screens are positioned in an arbitrary layout, e.g.

    enter image description here

  • In the output of xrandr, we can find information on both screens, their resolutions and their positions in the spanning layout, like:

    1280x1024+1680+128
    
  • To get the targeted position per screen (center), all we need to do is divide the first and second number (1280x1024, the resolution) by 2, subsequently add the two calculated positions to the offset per screen (+1680+128 in this case). That is exactltly what the script does.

    Subsequently, the found numbers are used as arguments to run the xdotool mousemove-command.

4
  • This doesn't seem to be working on Ubuntu 17.10 :(
    – rebelliard
    Nov 22, 2017 at 18:35
  • It should @rebelliard. What is the error message when run from terminal? Nov 22, 2017 at 18:38
  • I run xdotool mousemove 300 500 and quite simply, nothing happens. FYI, I have 2 displays + laptop.
    – rebelliard
    Nov 23, 2017 at 12:56
  • 1
    Could be you're using wayland, @rebelliard? xdotool only works with xorg.
    – MiniGod
    Jun 28, 2019 at 12:02
2

In Ubuntu 18.04 there are no custom shortcuts, so following @JacobVlijm's approach, I calculated my centers and wrote aliases in my shell:

sudo echo -e "\
alias lt='xdotool mousemove 640 360' \n\  # lt means left
alias rt='xdotool mousemove 1920 512'\   # rt means right
" >> .bashrc
1

For this very reason, I created a very quick script today: https://github.com/techgaun/easy-mouse-mover It allows you to move to left or right (and moves to center of screen) and is circular by design meaning if you are on left-most or right-most and if you try to move mouse to left or right respectively, it will go to the right-most or left-most respectively which I find personally useful as a keyboard shortcut to be able to move around freely. I hope it is useful for others. Also, it supports any number of monitors.

0

In Ubuntu Gnome, I use this very simple bash script to instantly move my mouse pointer to the opposite monitor on a dual-screen setup.

https://github.com/vijay-prema/swap-screen

You just need to enter your display resolutions into the script, and bind the script to a hotkey. If anyone knows how to make it auto-detect resolutions, let me know!

You must log in to answer this question.

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