13

使用 touchesBeganWithEvent、touchesEndedWithEvent 等,您可以从多点触控板获取触摸数据,但是有没有办法阻止该触摸数据移动鼠标/激活系统范围的手势(类似于中文文本输入中所做的) ?

4

2 回答 2

7

正如 valexa 所指出的,将 NSEventMask 用于 CGEventTap 是一种 hack。Tarmes 还指出 Rob Keniger 的答案不再有效(OS X >= 10.8)。幸运的是,Apple 提供了一种通过kCGEventMaskForAllEvents在回调中使用 CGEventRef 并将其转换为 NSEvent 的方法来轻松完成此操作:

NSEventMask eventMask = NSEventMaskGesture|NSEventMaskMagnify|NSEventMaskSwipe|NSEventMaskRotate|NSEventMaskBeginGesture|NSEventMaskEndGesture;

CGEventRef eventTapCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef eventRef, void *refcon) {
  // convert the CGEventRef to an NSEvent
  NSEvent *event = [NSEvent eventWithCGEvent:eventRef];

  // filter out events which do not match the mask
  if (!(eventMask & NSEventMaskFromType([event type]))) { return [event CGEvent]; }

  // do stuff
  NSLog(@"eventTapCallback: [event type] = %d", [event type]);

  // return the CGEventRef
  return [event CGEvent];
}

void initCGEventTap() {
  CFMachPortRef eventTap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, kCGEventTapOptionListenOnly, kCGEventMaskForAllEvents, eventTapCallback, nil);
  CFRunLoopAddSource(CFRunLoopGetCurrent(), CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0), kCFRunLoopCommonModes);
  CGEventTapEnable(eventTap, true);
  CFRunLoopRun();
}

请注意,CFRunLoopRun()由于此代码段是从无法使用 NSApplication 但具有基本 CFRunLoop 的项目中获取的,因此包含对 的调用。如果您使用 NSApplication,请忽略它。

于 2012-12-07T00:52:06.987 回答
2

更新:我下面的答案不再有效。在这里查看答案。

通常要做到这一点,您需要使用 Quartz Event Tap,尽管 CGEvent API 似乎没有“正式”支持触摸事件。NSEvent.h 中的非多点触控事件类型似乎映射到 CGEventTypes.h 中的 CGEvent 类型,因此多点触控事件可能会起作用,即使它们没有记录在案。

为了阻止事件传播,您需要从事件点击回调中返回 NULL。

你需要一些这样的代码:

#import <ApplicationServices/ApplicationServices.h>

//assume CGEventTap eventTap is an ivar or other global

void createEventTap(void)
{
 CFRunLoopSourceRef runLoopSource;

 //listen for touch events
 //this is officially unsupported/undocumented
 //but the NSEvent masks seem to map to the CGEvent types
 //for all other events, so it should work.
 CGEventMask eventMask = (
  NSEventMaskGesture       |
  NSEventMaskMagnify       |
  NSEventMaskSwipe         |
  NSEventMaskRotate        |
  NSEventMaskBeginGesture  |
  NSEventMaskEndGesture
 );

 // Keyboard event taps need Universal Access enabled, 
 // I'm not sure about multi-touch. If necessary, this code needs to 
 // be here to check whether we're allowed to attach an event tap
 if (!AXAPIEnabled()&&!AXIsProcessTrusted()) { 
  // error dialog here 
  NSAlert *alert = [[[NSAlert alloc] init] autorelease];
  [alert addButtonWithTitle:@"OK"];
  [alert setMessageText:@"Could not start event monitoring."];
  [alert setInformativeText:@"Please enable \"access for assistive devices\" in the Universal Access pane of System Preferences."];
  [alert runModal];
  return;
 } 


 //create the event tap
 eventTap = CGEventTapCreate(kCGHIDEventTap, //this intercepts events at the lowest level, where they enter the window server
        kCGHeadInsertEventTap, 
        kCGEventTapOptionDefault, 
        eventMask,
        myCGEventCallback, //this is the callback that we receive when the event fires
        nil); 

 // Create a run loop source.
 runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);

 // Add to the current run loop.
 CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);

 // Enable the event tap.
 CGEventTapEnable(eventTap, true);
}


//the CGEvent callback that does the heavy lifting
CGEventRef myCGEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef theEvent, void *refcon)
{
 //handle the event here
 //if you want to capture the event and prevent it propagating as normal, return NULL.

 //if you want to let the event process as normal, return theEvent.
 return theEvent;
}
于 2009-09-06T01:43:01.733 回答