Listener

Methods:

class Leap.Listener

The Listener class defines a set of callback functions that you can override in a subclass to respond to events dispatched by the Controller object.

To handle Leap Motion events, create an instance of a Listener subclass and assign it to the Controller instance. The Controller calls the relevant Listener callback function when an event occurs, passing in a reference to itself. You do not have to implement callbacks for events you do not want to handle.

The Controller object calls these Listener functions from a thread created by the Leap Motion library, not the thread used to create or set the Listener instance.

New in version 1.0.

classmethod Listener()

Constructs a Listener object.

If you override the __init__() method in a subclass, you must call the super class __init__() method.

New in version 1.0.

on_connect(controller)

Called when the Controller object connects to the Leap Motion software and hardware, or when this Listener object is added to a Controller that is already connected.

def on_connect(self, controller):
    print "Connected"
Parameters:controller (Controller) – The Controller object invoking this callback function.

New in version 1.0.

on_device_change(controller)

Called when a Leap Motion controller is plugged in, unplugged, or the device changes state.

State changes include entering or leaving robust mode and low resource mode. Note that there is no direct way to query whether the device is in these modes, although you can use Controller.is_lighting_bad to check if there are environmental IR lighting problems.

def on_device_change(self, controller):
    print "Device change"
Parameters:controller (Controller) – The Controller object invoking this callback function.

New in version 1.2.

on_device_failure(controller)

Called when a Leap Motion controller device is plugged into the client computer, but fails to operate properly.

Get the list containing all failed devices using Controller.failed_devices. The members of this list provide the device pnpID and reason for failure.

Parameters:controller (Controller) – The Controller object invoking this callback function.

New in version 2.3.2.

on_diagnostic_event(controller)

Called when the results of a device diagnostic check are available.

Request a device diagnostic check using Controller.request_diagnostic().

Parameters:controller (Controller) – The Controller object invoking this callback function.

New in version 2.3.2.

on_disconnect(controller)

Called when the Controller object disconnects from the Leap Motion software. The controller can disconnect when the Leap Motion device is unplugged, the user shuts the Leap Motion software down, or the Leap Motion software encounters an unrecoverable error.

def on_disconnect(self, controller):
    # Note: not dispatched when running in a debugger.
    print "Disconnected"

Note: When you launch a Leap-enabled application in a debugger, the Leap Motion library does not disconnect from the application. This is to allow you to step through code without losing the connection because of time outs.

Parameters:controller (Controller) – The Controller object invoking this callback function.

New in version 1.0.

on_exit(controller)

Called when this Listener object is removed from the Controller or the Controller instance is destroyed.

def on_exit(self, controller):
    print "Exited"
Parameters:controller (Controller) – The Controller object invoking this callback function.

New in version 1.0.

on_focus_gained(controller)

Called when this application becomes the foreground application.

Only the foreground application receives tracking data from the Leap Motion Controller. This function is only called when the controller object is in a connected state.

def on_focus_gained(self, controller):
    print "Focused"
Parameters:controller (Controller) – The Controller object invoking this callback function.

New in version 1.0.

on_focus_lost(controller)

Called when this application loses the foreground focus.

Only the foreground application receives tracking data from the Leap Motion Controller. This function is only called when the controller object is in a connected state.

def on_focus_lost(self, controller):
    print "Unfocused"
Parameters:controller (Controller) – The Controller object invoking this callback function.

New in version 1.0.

on_frame(controller)

Called when a new frame of hand and finger tracking data is available. Access the new frame data using the Controller.frame() function.

def on_frame(self, controller):
    print "Frame available"

Note, the Controller skips any pending on_frame events while your on_frame handler executes. If your implementation takes too long to return, one or more frames can be skipped. The Controller still inserts the skipped frames into the frame history. You can access recent frames by setting the history parameter when calling the Controller.frame() function. You can determine if any pending on_frame events were skipped by comparing the ID of the most recent frame with the ID of the last received frame.

Parameters:controller (Controller) – The Controller object invoking this callback function.

New in version 1.0.

on_images(controller)

Called when a new set of images from the Leap Motion cameras is available.

def on_images(self, controller):
    print "Images available"
    images = controller.images
    left_image = images[0]
    right_image = images[1]
Parameters:controller (Controller) – The Controller object invoking this callback function.

New in version 2.2.1.

on_init(controller)

Called once, when this Listener object is newly added to a Controller.

def on_init(self, controller):
    print "Initialized"
Parameters:controller (Controller) – The Controller object invoking this callback function.

New in version 1.0.

on_service_change(controller)

Called when the Leap Motion service is paused or resumed or when a controller policy is changed.

The service can change states because the computer user changes settings in the Leap Motion Control Panel application or because an application connected to the service triggers a change. Any application can pause or unpause the service, but only runtime policy changes you make apply to your own application.

You can query the pause state of the controller with Controller.is_paused(). You can check the state of those policies you are interested in with Controller.is_policy_set().

Parameters:controller (Controller) – The Controller object invoking this callback function.

New in version 2.3.2.

on_service_connect(controller)

Called when the Leap Motion daemon/service connects to your application :py:class`Controller`.

def on_service_connect(self, controller):
    print "Service connected"
Parameters:controller (Controller) – The Controller object invoking this callback function.

New in version 1.2.

on_service_disconnect(controller)

Called when the Leap Motion daemon/service disconnects from your application :py:class`Controller`.

Normally, this callback is not invoked. It is only called if some external event or problem shuts down the service or otherwise interrupts the connection.

def on_service_connect(self, controller):
    print "Service connected"
Parameters:controller (Controller) – The Controller object invoking this callback function.

New in version 1.2.