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?
-
@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– PhoenixPersonDec 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– PhoenixPersonDec 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?– PhoenixPersonDec 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– PhoenixPersonDec 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– PhoenixPersonDec 22, 2016 at 19:26
4 Answers
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.
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
The script needs
xdotool
:sudo apt-get install xdotool
Copy the script into an empty file, save it as
move_mouse.py
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
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.
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.
-
-
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. Nov 23, 2017 at 12:56 -
1Could be you're using wayland, @rebelliard?
xdotool
only works with xorg.– MiniGodJun 28, 2019 at 12:02
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
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.
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!