In Objective-C, you can use the LeapListener protocol to receive NSNotifications for Leap Motion events or use the LeapDelegate protocol to designate a single object to handle the events.
Methods:
The LeapListener protocol defines a set of methods that you can implement to respond to NSNotification messages dispatched by a LeapController object.
To use the LeapListener protocol, implement a class adopting the protocol and assign an instance of that class to a LeapController instance:
MYListener *listener = [[MYListener alloc] init]; LeapController *controller = [[LeapController alloc] initWithListener:listener];The controller subscribes the LeapListener instance to the appropriate NSNotifications for the Leap events. When a new frame of data is ready, the controller dispatches an NSNotification on the main application thread, which is handled by your [LeapListener onFrame:] implementation.
You can handle the other Leap events, onInit, onConnect, onDisconnect, onServiceConnect, onServiceDisconnect, onDeviceChange, onExit, onFocusGained, and onFocusLost in the same manner.
You must have a running NSRunLoop to receive NSNotification objects. This is usually present and running by default in a Cocoa application. Calling [LeapController addListener:] takes care subscribing the listener object to the appropriate notifications. The LeapListener object is the notification observer, while the LeapController object is the notification sender. You can also subscribe to notifications manually. For example, to subscribe to the OnFrame message, call:
[[NSNotificationCenter defaultCenter] selector:@selector(onFrame:) name:@"OnFrame" object:controller]]However, at least one listener must be added to the controller with [LeapController addListener:] or the controller does not bother to dispatch notification messages.
Using the LeapListener protocol is not mandatory. You can also use a delegate implementing the LeapDelegate protocol or simply poll the controller object (as described in the LeapController class overview).
Public Functions
- Since 1.0
- (void) onConnect:(NSNotification *) notificationDispatched when the LeapController object connects to the Leap software, or when this ListenerListener object is added to a controller that is already connected.
- Since 1.0
- Parameters
- notification -
The LeapController object dispatching the notification.
- (void) onDeviceChange:(NSNotification *) notificationCalled when a Leap Motion controller plugged in, unplugged, or the device changes state.
State changes include changes in frame rate and entering or leaving “robust” mode. Note that there is currently no way to query whether a device is in robust mode. You can use Frame::currentFramerate() to get the framerate.
- (void)onDeviceChange:(NSNotification *)notification { NSLog(@"Device Changed"); }
- Since
- 1.2
- Parameters
- notification -
The LeapController object dispatching the notification.
- (void) onDeviceFailure:(NSNotification *) notificationCalled 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 [LeapController failedDevices]. The members of this list provide the device pnpID and reason for failure.
- Since
- 3.0
- Parameters
- notification -
The LeapController object dispatching the notification.
- (void) onDisconnect:(NSNotification *) notificationDispatched when the LeapController object disconnects from the Leap software.
The controller can disconnect when the Leap device is unplugged, the user shuts the Leap software down, or the Leap software encounters an unrecoverable error.
- (void)onDisconnect:(NSNotification *)notification { NSLog(@"Disconnected"); }Note: When you launch a Leap-enabled application in a debugger, the Leap library does not disconnect from the application. This is to allow you to step through code without losing the connection because of time outs.
- Since 1.0
- Parameters
- notification -
The LeapController object dispatching the notification.
- (void) onExit:(NSNotification *) notificationDispatched when this LeapListener object is removed from the LeapController or the controller instance is destroyed.
- Since 1.0
- Parameters
- notification -
The LeapController object dispatching the notification.
- (void) onFocusGained:(NSNotification *) notificationCalled 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.
- (void)onFocusGained:(NSNotification *)notification { NSLog(@"Focus Gained"); }
- Since 1.0
- Parameters
- notification -
The LeapController object dispatching the notification.
- (void) onFocusLost:(NSNotification *) notificationCalled 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.
- (void)onFocusLost:(NSNotification *)notification { NSLog(@"Focus Lost"); }
- Since 1.0
- Parameters
- controller -
The parent LeapController object.
- (void) onFrame:(NSNotification *) notificationDispatched when a new LeapFrame containing hand and finger tracking data is available.
Access the new frame data using the [LeapController frame:] function.
- (void)onFrame:(NSNotification *)notification { NSLog(@"New LeapFrame"); LeapController *controller = (LeapController *)[notification object]; LeapFrame *frame = [controller frame:0]; //... }Note, the LeapController skips any pending onFrame notifications while your onFrame 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 [LeapController frame:] function. You can determine if any pending onFrame events were skipped by comparing the ID of the most recent frame with the ID of the last received frame.
- Since 1.0
- Parameters
- notification -
The LeapController object dispatching the notification.
- (void) onImages:(NSNotification *) notificationCalled when new images are available.
Access the NSArray containing the new images using the [LeapController images] function.
- (void)onImages:(NSNotification *)notification { NSLog(@"New image list"); LeapController *controller = (LeapController *)[notification object]; NSArray *images = [controller images]; //... }
- Since
- 2.2.1
- (void) onInit:(NSNotification *) notificationDispatched once, when the LeapController has finished initializing.
Only the first LeapListener added to the controller receives this notification.
- (void)onInit:(NSNotification *)notification { NSLog(@"Initialized"); //... }
- Since 1.0
- Parameters
- notification -
The LeapController object dispatching the notification.
- (void) onServiceChange:(NSNotification *) notificationCalled 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 [LeapController isPaused]. You can check the state of those policies you are interested in with [LeapController isPolicySet].
- Since
- 3.0
- Parameters
- notification -
The LeapController object dispatching the notification.
- (void) onServiceConnect:(NSNotification *) notificationDispatched when the LeapController object connects to the Leap software, or when this ListenerListener object is added to a controller that is already connected.
- Since 1.0
- Parameters
- notification -
The LeapController object dispatching the notification.
- (void) onServiceDisconnect:(NSNotification *) notificationDispatched when the LeapController object disconnects from the Leap software.
The controller can disconnect when the Leap device is unplugged, the user shuts the Leap software down, or the Leap software encounters an unrecoverable error.
- (void)onServiceDisconnect:(NSNotification *)notification { NSLog(@"Service Disconnected"); }Note: When you launch a Leap-enabled application in a debugger, the Leap library does not disconnect from the application. This is to allow you to step through code without losing the connection because of time outs.
- Since 1.0
- Parameters
- notification -
The LeapController object dispatching the notification.