WINNENDEN, Germany - The 17-year-old gunman who went on a rampage at his former school and killed 15 people before taking his own life gave a warning in an Internet chatroom only hours earlier and said he was "sick of this life," officials said Thursday.

The suspect, whose name appears as Tim Kretschmer in police documents, told others in the chat room he planned to attack his school in Winnenden, said Baden Wuerttemburg state Interior Minister Heribert Rech.

Rech said the suspect wrote, "You will hear from me tomorrow, remember the name of a place called Winnenden."

In the first indication of a motive in the shooting, Rech said the teenager told others in the German-language chat room that: "Everyone laughs at me, nobody recognizes my potential."

"I'm serious. ... I have a weapon here," Rech said the youth wrote. "Early tomorrow morning I will go to my former school."

Rech said the chat had occurred the night before the attack, but a police official, Erwin Hetger, later said it was in the early morning Wednesday, about six hours before the 9:30 a.m. shooting.

The youth ended the chat saying, "No reports to the police now, don't worry, I'm just baiting you."

A Bavarian man told police about the chat after the school shooting in Winnenden had taken place, Rech said. He told authorities his 17-year-old son only told him about it after seeing the news reports and had not taken the threat seriously, replying to the message: LOL -- shortform for "laughing out loud."

Investigator Siegfried Mahler said authorities had learned that the suspect was treated for depression in 2008, with five visits to a psychiatrist between April and September at an area clinic. He was supposed to continue the treatment at another clinic, but apparently did not show up, Mahler said.

Despite the high death toll, the shooting could have been worse if the principal of the high school had not been able to warn teachers with a prearranged code over the public address system when the suspect burst into the school.

After the suspect entered the school in Winnenden on Wednesday morning and opened fire, the principal put the emergency plan in effect, quickly broadcasting a coded message to teachers: "Frau Koma is coming," students told German media.

"Then our teacher closed the door and said we should close the windows and sit on the floor," a student, identified only as Kim S., told ZDF television.

In German the word "amoklauf" is used to describe school shootings, and "koma" is the reverse of the word "amok." Hetger said the coded alert was worked out by German educators after a deadly school shooting in Erfurt in 2002 as a way to warn teachers.

Hetger said that police in Baden-Wuerttemburg had received special training that involved sending small teams into the building in the event of a school shooting, as happened on Wednesday.

He credited this, coupled with the warning from the principal, for preventing further deaths.

Although the gunman shot at the officers as they entered the building, they succeeded in chasing him from the premises.

After he escaped, he hijacked a car and was eventually caught in a police shootout. The rampage ended with 15 victims slain and the assailant taking his own life, authorities said.

His victims were primarily female: eight of nine students killed were girls, and all three teachers were women. Three men were killed later by the suspect as he fled.

The dark-haired teen, shown wearing glasses in pictures on German television, apparently took the weapon from his father's collection of 15 firearms along with a "multitude of ammunition," police said. His father, a businessman, was a member of the local gun club and kept the weapons locked away except for the pistol, which was kept in the bedroom.

The high school was closed Thursday, still cordoned off by red and white police tape as investigators pored through the building. Scores of candles lit by mourners adorned the grounds amid bunches of flowers and notes with messages and questions like "Why?"

A man carried a sign saying, "God: Where were you?"

The government ordered all federal buildings to fly their flags at half staff, and schools across the country held moments of silence for the victims. Germany's national soccer league, the Bundesliga, said players would wear black armbands in upcoming games.

Injured student Patrick S., 15, was quoted by Bild newspaper as saying Kretschmer burst into his German class at about 9:30 a.m.

"We flipped over the desks to duck behind for cover. ... Suddenly I saw that I was hit -- in the back, in the arm and in the cheek," he was quoted as saying. "Suddenly he was gone and we barricaded the door. And then I saw my classmate Chantal. She sat at the door. Dead."

Local police spokesman Nik Brenner said that authorities had found 60 shell casings in the school.