We can find out which character key is pressed with the e.which
property and the String.fromCharCode
method.
For instance, if we have the following input:
<input type="text" />
Then we can listen to the keypress event and check which key is pressed by writing:
const input = document.querySelector('input')
input.addEventListener('keypress', (e) => {
console.log(String.fromCharCode(e.which));
})
We call String.fromCharCode
with e.which
in the keypress event handler callback to get the character of the key that’s pressed.