Re: Catching global keystrokes from cocoa
Re: Catching global keystrokes from cocoa
- Subject: Re: Catching global keystrokes from cocoa
- From: "Joshua Martin" <email@hidden>
- Date: Wed, 7 Jun 2006 21:48:47 -0700
I added an NSApplication subclass and now it works like a champ. For
some reason it still never uses the callback i specify in my
InstallEventHandler method. Apparently registering the event handler
in this way makes all global keypresses come in as a regular old
keydown. Here is the code i have working if anyone is interested (and
for list archive purposes).
-josh
#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
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden