ControllerΒΆ

Controller Class Reference

The Controller class is your main interface to the Leap Motion Controller. More...

Inherits Interface.

Classes

enum  PolicyFlag
 The supported controller policies. More...
 

Public Member Functions

boolean addListener (Listener listener)
 Adds a listener to this Controller. More...
 
void clearPolicy (Controller.PolicyFlag policy)
 Requests clearing a policy. More...
 
Config config ()
 Returns a Config object, which you can use to query the Leap Motion system for configuration information. More...
 
 Controller ()
 Constructs a Controller object. More...
 
 Controller (Listener listener)
 Constructs a Controller object. More...
 
DeviceList devices ()
 The list of currently attached and recognized Leap Motion controller devices. More...
 
void enableGesture (Gesture.Type type, boolean enable)
 Enables or disables reporting of a specified gesture type. More...
 
void enableGesture (Gesture.Type type)
 Enables or disables reporting of a specified gesture type. More...
 
FailedDeviceList failedDevices ()
 A list of any Leap Motion hardware devices that are physically connected to the client computer, but are not functioning correctly. More...
 
Frame frame (int history)
 Returns a frame of tracking data from the Leap Motion software. More...
 
Frame frame ()
 Returns a frame of tracking data from the Leap Motion software. More...
 
boolean hasFocus ()
 Reports whether this application is the focused, foreground application. More...
 
ImageList images ()
 The most recent set of images from the Leap Motion cameras. More...
 
boolean isConnected ()
 Reports whether this Controller is connected to the Leap Motion service and the Leap Motion hardware is plugged in. More...
 
boolean isGestureEnabled (Gesture.Type type)
 Reports whether the specified gesture type is enabled. More...
 
boolean isPaused ()
 Reports whether the Leap Motion service is currently paused. More...
 
boolean isPolicySet (Controller.PolicyFlag policy)
 Gets the active setting for a specific policy. More...
 
boolean isServiceConnected ()
 Reports whether your application has a connection to the Leap Motion daemon/service. More...
 
long now ()
 Returns a timestamp value as close as possible to the current time. More...
 
Controller.PolicyFlag policyFlags ()
 This function has been deprecated. More...
 
boolean removeListener (Listener listener)
 Remove a listener from the list of listeners that will receive Leap Motion events. More...
 
void setPaused (boolean pause)
 Pauses or resumes the Leap Motion service. More...
 
void setPolicy (Controller.PolicyFlag policy)
 Requests setting a policy. More...
 
void setPolicyFlags (Controller.PolicyFlag flags)
 This function has been deprecated. More...
 

Detailed Description

The Controller class is your main interface to the Leap Motion Controller.

Create an instance of this Controller class to access frames of tracking data and configuration information. Frame data can be polled at any time using the Controller::frame() function. Call frame() or frame(0) to get the most recent frame. Set the history parameter to a positive integer to access previous frames. A controller stores up to 60 frames in its frame history.

Polling is an appropriate strategy for applications which already have an intrinsic update loop, such as a game. You can also add an instance of a subclass of Leap::Listener to the controller to handle events as they occur. The Controller dispatches events to the listener upon initialization and exiting, on connection changes, when the application gains and loses the OS input focus, and when a new frame of tracking data is available. When these events occur, the controller object invokes the appropriate callback function defined in your subclass of Listener.

To access frames of tracking data as they become available:

  1. Implement a subclass of the Listener class and override the Listener::onFrame() function.
  2. In your Listener::onFrame() function, call the Controller::frame() function to access the newest frame of tracking data.
  3. To start receiving frames, create a Controller object and add an instance of the Listener subclass to the Controller::addListener() function.

When an instance of a Listener subclass is added to a Controller object, it calls the Listener::onInit() function when the listener is ready for use. When a connection is established between the controller and the Leap Motion software, the controller calls the Listener::onConnect() function. At this point, your application will start receiving frames of data. The controller calls the Listener::onFrame() function each time a new frame is available. If the controller loses its connection with the Leap Motion software or device for any reason, it calls the Listener::onDisconnect() function. If the listener is removed from the controller or the controller is destroyed, it calls the Listener::onExit() function. At that point, unless the listener is added to another controller again, it will no longer receive frames of tracking data.

The Controller object is multithreaded and calls the Listener functions on its own thread, not on an application thread.

Since
1.0

Constructor & Destructor Documentation

Constructs a Controller object.

When creating a Controller object, you may optionally pass in a reference to an instance of a subclass of Leap::Listener. Alternatively, you may add a listener using the Controller::addListener() function.

Since
1.0
Controller ( Listener  listener)

Constructs a Controller object.

When creating a Controller object, you may optionally pass in a reference to an instance of a subclass of Leap::Listener. Alternatively, you may add a listener using the Controller::addListener() function.

SampleListener listenerSubclass = new SampleListener();
Controller leapController = new Controller(listenerSubclass);
Parameters
listenerAn instance of Leap::Listener implementing the callback functions for the Leap Motion events you want to handle in your application.
Since
1.0

Member Function Documentation

boolean addListener ( Listener  listener)

Adds a listener to this Controller.

