Refactor key event handlers