The initial cause for Hamlet's gloom and despair is the murder of his father, Old King Hamlet. Hamlet is further enraged by his mother's quick marriage to his Uncle Claudius (Old King Hamlet's brother). Hamlet becomes melancholy and has trouble trusting anyone, especially women (because he is so upset with his mother's marriage to Claudius so soon after her husband's death).
But Hamlet's insanity or madness is a bit different. It is not solely caused by external events (as his melancholy and distrust are caused by his father's death and his mother's marriage). Hamlet's madness is a combination of his reactions to these things around him but it is also a conscious "acting" - meaning that he is acting mad for a purpose. Polonius points out Hamlet's insane behavior in Act II, Scene 2:
I will be brief. Your noble son is mad.
Mad call I it; for, to define true madness,
What is't but to be nothing else but mad?
But let that go. (II.ii.98-101)
Later in this scene, Polonius remarks that there must be some reason or method behind Hamlet's madness. "Though this be madness, yet there is method / in't." (II.ii.216-17)
In the same scene, Hamlet tells Rosencrantz and Guildenstern that he knows they were sent to figure out why he is acting so strange. Whether primarily a reaction to his father's murder and his mother's hasty marriage or a part of his voluntary isolation in pursuit of his plan for revenge, Hamlet is well aware of his mad behavior. This suggests that at least part (or the majority) of his madness is an act, one to confuse the king and thus distract the king (and anyone else) from discovering his (Hamlet's) plans for revenge.
Hamlet essentially admits to Rosencrantz and Guildenstern that his madness is faked in this scene as well:
I am but mad north-north-west. When the wind is
southerly I know a hawk from a handsaw. (II.ii.376-77)
It has never been agreed by critics that Hamlet is really mad. When he acts the most madly he is obviously just acting. This is right after he has killed Polonius and he runs away from Rosencrantz and Guildenstern pretending to think they are all, including Polonius, playing a game similar to Hide and Seek. It would appear to me that Hamlet decides to pretend to be mad right after his meeting with his father's ghost and that the idea occurs to him because he thinks it might be a good way to prevent Claudius and such people as Rosencrantz and Guildenstern from spying on him effectively. How can anybody know what Hamlet is really thinking or planning if he is mad? He gives them plenty to talk and wonder about, but he manages to conceal what they would really like to know.
Hamlet feels like a prisoner who is under close surveillance even before he meets his father's ghost. After that traumatic meeting he feels it would be difficult to conceal his new knowledge from all the people he knows to be spying on him, as well as from others he hasn't even identified. Claudius is intent on finding out what is going on inside his stepson's mind. The King suspects that Hamlet must be planning a coup because people tend to judge others by themselves, and Claudius gained the throne through treachery and murder. Claudius is a worthy opponent for the highly intelligent Hamlet. The play is largely a battle of wits between these two equally matched men. Claudius explains that he does not want to take strong action against his stepson for two reasons: (1) the people love him and might revolt; (2) Gertrude loves her son and he loves Gertrude so much that he wouldn't want to cause her grief. But if Claudius found any evidence that Hamlet was involved in any kind of plot against him, he would act ruthlessly, as he has shown himself to be thoroughly capable of doing. These two men hate each other and they also fear each other. Hamlet realizes that his life is in danger once his father has told him:
'tis given out that, sleeping in mine orchard,
A serpent stung me. So the whole ear of Denmark
Is by a forged process of my death
Rankly abused. But know, thou noble youth,
The serpent that did sting thy father's life
Now wears his crown.
Hamlet probably doesn't think he can continue to act in the same natural way now that he has this dangerous knowledge. How can he look the cunning Claudius in the eyes without betraying that he knows something he shouldn't? Claudius is his uncle, he has known Hamlet since he was an infant. Claudius is not only watching him but has set others to watch him and report back to him, including Hamlet's own mother. The King has even sent for Rosencrantz and Guildenstern, pretending to be concerned about his stepson's melancholia.
There are a number of scenes in which Hamlet is acting madly--but where is there a scene in which he is obviously not "acting"?