keydown
kEventClassKeyboard
NSResponder

// ----------------------------------------------------------------------
// Listen to events. Only switch if the tab value has changed.

pascal OSStatus TabEventHandler(EventHandlerCallRef inHandlerRef, EventRef inEvent, void *inUserData)
{
    OSStatus result = eventNotHandledErr;
    
    ControlRef theControl;
    ControlID controlID;
    
    GetEventParameter(inEvent, kEventParamDirectObject, typeControlRef, NULL, sizeof( ControlRef ), NULL, &theControl );
    
    GetControlID(theControl, &controlID);
    
    // If this event didn't trigger a tab change, give somebody else a chance to handle it.
    if (controlID.id == TAB_ID && GetControlValue(theControl) != lastTabIndex) {
        result = noErr;
        SelectItemOfTabControl(theControl);
    }    
    
    return result;
}

// ----------------------------------------------------------------------

void InstallTabHandler(WindowRef window)
{
    EventTypeSpec	controlSpec = { kEventClassControl, kEventControlHit }; // event class, event kind
    ControlRef 		tabControl;
    ControlID 		controlID;

    // Find the tab control and install an event handler on it.
    controlID.signature = TAB_SIGNATURE;
    controlID.id = TAB_ID;
    GetControlByID(window, &controlID, &tabControl);

    InstallEventHandler(GetControlEventTarget(tabControl),
                        NewEventHandlerUPP( TabEventHandler ),
                        1,
                        &controlSpec,
                        0,
                        NULL);

    //Select the active tab to start with.
    lastTabIndex = GetControlValue(tabControl);
    SelectItemOfTabControl(tabControl); 
}

