Alert When Keyboard Arrow Key Is Pressed By Jquery

there is simple jquery code below. by this code you can  alert or trigger any event on a keyboard key. Must add jquery script.

<script   src="https://code.jquery.com/jquery-3.4.1.min.js"   integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="   crossorigin="anonymous"></script>

 

<script>

$(function () {

$(document).on('keyup keydown keypress', function (event) {

if (event.keyCode == 39) {

alert("right arrow is pressed");
}

else if (event.keyCode == 37) {
alert("left arrow is pressed");
}

else if (event.keyCode == 38) {
alert("up arrow is pressed");
}

else if (event.keyCode ==40) {
alert("down arrow is pressed");
}

});
});

</script>

 

 

admin
Leave a Reply

Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages