What historical events or trends would have to be altered?
It is hard to point at a single instant in time and say that yes, that was the turning point, but by way of an example I'd look to the fall of Constantinople in 1453. The Ottoman army used some exceptionally large cannon that took hundreds of men to manoeuver and operate, and (to grossly oversimplify) used them to wreck the defenses of a walled city, end the Roman empire and wrap up the european middle ages (depending on who you ask). It would have been quite unambiguous for all involved that when those cannon brought the walls down that the world was changing and that gunpowder was the future of warfare. They were behind the curve, though.
The cannon that they used were already the products of a long tradition of gunpowder warfare. Fire-arrows had been about for hundreds of years, and had evolved into the precursors of rocket artillery. Gunpowder hand grenades, the precursors to more substantial bombs and explosive shells, were similarly old. Cannon had been developed some 200 years previously, and had been steadily improving and spreading during that time.
Basically, you'd need some kind of apocalyptically devastating event to occur in China about a thousand years ago to slow all that down... plague, or meteorite strike might have done the job but really, the genie was already out of the bottle by then. By the time Constantinople fell it would have been far, far too late.
To slightly misquote William Gibson, the future was already there. It just hadn't been widely distributed yet.
instead of shooting at distant specks and twitch-spraying bullets in close quarters
Do note that it took hundreds of years of scientific and technological development to develop guns capable of that sort of thing. Troops armed with mêlée weapons were still found on battlefields for hundreds of years... spanish tercios were still useful units in the mid 17th century, some 200 years later, and cavalry armed with lances and sabres were still fielded in the 19th century to occasional good effect.
That's the best part of 400 years of sword-wielding heroes (or villains) and knights in shining armour across Europe. If you can't make a good story out of that, it seems like maybe you're just not trying!
Fantasy writers tend to prefer forms of combat that predate guns, because they are more showy, and they involve close and long-duration combat with an identified adversary
Its because they like lazy stereotypes. That's ok, because the readers like them too. The advent of gunpowder siege weaponry rather ruins castles and walled cities, and everyone likes a good fortification, but everything else associated with those well-worn fantasy tropes would have carried on working just fine for a long, long time before it became unambiguously foolish to go to war without firearms.