Re-wrote key-event function as more general event function