Yeah, mame is kind of weird about the way it deals with multiple devices, but it's not entirely mame's fault.
the real issue is that Windows, when it sees multiple devices of the same type connected (this is USB devices, btw, I'm not talking about other stuff right now), it does two things.
1) it stashed the device info in essentially an array of devices and that array is always kept filled. That means that if you plug device a in and then b, a will be in slot 1 and b in slot 2. If you then unplug them and plug them in b first, b will now be in slot 1 and a in slot 2.
2) For many devices, windows does log a unique ID for the device. however, from what I can tell, MAME doesn't use it. Mame bases everything on those "slot" numbers I mention above.
This is particularly problematic for keyboards and mice, because in both cases, +usually+ all mice are treated by MAME as if they're a single mouse. Same with Keyboards.
HOWEVER, if you have a mouse and several trackballs connected (trackballs look like mice to windows and mame), then if you DON'T enable the multiplemice option, mame will treat all the mice/trackballs as a single mouse.
If you enable it, then you get numbering similar to what you're seeing for the keyboard devices.
If you DON'T want the keyboard devices considered independently, just turn off that multiplekeyboard option, then run through all your CFG files and replace KEYBOARD_1_ with KEYBOARD_
Same with KEYBOARD_2_, etc
So you'll end up with just KEYBOARD_A, KEYBOARD_B etc.
Alternatively, (and with a bit more work), you might use my ControllerRemap utility (see my sig) to automatically remap controllers dynamically each time you start mame.
I use it in particular because often when I connect or disconnect my drum or guitar controllers, doing so shifts around trackballs, or joysticks. ControllerRemap uses the uniqueID that's available to specifically rewrite mame config files to point to whatever slot the applicable controller is now "in".
It's still a bit of a pain, but once you get it going, it's pretty seamless.