Phrase Finder says this originated in the American West, where the signal for a fight was often to drop one’s hat.
In the 19th century it was occasionally the practice in the United States to signal the start of a fight or a race by dropping a hat or sweeping it downward while holding it in the hand. The quick response to the signal found its way into the language for any action that begins quickly without much need for prompting. Dictionary of Cliches by James Rogers (Wings Books, Originally New York: Facts on File Publications, 1985).
The earliest written citation, from Worldwidewords,
is some way from the American frontier: They could agree in the twinkling of an eye — at the drop of a hat — at the crook of a finger — to usurp the sovereign power; they cannot agree, in four months, to relinquish it. Register of Debates in Congress, 12 Oct. 1837, [which] shows that even at this early date the expression was already idiomatic.
There’s little doubt about the matter, despite the regrettable failure of any early user to put its origin on record for us.