K, this isn't as great as what I had bookmarked, because what I had bookmarked was some actual Apple developer example code, but this'll get you where you need to be.
CODE
#import <Cocoa/Cocoa.h>
#import <Carbon/Carbon.h>
OSStatus keyPressed(EventHandlerCallRef nextHandler, EventRef
theEvent, void *userData) {
NSLog(@"Event received!\n");
return CallNextEventHandler(nextHandler, theEvent);
}
@interface KeyLoggerApplication : NSApplication
{
}
@end
@interface KeyLoggerController : NSObject
{
EventHotKeyRef reference;
}
- (IBAction)attach:(id)sender;
@end
@implementation KeyLoggerController
- (IBAction)attach:(id)sender
{
EventTypeSpec eventType;
eventType.eventClass = kEventClassKeyboard;
eventType.eventKind = kEventRawKeyUp;
EventHandlerUPP handlerFunction = NewEventHandlerUPP(keyPressed);
InstallEventHandler(GetEventMonitorTarget(), handlerFunction, 1,
&eventType, NULL, NULL);
}
@end
@implementation KeyLoggerApplication
- (void)sendEvent:(NSEvent *)anEvent {
NSEventType type = [anEvent type];
bool handled = NO;
if (type == NSKeyUp)
{
switch( [anEvent keyCode] )
{
case 36: //return
NSLog(@"Pressed return");
handled = YES;
break;
default:
NSLog(@"Keypressed: %d, **%@**", [anEvent keyCode], [anEvent characters]);
break;
}
}
//handle only the keys i need then let the other go through the
regular channels
//this stops the annoying beep
if( !handled )
[super sendEvent:anEvent];
}
@end
I'd test it for ya.. but I still don't have a Mac.