Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Global Mouse Hooking for Mac OS
Himanshu Rohilla last edited by
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.
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";
CMouseEvent::CMouseEvent(QObject *parent) :
CFMachPortRef eventTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault, kCGEventMaskForAllEvents, handleCGEvent, NULL);
CFRunLoopSourceRef runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
CFRunLoopAddSource(CFRunLoopGetMain(), runLoopSource, kCFRunLoopCommonModes);
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....