如何将鼠标限制在多显示器系统上的特定显示器上?
How to confine mouse to a specific monitor on a multi-monitor system?
我有一个多显示器系统,运行宁两个 Python3.x Qt 应用程序 (PySide)。我成功地指定了哪个应用程序是 运行 在哪个监视器上。一个应用程序(因此是一个显示器)是用户输入终端(基本上是信息亭),而另一个应用程序(因此是另一个显示器)仅用于显示信息。
如何将鼠标限制在自助服务终端显示器上?我知道我可以 'disable' 第二个应用程序忽略鼠标和键盘事件,但我真的宁愿将实际的鼠标移动限制在第一个显示器上。
这是必须使用低级 Windows (Windows 7) 函数的东西,还是我可以在我的应用程序中实现 Python 中的东西来处理它?
如有任何意见或指导,我们将不胜感激!
谢谢!
编辑:最初发布此答案是为了回应一条评论,要求提供一些我已经写过的代码,这些代码不在 python 中,但实现了目标。该脚本在更下方,这里是一个 python 脚本,它仅适用于 windows 但将使用 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
,然后运行ning$ 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);
}
}
}
我有一个多显示器系统,运行宁两个 Python3.x Qt 应用程序 (PySide)。我成功地指定了哪个应用程序是 运行 在哪个监视器上。一个应用程序(因此是一个显示器)是用户输入终端(基本上是信息亭),而另一个应用程序(因此是另一个显示器)仅用于显示信息。
如何将鼠标限制在自助服务终端显示器上?我知道我可以 'disable' 第二个应用程序忽略鼠标和键盘事件,但我真的宁愿将实际的鼠标移动限制在第一个显示器上。
这是必须使用低级 Windows (Windows 7) 函数的东西,还是我可以在我的应用程序中实现 Python 中的东西来处理它?
如有任何意见或指导,我们将不胜感激!
谢谢!
编辑:最初发布此答案是为了回应一条评论,要求提供一些我已经写过的代码,这些代码不在 python 中,但实现了目标。该脚本在更下方,这里是一个 python 脚本,它仅适用于 windows 但将使用 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
,然后运行ning$ 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);
}
}
}