The man accused of killing five Capital Gazette staff had threatened the newspaper in 2013 but then “went dark,” police said. Until Thursday.

Shortly before Jarrod Ramos blasted out the glass doors of the newsroom near Annapolis, Maryland, at about 3 p.m., he sent another threat on social media, police said, and then unleashed his rampage, shooting with a legally purchased 12-gauge pump-action shotgun until he finally laid it down and hid under a desk as police arrived.

The news organization and its lawyer had reported the May 2013 threats and spoke with a detective who investigated. The newspaper decided not to pursue criminal charges because it might “exacerbate” the situation, Anne Arundel County Police Chief Timothy J. Altomare said Friday during a news conference.

The threats came amid a lawsuit Ramos filed accusing the paper of defaming him through a column describing his guilty plea to harassing a woman over social media. He lost the defamation case.

On Friday, a judge ordered Ramos of Laurel, Maryland, to remain detained and ordered him held without bond on five counts of murder in a court appearance where Ramos appeared via a video feed from a detention center.

At the bond hearing, Anne Arundel County State’s Attorney Wes Adams said Ramos barricaded the back exit of the newsroom so people could not escape and that he had avoided the main entrance to avoid detection. Adams called Ramos an “overwhelming threat and danger to our community.”

The prosecutor said Ramos worked his way through the office, shooting victims along the way. “There was one victim that attempted to escape through the back door but was shot,” he told the judge.

Four journalists and a sales associate for the Capital Gazette died and two people suffered what police called minor injuries.

Ramos appeared in the video feed in court in a blue, v-neck prison uniform. He said nothing and was expressionless. He stared at the camera.

Police said that after a search of his Laurel apartment, they found evidence showing that Ramos planned the attack.

“We can’t fathom why that person chose to do this,” Altomare said.

The shootings are believed to be the deadliest attack on journalists in the United States in decades.

According to Anne Arundel County Executive Steve Schuh, the message shortly before the attack said “something like ‘leave me alone’ or ‘leave me the hell alone.’”

He said it was not clear if that message from Ramos was directed at the Capital Gazette or “at the world.”

On Friday, the opinion page of the Capital Gazette read, “Today we are speechless.”

It went on, “This page is intentionally left blank today to commemorate victims of Thursday’s shooting at our office.”

The victims were Gerald Fischman, Rob Hiaasen, John McNamara, Rebecca Smith and Wendi Winters. Fischman and Hiaasen were editors, McNamara was a reporter, Smith was a sales assistant and Winters worked for special publications, according to the newspaper’s website.

The two people injured were likely hit by broken glass, according to officials.

Schuh said in an interview Friday that Ramos had a “long-standing grievance” with the newspaper and had filed lawsuits against the paper and “lost them all.”

Ramos lost a defamation case against the paper in 2015 over a 2011 column he contended defamed him. The column provided an account of Ramos’s guilty plea to criminal harassment of a woman over social media.

Officials said Ramos was identified using a facial recognition system.

Ramos was not cooperating with investigators as of Thursday night, officials said. On Friday, Altomare said Ramos “hasn’t said much the whole time.” He said there are still “a lot more unanswered questions.”

On Friday’s edition of ABC’s “Good Morning America,” Keith Cyphers — whose sales office is across the hall from the Gazette’s — recalled the Thursday shooting scene.

He said he was on the phone with a client and heard an “incredibly loud noise” and thought it was possibly an explosion.

When he leaned from his desk and looked out into the hallway and into the Gazette’s office and lobby, he saw that the newspaper office’s glass door was gone, he said.

“It was broken into a million pieces in the hallway,” Cyphers said. Then he saw the gunman “holding a black shotgun.”

“It was up against his chest,” Cyphers said. “He was moving through the lobby” of the Gazette. “He was moving while aiming deeper into the office.”

“He looked comfortable holding a weapon,” Cyphers said. “He didn’t look like someone who was out of control. He looked like someone who was comfortable with what he was doing.

“It was terrifying,” he said, “Absolutely terrifying.”

Ramos seemed to carry a grudge for years against the Gazette after he was the subject of a column describing how he harassed a former classmate from Arundel High School, first on Facebook and then through emails. Ramos pleaded guilty in July 2011 to harassment. In a column written by Eric Hartley several days later, the woman described how Ramos had stalked her online and perhaps caused her to lose her job.

Ramos then apparently created a website that detailed his complaints against Hartley and the newspaper. Hartley is no longer at the Gazette and now works as an editor at the Virginian-Pilot in Norfolk, Virginia.

In 2013, the newspaper had an incident with Ramos that was reported and investigated by law enforcement, but the Gazette decided “not to press forward because they were afraid it was going to exacerbate the situation,” according to the county police chief.

Ramos has worked at the federal Bureau of Labor Statistics, according to a lawyer who represented him in 2011, but whether he still worked there could not be confirmed Thursday.

Investigators are still piecing together exactly how Thursday’s shooting unfolded.

The shooting began about 3 p.m. in a brown five-story office building just outside downtown Annapolis. It sits about four miles west of Maryland’s statehouse.

Schuh said Ramos used a rental car to drive to the Gazette office. The car was identified Thursday night by tag-reading software.

