Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, why is your first "if" allowing key-down and key-up events to pass? Your second "if" only lets system events pass through anyway. So for all key-down/-up events you create a NSEvent, just to drop the event one line further downwards. That makes little sense. An Event Tap should always be as fast as possible, otherwise it will slow down all event processing of the whole system. Your callback should not even be called for key-down/-up events, since system events are not key-down/-up events, they are system events. If they were key events, you would for sure never access data1, but instead use the "type" and "keyCode" methods to get the relevant information from them.</p> <pre><code>static CGEventRef event_tap_callback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) { NSEvent * sysEvent; // No event we care for? return ASAP if (type != NX_SYSDEFINED) return event; sysEvent = [NSEvent eventWithCGEvent:event]; // No need to test event type, we know it is NSSystemDefined, // becuase that is the same as NX_SYSDEFINED </code></pre> <p>Also you cannot determine if that is the right kind of event by just looking at the data, you must also verify the subtype, that must be 8 for this kind of event:</p> <pre><code> if ([sysEvent subtype] != 8) return event; </code></pre> <p>The next logical step is to split the data up into its components:</p> <pre><code> int data = [sysEvent data1]; int keyCode = (data &amp; 0xFFFF0000) &gt;&gt; 16; int keyFlags = (data &amp; 0xFFFF); int keyState = (keyFlags &amp; 0xFF00) &gt;&gt; 8; BOOL keyIsRepeat = (keyFlags &amp; 0x1) &gt; 0; </code></pre> <p>And you probably don't care for repeating key events (that is when I keep the key pressed and it keeps sending the same event over and over again).</p> <pre><code> // You probably won't care for repeating events if (keyIsRepeat) return event; </code></pre> <p>Finally you should not define any own constant, the system has ready to use constants for those keys:</p> <pre><code> // Analyze the key switch (keyCode) { case NX_KEYTYPE_PLAY: // Play/Pause key if (keyState == 0x0A) { // Key down // ...do your stuff here... return NULL; } else if (keyState == 0x0B) { // Key Up // ...do your stuff here... return NULL; } // If neither down nor up, we don't know // what it is and better ignore it break; case NX_KEYTYPE_FAST: // (Fast) Forward break; case NX_KEYTYPE_REWIND: // Rewind key break; } // If we get here, we have not handled // the event and want system to handle it return event; } </code></pre> <p>And if this still not works, my next question would be what your post_notification function looks like and do you also see the described problem if you don't call post_notification there, but just make a NSLog call about the event you just saw?</p>
    singulars
    1. This table or related slice is empty.
    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. 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