jars121
jars121

Reputation: 1147

How to confine mouse to a specific monitor on a multi-monitor system?

I have a multi-monitor system, running two Python3.x Qt applications (PySide). I successfully designated which application is to run on which monitor. One application (and thus one monitor) is a user input terminal (basically a kiosk), while the other application (and thus the other monitor) is used solely for displaying information.

How can I confine the mouse to the kiosk monitor? I know I can 'disable' the second application such that mouse and keyboard events are ignored, but I'd really rather confine the actual mouse movements to the first monitor.

Is this something that has to make use of low-level Windows (Windows 7) functions, or can I implement something in Python within my applications to handle it?

Any input or guidance here would be much appreciated!

Thanks!

Upvotes: 2

Views: 859

Answers (1)

John Dorian
John Dorian

Reputation: 1904

Edit: originally postesd this answer as a response to a comment asking for some code I already happened to have written that was not in python but accomplished the goal. That script is farther down, here is a python script that only works on windows but will perform the same function using 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))

Posting for @PavelStrakhov. Here's a java script that will keep the cursor within a certain range of x coordinates (cross platform).

To run it save the below code as mouseWatcher.java, run $ javac mouseWatcher.java, and then running $ java mouseWatcher will start it.

Be careful though. If you run this and don't know how to stop it without your mouse and your set range doesn't allow you to move your mouse where you need to, you won't be able to stop it. :-)

/* 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);
        }
    }
}

Upvotes: 3

Related Questions