Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Global Mouse Hooking for Mac OS



  • Hello everyone,

    I want the mouse events like Mouse Move, Mouse Up, Mouse Down globally means also find out these events outside the application on the Mac OS. I have some code which execute perfect with the Cocoa Application. But, when i use that code in Qt Application, It does not work. THis is my code.

    @#include <QDebug>

    #include <CoreFoundation/CoreFoundation.h>
    #include <ApplicationServices/ApplicationServices.h>

    extern CGEventRef handleCGEvent(CGEventTapProxy proxy, CGEventType type, CGEventRef eventRef, void *refcon);

    CGEventRef handleCGEvent(CGEventTapProxy proxy, CGEventType type, CGEventRef eventRef, void *refcon)
    {
    if (type == kCGEventLeftMouseDown )
    {
    qDebug() << "kCGEventLeftMouseDown";
    }
    else if(type == kCGEventLeftMouseUp)
    {
    qDebug() << "kCGEventLeftMouseUp";
    }
    else if(type == kCGEventMouseMoved)
    {
    qDebug() << "kCGEventMouseMoved";
    }

    return eventRef;
    

    }

    CMouseEvent::CMouseEvent(QObject *parent) :
    QObject(parent)
    {
    CFMachPortRef eventTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault, kCGEventMaskForAllEvents, handleCGEvent, NULL);
    CFRunLoopSourceRef runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
    CFRunLoopAddSource(CFRunLoopGetMain(), runLoopSource, kCFRunLoopCommonModes);
    CGEventTapEnable(eventTap, true);
    }
    @

    This is a class which is used for mouse events. I am creating an object of this class in main() function. My application is console application.

    When i run this code, it stuck in the code. I don't know where it stuck. Mouse only move but unable to click anywhere.

    Thanks in advance....


Log in to reply