The hanky code is a signaling system used by gay men and members of the fetish and BDSM communities to indicate sexual preference. Signals are conveyed by wearing a handkerchief of a specific color or pattern in either the right or left back pocket, conveying preference to anyone in the vicinity. While wearing a hanky, someone is said to be flagging, and the code is known as flagging, the bandanna code, or the handkerchief code.
Some people have suggested that the basics of the hanky code may go as far back to the 1800s, when men in isolated regions of the American West would wear red handkerchiefs to indicate that they were willing to take a woman's role in a dance. However, this is probably apocryphal, and has never been verified. More solidly, the seeds for the code appear to have been sown in the 1970s, when gay men first started wearing handkerchiefs according to guidelines published in The Village Voice, a newspaper in New York City.
There are a number of advantages to the hanky code which explain why it endures to this day. In the 1970s, when the gay community was less outspoken, the code allowed gay men to communicate with each other without attracting attention, and allowed men to more easily scope out potential dates. In communities where it was prevalent, men knew that pursuing men who weren't flagging could be risky, and they could seek out partners on the basis of preference by checking their back pockets.
This code also turned out to be handy in crowded environments like gay bars, where the noise and crowd could make it hard to establish a connection with someone. As the hanky code spread in the gay community, it began to be picked up more generally in the fetish and BDSM communities, and complex permutations of colors, patterns, and placements began to proliferate.
As a general rule, someone who flags in the left pocket indicates that he or she is a top, while someone who flags in the right pocket is a bottom. Bottoms prefer to participate as recipients in sexual and fetish encounters, while tops prefer just the opposite.
Knowing about the hanky code can be useful if you are traveling to an area with a large queer or fetish-oriented population, as you might unwittingly send out a signal if you leave a handkerchief in your back pocket. However, many people rely on other social cues beyond the hanky code; if you happen to have, for example, a gray handkerchief in your right pocket while innocently riding the subway, you are unlikely to be ravaged by eager bondage tops, although someone might approach with a courteous introduction.