Add custom events support

Based on code by Dqubes and review by koggdal.