The prosecution claims Muhammad Khan wanted to look in the coffin to "check for himself" the Queen was there.
Mr Khan, 28, also allegedly planned to go to royal residences to "try to make contact" with the monarch.
He was granted bail on condition he remains in a mental health hospital until his next court hearing.
Doctors assessed Mr Khan as not fit to take part in proceedings at Westminster Magistrates' Court, where he appeared charged with two offences under the Public Order Act.
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."
Prosecution lawyer Luke Staton, told the court Mr Khan had reached Westminster Hall when he was seen by officers 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," Mr Staton said.
The court heard Mr Khan was quickly detained, arrested and interviewed by police.
"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," said Mr Staton.
"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 Mr Khan said if he was unsuccessful that he "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."
Mr Khan was among about 250,000 people who filed through the hall between 17:00 Wednesday and early on Monday morning to pay their respects.