The Controller dispatches Leap Motion events to each associated listener. The order in which listener callback functions are invoked is arbitrary. If you pass a listener to the Controller's constructor function, it is automatically added to the list and can be removed with the Controller::removeListener() function.

SampleListener listener = new SampleListener();
controller.addListener(listener);

The Controller does not keep a strong reference to the Listener instance. Ensure that you maintain a reference until the listener is removed from the controller.

Parameters
listenerA subclass of Leap::Listener implementing the callback functions for the Leap Motion events you want to handle in your application.
Returns
Whether or not the listener was successfully added to the list of listeners.
Since
1.0
void clearPolicy ( Controller.PolicyFlag  policy)

Requests clearing a policy.

Policy changes are completed asynchronously and, because they are subject to user approval or system compatibility checks, may not complete successfully. Call Controller::isPolicySet() after a suitable interval to test whether the change was accepted.

controller.clearPolicy(Controller.PolicyFlag.POLICY_OPTIMIZE_HMD);
Parameters
flagsA PolicyFlag value indicating the policy to request.
Since
2.1.6
Config config ( )

Returns a Config object, which you can use to query the Leap Motion system for configuration information.

controller.config().setFloat("Gesture.Circle.MinRadius", 15f);
controller.config().setFloat("Gesture.Circle.MinArc", 2f);
boolean saved = controller.config().save();
Returns
The Controller's Config object.
Since
1.0
DeviceList devices ( )

The list of currently attached and recognized Leap Motion controller devices.

The Device objects in the list describe information such as the range and tracking volume.

DeviceList connectedLeaps = controller.devices();

Currently, the Leap Motion Controller only allows a single active device at a time, however there may be multiple devices physically attached and listed here. Any active device(s) are guaranteed to be listed first, however order is not determined beyond that.

Returns
The list of Leap Motion controllers.
Since
1.0
void enableGesture ( Gesture.Type  type,
boolean  enable 
)

Enables or disables reporting of a specified gesture type.

controller.enableGesture(Gesture.Type.TYPE_CIRCLE);
Parameters
typeThe type of gesture to enable or disable. Must be a member of the Gesture::Type enumeration.
enableTrue, to enable the specified gesture type; False, to disable.
See also
Controller::isGestureEnabled()
Deprecated:
3.0
void enableGesture ( Gesture.Type  type)

Enables or disables reporting of a specified gesture type.

controller.enableGesture(Gesture.Type.TYPE_CIRCLE);
Parameters
typeThe type of gesture to enable or disable. Must be a member of the Gesture::Type enumeration.
enableTrue, to enable the specified gesture type; False, to disable.
See also
Controller::isGestureEnabled()
Deprecated:
3.0
FailedDeviceList failedDevices ( )

A list of any Leap Motion hardware devices that are physically connected to the client computer, but are not functioning correctly.

The list contains FailedDevice objects containing the pnpID and the reason for failure. No other device information is available.

FailedDeviceList badDevices = controller.failedDevices();
Since
3.0
Frame frame ( int  history)

Returns a frame of tracking data from the Leap Motion software.

Use the optional history parameter to specify which frame to retrieve. Call frame() or frame(0) to access the most recent frame; call frame(1) to access the previous frame, and so on. If you use a history value greater than the number of stored frames, then the controller returns an invalid frame.

if(controller.isConnected()) //controller is a Controller object
{
Frame frame = controller.frame(); //The latest frame
Frame previous = controller.frame(1); //The previous frame
}

You can call this function in your Listener implementation to get frames at the Leap Motion frame rate:

class FrameListener extends Listener
{
public void onFrame(Controller controller)
{
Frame frame = controller.frame(); //The latest frame
Frame previous = controller.frame(1); //The previous frame
//...
}
}
Parameters
historyThe age of the frame to return, counting backwards from the most recent frame (0) into the past and up to the maximum age (59).
Returns
The specified frame; or, if no history parameter is specified, the newest frame. If a frame is not available at the specified history position, an invalid Frame is returned.
Since
1.0
Frame frame ( )

Returns a frame of tracking data from the Leap Motion software.

Use the optional history parameter to specify which frame to retrieve. Call frame() or frame(0) to access the most recent frame; call frame(1) to access the previous frame, and so on. If you use a history value greater than the number of stored frames, then the controller returns an invalid frame.

if(controller.isConnected()) //controller is a Controller object
{
Frame frame = controller.frame(); //The latest frame
Frame previous = controller.frame(1); //The previous frame
}

You can call this function in your Listener implementation to get frames at the Leap Motion frame rate:

class FrameListener extends Listener
{
public void onFrame(Controller controller)
{
Frame frame = controller.frame(); //The latest frame
Frame previous = controller.frame(1); //The previous frame
//...
}
}
Parameters
historyThe age of the frame to return, counting backwards from the most recent frame (0) into the past and up to the maximum age (59).
Returns
The specified frame; or, if no history parameter is specified, the newest frame. If a frame is not available at the specified history position, an invalid Frame is returned.
Since
1.0
boolean hasFocus ( )

Reports whether this application is the focused, foreground application.

