@implementation MyApplication
static OSStatus MonitorHandler(EventHandlerCallRef nextHandler, EventRef theEvent, void * inRefcon)
{
// Useless on 10.4 for keyboard events. If you requested to watch other events
// (mouse or tablet events for example) they would into funnel to here.
NSLog(@"it's working!!!!\n");
return CallNextEventHandler(nextHandler, theEvent);
}
- (void)installMonitorHandler;
{
OSStatus error = noErr;
EventTypeSpec kEvents[] =
{
{ kEventClassKeyboard, kEventRawKeyDown },
{ kEventClassKeyboard, kEventRawKeyUp }
};
// the magic happens here with the call to GetEventMonitorTarget as described in CarbonEvents.h
// The event monitor target is a special event target used to monitor user input events across all processes.
// When the monitor target detects a matching event, then MonitorHandler is called.
error = InstallEventHandler( GetEventMonitorTarget(), MonitorHandler, GetEventTypeCount( kEvents ),
kEvents, NULL, &mMonitorHandlerRef );
if (error != noErr) {
NSBeep();
NSLog(@"InstallEventHandler error %d", error);
}
}
- (void)uninstallMonitorHandler;
{
if (mMonitorHandlerRef) {
RemoveEventHandler(mMonitorHandlerRef);
mMonitorHandlerRef = NULL;
}
}
- (void)sendEvent:(NSEvent *)event;
{
NSEventType eventType = [event type];
// Catch keyboard events destined for other apps when recording
if (![self isActive] && (eventType == NSKeyDown || eventType == NSKeyUp || eventType == NSFlagsChanged)) {
NSLog(@"sendEvent: interior");
return;
}
[super sendEvent:event];
}