• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Catching global keystrokes from cocoa
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >Catching global keystrokes from cocoa (From: "Joshua Martin" <email@hidden>)

  • Prev by Date: Help in Getting NSFontFamilyClass
  • Next by Date: Localizing NSString Literals
  • Previous by thread: Catching global keystrokes from cocoa
  • Next by thread: Re: Catching global keystrokes from cocoa
  • Index(es):
    • Date
    • Thread