Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's how to setup the event listener</p> <pre><code>CFMachPortRef eventTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault, CGEventMaskBit(kCGEventKeyDown), &amp;KeyDownCallback, NULL); CFRunLoopSourceRef runLoopSource = CFMachPortCreateRunLoopSource(NULL, eventTap, 0); CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes); CFRelease(runLoopSource); CGEventTapEnable(eventTap, true); </code></pre> <p>And then here is the "callback":</p> <pre><code>static CGEventRef KeyDownCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) { /* Do something with the event */ NSEvent *e = [NSEvent eventWithCGEvent:event]; return event; } </code></pre> <p>on the parsed NSEvent, there are the <code>modifierFlags</code> and <code>keyCode</code> properties. <code>keyCode</code> is the code of the key that was pressed, and <code>modifierFlags</code> is the different modifiers that were pressed (Shift, Alt/Option, Command, etc).</p> <p>Simply <code>return NULL;</code> in the <code>KeyDownCallback</code> method to stop the event from propogating.</p> <p>Note: there seems to be an issue with the event tap timing out, to solve this problem you can 'reset' the event tap.</p> <p>In the <code>KeyDownCallback</code> method, check if the <code>CGEventType type</code> is <code>kCGEventTapDisabledByTimeout</code> like so:</p> <pre><code>if (type == kCGEventTapDisabledByTimeout) { /* Reset eventTap */ return NULL; } </code></pre> <p>and where <code>Reset eventTap</code> is, execute the setup of the event listener above again.</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.
    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