diff options
| -rw-r--r-- | DomeKey/HeadphoneKey.h | 12 | ||||
| -rw-r--r-- | DomeKey/HeadphoneKey.m | 27 | 
2 files changed, 39 insertions, 0 deletions
| diff --git a/DomeKey/HeadphoneKey.h b/DomeKey/HeadphoneKey.h index 0d4e729..c1b8e85 100644 --- a/DomeKey/HeadphoneKey.h +++ b/DomeKey/HeadphoneKey.h @@ -14,8 +14,20 @@ typedef enum KeyPress : BOOL {      KeyPressUp = NO  } KeyPress; +typedef enum HeadphoneButton : NSUInteger { +    HeadphoneButton_Play, +    HeadphoneButton_Up, +    HeadphoneButton_Down +} HeadphoneButton; + +static const unsigned int TIMEOUT_MILLISECONDS = 1000; +  @interface HeadphoneKey : NSObject {      NSArray *_mikeys; +    NSMutableArray *_key_buffer;  } +- (void)handleDeadKey:(HeadphoneButton)button; +- (void)maybeRunAction; +  @end diff --git a/DomeKey/HeadphoneKey.m b/DomeKey/HeadphoneKey.m index 0593173..d5fdb8d 100644 --- a/DomeKey/HeadphoneKey.m +++ b/DomeKey/HeadphoneKey.m @@ -14,6 +14,8 @@  {      self = [super init];      if (self) { +        _key_buffer = [[NSMutableArray alloc] initWithCapacity:5]; +          _mikeys = [DDHidAppleMikey allMikeys];          [_mikeys makeObjectsPerformSelector:@selector(setDelegate:)              withObject:self]; @@ -32,15 +34,40 @@          switch (usageId) {          case kHIDUsage_Csmr_PlayOrPause:              NSLog(@"Middle"); +            [self handleDeadKey:HeadphoneButton_Play];              break;          case kHIDUsage_Csmr_VolumeIncrement:              NSLog(@"Top"); +            [self handleDeadKey:HeadphoneButton_Up];              break;          case kHIDUsage_Csmr_VolumeDecrement:              NSLog(@"Bottom"); +            [self handleDeadKey:HeadphoneButton_Down];              break;          }      }  } +- (void)handleDeadKey:(HeadphoneButton)button +{ +    NSNumber *storable_button = [NSNumber numberWithUnsignedInteger:button]; +    [_key_buffer addObject:storable_button]; + +    [NSObject cancelPreviousPerformRequestsWithTarget:self +        selector:@selector(maybeRunAction) +        object:nil]; + +    NSTimeInterval timeout_seconds = TIMEOUT_MILLISECONDS / 1000.0; +    [self performSelector:@selector(maybeRunAction) +        withObject:nil +        afterDelay:timeout_seconds]; +} + +- (void)maybeRunAction +{ +    NSLog(@"%@", _key_buffer); + +    [_key_buffer removeAllObjects]; +} +  @end | 
