2009-09-05 15 views
11

स्पर्शों का उपयोग करनाबगनविथवेन्ट, टच्स एंडेड विथवेन्ट, आदि आप मल्टीटाउच ट्रैकपैड से टच डेटा प्राप्त कर सकते हैं, लेकिन उस स्पर्श डेटा को माउस को स्थानांतरित करने/सिस्टम-वाइड जेस्चर को सक्रिय करने से रोकने का कोई तरीका है (समान चीनी पाठ इनपुट में क्या किया जाता है)?कोको में सभी मल्टीटाउच ट्रैकपैड इनपुट को कैप्चर करना

+1

मैं स्वीकार किए गए उत्तर को अपडेट करने का सुझाव देता हूं। – ArtOfWarfare

उत्तर

6

:

आप इस तरह की कुछ कोड की जरूरत होगी। टर्मिस ने यह भी नोट किया कि रॉब केनिगर का जवाब अब काम नहीं करता है (ओएस एक्स> = 10.8)। सौभाग्य से, एप्पल kCGEventMaskForAllEvents का उपयोग करने और कॉलबैक के भीतर एक NSEvent को CGEventRef परिवर्तित करके काफी आसानी से यह करने के लिए एक तरह से प्रदान की गई है:

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() करने के लिए कॉल शामिल किया गया है जो एनएसएप्लिकेशंस का उपयोग नहीं कर सका लेकिन इसके बजाय एक नंगे हड्डियों CFRunLoop था। अगर आप एनएसएप्लिकेशंस का उपयोग करते हैं तो इसे छोड़ दें।

2

अद्यतन: मेरा उत्तर अब काम नहीं करता है। उत्तर here देखें।

आम तौर पर इस आप एक क्वार्ट्ज किया गया टैप उपयोग करने की आवश्यकता होगी करने के लिए है, हालांकि स्पर्श घटनाओं "आधिकारिक तौर पर" होने का CGEvent एपीआई द्वारा समर्थित नहीं है। NSEvent.h में गैर-मल्टीटाउच इवेंट प्रकार CGEventTypes.h में CGEvent प्रकारों को मैप करने लगते हैं, इसलिए मल्टीटाउच शायद काम करेंगे, भले ही वे दस्तावेज न हों।

प्रचार से ईवेंट को अवरुद्ध करने के लिए, आपको ईवेंट टैप कॉलबैक से NULL वापस करने की आवश्यकता है। valexa द्वारा बताया गया है, CGEventTap के लिए एक हैक NSEventMask उपयोग कर रहा है के रूप में

#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; 
} 
+0

यह गलत है कि CGEventMasks क्या वास्तव में होता है NSEventMasks नक्शा है कि उन लोगों के लिए मुखौटा की स्थापना kCGEventMaskForAllEvents को मुखौटा करने के समान ही है और इसके बारे में दमन परिणाम ट्रैकपैड पर हर एक स्पर्श के लिए NSEventTypeGesture केवल घटनाओं की एक भाप हो रही है है, जो न्यूल लौटने से वास्तव में किसी भी तरह का इशारा करता है। – valexa

+0

मैं 10.8 के तहत इस चल रहा की कोशिश की है और यह काम करने के लिए प्रतीत नहीं होता है - इशारों EventTap के माध्यम से सब पर अब और पारित नहीं किया जाता है। मुझे नहीं लगता कि आपको एक और समाधान मिला है? – tarmes

+0

-1: यह 10.8 के तहत काम नहीं करता है। मैं इस सवाल का जवाब उपयोग करने का सुझाव बजाय: http://stackoverflow.com/a/13755292/901641 – ArtOfWarfare

संबंधित मुद्दे