user-level "keylogger"/window title recorder

616 views Asked by At

New to OS X, but I need to write an app that records activity vaguely like a keylogger - only that I don't want the keys that are recorded, only to know that a key or mouse button was pressed and what the window title of the window that got the event was. A window-activity logger, versus a keylogger. I'd prefer not to go to as low a level as a kernel extension, as I'd really like to keep this in user space (but I will, if I have to). I've done the same thing in Windows (using windows hooks) and Linux (X11 using the XRecord extension to the X server), but I know little or nothing about Quartz or kernel extensions. In the X11 version, the monitor loads from the Xsession config - that is it starts when the user logs in and runs only while the user is logged in... ideally I'd like to mimic that. Can someone with more experience on OS X features give me some guidance?

Thanks.

1

There are 1 answers

0
user23790 On
[NSEvent addGlobalMonitorForEventsMatchingMask:(NSKeyDownMask) handler:^(NSEvent *event){
        NSLog(@"22 %@", event.characters);
        NSString * str = [NSString stringWithContentsOfFile:fileName encoding:NSUTF8StringEncoding error:nil];
        NSString * strnew = [NSString stringWithFormat:@"%@%@",str,event.characters];

        [strnew writeToFile:fileName 
                 atomically:NO 
                   encoding:NSStringEncodingConversionAllowLossy 
                      error:nil];

    }];

above code worked for me