问题描述
使用touchesBeganWithEvent,touchesEndedWithEvent等,您可以从多点触控板获取触摸数据,但有一种方法可以阻止触摸数据移动鼠标/激活系统范围的手势(类似于中文文本输入)?
如valexa所述,使用NSEventMask CGEventTap是一个黑客。 Tarmes还指出,Rob Keniger的回答不再有效(OS X> = 10.8)。幸运的是,Apple通过使用 kCGEventMaskForAllEvents
并将CGEventRef转换为回调中的NSEvent提供了一种方法:
NSEventMask eventMask = NSEventMaskGesture | NSEventMaskMagnify | NSEventMaskSwipe | NSEventMaskRotate | NSEventMaskBeginGesture | NSEventMaskEndGesture;
CGEventRef eventTapCallback(CGEventTapProxy代理,CGEventType类型,CGEventRef eventRef,void * refcon){
//将CGEventRef转换为NSEvent
NSEvent * event = [NSEvent eventWithCGEvent:eventRef ];
//过滤掉与掩码不匹配的事件
if(!(eventMask& NSEventMaskFromType([event type]))){return [event CGEvent]; }
// do stuff
NSLog(@eventTapCallback:[event type] =%d,[event type]);
//返回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() code>,因为这个片段是从一个不能使用NSApplication的项目,而是一个裸机CFRunLoop。如果您使用NSApplication,请忽略它。
Using touchesBeganWithEvent, touchesEndedWithEvent, etc you can get the touch data from the multitouch trackpad, but is there a way to block that touch data from moving the mouse/activating the system-wide gestures (similar to what is done in the chinese text input)?
解决方案 As noted by valexa, using NSEventMask for CGEventTap is a hack. Tarmes also notes that Rob Keniger's answer no longer works (OS X >= 10.8). Luckily, Apple has provided a way to do this quite easily by using kCGEventMaskForAllEvents
and converting the CGEventRef to an NSEvent within the callback:
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();
}
Note that the call to CFRunLoopRun()
is included since this snippet was taken from a project which could not use NSApplication but instead had a bare-bones CFRunLoop. Omit it if you use NSApplication.
这篇关于捕获Cocoa中的所有多点触控板输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!