Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting if Home/End Keys is pressed in JS

I am trying to implement an infinite scroll in JS, I am using $(window).scroll() to detect the scroll position, every thing is working fine, I just have one issue I want the url to change if the user clicks the 'Home' key and then change again if the user press the 'End' key.

So when Home is clicked the scroll position will be move to the top and when End is clicked the scroll position will be moved to the bottom.

My Question is, how do you detect these behaviors (pressing Home/End)?

Youssef

like image 843
Youssef Avatar asked Dec 18 '25 18:12

Youssef


1 Answers

KeyCode is 35 for END 36 for HOME!

  function myKeyPress(e){

        var keynum;

        if(window.event){ // IE                 
            keynum = e.keyCode;
        }else
            if(e.which){ // Netscape/Firefox/Opera                  
                keynum = e.which;
             }
        alert(String.fromCharCode(keynum));
    }
like image 143
Joakim M Avatar answered Dec 20 '25 09:12

Joakim M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!