I am trying to check if a key is pressed without focusing on any field.
The goal is to allow users to press the left and right arrows to move to the next image. There is no need for them to click on any text field or anything else ... just press these keys to go to the next or last image.
how
function keyEvent(e) { if(e.keyCode == 39) { run some code to get next image } else if (e.keyCode == 37) { run some code to get last image } }
It seems that jquery always needs a “selector”, as if I need to bind it to a field or something like that.
$('input[type=text]').on('keyup', function(e) { if (e.which == 39) { run some code } });
Any ideas?
EDIT:
I have this script in my body viewimage.php ... javascript still doesn't work when the page loads:
<script type="text/javascript"> $(document).ready(function() { $(document).keydown(function (e) { if (e.which == 39) { alert("39"); } }); }); </script>
thanks
source share