2016-08-21 27 views
1

我最近開始爲Leap Motion編寫代碼,這是我在多年前應用於開發時獲得的一些工具包。在公司進入beta階段的時候。當時,我家人擁有的筆記本電腦無法(以某種方式)運行Leap Motion的軟件或驅動程序或我爲其編寫的任何代碼。最近,我從學校購買了一臺「新」筆記本電腦,並試圖爲它編碼,而中提琴,它的工作原理!好吧,主要是。我一直在嘗試編寫一個小程序來根據我的手指在Leap Motion上方的位置移動鼠標,但似乎只是一個小問題。具有鼠標移動的行不起作用,儘管Eclipse沒有顯示任何錯誤,當我運行程序時,除了移動鼠標外,它的工作情況都很好。Java中的mouseMove()問題

爲防萬一它可能與問題有關,我在東芝Portege M780上運行Windows 10。

下面是代碼:

import com.leapmotion.leap.*; 
import java.awt.Dimension; 
import java.awt.Robot; 

class CustomListener extends Listener { 

    public Robot robot; 

    public void onConnect(Controller c) { 
     System.out.println("Connected."); 
    } 

    public void onFrame(Controller c) { 
     //System.out.println("Frame Available."); 
     Frame frame = c.frame(); 
     InteractionBox box = frame.interactionBox(); 
     for(Finger f : frame.fingers()) { 
      if(f.type() == Finger.Type.TYPE_INDEX) { 
       //Vector fingerpos = f.tipPosition(); 
       Vector boxFingerPos = box.normalizePoint(f.tipPosition()); 
       Dimension screen = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); 
       //robot.mouseMove(Math.round(screen.width * boxFingerPos.getX()), Math.round(screen.height - boxFingerPos.getY() * screen.height)); 
       int x = Math.round(boxFingerPos.getX()* screen.width); 
       int y = Math.round(screen.height - screen.height * boxFingerPos.getY()); 
       //robot.mouseMove(x, y); 
       System.out.println("Fingers: " + frame.fingers().count() + ", X: " + x + ", Y: " + y); 
       robot.mouseMove(x, y); 
      } 
     }  
    } 
} 

public class LeapMouse { 
    public static void main(String[] args) { 
     CustomListener l = new CustomListener(); 
     Controller c = new Controller(); 
     c.addListener(l); 
     System.out.println("Press enter to quit."); 
     c.setPolicy(Controller.PolicyFlag.POLICY_BACKGROUND_FRAMES); 
     c.setPolicy(Controller.PolicyFlag.POLICY_IMAGES); 
     c.setPolicy(Controller.PolicyFlag.POLICY_OPTIMIZE_HMD); 
     try { 
      System.in.read(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     c.removeListener(l); 
    } 
} 

在此之後,我也跟着上Leap Motion的相關網站上的說明使用自己的代碼庫,以編譯代碼和運行在管理員命令提示符的程序,但它不會動鼠標出於某種原因或其他原因。任何幫助表示讚賞。

謝謝!

回答

0

嗯,我設法弄清楚我的錯誤在哪裏。我不得不刪除創建機器人對象的方法,並將其移動到onFrame()方法並放入try/catch語句中。

import com.leapmotion.leap.*; 
import java.awt.Dimension; 
import java.awt.Robot; 

class CustomListener extends Listener { 

    public void onConnect(Controller c) { 
     System.out.println("Connected."); 
    } 

    public void onFrame(Controller c) { 
     Frame frame = c.frame(); 
     InteractionBox box = frame.interactionBox(); 
     for(Finger f : frame.fingers()) { 
      if(f.type() == Finger.Type.TYPE_INDEX) { 
       Vector boxFingerPos = box.normalizePoint(f.tipPosition()); 
       Dimension screen = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); 
       int x = Math.round(boxFingerPos.getX()* screen.width); 
       int y = Math.round(screen.height - screen.height * boxFingerPos.getY()); 
       try { 
        Robot robot = new Robot(); 
        robot.mouseMove(x, y); 
       } catch (AWTException z) { 
        z.printStackTrace(); 
       } 
      } 
     }  
    } 
} 

public class LeapMouse { 
    public static void main(String[] args) { 
     CustomListener l = new CustomListener(); 
     Controller c = new Controller(); 
     c.addListener(l); 
     System.out.println("Press enter to quit."); 
     c.setPolicy(Controller.PolicyFlag.POLICY_BACKGROUND_FRAMES); 
     c.setPolicy(Controller.PolicyFlag.POLICY_IMAGES); 
     c.setPolicy(Controller.PolicyFlag.POLICY_OPTIMIZE_HMD); 
     try { 
      System.in.read(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     c.removeListener(l); 
    } 
}