1

My Run Loop Observer is written as follows:

void observerCallback(CFRunLoopObserverRef observer,
    CFRunLoopActivity activity, void* info)
{
    println("%u", activity);
}
//----------------------------- 
void InstallObserver()
{
    CFRunLoopObserverRef myObserver = NULL;
    int myActivities = kCFRunLoopEntry;

    myObserver = CFRunLoopObserverCreate(NULL, myActivities, YES,
        /* repeat */ 0, &observerCallback, NULL);

    if (myObserver)
    {
        CFRunLoopAddObserver(CFRunLoopGetCurrent(), myObserver,
            kCFRunLoopCommonModes);
    }
}

Every time I press any key in my Application the observerCallback is called 4 times. The question is: How can I obtain key code inside observerCallback? Thanks.

mh taqia
  • 3,506
  • 1
  • 24
  • 35
  • What are you trying to do here with the run loop that you can't do by responding to the event stream? CFRunLoop happens to be triggered with these events, but the trigger is basically to process the event using the responder framework, and as such, the run loop itself isn't getting the information you are seeking. – gaige Jul 12 '12 at 09:00
  • My friend gaige! How can I use "event stream" for this purpose? Is It possible to help me? thankyou. – mh taqia Jul 12 '12 at 10:29
  • Possibly. What are you trying to do? – gaige Jul 12 '12 at 11:15
  • I want to monitor keyboard events in my process space (not system wide). It seems the InstallEventHandler (with target = GetApplicationEventTarget) not supported in cocoa apps (needs carbon run loop). if target be GetEventMonitorTarget() then "Universal access" is needed to activate (I do not want it). – mh taqia Jul 12 '12 at 13:45
  • Is this an App that has no windows? The reason that I ask is that there are some approaches, but each has its own requirements and depending on what your requirements actually are will depend what solutions make sense. Do you have a window that you want to monitor events in? – gaige Jul 13 '12 at 09:07
  • No, I have no window. Indeed I have written a dylib that is injected into another programms (such as TextEdit) for some purposes. the code should be executed inside dylib considering the fact that no window is accessible. – mh taqia Jul 13 '12 at 10:12

1 Answers1

0

Based on the comments on your question, you want a local event monitor, AKA:

+[NSEvent addLocalMonitorForEventsMatchingMask:handler:]

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSEvent_Class/Reference/Reference.html#//apple_ref/occ/clm/NSEvent/addLocalMonitorForEventsMatchingMask:handler:

Integer Poet
  • 747
  • 5
  • 19