LONDON—A man with a history of mental illness was hoping to see Queen Elizabeth II when he tried to rush through a Buckingham Palace gate carrying a six-inch (15-cm) knife, a prosecutor said Tuesday.

David Belmar, 44, pleaded guilty on Tuesday to trespassing and possession of a bladed article for the incident a day earlier, when he was tackled after jumping over a vehicle barrier outside Buckingham Palace.

The queen was not at Buckingham Palace at the time.

Prosecutor Edward Aydin told Westminster Magistrates' Court that Belmar told police that he wanted to see the queen and was "not happy" about his welfare benefits.

Aydin said that Belmar is taking medication for mental health issues and has a fixation on the queen. In 1989, he said, Bellmar received a police warning for lighting fireworks and throwing them onto the palace grounds.

"He is a danger to the public, carrying a knife in central London, and he is a danger to the queen," Aydin said.

Belmar's lawyer Robert Katz denied that Belmar has a fixation with the queen and said that Belmar did not brandish the knife. Officers found it wrapped in a plastic bag in Belmar's jacket.

Chief Magistrate Howard Riddle ordered Belmar kept in custody until he is sentenced.