使Leap Motion鼠标在后台运行(Java)

时间:2015-02-26 19:48:23

标签: java eclipse background mouse leap-motion

我为启用了跳跃动作的鼠标编写了一个代码。程序正在运行我也想要它,但是,只要eclipse不在我的桌面上(如果我最小化窗口或打开谷歌浏览器或类似的东西)我不能再使用跳跃运动作为mosue 。当eclipse被最小化时,程序仍在运行,当我点击eclipse并使其成为主窗口时,leapmotion鼠标再次运行。所以我的问题是,当eclipse被最小化时,我如何使用我的跳跃运动鼠标java程序作为鼠标?

这是代码。再次,它运行和完美运行(抱歉它有点凌乱)

import com.leapmotion.leap.*;
import com.leapmotion.leap.Gesture.State;
import com.leapmotion.leap.Gesture.Type;

import java.awt.Dimension;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
class CustomListener extends Listener {

public Robot robot;

public void onConnect(Controller c)
{
    c.enableGesture(Gesture.Type.TYPE_CIRCLE);
    c.enableGesture(Gesture.Type.TYPE_SCREEN_TAP);
    c.enableGesture(Gesture.Type.TYPE_SWIPE);
    c.enableGesture(Gesture.Type.TYPE_KEY_TAP);
}

public void onFrame(Controller c)
{
    try {robot = new Robot();} catch(Exception e) {}
    Frame frame = c.frame();
    InteractionBox box = frame.interactionBox();

        for (Finger f : frame.fingers())
        {
        for (Finger t: frame.fingers())
            {
        for (Finger p: frame.fingers())
        {
        for (Finger m: frame.fingers())
        {
        for (Finger r: frame.fingers())
        {
        if (f.type() == Finger.Type.TYPE_INDEX)
            {

            if (t.type() == Finger.Type.TYPE_THUMB)
                {
                if (p.type() == Finger.Type.TYPE_PINKY)
                {
                    if (m.type() == Finger.Type.TYPE_MIDDLE)
                    {
                        if (r.type() == Finger.Type.TYPE_RING)
                        {


            Vector indexPos = f.stabilizedTipPosition();
            Vector thumbPos = t.stabilizedTipPosition();
            Vector middlePos = p.stabilizedTipPosition();

            Vector notTouching1 = new Vector(20, 20, 20);
            Vector notTouching2 = new Vector(-20, -20, -20);

            float leftClickX = indexPos.get(0) - thumbPos.get(0);
            float leftClickY = indexPos.get(1) - thumbPos.get(1);
            float leftClickZ = indexPos.get(2) - thumbPos.get(2);


            float rightClickX = middlePos.get(0) - thumbPos.get(0); 
            float rightClickY = middlePos.get(1) - thumbPos.get(1);  
            float rightClickZ = middlePos.get(2) - thumbPos.get(2); 

            Vector leftClick = new Vector(leftClickX, leftClickY, leftClickZ);
            Vector rightClick = new Vector(rightClickX, rightClickY, rightClickZ);


            /**
            if((leftClick.get(0)) < (notTouching1.get(0)) && (leftClick.get(0)) > (notTouching2.get(0)))
            {
                if ((leftClick.get(1)) < (notTouching1.get(1)) && (leftClick.get(1)) > (notTouching2.get(1)))
                {
                    if ((leftClick.get(2)) < (notTouching1.get(2)) && (leftClick.get(2)) > (notTouching2.get(2)))
                    {
                        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
                            try{Thread.sleep(50);} catch(Exception e){}
                        robot.mouseRelease(InputEvent.BUTTON1_MASK);
                            try{Thread.sleep(50);} catch(Exception e){}
                    }

                }

            }





                if((rightClick.get(0)) < (notTouching1.get(0)) && (rightClick.get(0)) > (notTouching2.get(0)))
                {
                    if ((rightClick.get(1)) < (notTouching1.get(1)) && (rightClick.get(1)) > (notTouching2.get(1)))
                    {
                        if ((rightClick.get(2)) < (notTouching1.get(2)) && (rightClick.get(2)) > (notTouching2.get(2)))
                        {
                            robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
                            try{Thread.sleep(50);} catch(Exception e) {}
                            robot.mouseRelease(InputEvent.BUTTON3_MASK);
                            try{Thread.sleep(50);} catch(Exception e){}


                        }

                    }

                }

                }
                }
                */
            if (f.isExtended() && m.isExtended() && r.isExtended() && p.isExtended()) // 4 fingers
            {
            for (Hand h : frame.hands())
            {
                Vector handPos = h.palmPosition();
                Vector boxHandPos = box.normalizePoint(handPos);
                Dimension screen = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
                robot.mouseMove((int)(screen.width * boxHandPos.getX()), (int)(screen.height - boxHandPos.getY() * screen.height));
            }
            }

    for (Gesture g : frame.gestures())
    {
        if (g.type() == Type.TYPE_CIRCLE)
        {
            CircleGesture circle = new CircleGesture(g);
            if (f.isExtended() && !m.isExtended() && !r.isExtended() && !p.isExtended()){
            if (circle.pointable().direction().angleTo(circle.normal()) <= Math.PI/4)
            {
                robot.mouseWheel(1);
                try{Thread.sleep(50);} catch(Exception e){}
            }
            else
            {
                robot.mouseWheel(-1);
                try{Thread.sleep(50);} catch(Exception e) {}
            }
            }
        }
        if (g.type() == Type.TYPE_KEY_TAP)
        {
            if (f.isExtended() && !m.isExtended()){ // 
            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
            try{Thread.sleep(50);} catch(Exception e) {}
            robot.mouseRelease(InputEvent.BUTTON1_MASK);

        }
            if (f.isExtended() && m.isExtended()){ // first two fingers
                robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
                try{Thread.sleep(50);} catch(Exception e) {}
                robot.mouseRelease(InputEvent.BUTTON3_MASK);
        }
        }
        if (g.type() == Type.TYPE_SCREEN_TAP)
        {
            if (f.isExtended() && !m.isExtended()){ // 
            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
            try{Thread.sleep(50);} catch(Exception e) {}
            robot.mouseRelease(InputEvent.BUTTON1_MASK);

        }
            if (f.isExtended() && m.isExtended()){ // first two fingers
                robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
                try{Thread.sleep(50);} catch(Exception e) {}
                robot.mouseRelease(InputEvent.BUTTON3_MASK);
        }
        }
        }
    }
}
}
        }

}
}
}
}
}
}
}
}
public class LeapMouse {

public static void main(String[] args) {

    CustomListener l = new CustomListener();
    Controller controller = new Controller();

    controller.addListener(l);

    try {
        System.in.read();
    }
    catch(Exception e ) {}
    controller.removeListener(l);
    }
}

1 个答案:

答案 0 :(得分:1)

您只需要设置background frames policy

controller.setPolicy(Controller.PolicyFlag.POLICY_BACKGROUND_FRAMES);