0
votes

What I want to achieve:

The target is to make a Mac OS X library - dylib/bundle/framework, that intercepts all mouse and keyboard events of the application that loaded the library. I just want to log these events, not change them. The library needs to have a C API that exposes the logged events' details.

What I have currently made:

I've implemented two ways for detecting the application's mouse and keyboard events from within the library that the application loads:

1) Installing an event handler using Carbon. For some reason using InstallApplicationEventHandler or InstallEventHandler( GetApplicationEventTarget(), ... ) didn't do the trick. It was only possible to catch all non-application events (all events except for those delivered to the target application that loads my library).

2) CGEventTaps: mouse (http://osxbook.com/book/bonus/chapter2/altermouse/) and keyboard (http://osxbook.com/book/bonus/chapter2/alterkeys/) ones. These work, but are system-wide. I want my library to catch only the events delivered to the application that has loaded it.

Does anyone know how to create application-wide keyboard and mouse hooks/taps, not system-wide ones?

1
"I just want to log these events" What for?Daniel Fischer
It is not a keylogger if it is what you are asking for. I want this library to remember the last pressed key and the last position where the mouse have been clicked at.Ivan Caravanio

1 Answers

0
votes

Application specific Event Taps can be created using CGEventTapCreateForPSN. Current application process serial number can be get using
ProcessSerialNumber currentProcess; GetCurrentProcess(&currentProcess);

Then create and event tap for this PSN.