By default, your application only receives tracking information from the Leap Motion controller when it has the operating system input focus. To receive tracking data when your application is in the background, the background frames policy flag must be set.

if(controller.hasFocus()){
//perform action that requires application to be focused
}
Returns
True, if application has focus; false otherwise.
See also
Controller::setPolicyFlags()
Since
1.0
ImageList images ( )

The most recent set of images from the Leap Motion cameras.

ImageList images = controller.images();

Depending on timing and the current processing frame rate, the images obtained with this function can be newer than images obtained from the current frame of tracking data.

Returns
An ImageList object containing the most recent camera images.
Since
2.2.1
boolean isConnected ( )

Reports whether this Controller is connected to the Leap Motion service and the Leap Motion hardware is plugged in.

When you first create a Controller object, isConnected() returns false. After the controller finishes initializing and connects to the Leap Motion software and if the Leap Motion hardware is plugged in, isConnected() returns true.

You can either handle the onConnect event using a Listener instance or poll the isConnected() function if you need to wait for your application to be connected to the Leap Motion software before performing some other operation.

if(controller.isConnected()){
//perform action that requires a connected controller
}
Returns
True, if connected; false otherwise.
Since
1.0
boolean isGestureEnabled ( Gesture.Type  type)

Reports whether the specified gesture type is enabled.

if(!controller.isGestureEnabled(Gesture.Type.TYPE_SWIPE)){
controller.enableGesture(Gesture.Type.TYPE_SWIPE);
}
Parameters
typeThe type of gesture to check; a member of the Gesture::Type enumeration.
Returns
True, if the specified type is enabled; false, otherwise.
See also
Controller::enableGesture()
Deprecated:
3.0
boolean isPaused ( )

Reports whether the Leap Motion service is currently paused.

if(controller.isPaused()){
//Display message to user.
}
Returns
True, if the service is paused; false, otherwise.
Since
3.0
boolean isPolicySet ( Controller.PolicyFlag  policy)

Gets the active setting for a specific policy.

Keep in mind that setting a policy flag is asynchronous, so changes are not effective immediately after calling setPolicyFlag(). In addition, a policy request can be declined by the user. You should always set the policy flags required by your application at startup and check that the policy change request was successful after an appropriate interval.

If the controller object is not connected to the Leap Motion software, then the default state for the selected policy is returned.

boolean isImagePolicySet = controller.isPolicySet(Controller.PolicyFlag.POLICY_IMAGES);
Parameters
flagsA PolicyFlag value indicating the policy to query.
Returns
A boolean indicating whether the specified policy has been set.
Since
2.1.6
boolean isServiceConnected ( )

Reports whether your application has a connection to the Leap Motion daemon/service.

Can be true even if the Leap Motion hardware is not available.

Since
1.2
long now ( )

Returns a timestamp value as close as possible to the current time.

Values are in microseconds, as with all the other timestamp values.

Since
2.2.7
Controller.PolicyFlag policyFlags ( )

This function has been deprecated.

Use isPolicySet() instead.

Deprecated:
2.1.6
boolean removeListener ( Listener  listener)

Remove a listener from the list of listeners that will receive Leap Motion events.

A listener must be removed if its lifetime is shorter than the controller to which it is listening.

SampleListener listenerObject = new SampleListener();
controller.addListener(listenerObject);
// ... much later
controller.removeListener(listenerObject);
Parameters
listenerThe listener to remove.
Returns
Whether or not the listener was successfully removed from the list of listeners.
Since
1.0
void setPaused ( boolean  pause)

Pauses or resumes the Leap Motion service.

When the service is paused no applications receive tracking data and the service itself uses minimal CPU time.

Before changing the state of the service, you must set the POLICY_ALLOW_PAUSE_RESUME using the Controller::setPolicy() function. Policies must be set every time the application is run.

controller.setPolicy(Controller.PolicyFlag.POLICY_ALLOW_PAUSE_RESUME);
controller.setPaused(true);
Parameters
pauseSet true to pause the service; false to resume.
Since
3.0
void setPolicy ( Controller.PolicyFlag  policy)

Requests setting a policy.

A request to change a policy is subject to user approval and a policy can be changed by the user at any time (using the Leap Motion settings dialog). The desired policy flags must be set every time an application runs.

Policy changes are completed asynchronously and, because they are subject to user approval or system compatibility checks, may not complete successfully. Call Controller::isPolicySet() after a suitable interval to test whether the change was accepted.

controller.setPolicy(Controller.PolicyFlag.POLICY_ALLOW_PAUSE_RESUME);
controller.setPolicy(Controller.PolicyFlag.POLICY_BACKGROUND_FRAMES);
controller.setPolicy(Controller.PolicyFlag.POLICY_IMAGES);
controller.setPolicy(Controller.PolicyFlag.POLICY_OPTIMIZE_HMD);
Parameters
policyA PolicyFlag value indicating the policy to request.
Since
2.1.6
void setPolicyFlags ( Controller.PolicyFlag  flags)

This function has been deprecated.

Use setPolicy() and clearPolicy() instead.

Deprecated:
2.1.6