Keyevent for cmd-c cmd-v cmd-x on OSX ?

hm, …
is there a way to have key events for cmd-c cmd-v cmd-x on OSX ?

greetings ascorbin

I think it’s pretty tricky in GLUT since that only grabs ctrl, alt, and shidt. You might check here for some info on how other people have tried to tackle it: https://github.com/openframeworks/openFrameworks/pull/807

thank you joshua

very helpful. im not finished with it, problem is that GLUT swallows some combinations like cmd-c, cmd-h, cmd-m, but lets through cmd-z or cmd-x. for that i found a workaround with NSNotification, but currently it fires 4 events for each keyDown…

KeyModifiers.h

  
#ifndef _H_KEYMODIFIERS  
#define _H_KEYMODIFIERS  
  
#include "ofMain.h"  
  
#ifdef __OBJC__  
@class AppleKeyModifiers;  
#endif  
  
class KeyModifiers {  
      
public:  
      
    KeyModifiers();  
    ~KeyModifiers();  
      
    bool getAppleCommandModifier();  
    int getKey();  
      
private:  
      
#ifdef __OBJC__  
    AppleKeyModifiers* modifiers; //only obj-c needs to know the type of this protected var  
#else  
    void* modifiers;  
#endif  
};  
  
  
#endif  

KeyModifiers.mm

  
#include "KeyModifiers.h"  
  
#import <AppKit/AppKit.h>  
#include "testApp.h"  
  
@interface AppleKeyModifiers : NSObject  
{  
      
}  
  
- (id)setup;  
- (BOOL)getAppleCommandModifier;  
  
@end  
  
@implementation AppleKeyModifiers;  
  
- (id) setup{  
      
    NSLog(@"KeyModifiers setup");  
    self = [super init];  
      
      
    [[NSNotificationCenter defaultCenter]  
     addObserver:self  
     selector:@selector(handleNotification:)  
     name:nil  
     object:nil];   
      
    return self;  
}  
  
-(void)handleNotification:(NSNotification *)pNotification  
{  
    if([[NSApp currentEvent] type] == NSKeyDown) {  
    NSLog(@"message = %@",(NSString*)[pNotification object]);  
        testApp *p = (testApp*) ofGetAppPtr();  
        unichar key = [[[NSApp currentEvent] charactersIgnoringModifiers] characterAtIndex:0];  
        p->keyPressed(key);  
    }  
}  
  
// could impliment all of these on mac, but I guess we really just want the command key??  
//enum {  
//    NSAlphaShiftKeyMask         = 1 << 16,  
//    NSShiftKeyMask              = 1 << 17,  
//    NSControlKeyMask            = 1 << 18,  
//    NSAlternateKeyMask          = 1 << 19,  
//    NSCommandKeyMask            = 1 << 20,  
//    NSNumericPadKeyMask         = 1 << 21,  
//    NSHelpKeyMask               = 1 << 22,  
//    NSFunctionKeyMask           = 1 << 23,  
//#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4  
//    NSDeviceIndependentModifierFlagsMask    = 0xffff0000UL  
//#endif  
//};  
  
- (BOOL)getAppleCommandModifier{  
    return (([[NSApp currentEvent] modifierFlags] & NSCommandKeyMask) == NSCommandKeyMask);  
}  
  
- (int)getKey{  
      
    if([[NSApp currentEvent] type] == NSKeyDown) {  
        return (int) [[NSApp currentEvent] keyCode];  
    } else return -1;      
}  
  
@end  
  
//--------------------------------------------------------------  
KeyModifiers::KeyModifiers(){  
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
    modifiers = [[AppleKeyModifiers alloc] setup];  
    [pool release];  
}  
  
//--------------------------------------------------------------  
KeyModifiers::~KeyModifiers(){  
      
}  
  
bool KeyModifiers::getAppleCommandModifier(){  
    return (bool)[modifiers getAppleCommandModifier];  
}  
  
int KeyModifiers::getKey(){  
    return (int) [modifiers getKey];  
}