BROCKTON — In gruesome detail, the self-described white supremacist accused of killing two Cape Verdean nationals and raping one victim’s sister in 2009 confessed to police that he targeted nonwhites and planned to end the murderous day by committing suicide.

The disturbing confession was recounted Tuesday in Brockton Superior Court, where Keith Luke is on trial in the Jan. 21, 2009, fatal shooting of Selma Goncalves, 20, and Arlindo DePina Goncalves, 79, who were not related. Luke, 26, of Brockton, is also accused of raping and shooting Selma Goncalves’ 22-year-old sister.

The sister survived. The Globe is not identifying her because she is the victim of an alleged sexual assault.

Luke described to police his plot to invade the homes of nonwhites, kill them, and drive on bingo night to a synagogue where he had ­intended to have a “happy ending” by shooting himself in the head.

He was arrested before he could complete the plan.

“I decided I was going to kill as many nonwhites as possible, and then kill myself,’’ Luke told police shortly after he was ­arrested, said his attorney, ­Joseph F. Krowski Jr.

Since the trial began Thursday, prosecutors have sought to portray Luke as a calculating killer who amassed a trove of weapons including hammers, crowbars, and medical devices that prosecutors allege he ­intended to use in a war against anyone who was not white.

In court Tuesday, Krowski used his client’s words to paint a picture of a disturbed young man who had languished in psychiatric hospitals and then in isolation, where he cultivated hateful, deadly thoughts.

Luke confessed to the crimes, but gave a series of ­bizarre explanations for his ­actions, saying, among other things, that he was angry about black men raping and killing white women on a “genocidal scale’’ and that “Jew-controlled media” have done nothing to report it, his lawyer said in court.

Krowski said Luke told police he had no friends or family. He said he had never had sex and that he had intended to have intercourse with a girl ­before he died, Krowski said.

When asked by police why he raped the woman, Luke replied that women rejected him and that he believed the reason was that black men were dating all the white women. “How am I going to get a girl?” Krowski quoted him as saying.

He said that Luke told police that “white cowards” have ­allowed other whites to be ­degraded and that he was going to war with nonwhites.

“I have to sacrifice my life for something,’’ he said, according to testimony.

Questioning the chief investigator in the case, Krowski sought to show that Luke was not in touch with reality, saying that his client described a “Zionist-­occupied government” that keeps a list of white survivalists, including him and that the government was planning to round them up and put them in camps.

“You didn’t go out and investigate whether there was a ­Zionist occupation for the case, did you?’’ Krowski asked the ­investigator, State Police trooper Elvin Morales.

“No,’’ Morales replied.

“Because you know that is gibberish, right?’’ Krowski said.

“Yes,” Morales responded.

Morales testified that Luke made the statements while ­police were filling out paperwork at booking and then later in a separate interview.

The trooper said that he asked Luke a series of routine questions, including whether he was married or single and that Luke responded in a chilling monotone.

“I am fighting for the dying race,’’ he told the trooper. “I am speaking the truth. I’m banned from a list that has blackballed me from everything.”

Luke, in white T-shirt and prison-issued green pants, sat quietly with one arm folded over the other during the trial. At one point, he wrote something on a yellow legal pad, and he smirked while his lawyer ­recounted his words for jurors.

His defense attorney said Luke had been in a psychiatric hospital for three years prior to 2009 and that prior to the killings, he had spent a majority of the time isolated. He had held a job for only one day in his life.