Moved document event listener code for handling key presses into separate function handleDocumentKeyPresses().