For Donald Trump, the deaths of 49 people in a massacre at a gay nightclub in Orlando on Sunday morning came as a sort of vindication. The presumptive Republican presidential nominee had been talking about the risk of terrorism for months. "It's probably why I'm number one in the polls," he said in March. As he saw it, the attack had proved him correct.

"Appreciate the congrats for being right on Islamic terrorism," he tweeted. He wasn't gloating, though: "I don't want congrats," he added. "I want toughness and vigilance."

Although Trump's comments were widely criticized as crass, many pundits and political observers agreed with him that the worst mass shooting in American history will help him at the polls.

"There appears to be a general consensus -- met with glee by Trumpites and frustration by Democrats -- that the atrocity in Orlando will be a boon to the campaign of Donald Trump," wrote Josh Marshall, the editor of Talking Points Memo.

"Because this is getting cast as terrorism, his response will work with a lot of people," a Democratic pollster told The Washington Post's Chris Cillizza.

In fact, the evidence from political science and psychology for the idea that Trump will benefit from the attack is decidedly mixed. There are a few reasons that Trump might be in a better position than Democratic presidential candidate Hillary Clinton to make a case based on national security, but studies show that people can react to terrorism in divergent ways based on the national mood and the decisions of political leaders.

By raising the stakes for the campaign, the killings in Orlando could reinforce the bases of both candidates.

"It is unlikely, in my view, that Trump will gain a significant advantage as a result of events like this," said Nicholas Valentino, a political scientist at the University of Michigan. "The reaction to this may be to galvanize support on either side more than to shift a lot of votes from one party to the other."

In general, terrorist attacks involving international conflicts seem to increase public attitudes that are associated with political conservatism -- suspicion of foreigners and ethnic or religious minorities and greater aggression in foreign policy. Yet two economists who studied the issue in Israel found that beyond increasing voters' conservatism, terrorism also had a polarizing effect.

The attacks increased the number of votes won by conservative parties in places in Israel that terrorists targeted directly. At the same time, however, terrorism increased support for liberal parties in areas where those parties already had an advantage.

In other words, the research from Israel suggests that how individual voters respond to attacks depends on how directly they were affected, as well as their previously held political views. How terrorism affects a society broadly could depend on the existing political climate as well as on the frequency and the extent of the attacks.

Researchers studied U.S. voters' responses to the terrorist attacks of Sept. 11, 2001. A couple of surveys, one by a group of researchers at Carnegie Mellon University and another from the University of Illinois at Chicago found a divergence based on Americans' specific emotions about the attacks.

Those who felt primarily anger had more negative views of Arab Americans and immigrants, and they were also more likely to endorse punitive policies such as deportation. For those who felt afraid as opposed to angry, the attacks had a more ambiguous effect on their political preferences.

In general, researchers have found that men are more likely than women to experience anger in response to these types of terrorist attacks. Trump, however, is already doing exceptionally well among men. He had the support of 57 percent of male voters in the most recent poll by The Washington Post and ABC News -- another reason that terrorist attacks might be less likely to change voters' opinions than to confirm them in their existing decisions to support one candidate or the other.

Politicians, though, can always try to change how their constituents respond to tragedies. Describing events as unpredictable and beyond anyone's control can create fear. On the other hand, characterizing events as predictable and preventable can incite anger by focusing public attention on the particular groups or people who were allegedly culpable.

"Emotions are malleable," said Jennifer Lerner, a psychologist at Harvard University who was previously part of the group at Carnegie Mellon. "The way that political leaders and the media shape our emotions is crucially important."

Finally, it is still five months until the election. Any political consequences of Sunday's attack in Orlando could be transitory.

Joscha Legewie, a sociologist at New York University, studied how public opinion among Europeans changed in response to deadly bombings in Kuta, Indonesia, in 2002 and in Madrid in 2004. Europeans' attitudes toward immigrants became more hostile after both events, but the effect disappeared in a matter of weeks.

Legewie also found that the changes in attitudes were most pronounced in economically depressed areas and in areas with an increasing population of immigrants. Again, if a similar pattern holds in the United States, the research is more reason to think that terrorist attacks will mainly help Trump with people who are already likely to support him.

More from Wonkblog: