I've recently started coding for the Leap Motion, a little bit of kit I got a number of years ago when I applied to dev. for the company when it was in its beta stage. At the time, the laptop my family owned was unable (somehow) to run the software or drivers for the Leap Motion or any code I wrote for it. Recently, I got a "new" laptop from my school and tried to code for it, and viola, it works! Well, mostly. I've been trying to write a little program to move the mouse around the screen based on where my finger is above the Leap Motion, but there seems to be just a small problem. The line that has the mouse move doesn't work, though Eclipse is showing no errors and when I run the program, it works just fine except for moving the mouse.
Just in case it may have something to do with the problem, I am running Windows 10 on a Toshiba Portege M780.
Here is the code:
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);
}
}
After this, I followed the instructions on Leap Motion's website to compile code using their code library and ran the program on the Admin command prompt, but it wouldn't move the mouse for some reason or other. Any help is appreciated.
Thank you!