A keychain event is generated when one of the following occurs:
If you wish to be notified of such events, you should write your own notification callback function and register it by calling function
KCAddCallback
. Indicate the events you want to receive in the
eventMask
parameter. The Keychain Manager will invoke your callback when the specified event occurs. When you no longer want to handle keychain events, call the function
KCRemoveCallback
to unregister your callback function. For a description of how to write a keychain notification callback, see
KCCallbackProcPtr
.
There are several reasons you may wish to create your own callback function. The simplest reason is to be able to respond to the event
kSystemKCEvent
when the Keychain Manager causes an update event to occur for your application's user interface. You may wish to write your own callback function to enable the dialog boxes displayed by Keychain Manager to be movable and resizable. If this is the case, you should set the bit specified by the mask constant
kSystemKCEvent
in the
eventMask
parameter of the function
KCAddCallback
to ensure that window updating occurs correctly.
If your application is not threaded, you may wish to write your own callback function to handle keychain idle events. Your callback function will be called periodically until the function completes. In this case, your application should call the functions
YieldToAnyThread
or
WaitNextEvent
when an idle event is generated. If your callback does not specify that it will handle idle events, the Keychain Manager will periodically call
YieldToAnyThread.
Listing 3-2 shows how you might register your callback function to handle keychain events. Listing 3-3 illustrates a sample keychain event handling callback function.
Listing 3-2 Registering your callback function
OSStatus RegisterMyCallBackProc (Ptr myDataPtr) { OSStatus status = noErr; static KCCallbackUPP myCallbackUPP = nil; if (!myCallbackUPP) { // create a UPP for callback function myCallbackUPP = NewKCCallbackProc(MyCallbackProc); } status = KCAddCallback(myCallbackUPP, kcEveryEvent, myDataPtr); return (status); }
Listing 3-3 Creating your own callback function
pascal void MyCallbackProc ( KCEvent inEvent, KCCallbackInfo *info, void *userContext) { MyDataPtrType myDataPtr = (MyDataPtrType) userContext; if (inEvent == kcIdleEvent) { YieldToAnyThread(); } else if (myDataPtr != nil) { // it may not be safe to allocate or move memory here, // so you may want to queue the event for later processing myDataPtr->event = inEvent; myDataPtr->item = inInfo->Item; myDataPtr->gotAnEvent = True; } }