Add custom events support

Based on code by Dqubes and review by koggdal.
Merge pull request #87 from xrmx/customevents3

Add custom events support