我最近开始为Leap Motion编写代码,这是我多年前申请开发时的一些工具包。该公司处于测试阶段。当时,我家人拥有的笔记本电脑无法(以某种方式)运行Leap Motion的软件或驱动程序或我为其编写的任何代码。最近,我从我的学校买了一台“新”笔记本电脑并试图为它编码,而中提琴,它的确有效!好吧,主要是。我一直在尝试编写一个小程序,根据我的手指在Leap Motion上方的位置在屏幕上移动鼠标,但似乎只是一个小问题。虽然Eclipse没有显示任何错误,但是鼠标移动的行不起作用,当我运行程序时,除了移动鼠标外,它的工作正常。
万一它可能与问题有关,我在Toshiba 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网站上的说明使用他们的代码库编译代码并在Admin命令提示符下运行程序,但由于某种原因它不会移动鼠标。任何帮助表示赞赏。
谢谢!
答案 0 :(得分: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);
}
}