Here’s a simple class that lets your app respond to a Keyspan remote (URM - 15T).
@interface KeyspanObserver : NSObject {
} @end
static NSDictionary *KeyspanActions = nil; static BOOL ShouldRespondToKeyspanActionsInBackground = NO;
@implementation KeyspanObserver
static BOOL KeyspanObserverAction(SEL action, NSResponder *responder, NSNotification *note, NSMethodSignature *delegateSignature) { if ([responder respondsToSelector:action]) { [responder performSelector:action withObject:note]; return YES; } id delegate; if ([responder respondsToSelector:@selector(delegate)] && responder methodSignatureForSelector:@selector(delegate)] isEqual:delegateSignature] && [(delegate = [responder performSelector:@selector(delegate)]) respondsToSelector:action]) { [delegate performSelector:action withObject:note]; return YES; } return NO; }
([[NSResponder *)firstResponder { NSResponder *firstResponder = [[NSApp keyWindow] firstResponder]; NSArray *orderedWindows; if (!firstResponder && [(orderedWindows = [NSApp orderedWindows]) count]) firstResponder = [(NSWindow *)[orderedWindows objectAtIndex:0] firstResponder]; return firstResponder; }
(void)keyspanMessage:(NSNotification *)note {
if (![NSApp isActive] && !ShouldRespondToKeyspanActionsInBackground) return;
NSString *message = note userInfo] objectForKey:@”message”]; if ([message isKindOfClass:[[[NSString class]]) { SEL action = NSSelectorFromString([KeyspanActions objectForKey:message]); NSResponder *responder; NSMethodSignature *sig = [NSWindow instanceMethodSignatureForSelector:@selector(delegate)]; if (action) { BOOL didHandle = NO; for (responder = [self firstResponder]; responder && !(didHandle = KeyspanObserverAction(action, responder, note, sig)); responder = [responder nextResponder]); if (!didHandle) KeyspanObserverAction(action, NSApp, note, sig); } }
}
The responder chain is searched first for an action handler. If the responder chain doesn’t handle the action, NSApp and its delegate get a shot at it.
You can have a document object, an app delegate or a responder object handle remote button actions.
(void)keyspanRemotePlayButtonDown:(NSNotification *)note { NSLog(@”<%p>%s:”, self, PRETTY_FUNCTION); }
–zootbobbalu