2011-06-21 81 views

回答

2

找到解決方案:

self.machPortRef = CGEventTapCreate(kCGSessionEventTap, 
              kCGTailAppendEventTap, 
              kCGEventTapOptionDefault, 
              CGEventMaskBit(kCGEventKeyDown), 
              (CGEventTapCallBack)eventTapFunction, 
              self); 
     if (self.machPortRef == NULL) 
     { 
      printf("CGEventTapCreate failed!\n"); 
     } else { 
      self.eventSrc = CFMachPortCreateRunLoopSource(NULL, self.machPortRef, 0); 
      if (self.eventSrc == NULL) 
      { 
       printf("No event run loop src?\n"); 
      }else { 
       CFRunLoopRef runLoop = CFRunLoopGetCurrent(); //GetCFRunLoopFromEventLoop(GetMainEventLoop()); 

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

      } 
     } 

屬性:

CFMachPortRef machPortRef; 
CFRunLoopSourceRef eventSrc; 

事件處理程序:

CGEventRef eventTapFunction(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) 
{ 
    //printf("eventTap triggered\n"); 
    return event; 
}