The newsroom is on the first floor of the office building and is easily accessible from the main entrance, said according to a local politician.

Ramos also carried canisters with smoke grenades that he used inside, police said.

According to the charging documents, Ramos used a “long gun firearm and shot out the business doors” at the Gazette’s office. He then went inside and shot “individuals he encountered within the business,” the documents stated.

His shotgun, according to the county executive, was loaded with buckshot.

“This person was prepared today to come in, this person was prepared to shoot people,” Anne Arundel County Deputy Police Chief William Krampf said. “His intent was to cause harm.” He appears to have acted alone, officials said.

Shortly after the attack, Gazette reporter Phil Davis posted this message on Twitter: “There is nothing more terrifying than hearing multiple people get shot while you’re under your desk and then hear the gunman reload.”

Davis characterized the scene as a “war zone” and a situation that would be “hard to describe for a while,” in a news story posted to the daily newspaper’s website within 45 minutes of the shooting.

First responders entered the building and immediately encountered a woman with life-threatening injuries and other wounded people. He said no gunfire was exchanged between the man and police.

Four people died at the scene, and one woman was pronounced dead at the University of Maryland Medical Center.

Police carrying automatic weapons rushed about 170 office workers with their hands above their heads out of the building to a department store across the street.

“Devastated & heartbroken. Numb,” Gazette editor Jimmy DeButts said on Twitter. “Please stop asking for information/interviews. I’m in no position to speak, just know @capgaznews reporters & editors give all they have every day. There are no 40 hour weeks, no big paydays — just a passion for telling stories from our community.”

Video from a surveillance camera in the office helped investigators identify Ramos as the gunman, according to the two-page charging documents. Ramos had no wallet or other identification on him at the time.

Sgt. Amy Miguez, an Annapolis Police Department spokeswoman, said that early on Thursday she received a text message from Phil Davis and that she referred the reporter to county police. Davis had said he needed to write a story about jurisdiction lines between city and county police to help him get it straight.

At 2:41, Davis texted Miguez again and wrote: “Help. Shooting at office.”

Miguez initially thought it was a joke and again referred him to call county police, because they have jurisdiction at the Gazette offices.

Davis quickly responded that he couldn’t call and that he was trying to stay as quiet as possible.

Miguez said she immediately dialed 911 and gave the location of the paper to report the shooting.

Fears rose in the building as people heard there was a shooter.

“I was so scared,” said Rayne Foster, who works on the fourth floor. “I was very scared.”

Locked in a room with about a dozen others, Foster had sent a text to her daughter: “There’s an active shooter. I love you.”

“I was taking deep breaths,” she said. “We could hear them busting out the glass doors and windows. It was so surreal.”

Maryland Gov. Larry Hogan commended the quick police response. On Friday, he ordered Maryland flags to be flown at half-staff.

“It’s a tragic situation, but there were some very brave people who came in and kept it from being worse, and the response time was incredible,” said Hogan.

Annapolis Mayor Gavin Buckley said Friday that the shooting happened in a tightknit community, “a Mayberry kind of place.”

The violence, he said, shows that the “country is so polarized.”

“We all want the same things, and I think we’re losing sight of that,” Buckley said. “We all know journalists. We deal with them every day. They care about us.”

He said that after the March for Our Lives in Washington, following the shooting at a school in Parkland, Florida, he thought, “Finally these kids are going to get listened to.”

“We can’t let this just happen,” he said. “We can’t be numb to this event.”

The effect of the shooting reverberated through newsrooms outside of Maryland. The New York Police Department said it “deployed counterterrorism teams to media organizations” in and around the city “out of an abundance of caution.” D.C. police planned to station two uniformed officers outside The Washington Post building, and other media outlets also increased security.

The Capital Gazette, Annapolis’s daily newspaper, is widely read in Maryland’s capital and in surrounding Anne Arundel County. The paper promotes itself as one of the oldest publishers in the country, with roots dating to the Maryland Gazette in 1727.

The paper has 31 people on its editorial staff and had a daily circulation of about 29,000 and a Sunday circulation of 34,000 as of 2014.

Commonly referred to as the Capital, the paper was founded in 1884 as the Evening Gazette. The Baltimore Sun Media Group — owned by Chicago-based Tronc — bought the paper in 2014 from Landmark Media Enterprises, based in Norfolk. The new owners converted it from an afternoon publication into a morning paper in 2015.

The paper had previously been part-owned by Philip Merrill, who was the owner and publisher of Washingtonian magazine.

The newsroom is an open space, and “the desk would be the only place you could hide,” Buckley said.

Buckley said Annapolis, which has a population of about 39,000, is a small town where officials all know the newspaper’s reporters, who cover zoning issues, local crime and even a cat stuck in a tree.

“They don’t make a lot of money — maybe $30,000 a year,” Buckley said. “It’s immoral that their lives were at risk.”

The Washington Post’s Michael Brice-Saddler, Lynh Bui, Paul Farhi, Joe Heim, Peter Hermann, Arelis R. Hernández, Reis Thebault, Rachel Weiner and Clarence Williams contributed to this report.