Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript when was a key released

Tags:

javascript

I want to call a function, if a key was released, because I program a little Jump'n'Run and for that I have to know that, because if I use a "normal" handler which is called if a key is pressed, it don't works, because the handler is not called often enough.
I don't want to use jquery or something else, only native JavaScript.

Thank you in advance.


2 Answers

Checkout the snipper below. I think this is all you're looking for.

 document.addEventListener('keyup', (event) => {
  const keyName = event.key;
  alert('keyup event\n\n' + 'key: ' + keyName);
});
like image 168
bstory Avatar answered Nov 18 '25 20:11

bstory


MDN documentation for keyup event

document.getElementById('myField').addEventListener('keyup', e => {
  console.log(`keyup event fired for key: ${e.key}`);
});
Type here: <input id="myField" type="text" />
like image 23
Tom O. Avatar answered Nov 18 '25 19:11

Tom O.