Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: Trying out CGEventTapCreate, but it appears to be broken!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Trying out CGEventTapCreate, but it appears to be broken!




Thanks a lot for your help! This fixed the problem like a charm. (I'm not nearly as familiar with CoreFoundation apps as I am with Carbon and Cocoa.)


- Brian


On Sun, 09 Oct 2005 13:00:46 -0400, Mike Paquette <email@hidden> wrote:

So far, so good. You have an event tap port. Now you need to turn it into a runloop source, and in this case, add it to the runloop behind the Carbon main event loop. Note that I normally don't recommend doing this, as it will throttle the flow of events to the rate at which the main loop can process and draw. In this case, the main event loop will just be servicing the CGEventTap, so we might be able to get away with this.


CFRunLoopSourceRef eventSrc; CFRunLoopRef runLoop;

     eventSrc = CFMachPortCreateRunLoopSource(NULL, machPortRef, 0);
     if ( eventSrc == NULL )
         printf( "No event run loop src?\n" );

// Get the CFRunLoop primitive for the Carbon Main Event Loop, and add the new event souce
CFRunLoopAddSource(GetCFRunLoopFromEventLoop(GetMainEventLoop()), eventSrc, kCFRunLoopDefaultMode);


    RunApplicationEventLoop();

    if (machPortRef)
        CFRelease(machPortRef);

if ( eventSrc ) CFRelease(eventSrc);
}



In a command line tool, faceless app, or daemon, you can do something similar purely at the CoreFoundation level:

#include <ApplicationServices/ApplicationServices.h>

CGEventRef printEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon)
{
printf( "Got event of type %d\n", type )
return event;
}


int main(int argc, char ** argv)
{
     CFMachPortRef eventPort;
     CFRunLoopSourceRef  eventSrc;
     CFRunLoopRef    runLoop;

     eventPort = CGEventTapCreate(kCGSessionEventTap,
                                 kCGHeadInsertEventTap,
                                 kCGEventTapOptionListenOnly,
                                 CGEventMaskBit(kCGEventOtherMouseDown),
                                 printEventCallback,
                                 NULL );
     if ( eventPort == NULL )
     {
         printf( "NULL event port\n" );
         exit( 1 );
     }

     eventSrc = CFMachPortCreateRunLoopSource(NULL, eventPort, 0);
     if ( eventSrc == NULL )
         printf( "No event run loop src?\n" );

     runLoop = CFRunLoopGetCurrent();
     if ( runLoop == NULL )
         printf( "No run loop?\n" );

     CFRunLoopAddSource(runLoop,  eventSrc, kCFRunLoopDefaultMode);
     CFRunLoopRun();
}


_______________________________________________
Do not post admin requests to the list. They will be ignored.
Quartz-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Trying out CGEventTapCreate, but it appears to be broken! (From: "Brian Kendall" <email@hidden>)
 >Re: Trying out CGEventTapCreate, but it appears to be broken! (From: Mike Paquette <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.