click button or keydown!
const result = document.getElementById('result')
const left = document.getElementById('left')
const right = document.getElementById('right')
left.addEventListener('click', () => result.textContent = 'left')
right.addEventListener('click', () => result.textContent = 'right')
document.addEventListener('keydown', onKeyDown)
function onKeyDown(e) {
const code = {
'ArrowLeft': () => result.textContent = 'left',
'ArrowRight': () => result.textContent = 'right',
'Escape': () => result.textContent = 'Escape',
}
code[e.code]()
}
<p id="result">click button!</p> <button id="left">left</button> <button id="right">right</button>