At some point the Mad Hatter obtained a sample of the Scarecrow's
fear gas, which he modified with other chemicals to make his own
variant of the drug.
After Batman infiltrates the Mad Hatter's Hat Shop and defeats the
Wonderland Gang on his mission to free Vicki Vale from Hatter's tea
party, Hatter uses the gas to infect Batman. The gas causes him to
see Poison Ivy, Catwoman and the Carpenter as Lewis Carrol styled
characters.
Batman injects himself after the fight with the “Jabberwocky,” so
that the gas won't completely take him over. He keeps a syringe full
of an agent that brakes down the toxin in his utility belt, making
sure before he did use the agent that his adrenaline levels subside.