2

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

Youssef
  • 190
  • 1
  • 3
  • 13
  • Nice. I came here at the same point in my code/testing for my infinite scroll implementation. Is the keypress event the solution that you stuck with? – Brian Layman Sep 12 '16 at 03:17

4 Answers4

9

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));
    }
Joakim M
  • 1,793
  • 2
  • 14
  • 29
5

Home, PageUp, PageDown, End and some other keys are only triggered by onkeydown, not onkeypress see this post

Community
  • 1
  • 1
Alex Joe
  • 369
  • 3
  • 8
2

its simple to detect please look at following code :

<input type="text" id="txtKey" onkeyup="keyPressEvent(event)" />

   function keyPressEvent(e) {


        var code = (e.keyCode ? e.keyCode : e.which);
        if (code == 35) {
            alert('end key press');
        }
        if (code == 36) {
            alert('home key press');
        }
        return false;
    }
Abhishek Sharma
  • 281
  • 2
  • 6
2

In an onkeydown callback, check for e.key === 'Home' and e.key === 'End'

https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values

Scott Wager
  • 758
  • 11
  • 9