3

I want to simulate keypresses in Javascript. There are no listener for those key presses. For example: The function will trigger key '32' (spacebar) and this should result in scrolling of page like pressing normal spacebar.

I tried keypress trigger from Simulating a Keypress Event from Javascript Console but it didn't worked.

Edit
Key presses are not limited to spacebar only. It can be combined keys like alt+Ctrl+D and the browser should respond to them like keypresses from physical keyboard.

2 Answers 2

1

If you're only looking to scroll the document, you can approximate a Spacebar or Pg Dn scroll by using 85% of window.innerHeight - no jQuery needed:

window.scrollBy(0, window.innerHeight * .85);
Sign up to request clarification or add additional context in comments.

1 Comment

Scrolling was just an example to illustrate the point. The requirement is general.
0

By this way you can bind keypress event to body and identify which key is pressed.

jQuery('body').on('keyup',function(e){ 
     if(e.which == 32) {
          // spacebar pressed, your scroll code here.
     }
}) ; 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.