c++ - How to detect global mouse button events -
i'm wondering how can write code monitor mouse buttons globally. os x, , i'd try writing in qt/c++.
to begin don't know how capture global events. monitor application not display gui, it'd process runs in background , detects mouse buttons being clicked.
in second part of program launch hot-keys depending of mouse key pressed.
my final idea make free program steermouse, figure out how done.
i'm asking guidance of start - how can detect mouse button events globally?
it's not possible using qt. there's another question details issues. boils down to:
installing event filter on
qapplicationlet receive mouse events while cursor on application window, not outside of it. that's not helpful in case.if widget grabs mouse using
grabmouse(), receive mouse events globally, interaction other applications becomes impossible.
so, you'll need resort using platform-specific apis - means cocoa , writing in objective c/c++. there's question excellent answers provides need qt integration.
the missing part, shown below, integrating stand-alone code qt. code shows empty widget demonstrate correctly handle mouse events both our application, , outside of it.
this complete, working example, using cocoa. needs go .mm file; don't forget add objective_sources in qmake project file (not sources!).
unfortunately, there's isn't single function/method translate nsevent qmouseevent. best 1 can copy&paste code qnsview.mm. unfortunate results design of qt platform abstraction: platform code ends calling qwindowsysteminterface::handlemouseevent(....) post event application.
#include <qapplication> #include <qabstractnativeeventfilter> #include <qtextstream> #include <qwidget> #include <cstdio> #import <appkit/appkit.h> qtextstream out(stdout); class myeventfilter : public qabstractnativeeventfilter { public: bool nativeeventfilter(const qbytearray &eventtype, void *message, long *result) { q_unused(eventtype) q_unused(result) nsevent * event = (nsevent*)message; switch ([event type]) { case nsleftmousedown: out << "lv"; break; case nsleftmouseup: out << "l^"; break; case nsrightmousedown: out << "rv"; break; case nsrightmouseup: out << "r^"; break; case nsothermousedown: out << [event buttonnumber] << "v"; break; case nsothermouseup: out << [event buttonnumber] << "^"; break; default: return false; } out << endl; return false; } }; int main(int argc, char *argv[]) { qapplication a(argc, argv); qsharedpointer<qabstractnativeeventfilter> filter(new myeventfilter); const int mask = nsleftmousedownmask | nsleftmouseupmask | nsrightmousedownmask | nsrightmouseupmask | nsothermousedownmask | nsothermouseupmask; // global monitoring handler *not* called events sent our application id monitorid = [nsevent addglobalmonitorforeventsmatchingmask:mask handler:^(nsevent* event) { filter->nativeeventfilter("nsevent", event, 0); }]; // need handle events coming our application a.installnativeeventfilter(filter.data()); qwidget w; w.show(); int rc = a.exec(); [nsevent removemonitor:monitorid]; return rc; }
Comments
Post a Comment