Combine key event listeners into one function