【问题标题】:Issue with mouseMove() in JavaJava 中 mouseMove() 的问题
【发布时间】:2016-08-21 04:32:28
【问题描述】:

我最近开始为 Leap Motion 编写代码,这是我多年前申请开发时获得的一些工具包。对于处于测试阶段的公司而言。当时,我家拥有的笔记本电脑(不知何故)无法运行 Leap Motion 的软件或驱动程序或我为它编写的任何代码。最近,我从学校买了一台“新”笔记本电脑并尝试为它编写代码,中提琴,它可以工作!嗯,大部分。我一直在尝试编写一个小程序,根据我的手指在 Leap Motion 上方的位置在屏幕上移动鼠标,但似乎只是一个小问题。移动鼠标的那一行不起作用,虽然 Eclipse 没有显示任何错误,当我运行程序时,除了移动鼠标之外它工作得很好。

以防万一它可能与问题有关,我在东芝 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 命令提示符下运行程序,但由于某种原因它不会移动鼠标。任何帮助表示赞赏。

谢谢!

【问题讨论】:

    标签: java mousemove leap-motion


    【解决方案1】:

    嗯,我设法弄清楚我的错误在哪里。我必须删除创建机器人对象的方法并将其移至 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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多