You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

双屏设置下如何用Python修改鼠标运动轴解决旋转屏适配问题

解决方案

要解决双屏旋转导致的鼠标轴混乱问题,你需要拦截系统全局的鼠标输入并修改坐标映射——pyglet仅能处理自身窗口内的鼠标事件,无法覆盖系统级操作,所以无效。以下是基于pynput和screeninfo的可行方案:

步骤1:安装依赖库

pip install pynput screeninfo

步骤2:编写代码实现坐标转换

from pynput.mouse import Listener, Controller
from screeninfo import get_monitors

# 获取所有显示器信息,确认旋转屏幕的索引(运行后可打印monitors查看)
monitors = get_monitors()
# 假设第二个显示器是旋转的(根据实际情况修改索引)
rotated_monitor = monitors[1]

mouse = Controller()
last_x, last_y = mouse.position

def on_move(x, y):
    global last_x, last_y
    # 计算鼠标移动增量
    dx = x - last_x
    dy = y - last_y
    last_x, last_y = x, y

    # 判断鼠标是否处于旋转屏幕范围内
    if (rotated_monitor.x <= x <= rotated_monitor.x + rotated_monitor.width and
        rotated_monitor.y <= y <= rotated_monitor.y + rotated_monitor.height):
        # 根据旋转方向调整坐标转换逻辑:
        # 示例为**顺时针旋转90度**,若为逆时针则改为 new_dx=dy, new_dy=-dx
        new_dx = -dy
        new_dy = dx
        # 计算修正后的鼠标位置
        new_x = x + new_dx - dx
        new_y = y + new_dy - dy
        # 移动鼠标到修正位置
        mouse.position = (new_x, new_y)
        # 返回False阻止原始鼠标事件传递,避免回弹
        return False
    # 在正常屏幕上不做处理
    return True

# 启动鼠标监听
with Listener(on_move=on_move) as listener:
    listener.join()

关键说明

  1. 确认旋转屏幕索引:运行代码前先执行print(monitors),查看每个屏幕的x/y(起始坐标)、width/height,确认哪个是旋转后的大显示器。
  2. 调整转换逻辑:
    • 顺时针旋转90度:new_dx = -dy, new_dy = dx
    • 逆时针旋转90度:new_dx = dy, new_dy = -dx
  3. 权限要求:部分系统需要以管理员权限运行该脚本,才能拦截全局鼠标事件。
  4. 停止脚本:按下Ctrl+C即可终止监听。

内容的提问来源于stack exchange,提问作者Benjamin Symalla

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 22:32:33