A man who appeared to grab the flag draped over the Queen's coffin planned to trespass at royal residences because he did not believe she was dead, a court has heard.
Muhammad Khan, 28, allegedly left the queue in Westminster Hall on Friday night while the Queen was lying in state as the live feed briefly cut away.
Khan was arrested and appeared at Westminster Magistrates' Court on Tuesday, charged with two counts under the Public Order Act.
Prosecutor Luke Staton said Khan had been among around 250,000 people who filed through the hall between 5pm on Wednesday until early Monday morning after queuing for hours to see the Queen's coffin.
Mr Staton said the defendant was seen by officers, who were present, to approach the coffin.
"He stepped off the carpet in the direction of the catafalque, then grabbed hold of the Royal Standard flag draped over the coffin with both of his hands," he added.
The court heard he was quickly detained, arrested and interviewed by police.
Mr Staton added: "The defendant did express the idea that the Queen is not dead and that he approached the coffin because he wanted to check for himself.
"He did say, prior to the state funeral, he was planning on going to the funeral.
"He planned to write to the royal family and if they did not reply he planned to go to Windsor Castle, Buckingham Palace and Balmoral to try to speak to the Queen."
The court heard Khan said if he was unsuccessful: "I would have to trespass in order to try and make contact."
And when asked how many times he would try, replied: "As long as I'm living."
District Judge Michael Snow did question Khan, who was not represented by a lawyer, after doctors had assessed him as not fit to take part in proceedings.
The court heard he was suffering from delusions and the judge told him: "At the time when you were in Westminster you didn't accept that the Queen was dead and that was the reason you were moving towards the coffin to satisfy yourself that she was."
He added: "He is delusional still and thinks the Queen is not dead, thinks King Charles has something to do with it and may go to Windsor Castle to pay his respects but also because he still thinks she is alive."
Khan spoke to confirm his name, date of birth and that he was staying at a friend's address in Wood Green, north London, during the hearing.
The judge granted him bail on condition he remains in an east London mental health hospital until his next appearance at the same court on 18 October.
Last night, the Queen was buried at the King George VI Memorial Chapel at Windsor Castle.