This is one way to do it--set up a monitor. This is what I use to intercept key presses for my typewriter sounds.
This only needs to be called once, say in your app delegate. Ignore the [self playTypewriterSound:?]--these are local functions for playing different sounds. I think you can get the idea from this example.
The keyDownMonitor instance is declared in the interface. I have another function to remove the monitor when it is not needed.
//------------------------------------------------------------------------------------------------
-(void)setupTypewriterSounds {
keyDownMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskKeyDown handler:^(NSEvent *event) {
// filter out anything with a command key press
NSUInteger keyPress = [event modifierFlags];
keyPress = keyPress & (unsigned long)NSEventModifierFlagCommand;
if(keyPress) {
return (NSEvent *)event;
}
// filter out if the window is not key (ie. focus is on preference window, etc.)
if([[NSApplication sharedApplication] isOtherKey]) {
return (NSEvent *)event;
}
// else, make some noise
NSString *chars = [event characters];
unichar character = [chars characterAtIndex: 0];
if ((character > 96 ) && (character < 127)) { // lower case and some punctuation
[self playTypewriterSound:5];
return (NSEvent *)event;
}
if (character == 32 ) { // space
[self playTypewriterSound:2];
return (NSEvent *)event;
}
if ((character > 32 ) && (character < 97)) { // upper case, numbers, some punctuation
[self playTypewriterSound:3];
return (NSEvent *)event;
}
if (character == 13 ) { // enter
[self playTypewriterSound:1];
return (NSEvent *)event;
}
if (character == 127 ) { // delete
[self playTypewriterSound:4];
return (NSEvent *)event;
}
[self playTypewriterSound:4];
return (NSEvent *)event;
}];
}