Get breaking news alerts and special reports. The news and stories that matter, delivered weekday mornings.

President Barack Obama on Saturday criticized Republican Sen. Pat Toomey of Pennsylvania after the incumbent locked in a tight race against a Democratic rival released a last-minute ad showing Obama praise him in the past for his position on gun legislation.

"Pat Toomey may have done the right thing on one vote, but courage is telling Pennsylvania voters where you stand on the tough issues, not just the easy ones like background checks,” Obama said in a statement released by the Democratic Senatorial Campaign Committee.

Let our news meet your inbox. The news and stories that matters, delivered weekday mornings. This site is protected by recaptcha

The Toomey campaign advertisement says "In Washington, the rarest political quality is courage.” The narrator then asks viewers to “listen to what President Obama says about Pat Toomey."

In this Monday, Oct. 24, 2016 combination of photos, Pennsylvania U.S. Senate candidates Democrat Katie McGinty, left, and Republican Sen. Pat Toomey take part in a debate at Temple University in Philadelphia. AP

Meant to display Pat Toomey’s ability to find bipartisan compromise in the Democratic-leaning state, the video plays a brief clip of Obama providing the senator praise for helping write legislation that would require background checks on all firearms bought online and at gun shows.

The bill ultimately failed, but Obama still thanked Toomey and Democratic Sen. Joe Manchin of West Virginia for working on it.

Related: Women Look to Make More Gains in U.S. Senate

This is the biggest shift toward the middle Toomey has made this election. The beleaguered senator is in a tough race against Democratic challenger Katie McGinty, and Toomey is a top target for Democrats as they try to retake the Senate.

Seeking to avoid alienating Republicans and moderates, Toomey has ducked questions about whether he would vote for GOP nominee Donald Trump — something Obama pointed out Saturday.

"Pat Toomey won't tell Pennsylvania voters where he stands on Donald Trump, trying instead to have it both ways by telling different people what he thinks they want to hear,” Obama said in the statement. “That's not courage."

The president also criticized Toomey for voting in favor of shutting down the government and against the terrorist gun loophole, and reiterated his support for McGinty.

Toomey typically voted with the NRA and has one of the most conservative voting records in Congress, The Associated Press reported. He is one of the most vulnerable incumbent senators this election cycle, as his state has a steady Democratic advantage.