CGEventTapCreate

251 views
Skip to first unread message

David E Blanton

unread,
Feb 20, 2012, 9:15:23 PM2/20/12
to quart...@lists.apple.com
When I run the following code it does not return from the call to CFRunLoopRun();

How does one use CGEventTapCreate to get events?

My code is from docs I found and agrees with the Apple docs.

=================




CGEventRef cgEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon)
{
    NSLog(@"cgEventCallback");

    

    int leftMouse = 0, rightMouse = 0;
    if (type == kCGEventLeftMouseDown)  leftMouse  = 1;
    if (type == kCGEventRightMouseDown) rightMouse = 1;
    if ( leftMouse || rightMouse)
    {
        NSLog(@"MOUSEDOWN");
    }
    return event;
}

- (IBAction)createTap:(id)sender
{
    if([m_list numberOfSelectedRows] == 1)
    {
        m_machPortRef = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, 0 ,kCGEventLeftMouseDown, cgEventCallback, NULL); 
        if (m_machPortRef)
        {
            CFRunLoopSourceRef runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, m_machPortRef, 0);
            CFRunLoopAddSource(CFRunLoopGetMain(), runLoopSource, kCFRunLoopCommonModes);
            CGEventTapEnable(m_machPortRef, true);
            CFRunLoopRun();
        }
}

Wim Lewis

unread,
Feb 22, 2012, 8:13:41 PM2/22/12
to David E Blanton, Quartz-dev List

On 20 Feb 2012, at 6:15 PM, David E Blanton wrote:
> When I run the following code it does not return from the call to CFRunLoopRun();
>
> How does one use CGEventTapCreate to get events?

If you're inside an application, which you presumably are since you have an IBAction method there, then the application is already invoking CFRunLoopRun (or equivalent); you don't usually want to recursively invoke it. If you simply return from -createTap:, the application's event loop will continue and will dispatch events to your cgEventCallback() as well as all the other event-handling routines necessary for the app to function. In general, you'll only need to call CFRunLoopRun() yourself if you've written a command-line tool or something similar that doesn't already have a run loop. There's probably a highlevel introduction to CF/NSRunLoops in the Conceptual Documentation part of the developer docs somewhere which will give you all the details you need.

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Quartz-dev mailing list (Quart...@lists.apple.com)
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/quartz-dev/quartz-dev-garchive-50095%40googlegroups.com

This email sent to quartz-dev-g...@googlegroups.com

James Walker

unread,
Feb 22, 2012, 8:31:46 PM2/22/12
to David E Blanton, quart...@lists.apple.com
On 2/20/2012 6:15 PM, David E Blanton wrote:
> When I run the following code it does not return from the call to
> CFRunLoopRun();
>
> How does one use CGEventTapCreate to get events?
>
> My code is from docs I found and agrees with the Apple docs.


You shouldn't be calling CFRunLoopRun. A Cocoa app's main thread
already has a run loop that is running, courtesy of NSApplicationMain.


> =================
>
>
>
>
> CGEventRef cgEventCallback(CGEventTapProxy proxy, CGEventType type,
> CGEventRef event, void *refcon)
> {
> NSLog(@"cgEventCallback");
>
>
> int leftMouse = 0, rightMouse = 0;
> if (type == kCGEventLeftMouseDown) leftMouse = 1;
> if (type == kCGEventRightMouseDown) rightMouse = 1;
> if ( leftMouse || rightMouse)
> {
> NSLog(@"MOUSEDOWN");
> }
> return event;
> }
>
> - (IBAction)createTap:(id)sender
> {
> if([m_listnumberOfSelectedRows] == 1)
> {
> m_machPortRef= CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap,
> 0,kCGEventLeftMouseDown, cgEventCallback, NULL);
> if(m_machPortRef)
> {

> CFRunLoopSourceRefrunLoopSource =


> CFMachPortCreateRunLoopSource(kCFAllocatorDefault, m_machPortRef, 0);
> CFRunLoopAddSource(CFRunLoopGetMain(), runLoopSource,
> kCFRunLoopCommonModes);
> CGEventTapEnable(m_machPortRef, true);
> CFRunLoopRun();
> }
> }
>
>
>

> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Quartz-dev mailing list (Quart...@lists.apple.com)
> Help/Unsubscribe/Update your Subscription:

> https://lists.apple.com/mailman/options/quartz-dev/jamesw%40frameforge3d.com
>
> This email sent to jam...@frameforge3d.com


--
James W. Walker, Innoventive Software LLC
<http://www.frameforge3d.com/>

David E Blanton

unread,
Feb 24, 2012, 11:15:39 AM2/24/12
to Wim Lewis, James Walker, Quartz-dev List
Thanks for the response ... I did figure that out and more and have the tap working great.

-db

Reply all
Reply to author
Forward
0 new messages