Generalized event matching code.