NYPD Commissioner Ray Kelly admitted that police target blacks and Hispanics for the controversial stop-and-frisk program in an effort to keep guns off the streets, a cop-turned-lawmaker testified yesterday.

State Sen. Eric Adams said he twice heard Kelly make the admission, the first time during a 2010 meeting with officials, including then-Gov. David Paterson and then-Assemblyman Hakeem Jeffries.

Kelly called Adams’ allegations “ludicrous” and “absolutely, categorically untrue” after the Brooklyn Democrat made them while under oath in Manhattan federal court on behalf of plaintiffs challenging stop-and frisk.

“It’s interesting. Apparently, only Mr. Adams heard the statement, although other people were present,” Kelly told reporters.

“It just defies logic. Anybody knowing Mr. Adams’ history with this department and how often he’s criticized it, that I would make that type of statement in front of three elected African-American officials.”

Adams, a former NYPD captain and co-founder of 100 Blacks in Law Enforcement Who Care, testified that the July 2010 meeting came days before Paterson signed a law that bans the NYPD from keeping a database of everyone who has been stopped and frisked.

Adams, who co-sponsored the bill with Jeffries, said he complained that a “disproportionate” number of blacks and Hispanics were being subjected to stop-and-frisk.

According to Adams, Kelly “stated that he targeted or focused on that group because he wanted to instill fear in them that any time they leave their homes they could be targeted by police.”

Adams said he was “amazed” and “shocked” by Kelly’s alleged remarks, adding, “I told him that was illegal.”

He said Kelly responded by asking, “How else are we going to get rid of guns?”

Adams also said Kelly made similar comments during an August 2010 meeting of black elected officials in Brooklyn.

Another participant in the July 2010 meeting, state Sen. Martin Golden (R-Brooklyn), backed up the city’s top cop, saying of Adams’ testimony, “That’s not my recollection of that at all.”

Jeffries, now a Democratic congressman representing parts of Brooklyn and Queens, said Kelly, “in my view, articulated a deterrence theory to support the NYPD’s aggressive stop-and-frisk activity in black and Latino neighborhoods.”

“Did he use the word ‘target’? I can’t say one way or the other. But the whole conversation was taking place in the context of disproportionate stop-and-frisk in black and Latino neighborhoods,” Jeffries said.

Paterson didn’t respond to a request for comment.

During cross-examination of Adams, city lawyer Heidi Grossman tried to read aloud from a written declaration in which Kelly denied Adam’s allegations, but was blocked by the judge, who called it a “back-door” attempt to introduce testimony from Kelly.

Manhattan federal Judge Shira Scheindlin then publicly invited Kelly to take the stand, saying: “If he’d like to come here, he’s welcome in this courtroom.”