Use the OnTabSwitch script trigger.
In this script, Get ( TriggerCurrentPanel ) can tell you what panel is being "left" and Get ( TriggerTargetPanel ) can tell you what new panel is being selected when this trigger is tripped.
Both of these get functions return a list of two values, the index number of the tab panel in question and it's object name. (Object names are given to layout objects by selecting them while in layout mode and then entering a name in the "name" box in the top of the Inspector's Position tab.)
You can look all of these up in FileMaker Help to learn the details.
Of particular interest is that if you use: Exit Script [False] in this script, it will prevent the selected tab from coming to the front--effectively canceling out the mouse click that selected a different panel in the tab control.