A man accused of trying to kill U.S. congresswoman Gabrielle Giffords was charged by federal prosecutors on Sunday after six people were killed and 14 others injured following a shooting spree at a public appearance in Arizona.

Jared Loughner has been charged with one count of attempted assassination of a member of Congress, two counts of killing an employee of the federal government and two counts of attempting to kill a federal employee.

A federal judge, one of the congresswoman's aides and a nine-year-old girl were among the dead.

Investigators formally laid charges after executing a search warrant at the suspect's Tucson home. Investigators said they found an envelope inside a safe that contained messages such as "I planned ahead," "My assassination" and the name "Giffords" next to what appears to be a signature.

The Associated Press is reporting Loughner had a run-in with Giffords three year ago, when he was dissatisfied with an answer he got from the congresswoman.

According to friends, Loughner asked, "What is government if words have no meaning?" at a public event.

Giffords read the question but did not answer it, his friends recalled.

"He did not like government officials, how they spoke. Like they were just trying to cover up some conspiracy," one friend told The Associated Press on Sunday.

The friends, who did not want to be named, said Loughner was confrontational and was obsessed with how words create reality.

Doctor ‘optimistic'

Giffords was the most seriously injured of the 14 victims wounded when a gunman opened fire at a public meeting Saturday morning outside a Tucson grocery store, shooting the Democratic congresswoman in the head at close range and then spraying bullets through a crowd of her constituents lined up to meet her.

On Sunday, doctors said they were "cautiously optimistic" about Giffords' chances of recovery.

Dr. Michael Lemole Jr., the chief of neurosurgery at University Medical Center in Tucson, said she was able to open her eyes and follow simple commands such as squeezing her hand or holding up two fingers.

"We are very encouraged by that," he told a news conference. "I am cautiously optimistic."

Doctors said Giffords remains in critical condition because of the massive damage done by the bullet, which he said struck her in the back of the head and passed completely through her skull.

"This is about as good as good can get," said trauma director Dr. Peter Rhee.

The congresswoman is currently in a medically induced coma and breathing on a ventilator so she cannot speak, the doctors said.

Giffords' Tucson synagogue was crammed with mourners praying for her quick recovery on Sunday. Candles lit a makeshift memorial outside the hospital, alongside flowers, flags and signs reading, "Peace + love are stronger," "God bless America" and "We love you, Gabrielle."

A man authorities said was seen with Loughner just before the shooting began was cleared of any involvement on Sunday.

Pima County Sheriff's Deputy Jason Ogan told The Associated Press that the man was a cab driver who drove the suspect to the grocery store outside which the shooting occurred.

Authorities had been seeking the man because he appeared in security camera footage near the gunman.

Ogan said the cab driver went into the store because the suspect was making change to pay his fare.

FBI Director Robert Mueller told a news conference on Sunday that a suspect arrested at the scene of the shooting was in federal custody.

"As the investigation goes on there may well be additional charges that will be filed," Mueller said.

Police have said Loughner had a criminal record and a history of mental issues, but Mueller would not say what motivated the shooting rampage.

"The investigation is barely 24 hours old and it's a little bit early to speculate on those motives," he said.

Loughner allegedly purchased the Glock pistol used in the attack at a Tucson gun store in November.

Court document obtained by The Associated Press show that Loughner had previous contact with Giffords, and a letter addressed to him on Giffords' congressional stationary thanked him for attended an event at a Tucson mall in 2007.

Heather Williams, an assistant federal public defender in Arizona, said the suspect does not yet have a lawyer but that her office was working to have one appointed. She said they are seeking an outside attorney because a judge was among those killed.

Witness says gunman ‘on a mission'

Witnesses described the gunman as a "shady individual" who showed up at the public gathering asking to speak to Giffords.

Event volunteer Alex Villec said the man was wearing a black cap and baggy pants and shirt. When told he would have to wait his turn, the man left but returned minutes later and rushed towards Giffords, raised a Glock nine-millimetre automatic handgun and began shooting.

"He was definitely on a mission," said Villec.

The gunman fired at the congresswoman from close range, hitting her in the head and then shot her district director, standing beside her.

"He was not more than three or four feet from the congresswoman and the district director," said Mark Kimble, a communications staffer for Giffords.

He then began firing indiscriminately at staff members and people standing in line to talk to the congresswoman.

Kimble described the scene as "just complete chaos, people screaming, crying."

Police said the shooting only stopped when the gunman was tackled and held down by two people in the crowd. There were reportedly still bullets remaining in the double magazine used by the shooter.

Pima County Sheriff Clarence Dubnik said the gunman emptied one 30-bullet magazine and was trying to reload when a woman and two men overpowered him.

Some of the scene's chaos was captured by emergency calls released by authorities, in which a person frantically described the mass shooting and says, "I do believe Gabby Giffords was hit."

Obama calls shooting ‘unspeakable act'

The shooting cast a pall over Washington as politicians of all stripes denounced the attack.

President Barack Obama ordered Mueller to take personal charge of the investigation, calling the shootings "a tragedy for our entire country."

"We don't yet know what provoked this unspeakable act," Obama told reporters.

Loughner had posted several nonsensical diatribes on social-media sites, complaining about illiteracy in Gifford's Congressional District 8, U.S. currency and "mind control and brainwash methods."

Loughner was described by a former classmate as a pot-smoking loner, and the U.S. Army said he tried to enlist in December 2008 but was rejected for undisclosed reasons.

Lynda Sorenson told the Arizona Daily Star that she took a math class with Loughner last summer at Pima Community College's Northwest campus and said he was "obviously very disturbed."

House Speaker John Boehner said in statement he was horrified by the attack and asked that flags on the House side of the Capitol be flown at half-staff in the wake of the shooting.

"An attack on one who serves is an attack on all who serve," Boehner said Sunday. "Such acts of violence have no place in our society."

Canada's Foreign Affairs Minister Lawrence Cannon expressed his grief and shock at the shooting and said in a statement that Canada stands by the United States, "our valued friend and neighbour," in its time of grief.

"On behalf of the government and all Canadians, I offer my condolences to the family and friends of federal judge John Roll and the other innocent victims of this senseless act of violence," Cannon said. "In particular, I offer sympathy to the family of the young girl among the victims, whose entire life was ahead of her."