Thank you for your reply. It helped a lot.
I tried using every button I had joystick + a/b/x/y/L1/R1/L2/R2/coin/players for two players plus one additional button to act as the hotkey for both sets. So one button as hotkey for both players. Unfortunately, nothing worked very well and I became extremely frustrated.
Finally I found a post in a Reddit forum where someone explained that every button must be plugged into the same position on the microswitch board as the other board. Also, when you configure one set of buttons through Retropie (I think the same applies for Batocera) it also configures the controls for the other player. If you try to configure both, it will overwrite the configuration of the first.