A man turned up at Buckingham Palace within hours of killing a stranger and demanded to see the Queen, the Old Bailey heard.
Ghodratollah Barani strangled rough sleeper Mark Morrison, 46, and spent the next week repeatedly trying to see the Queen,
He said he had been hearing voices telling him to kill someone so he could be king and that the Queen would help him.
Barani had been to the palace before the killing and was twice taken to hospital but doctors thought he was faking illness to further his asylum application.
After being turned away from the palace on 21 June last year, he went to Marble Arch in central London, and strangled Mr Morrison with a green cord in the middle of the night.
He was arrested on 29 June, after he was linked to the killing, when he spoke to police and soldiers in Horseguards Parade, the court heard.
The 27-year-old Afghan, who lived in Sheffield before arriving in London, pleaded guilty to manslaughter on the grounds of diminished responsibility.
He was ordered to be detained in a secure hospital without limit of time.
Former chef Mr Morrison, who was born in Dunblane, had been sleeping on a bench at Marble Arch since being made homeless a few months before.
Judge Christopher Moss criticised doctors who examined Barani before the killing.
He said: "I have no doubt from what I have read and heard that (the death) was entirely preventable."
The judge said he was satisfied Barani had been suffering from schizophrenia, although doctors had not diagnosed it.
Judge Moss told Barani: "Voices were telling you you had to kill someone in order to become king, that you had to kill in order to become king of England.
"After the killing, the voices told you to return to Buckingham Palace saying you will be king of England."
When he was stopped at the north gate of Buckingham Palace, he said he was the king of Afghanistan.
Barani had been taken to St Thomas' and Gordon hospitals after two previous attempts to enter the palace.
The judge continued: "You were taken to hospital and despite telling doctors you were trying to seek refuge there and wanted to see the Queen, you were not diagnosed with mental illness."
He said that "bizarrely" on another occasion, he was turned away from St Thomas' A&E department despite saying he had thoughts of killing someone in order to become king.
Zoe Johnson, prosecuting, said doctors believed his claims were linked to his asylum claim.