2017-10-18 79 views
0

我想写一个程序,根据时间,将鼠标光标移到某个坐标上,而不管用户。我使用Robot编写了一个简单的代码,但遇到了一个问题......我有两个显示器,根据当前显示器的不同,光标移动不正确,请告诉我如何解决问题。JAVA机器人mouseMove 2显示器

下面的代码是什么,我试图创建...

GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment(); 

    GraphicsDevice[] graphicsDevices = graphicsEnvironment.getScreenDevices(); 

    for(int i=0; i < graphicsDevices.length; i++) 
    { 
     System.out.println(graphicsDevices[i]);    
    } 

    try { 

     //Robot robot = new Robot(MouseInfo.getPointerInfo().getDevice());    

     Robot robot = new Robot();    

     while(true) 
     { 
      robot.mouseMove(-1640, -3); 

      robot.mousePress(InputEvent.BUTTON1_MASK); 
      robot.mouseRelease(InputEvent.BUTTON1_MASK); 

      Thread.sleep(10000); 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

回答

0

你应该得到解决的思想工作,然后从那里移动。你正在做ABSOLUTE移动,他们将在不同的设置工作不同。

您应使用此代码

GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); 
int width = gd.getDisplayMode().getWidth(); 
int height = gd.getDisplayMode().getHeight(); 

从那里你可以:

Robot robot = new Robot(); 
robot.mouseMove (width-10, height+3); 

所以,你会移动相对到显示器的规格。我希望我能帮上忙。