Thursday, 19 September 2013

Capturing global keystrokes in Objective C (OS X)

Capturing global keystrokes in Objective C (OS X)

I am quite new to Objective C and Xcode, and so I have experienced some
difficulty understanding how to implement advanced functionalities.
Requirements: Detect key presses (whether application window is in focus
or running in background) and execute some code depending on which keys
were pressed. Meant to function as hotkeys/shortcut keys.
KeyEvents in Cocoa and XCode appeared to contain potential solutions, but
I am uncertain as to whether the suggestions there are capable of
capturing key presses which occur outside of the application. I am also
not sure how exactly one would subclass the default NSView object created
by Interface Builder and tie it back to my NSWindow (also magically
created by Interface Builder) -- assuming something like this is required
at all. Not really sure how to refer from code to objects created by the
.xib, but that may be a separate question.
So, bottom line... if this is possible (without hackish OS security
overrides to enable keylogging), I would be interested to know exactly how
to go about it from the point of creating a new project in Xcode and
immediately trying to capture keystrokes.
Thanks

No comments:

Post a Comment