2016-10-08 2 views
1

mousePressed에 대한 많은 스레드가 제대로 작동하지 않지만, 아직 내 문제에 대한 답변을 찾지 못했습니다. 재미와 경험을 위해 프로그램을 만들었지 만 Robot 클래스의 mousePressed 메서드로 문제가 발생했습니다. 이것은 내 코드입니다 :Java mousePress가 올바르게 작동하지 않습니다.

//Program to say "Hi!" by moving cursor 
//https://sketch.io/sketchpad/ 
//Open up sketchpad, run OCursor, then click on the sketchpad 

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.InputEvent; 

public class OCursor { 
    public static void main(String[] args) { 
     try { 
      // These are the screen coordinates 
      int cd1 = 400; 
      int cd2 = 600; 
      int cd3 = 700; 
      int cd4 = 750; 
      int cd5 = 800; 
      int cd6 = 1000; 
      int cd7 = 1100; 
      int cd8 = 1200; 
      int cd9 = 1400; 

      // This is the time and amount of steps 
      int t = 300, n = 5000; 

      // 5 second delay to click on sketchpad 
      Robot robot = new Robot(); 
      robot.delay(5000); 

      // Move and click the cursor 
      robot.mouseMove(cd2, cd5); 
      robot.mousePress(InputEvent.BUTTON1_MASK); 
      mouseGlide(cd2, cd5, cd2, cd1, t, n); 
      mouseGlide(cd2, cd2, cd5, cd2, t, n); 
      mouseGlide(cd5, cd5, cd5, cd1, t, n); 
      robot.mouseRelease(InputEvent.BUTTON1_MASK); 
      robot.delay(100); 

      Robot robot2 = new Robot(); 
      robot2.mouseMove(cd6, cd1); 
      robot2.mousePress(InputEvent.BUTTON1_MASK); 
      mouseGlide(cd6, cd1, cd8, cd1, t, n); 
      mouseGlide(cd7, cd1, cd7, cd5, t, n); 
      mouseGlide(cd6, cd5, cd8, cd5, t, n); 
      robot2.mouseRelease(InputEvent.BUTTON1_MASK); 
      robot2.delay(100); 

      Robot robot3 = new Robot(); 
      robot3.mouseMove(cd9, cd5); 
      robot3.mousePress(InputEvent.BUTTON1_MASK); 
      mouseGlide(cd9, cd5, cd9, cd4, t, n); 
      robot3.mouseRelease(InputEvent.BUTTON1_MASK); 
      robot3.delay(100); 
      robot3.mouseMove(cd9, cd3); 
      robot3.mousePress(InputEvent.BUTTON1_MASK); 
      mouseGlide(cd9, cd3, cd9, cd1, t, n); 
      robot3.mouseRelease(InputEvent.BUTTON1_MASK); 
      robot3.delay(100); 
     } 
     catch (AWTException err) { 
      err.printStackTrace(); 
     } 
    } 
    public static void mouseGlide(int x1, int y1, int x2, int y2, int t, int n) { 
     // "t" being time and "n" being amount of steps, with more steps being smoother 
     // mouseGlide code borrowed from http://stackoverflow.com/questions/9387483/how-to-move-a-mouse-smoothly-throughout-the-screen-by-using-java 
     try { 
      Robot robot = new Robot(); 
      double dx = (x2 - x1)/((double) n); 
      double dy = (y2 - y1)/((double) n); 
      double dt = t/((double) n); 
      for (int step = 1; step <= n; step++) { 
       Thread.sleep((int) dt); 
       robot.mouseMove((int) (x1 + dx * step), (int) (y1 + dy * step)); 
      } 
     } 
     catch (AWTException e) { 
      e.printStackTrace(); 
     } 
     catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

문제점은 mousePress가 실제로 제대로 릴리스되지 않습니다. 그것은 전혀 풀어주지 않거나 누를 때 풀어 놓을 것입니다. 나는 그것을 release하기 위해 필요할 때마다 mouseMove를 사용함으로써 그것을 극복했다. 그러나 이것은 필요하지 않아야한다. 나는 각 mouseRelease가 그것을 고쳐야하는 후에 지연을 두는 것을 들었다. 그러나 그것은 아무것도하지 않았다. 왜 제대로 작동하지 않는 이유와 왜 mouseRelease 대신 mouseMove를 사용해야하는지 이해할 수 없습니다. 나는 또한 모든 편지에 대해 새로운 로봇을 사용해 보았지만 아무런 차이가 없다.

그리는 각 문자 사이에 mouseMove없이 코드를 실행하면 제대로 작동하지 않습니다.

또 다른 사람의 코드를 mouseGlide로 사용했습니다. 사용 해본 적이 없기 때문에 Robot 클래스를 테스트하고 있기 때문입니다.

답변

0

InputEvent.BUTTON1_DOWN_MASK;을 사용해보세요. mousePress와 mouseRelease를 동일한 방법으로 사용하고 있습니다. 차이점은 하나 있습니다.

관련 문제