FileDocCategorySizeDatePackage
MouseInfo.javaAPI DocJava SE 5 API4426Fri Aug 26 14:56:46 BST 2005java.awt

MouseInfo

public class MouseInfo extends Object
MouseInfo provides methods for getting information about the mouse, such as mouse pointer location and the number of mouse buttons.
version
1.2, 12/19/03
author
Roman Poborchiy
since
1.5

Fields Summary
Constructors Summary
private MouseInfo()
Private constructor to prevent instantiation.

    
Methods Summary
private static booleanareScreenDevicesIndependent(java.awt.GraphicsDevice[] gds)

        for (int i = 0; i < gds.length; i++) {
            Rectangle bounds = gds[i].getDefaultConfiguration().getBounds();
            if (bounds.x != 0 || bounds.y != 0) {
                return false;
            }
        }
        return true;
    
public static intgetNumberOfButtons()
Returns the number of buttons on the mouse. On systems without a mouse, returns -1.

exception
HeadlessException if GraphicsEnvironment.isHeadless() returns true
return
number of buttons on the mouse
since
1.5

        if (GraphicsEnvironment.isHeadless()) {
            throw new HeadlessException();
        }
        Object prop = Toolkit.getDefaultToolkit().
                              getDesktopProperty("awt.mouse.numButtons"); 
        if (prop instanceof Integer) {
            return ((Integer)prop).intValue();
        }

        // This should never happen.
        assert false : "awt.mouse.numButtons is not an integer property";
        return 0;
    
public static java.awt.PointerInfogetPointerInfo()
Returns a PointerInfo instance that represents the current location of the mouse pointer. The GraphicsDevice stored in this PointerInfo contains the mouse pointer. The coordinate system used for the mouse position depends on whether or not the GraphicsDevice is part of a virtual screen device. For virtual screen devices, the coordinates are given in the virtual coordinate system, otherwise they are returned in the coordinate system of the GraphicsDevice. See {@link GraphicsConfiguration} for more information about the virtual screen devices. On systems without a mouse, returns null.

If there is a security manager, its checkPermission method is called with an AWTPermission("watchMousePointer") permission before creating and returning a PointerInfo object. This may result in a SecurityException.

exception
HeadlessException if GraphicsEnvironment.isHeadless() returns true
exception
SecurityException if a security manager exists and its checkPermission method doesn't allow the operation
see
GraphicsConfiguration
see
SecurityManager#checkPermission
see
java.awt.AWTPermission
return
location of the mouse pointer
since
1.5

        if (GraphicsEnvironment.isHeadless()) {
            throw new HeadlessException();
        }

        SecurityManager security = System.getSecurityManager();
        if (security != null) {
            security.checkPermission(SecurityConstants.WATCH_MOUSE_PERMISSION);
        }

        Point point = new Point(0, 0);
        int deviceNum = Toolkit.getDefaultToolkit().getMouseInfoPeer().fillPointWithCoords(point);
        GraphicsDevice[] gds = GraphicsEnvironment.getLocalGraphicsEnvironment().
                                   getScreenDevices();
        PointerInfo retval = null;
        if (areScreenDevicesIndependent(gds)) {
            retval = new PointerInfo(gds[deviceNum], point);
        } else {
            for (int i = 0; i < gds.length; i++) {
                GraphicsConfiguration gc = gds[i].getDefaultConfiguration();
                Rectangle bounds = gc.getBounds();
                if (bounds.contains(point)) {
                    retval = new PointerInfo(gds[i], point);
                }
            }
        }

        return retval;