Back
I would like to know how can I use onkeydown and onkeyup events in javascript correctly with an event listener.
You can do something like this:
$(document).ready(function() { document.onkeydown = function(event) { if(event.keyCode=="38") console.log("Up key pressed"); else if(event.keyCode=="40") console.log("Down key pressed"); }; });<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
$(document).ready(function() {
document.onkeydown = function(event) {
if(event.keyCode=="38")
console.log("Up key pressed");
else if(event.keyCode=="40")
console.log("Down key pressed");
};
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Interested in full stack development? Check out the full stack developer course from Intellipaat.
31k questions
32.8k answers
501 comments
693 users