python and apple events ...

StŽphane akhar at gmx.net
Sun Apr 18 19:32:04 EDT 2004


Hello, I am wondering if any one knows how I could bind my python app to 
answer to a hotkey combination under mac os x. I know it is possible to do 
so in obj-c but it seems to call a pure C function, and this makes me a bit 
confused on how I would use it under pyobjc here is an example:

#import <Carbon/Carbon.h> 

const UInt32 kLockUIElementHotKeyIdentifier = 'lUIk'; 
const UInt32 kLockUIElementHotKey        = 109; //F10 will be the key to 
hit, in combo with Cmd 

AppShell *            gAppShell = NULL; 

EventHotKeyRef gMyHotKeyRef; 
EventHotKeyID    gMyHotKeyID; 
EventHandlerUPP    gAppHotKeyFunction; 

pascal OSStatus LockUIElementHotKeyHandler(EventHandlerCallRef 
nextHandler,EventRef theEvent, void *userData); 

- (void)awakeFromNib 
{ 
    EventTypeSpec eventType; 

    gAppHotKeyFunction = NewEventHandlerUPP(LockUIElementHotKeyHandler); 
    eventType.eventClass = kEventClassKeyboard; 
    eventType.eventKind = kEventHotKeyPressed; 

InstallApplicationEventHandler(gAppHotKeyFunction,1,&eventType,NULL,NULL); 

    gMyHotKeyID.signature = kLockUIElementHotKeyIdentifier; 
    gMyHotKeyID.id = 1; 

    RegisterEventHotKey(kLockUIElementHotKey, cmdKey, gMyHotKeyID, 
GetApplicationEventTarget(), 0, &gMyHotKeyRef); 
} 

pascal OSStatus LockUIElementHotKeyHandler(EventHandlerCallRef 
nextHandler,EventRef theEvent, void *userData) 
{ 
    // We only register for one hotkey, so if we get here we know the 
hotkey 
combo was pressed; we can do whatever we want here 

    NSLog(@"Hello, World! HotKey was pressed!"); 

    return noErr; 
} 
--- taken from a post on cocoa.mamasam.com

Thanks in advance for any help.
Stephane 



More information about the Python-list mailing list