Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Support for modifier keys & general clean up

  • Loading branch information...
commit 0c9b28756d0c7c47c30ddeb2c923a2753be937c4 1 parent 2e1abb1
@dreamcat4 authored
Showing with 159 additions and 90 deletions.
  1. +0 −2  MediaKeysApplication.h
  2. +159 −88 MediaKeysApplication.m
View
2  MediaKeysApplication.h
@@ -7,11 +7,9 @@
//
#import <Cocoa/Cocoa.h>
-#import <IOKit/hidsystem/ev_keymap.h>
@interface MediaKeysApplication : NSApplication
{
}
-- (BOOL)ignoreKey;
@end
View
247 MediaKeysApplication.m
@@ -9,6 +9,9 @@
#import "MediaKeysAppDelegate.h"
#import "MediaKeysApplication.h"
+#import <AppKit/NSEvent.h>
+#import <IOKit/hidsystem/ev_keymap.h>
+
@implementation MediaKeysApplication
BOOL mpd_started()
@@ -37,28 +40,41 @@ void mpd_stop()
}
}
-void mpd_play()
-{
- system("/opt/local/bin/mpc play");
-}
+void mpd_play () { system("/opt/local/bin/mpc play"); }
+void mpd_pause () { system("/opt/local/bin/mpc pause"); }
+void mpd_toggle () { system("/opt/local/bin/mpc toggle"); }
+void mpd_next () { system("/opt/local/bin/mpc next"); }
+void mpd_prev () { system("/opt/local/bin/mpc prev"); }
+void mpd_volume_up () { system("/opt/local/bin/mpc volume +10"); }
+void mpd_volume_down () { system("/opt/local/bin/mpc volume -10"); }
-void mpd_pause()
+BOOL _shift (int mod) { return !! (mod & NSShiftKeyMask); }
+BOOL _control (int mod) { return !! (mod & NSControlKeyMask); }
+BOOL _option (int mod) { return !! (mod & NSAlternateKeyMask); }
+BOOL _command (int mod) { return !! (mod & NSCommandKeyMask); }
+
+BOOL shift(int mod)
{
- system("/opt/local/bin/mpc pause");
+ if(_control(mod) && _option(mod) && _command(mod)) return false;
+ return _shift(mod);
}
-void mpd_toggle()
+BOOL control(int mod)
{
- system("/opt/local/bin/mpc toggle");
+ if(_shift(mod) && _option(mod) && _command(mod)) return false;
+ return _control(mod);
}
-void mpd_next()
+BOOL option(int mod)
{
- system("/opt/local/bin/mpc next");
+ if(_shift(mod) && _control(mod) && _command(mod)) return false;
+ return _option(mod);
}
-void mpd_prev()
+
+BOOL command(int mod)
{
- system("/opt/local/bin/mpc prev");
+ if(_shift(mod) && _control(mod) && _option(mod)) return false;
+ return _command(mod);
}
- (BOOL)ignoreKey
@@ -163,104 +179,158 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
exit(0);
}
-- (void)mediaKeyEvent: (int)key state: (BOOL)state repeat: (BOOL)repeat
+
+
+/* These are the same bits found in event modifier flags */
+enum {
+ MKNoModKey = 0,
+ MKShift = NSShiftKeyMask,
+ MKControl = NSControlKeyMask,
+ MKOption = NSAlternateKeyMask,
+ MKCommand = NSCommandKeyMask,
+};
+
+/* For when only 1 key was pressed (not in combination) */
+int respondOnOnlyOneModifier(int mod)
+{
+ if(shift(mod)) return MKShift;
+ if(control(mod)) return MKControl;
+ if(option(mod)) return MKOption;
+ if(command(mod)) return MKCommand;
+ return MKNoModKey;
+}
+
+- (void)mediaKeyEvent: (int)key modifier:(int)mod state: (BOOL)state repeat: (BOOL)repeat
{
- switch( key )
+ // Ignore all event except key down
+ if( state != 0 ) return;
+
+ switch( mod )
{
- case NX_KEYTYPE_PLAY:
- if( state == 0 )
- {
+ // No modifier key
+ case MKNoModKey:
+ switch( key )
+ {
+ case NX_KEYTYPE_PLAY:
; //Play pressed
if( ! [self ignoreKey])
{
mpd_start();
mpd_toggle();
}
- }
- break;
-
- case NX_KEYTYPE_NEXT:
- case NX_KEYTYPE_FAST:
- if( state == 0 )
- {
+ break;
+
+ case NX_KEYTYPE_NEXT:
+ case NX_KEYTYPE_FAST:
; //Next pressed or held
if( ! [self ignoreKey])
{
mpd_next();
}
- }
- break;
-
- case NX_KEYTYPE_PREVIOUS:
- case NX_KEYTYPE_REWIND:
- if( state == 0 )
- {
+ break;
+
+ case NX_KEYTYPE_PREVIOUS:
+ case NX_KEYTYPE_REWIND:
; //Previous pressed or held
if( ! [self ignoreKey])
{
mpd_prev();
}
- }
- break;
+ break;
- case NX_KEYTYPE_EJECT:
- if( state == 0 )
- {
+ case NX_KEYTYPE_EJECT:
; //Eject pressed
if( ! [self ignoreKey])
{
mpd_stop();
}
- }
- break;
-
- case NX_KEYTYPE_MUTE:
- if( state == 0 ) printf("%s\n","NX_KEYTYPE_MUTE");
- break;
-
- case NX_KEYTYPE_SOUND_UP:
- if( state == 0 ) printf("%s\n","NX_KEYTYPE_SOUND_UP");
- break;
-
- case NX_KEYTYPE_SOUND_DOWN:
- if( state == 0 ) printf("%s\n","NX_KEYTYPE_SOUND_DOWN");
- break;
-
-
- // These don't seem to work for Apple Wireless Keyboard (rev1)
- // Not sure about Apple Wireless keyboard revision 2
- // Some might respond specifically on certain MBP models only
- // Try them out in the XCode Debugger and see.
- case NX_KEYTYPE_CONTRAST_UP:
- printf("%s\n","NX_KEYTYPE_CONTRAST_UP");
- break;
- case NX_KEYTYPE_CONTRAST_DOWN:
- printf("%s\n","NX_KEYTYPE_CONTRAST_DOWN");
- break;
- case NX_KEYTYPE_LAUNCH_PANEL:
- printf("%s\n","NX_KEYTYPE_LAUNCH_PANEL");
- break;
- case NX_KEYTYPE_VIDMIRROR:
- printf("%s\n","NX_KEYTYPE_VIDMIRROR");
- break;
- case NX_KEYTYPE_ILLUMINATION_UP:
- printf("%s\n","NX_KEYTYPE_ILLUMINATION_UP");
- break;
- case NX_KEYTYPE_ILLUMINATION_DOWN:
- printf("%s\n","NX_KEYTYPE_ILLUMINATION_DOWN");
- break;
- case NX_KEYTYPE_ILLUMINATION_TOGGLE:
- printf("%s\n","NX_KEYTYPE_ILLUMINATION_TOGGLE");
- break;
- case NX_KEYTYPE_HELP:
- printf("%s\n","NX_KEYTYPE_HELP");
- break;
- case NX_POWER_KEY:
- printf("%s\n","NX_POWER_KEY");
- break;
- case NX_KEYTYPE_NUM_LOCK:
- printf("%s\n","NX_KEYTYPE_NUM_LOCK");
- break;
+ break;
+
+ case NX_KEYTYPE_MUTE:
+ printf("%s\n","NX_KEYTYPE_MUTE");
+ break;
+
+ case NX_KEYTYPE_SOUND_UP:
+ printf("%s\n","NX_KEYTYPE_SOUND_UP");
+ break;
+
+ case NX_KEYTYPE_SOUND_DOWN:
+ printf("%s\n","NX_KEYTYPE_SOUND_DOWN");
+ break;
+
+
+ // These don't seem to work for Apple Wireless Keyboard (rev1)
+ // Not sure about Apple Wireless keyboard revision 2
+ // Some might respond specifically on certain MBP models only
+ // Look in Console "All Messages" or the XCode Debugger to see.
+ case NX_KEYTYPE_CONTRAST_UP:
+ printf("%s\n","NX_KEYTYPE_CONTRAST_UP");
+ break;
+ case NX_KEYTYPE_CONTRAST_DOWN:
+ printf("%s\n","NX_KEYTYPE_CONTRAST_DOWN");
+ break;
+ case NX_KEYTYPE_LAUNCH_PANEL:
+ printf("%s\n","NX_KEYTYPE_LAUNCH_PANEL");
+ break;
+ case NX_KEYTYPE_VIDMIRROR:
+ printf("%s\n","NX_KEYTYPE_VIDMIRROR");
+ break;
+ case NX_KEYTYPE_ILLUMINATION_UP:
+ printf("%s\n","NX_KEYTYPE_ILLUMINATION_UP");
+ break;
+ case NX_KEYTYPE_ILLUMINATION_DOWN:
+ printf("%s\n","NX_KEYTYPE_ILLUMINATION_DOWN");
+ break;
+ case NX_KEYTYPE_ILLUMINATION_TOGGLE:
+ printf("%s\n","NX_KEYTYPE_ILLUMINATION_TOGGLE");
+ break;
+ case NX_KEYTYPE_HELP:
+ printf("%s\n","NX_KEYTYPE_HELP");
+ break;
+ case NX_POWER_KEY:
+ printf("%s\n","NX_POWER_KEY");
+ break;
+ case NX_KEYTYPE_NUM_LOCK:
+ printf("%s\n","NX_KEYTYPE_NUM_LOCK");
+ break;
+ }
+ break;
+
+ // shift + MediaKey pressed
+ case MKShift:
+ switch( key )
+ {
+ case NX_KEYTYPE_NEXT:
+ case NX_KEYTYPE_FAST:
+ ; //Next pressed or held
+ if( ! [self ignoreKey])
+ {
+ mpd_volume_up();
+ }
+ break;
+
+ case NX_KEYTYPE_PREVIOUS:
+ case NX_KEYTYPE_REWIND:
+ ; //Previous pressed or held
+ if( ! [self ignoreKey])
+ {
+ mpd_volume_down();
+ }
+ break;
+ }
+ break;
+
+ // control + MediaKey pressed
+ case MKControl:
+ break;
+
+ // option + MediaKey pressed
+ case MKOption:
+ break;
+
+ // command + MediaKey pressed
+ case MKCommand:
+ break;
}
}
@@ -270,12 +340,13 @@ - (void)sendEvent: (NSEvent*)event
{
int keyCode = (([event data1] & 0xFFFF0000) >> 16);
int keyFlags = ([event data1] & 0x0000FFFF);
+ int modFlags = [event modifierFlags];
int keyState = (((keyFlags & 0xFF00) >> 8)) ==0xA;
int keyRepeat = (keyFlags & 0x1);
- [self mediaKeyEvent: keyCode state: keyState repeat: keyRepeat];
+ int modifierKey = respondOnOnlyOneModifier(modFlags);
+ [self mediaKeyEvent: keyCode modifier: modifierKey state: keyState repeat: keyRepeat];
}
-
[super sendEvent: event];
}
@end
Please sign in to comment.
Something went wrong with that request. Please try again.