Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I do this by subclassing NSApplication (and setting that class as the application class in my target's info) with the following:</p> <pre><code>#import &lt;IOKit/hidsystem/ev_keymap.h&gt; </code></pre> <p>...</p> <pre><code>- (void)mediaKeyEvent:(int)key state:(BOOL)state { switch (key) { // Play pressed case NX_KEYTYPE_PLAY: if (state == NO) [(TSAppController *)[self delegate] togglePlayPause:self]; break; // FF pressed case NX_KEYTYPE_FAST: if (state == YES) [(TSAppController *)[self delegate] seekForward:self]; break; // RW pressed case NX_KEYTYPE_REWIND: if (state == YES) [(TSAppController *)[self delegate] seekBack:self]; break; } } - (void)sendEvent:(NSEvent *)event { // Catch media key events if ([event type] == NSSystemDefined &amp;&amp; [event subtype] == NX_SUBTYPE_AUX_CONTROL_BUTTONS) { int keyCode = (([event data1] &amp; 0xFFFF0000) &gt;&gt; 16); int keyFlags = ([event data1] &amp; 0x0000FFFF); int keyState = (((keyFlags &amp; 0xFF00) &gt;&gt; 8)) == 0xA; // Process the media key event and return [self mediaKeyEvent:keyCode state:keyState]; return; } // Continue on to super [super sendEvent:event]; } </code></pre> <p>The "state" in -mediaKeyEvent:state: is for up/down. In my app it makes sense to only react to the play/pause key when it's back up (done pressing), but I continuously react to RW/FF events while the key is down for seeking.</p> <p>I'd love to know of a better way to do this if it exists, though. Currently, unless the user disables these keys in the global keyboard shortcuts, it controls my app <em>and</em> iTunes. :-)</p> <p>This code has been used quite awhile in my <a href="http://bartastechnologies.com/transcriva" rel="noreferrer">transcription app</a> and works well (aside from the global keyboard shortcut issue above).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload