Styra datorn i luften som Tony Stark! (Skapa egna Leap Motion App) (5 / 6 steg)
Steg 5: Komplett kod!
< p > paketet starkmouse; < /p >< p > import java.io.IOException;
import java.awt.AWTException;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.MouseInfo;
import java.awt.Point;
importera java.awt.Robot; < /p >< p > import java.awt.event.InputEvent;
importera java.awt.event.KeyEvent; < /p >< p > import com.leapmotion.leap.*;
import com.leapmotion.leap.Controller.PolicyFlag; < /p >< p > allmän klass leapmoues {
offentliga statisk void main (String [] args) kastar AWTException {
Controller controller = nya Controller();
controller.setPolicyFlags(PolicyFlag.POLICY_BACKGROUND_FRAMES); < /p >< p > SampleListener lyssnaren = nya SampleListener();
controller.addListener(listener);
controller.enableGesture(Gesture.Type.TYPE_SCREEN_TAP);
controller.enableGesture(Gesture.Type.TYPE_SWIPE);
controller.enableGesture(Gesture.Type.TYPE_CIRCLE); < /p >< p > System.out.println ("Tryck Enter för att avsluta...");
försök {
System.in.Read();
} fånga (IOException e) {
e.printStackTrace();
} < /p >< p > controller.removeListener(listener);
}
} < /p >< p > klass SampleListener sträcker sig lyssnaren {< /p >< p > boolean readyForControl = false;
int screenWidth;
int screenHeight;
booleska iBoxGet = false;
InteractionBox iBox = null;
Robot robot;
booleska isMoving = false;
Boolean unGrip = false;
booleska wasFacingDown = sant;
booleska wasInTabState = false;
booleska wasTabbing = false;
booleska justCircleGestured = false;
booleska isResizing = false; < /p >< p > public void VidAnslutning (Controller controller) {
System.out.println("Connected");
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice();
screenWidth = gd.getDisplayMode().getWidth();
screenHeight = gd.getDisplayMode().getHeight();
System.out.println ("Skärmupplösning: X:" + screenWidth + ", H:"
+ screenHeight);
readyForControl = sant;
försök {
robot = nya Robot();
robot.setAutoDelay(5);
} fånga (AWTException e) {
TODO autogenererade catch-block
e.printStackTrace();
}
} < /p >< p > public void onFrame (Controller controller) {
Bildruta = controller.frame(); Den senaste bildrutan
Frame tidigare = controller.frame(1); Den föregående bildrutan
System.out.println ("Frame tillgängliga");
om (! iBoxGet) {
iBox = frame.interactionBox();
iBoxGet = sant;
System.out.println ("interaktion box set!");
}
Pointable furthestFront = frame.pointables().frontmost();
Hand höger = frame.hands().rightmost();
Vektorn palmV = rightHand.palmVelocity();
System.out.println ("Velocity: X:" + palmV.getX() + ", Y:" +
palmV.getY()
+ ", Z:" + palmV.getZ());
Vektorn palmN = rightHand.palmNormal();
System.out.println ("Normal: X:" + palmN.getX() + ", Y:"
+ palmN.getY() + ", Z:" + palmN.getZ()); < /p >< p > Point mouseLoc = MouseInfo.getPointerInfo().getLocation();
int currentMouseX = mouseLoc.x;
int currentMouseY = mouseLoc.y; < /p >< p > om (readyForControl & & rightHand.confidence() >.15) {
om (! isMoving & &! wasInTabState & & frame.hands().count() > 1) {
Lämna vänstra = frame.hands().leftmost();
om (leftHand.pinchStrength() > 8
& & rightHand.pinchStrength() > 8) {
om (! isResizing) {
System.out.println("resizing...");
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyRelease(KeyEvent.VK_SPACE);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_S);
robot.keyPress(KeyEvent.VK_DOWN);
robot.keyPress(KeyEvent.VK_RIGHT);
robot.keyRelease(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_RIGHT);
isResizing = sant;
}
} annat {
IF(isResizing) {
System.out.println ("storleksändring komplett!");
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
isResizing = false;
}
}
}
System.out.println ("förtroende:" + rightHand.confidence());
om (rightHand.grabStrength() >.99 & &! wasInTabState & &! isResizing) {
om (! isMoving & & palmN.getY() <.8) {
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyRelease(KeyEvent.VK_SPACE);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_R);
robot.keyRelease(KeyEvent.VK_R); < /p >< p > robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyRelease(KeyEvent.VK_SPACE);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_M);
robot.keyRelease(KeyEvent.VK_M);
robot.keyPress(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_DOWN);
isMoving = sant;
} < /P >< p > / / System.out.println(rightHand.grabStrength());
} annat {
System.out.println ("inte gripa tag");
om (isMoving) {< /p >< p > robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
isMoving = false; < /p >< p > om (palmN.getX()! = 0 & & palmN.getY()! = 0
& & palmN.getZ()! = 0) {
om (palmN.getY() <-.1 & & palmN.getZ() >-. 8) {
om (currentMouseY < = 8) {
robot.keyPress(KeyEvent.VK_WINDOWS);
robot.keyPress(KeyEvent.VK_UP);
robot.keyRelease(KeyEvent.VK_WINDOWS);
robot.keyRelease(KeyEvent.VK_UP);
} annat {
om (screenWidth - currentMouseX < = 12) {
robot.keyPress(KeyEvent.VK_WINDOWS);
robot.keyPress(KeyEvent.VK_RIGHT);
robot.keyRelease(KeyEvent.VK_WINDOWS);
robot.keyRelease(KeyEvent.VK_RIGHT);
} else om (currentMouseX < = 12) {
robot.keyPress(KeyEvent.VK_WINDOWS);
robot.keyPress(KeyEvent.VK_LEFT);
robot.keyRelease(KeyEvent.VK_WINDOWS);
robot.keyRelease(KeyEvent.VK_LEFT);
}
}
} annat {
System.out.println ("Normal: X:" + palmN.getX()
+ ", Y:" + palmN.getY() + ", Z:"
+ palmN.getZ());
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyRelease(KeyEvent.VK_SPACE);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_N);
robot.keyRelease(KeyEvent.VK_N);
}
}
}
} < /p >< p > om (! isMoving & &! isResizing) {
om (palmN.getY() <-.8 & & palmN.getZ() >-. 5) {
wasFacingDown = sant;
wasTabbing = false;
om (wasInTabState) {
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
wasInTabState = false;
}
} else om (palmN.getY() > = 8 & & wasFacingDown
& &! wasInTabState) {
System.out.println ("Alt tabbing");
wasFacingDown = false;
wasInTabState = true; < /p >< p > wasTabbing = false;
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_TAB);
robot.Delay(100);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_ALT);
försök {
Runtime.getRuntime () () .exec
"cmd/c start" + "C:\\WindowSwitcher.lnk");
} fånga (IOException e) {
TODO autogenererade catch-block
e.printStackTrace();
}
robot.Delay(300);
} else om (wasInTabState & &! wasFacingDown & &! wasTabbing
& & palmN.getY() <.45) {< /p >< p > wasTabbing = sant;
} else om (wasInTabState & &! wasFacingDown & & wasTabbing
& & palmN.getY() > 75) {
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
wasTabbing = false;
}
} < /p >< p > / *
* if (! isMoving & &! wasInTabState) {/ * if(palmN.getZ() < =-.7 & &
* rightHand.grabStrength() <. 1) {
* System.out.println("Palm vertical velocity:" +
* rightHand.palmVelocity().getY()); flyta resultVerticalV =
* Math.round(Math.abs(rightHand.palmVelocity().getY()) - 1);
* //if(resultVerticalV > 0) {robot.mouseWheel((int)
* Math.round(((rightHand.palmVelocity().getY()) / 500))); //}
* euro} annat {
* / < /p >< p > om (! isMoving & &! wasInTabState & & frame.gestures().count() > 0
& & frame.hands().count() == 1 & &! isResizing) {
CircleGesture circleGesture = ny CircleGesture (frame
.Gestures().get(0));
System.out.println ("Pinch styrka:" +
rightHand.pinchStrength());
om (circleGesture.durationSeconds() >.5 & &! justCircleGestured
& & rightHand.pinchStrength() > 8) {
System.out.println ("stängt ett fönster!");
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_F4);
robot.keyRelease(KeyEvent.VK_F4);
robot.keyRelease(KeyEvent.VK_ALT);
justCircleGestured = sant;
}
} annat {
justCircleGestured = false;
} < /p >< p > float xSpeed = (palmV.getX() / 6);
flyta ySpeed = (palmV.getY() / 6);
System.out.println ("xSpeed:" + xSpeed + ", ySpeed:" + ySpeed);
robot.mouseMove((int) (currentMouseX + xSpeed),
(int) (currentMouseY - ySpeed)); < /p >< p > / /}
}
}
} < /p >