The LeapMotion controller may extends com.leapmotion.leap.Listener.
this is an example of an empty LeapMotion controller.
Sample Code
public class LeapMotionController extends Listener{
//Called once, when this Listener object is newly added to a Controller.
@Override
public void onInit(Controller controller) {
}
//Called when the Controller object connects to the Leap Motion software and the Leap Motion hardware device is plugged in, or when this Listener object is added to a Controller that is already connected.
@Override
public void onConnect(Controller controller) {
//enable gesture detected by the leap, here we enable all the gesture.
controller.enableGesture(Gesture.Type.TYPE_SWIPE);
controller.enableGesture(Gesture.Type.TYPE_CIRCLE);
controller.enableGesture(Gesture.Type.TYPE_SCREEN_TAP);
controller.enableGesture(Gesture.Type.TYPE_KEY_TAP);
}
//Called when the Controller object disconnects from the Leap Motion software or the Leap Motion hardware is unplugged.
@Override
public void onDisconnect(Controller controller) {
// Note: not dispatched when running in a debugger.
}
//Called when this Listener object is removed from the Controller or the Controller instance is destroyed.
@Override
public void onExit(Controller controller) {
}
//Called when a new frame of hand and finger tracking data is available.
@Override
public void onFrame(Controller controller) {
}
}
For more information:
https://developer.leapmotion.com/documentation/v2/java/api/Leap.Listener.html
The leapmotion library already detects four gestures: swipe, circle, screen tap, and key tap.
This is an example to use gesture in your code:
Sample Code
public void onFrame(Controller controller){
CircleGesture cg;
// the current frame
Frame frame = controller.frame();
// all gestures that have occurred since the specified frame.
GestureList gesture=frame.gestures();
for (Gesture g : gesture) {
//if the detected gesture is a Circle gesture.
if(g.type().equals(Gesture.Type.TYPE_CIRCLE)){
cg=new CircleGesture(g);
//if the CircleGesture is execute by a finger.
if(cg.pointable().isFinger()){
//do what you want.
}
}
}
}
for more information:
https://developer.leapmotion.com/documentation/v2/java/api/Leap.Gesture.html#id18
The following example is about how to detect fingers and use them.
Sample Code
public void onFrame(Controller controller) {
Frame frame = controller.frame();
//The list of Hand objects detected in this frame
HandList hands = frame.hands();
FingerList fingers;
for(Hand h :hands){
//he list of Finger objects detected in this frame that are attached to this hand
fingers=h.fingers();
//thumb
fingers.get(0);
//index finger
fingers.get(1);
//middle finger
fingers.get(2);
//ring finger
fingers.get(3);
//pinky
fingers.get(4);
}
}