2017-10-18 73 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); 

所以,你會移動相對到顯示器的規格。我希望我能幫上忙。