Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulate keypress event in Javascript or Jquery

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.

like image 832
Dheerendra Avatar asked Sep 24 '26 17:09

Dheerendra


1 Answers

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);
like image 150
Ben Grimm Avatar answered Sep 26 '26 06:09

Ben Grimm



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!