python - 如何将鼠标限制在多显示器系统上的特定显示器上?

标签 python qt pyside

我有一个多显示器系统,运行两个 Python3.x Qt 应用程序 (PySide)。我成功指定了哪个应用程序在哪个显示器上运行。一个应用程序(以及一个显示器)是用户输入终端(基本上是一个信息亭),而另一个应用程序(以及另一个显示器)仅用于显示信息。

如何将鼠标限制在信息亭显示器上?我知道我可以“禁用”第二个应用程序,以便忽略鼠标和键盘事件,但我真的宁愿将实际的鼠标移动限制在第一个监视器上。

这是否必须使用低级 Windows (Windows 7) 函数,或者我可以在应用程序中用 Python 实现某些功能来处理它吗?

如果您有任何意见或指导,我们将不胜感激!

谢谢!

最佳答案

编辑:最初发布这个答案是为了回应一条评论,要求提供一些我已经编写的代码,这些代码不是用 python 编写的,但实现了目标。该脚本位于更下方,这是一个仅适用于 Windows 的 Python 脚本,但将使用 win32api 执行相同的功能。

import win32api

# set these to whatever you want
xMin = 300
xMax = 800

running = True
while running:
        x, y = win32api.GetCursorPos()
        if x < xMin:
                win32api.SetCursorPos((xMin,y))
        elif x > xMax:
                win32api.SetCursorPos((xMax,y))

为@PavelStrakhov 发帖。这是一个 Java 脚本,它将把光标保持在一定的 x 坐标范围内(跨平台)。

要运行它,请将以下代码保存为 mouseWatcher.java,运行 $ javac mouseWatcher.java,然后运行 ​​$ java mouseWatcher 将启动它。

但要小心。如果您运行此程序并且不知道如何在没有鼠标的情况下停止它,并且您设置的范围不允许您将鼠标移动到需要的位置,那么您就赢了无法阻止它。 :-)

/* to control the mouse */
import java.awt.AWTException;
import java.awt.Robot;

/* to get the mouse position */
import java.awt.MouseInfo;

public class mouseWatcher {

    public static void main(String[] args) {
        /* the minimum and maximum x positions the cursor is allowed at */
        int xMin = 200;
        int xMax = 800;

        /* repeat forever */
        boolean running = true;
        while (running) {
            /* get the current cursor position */           
            int[] position = cursorGetPos();

            /* if they try to move it to the left of the acceptable area */
            if (position[0] < xMin)
                /* move the cursor the left most acceptable point */
                mouseMove(xMin, position[1]);

            /* if they try to move it to the right of the acceptable area */
            else if (position[0] > xMax)
                /* move the cursor to the right most acceptable point */
                mouseMove(xMax, position[1]);
        }
    }

    private static void mouseMove( int x, int y) {
        try {
            Robot r = new Robot();
            r.mouseMove(x, y);
        } catch (AWTException e) {
            throw new RuntimeException(e);
        }
    }

    private static int[] cursorGetPos() {
        int X = MouseInfo.getPointerInfo().getLocation().x; 
        int Y = MouseInfo.getPointerInfo().getLocation().y; 
        int[] coords = {X,Y};
        return coords;
    }

    private static void sleep( int milliseconds ) {
        try {
            Robot r = new Robot();
            r.delay(milliseconds);
        } catch(AWTException e) {
            throw new RuntimeException(e);
        }
    }
}

关于python - 如何将鼠标限制在多显示器系统上的特定显示器上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28616409/

相关文章:

基于 C++ 范围的 for 循环给出 SIGABRT,而普通循环工作正常

python - 如何在 QAbstractTableModel 中右对齐和垂直居中?

python - PySide + QGraphicsScene 仅显示为黑色

Python 列表 append 问题

python - 如何正确使用 PyDev 和两个不同的 Python 版本以及调用其他 python 脚本的脚本?

python - 如何在这个 pandas 数据框中分组、排序和计算差异?

c++ - 确定 QKeyEvent 的来源

python - 在 Twilio 的 Django/Python 中访问 DateCreated 和 DateUpdated

c++ - Qt 调试器输入不工作

python - 通过 QBuffer 到 QGeometry 的 Numpy 数组