2017-06-09 8 views
1

ちょっと左のマウスボタンを押したままクリックが始まるオートクリッカーをコーディングしています。私の問題は、リリース時にクリックが止まらないことです!私はどんな助けもありがとう!これまでの私のコードは以下の通りです!Java Native Hookが無限ループに詰まっています

public void nativeMouseClicked(NativeMouseEvent e) { 
    System.out.println("Mouse Clicked: " + e.getClickCount()); 
} 

public static boolean released; 

public void nativeMouseReleased(NativeMouseEvent e) { 
    released=true; 
} 

public void nativeMousePressed(NativeMouseEvent e) { 

    released = false; 

     while(released != true) { 
      try { 
       Mouse.sendLeftClick(); 
      } catch (AWTException e1) { 
       e1.printStackTrace(); 
      } 
      double deviation = 31; 
      double mean = 95; 
      int min = 65; 
      int max = 117; 
      Random r = new Random(); 
      double randGauss = (r.nextGaussian() * deviation); 
      long delayPreClamp = Math.round(randGauss + mean); 
      long delay = (long) MathUtil.clamp(delayPreClamp, min, max); 
      try { 
       Thread.sleep(delay); 
      } catch (InterruptedException e2) { 
       e2.printStackTrace(); 
       } 
      } 
} 

public void nativeMouseMoved(NativeMouseEvent e) { 
    System.out.println("Mouse Moved: " + e.getX() + ", " + e.getY()); 
} 

public void nativeMouseDragged(NativeMouseEvent e) { 
    System.out.println("Mouse Dragged: " + e.getX() + ", " + e.getY()); 
} 

static Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName()); 

public static void main(String[] args) { 
    try { 
     logger.setLevel(Level.OFF); 
     GlobalScreen.registerNativeHook(); 
    } 
    catch (NativeHookException ex) { 
     System.err.println("There was a problem registering the native hook."); 
     System.err.println(ex.getMessage()); 

     System.exit(1); 
    } 

    // Construct the example object. 
    Inter inter = new Inter(); 

    // Add the appropriate listeners. 
    GlobalScreen.addNativeMouseListener(inter); 
    GlobalScreen.addNativeMouseMotionListener(inter); 
} 

ありがとうございます!

答えて

1

これは、whileループでイベントスレッドをブロックしているためです(nativeMousePressed)。この処理は、別のスレッドで行う必要があります。

Thread,ExecutorServiceなど

関連する問題