pan_setbutton returns a number of a button and 0 if it was not found. Itarate from 1 to n until pan_setbutton returns 0.