Help - Search - Members - Calendar
Full Version: C++ Keyboard Input...
TSF - Mac Security Forums > Discussion > Programming
Macpunk
Does anyone know how I can tell if a key is down, and if one isn't, continue with my code?

Basically, I'm looking for the Mac version of GetASyncKeyState

Thanks,
Macpunk
ChefNinja
Damn. I had the code for this bookmarked on my mac. Too bad I formatted it then sold it :( I'll see if I can find it again.
Macpunk
Thanks ChefNinja, that would be a LOT of help. You'll get creds in my hack. :p

--Macpunk
ChefNinja
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.
Macpunk
Yea...I'm keeping that code for future use, but what I need is a simple call to check if a certain key is pressed.

I'm injecting my code into a carbon application, and hooking a few functions, so in one of the hooks, I need to test if certain keys are down, and do something if they are. If they aren't then continue on with the code, and keep the ones that are down, but aren't the ones I need available to the rest of the application.

If anyone has anything like this...it would be much appreciated.

Thanks for your help though,
Macpunk
ChefNinja
Oh, here ya go. Take a look at GetKeys().

CODE
UInt8 PushButtonStatus()
{
    KeyMap theKeys;
    KeyMap f5Key = { 0x00000000, 0x0000000C, 0x00000000, 0x00000000 };
    
    GetKeys(theKeys);
    return (0 == memcmp(theKeys, f5Key, sizeof(KeyMap)));
}


Btw, my aim is chefninja, so stop being a poon and aim me :P
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.