DEPRECATED: Using keypress To Detect Keyboard Events In JavaScript

December 2020

DEPRECATED

The code on this page uses keypress events. Those have been deprecated in favor of keydown and keyup.

You should use those instead.

See: Capture Keyboard Events On A Web Page With JavaScript

Old Notes

This is the original approach I used to detect keyboard events on web pages:

HTML

<input type="text" id="input-example" value="" />

Output

JavaScript
const inputEl = document.querySelector('#input-example')

inputEl.addEventListener('keypress', (event) => {
    console.log(`You like ${event.key}`)
})

Check the console for the output.

end of line