A man turned up at Buckingham Palace within hours of killing a stranger and demanded to see the Queen, the Old Bailey has 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 June 21 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 June 29, 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."