如何通过向左移动光标从屏幕 1 移动到屏幕 3,反之亦然?

ubu*_*ico 6 multiple-monitors unity

如果我非要把鼠标从左边屏幕一直移动到右边屏幕,距离还是挺大的。

有没有办法虚拟连接屏幕的两侧,就好像它们排列成一个圆圈一样?然后,只需将光标向左移动,我就可以从左侧屏幕移动到右侧屏幕。

Jac*_*ijm 5

连接屏幕“圆形”的脚本

下面的脚本将按照您的描述执行;如果鼠标触摸右侧(-最)屏幕的右边缘,鼠标将重新出现在左侧(-最)屏幕上。如果它接触到左侧屏幕的左侧,它会重新出现在右侧屏幕的右侧。

内置预防措施

该脚本假定屏幕以非重叠配置排列,x 方向,但它具有内置校正,以防屏幕未顶部对齐或具有不同的 y 分辨率。尽管在大多数情况下您不会遇到问题,但在以下情况下您会遇到问题,除非脚本考虑到 y 分辨率和/或(非)屏幕对齐可能存在的差异:


在此处输入图片说明

如果左侧屏幕的顶部低于右侧屏幕的顶部,则光标从右上角移动到左侧屏幕的顶部。可能未对齐的底部同上同上


剧本

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

def get_screendata():
    data = [s.split("+") for s in subprocess.check_output(["xrandr"]).decode("utf-8").split() \
            if s.count("+") == 2]
    # calculate total x-size of spanning screens
    x_span = sum([int(item[0].split("x")[0]) for item in data])
    # sort screens to find first/last screen (also for 2+ screens)
    data.sort(key=lambda x: x[1])
    # find (possible) screen offset of first/last screen and vertical area
    scr_first = data[0]; shiftl = int(scr_first[2])
    areal = [shiftl, shiftl+int(scr_first[0].split("x")[1])] 
    scr_last = data[-1]; shiftr = int(scr_last[2])
    arear = [shiftr, shiftr+int(scr_last[0].split("x")[1])]   
    return (x_span, areal, arear)

screendata = get_screendata()
x_span = screendata[0]; areal = screendata[1]; arear = screendata[2]
new_coords = []

while True:
    time.sleep(0.5)
    new_coords = []
    # read the current mouse position
    pos = [int(s.split(":")[-1]) for s in \
           subprocess.check_output(["xdotool", "getmouselocation"]).decode("utf-8").split()\
           if any(["x" in s, "y" in s])]
    # if the mouse is on the left of the first screen
    if pos[0] == 0:
        new_coords.append(x_span-2)
        if pos[1] <=  arear[0]:
            new_coords.append(arear[0]+2)
        elif pos[1] >= arear[1]:
            new_coords.append(arear[1]-2)
        else:
            new_coords.append(pos[1])
    # if the mouse is on the right of the last screen
    elif pos[0] > x_span-2:
        new_coords.append(2)
        if pos[1] <=  areal[0]:
            new_coords.append(areal[0]+2)
        elif pos[1] >= areal[1]:
            new_coords.append(areal[1]-2)
        else:
            new_coords.append(pos[1])
    # move the mouse
    if new_coords:
        subprocess.Popen(["xdotool", "mousemove", str(new_coords[0]), str(new_coords[1])])
Run Code Online (Sandbox Code Playgroud)

如何使用

  1. 脚本需要 xdotool

    sudo apt-get install xdotool
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将脚本复制到一个空文件中,另存为 circular_mouse.py
  3. 通过在终端中运行来测试运行脚本:

    python3 /path/to/circular_mouse.py
    
    Run Code Online (Sandbox Code Playgroud)

    您应该能够向左或向右无限移动鼠标,在屏幕上循环。

  4. 如果一切正常,请将其添加到启动应用程序:Dash > Startup Applications > 添加命令:

    /bin/bash -c "sleep 15 &&  python3 /path/to/circular_mouse.py" 
    
    Run Code Online (Sandbox Code Playgroud)