touchesBeganWithEvent、touchesEndedWithEvent などを使用すると、マルチタッチ トラックパッドからタッチ データを取得できますが、そのタッチ データがマウスを動かしたり、システム全体のジェスチャをアクティブにしたりするのをブロックする方法はありますか (中国語のテキスト入力で行われることと同様)。 ?
2 に答える
valexa が指摘したように、CGEventTap に NSEventMask を使用するのはハックです。Tarmes は、Rob Keniger の回答が機能しなくなったことにも言及しています (OS X >= 10.8)。kCGEventMaskForAllEvents
幸いなことに、Apple は、コールバック内で 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 を使用する場合は省略してください。
更新: 以下の私の答えは機能しなくなりました。答えはこちらをご覧ください。
通常、これを行うには 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;
}