You can put this in any Module file (those listed under the Modules section on the left project pane)
No worries, this stuff can be tough to get your head around, especially this event as it isn't documented.