I'm trying to find a way to simulate a keypress.
For example, when function launched, the key "Arrow Down" should be pressed and so the webpage should be slightly scrolled.
I'm interested only in Chrome, and both jQuery or plain JS will be appropriate. (Plain JS will be more preferable).
That's one of the code examples I tried:
var e = $.Event("keydown", { keyCode: 40}); // 40 = down arrow
$("body").trigger(e);
// When I launch it the console, nothing happens. The page is not scrolled.
// May be I missed some obvious?
I searched and found the following related questions, but the solutions did not work for me:
- Definitive way to trigger keypress events with jQuery
- Firing a Keyboard Event in JavaScript
- How to trigger event in JavaScript?
- Simulate left and right arrow key event with javascript
- Simulate Keypress With jQuery
- Simulating a Keypress Event from Javascript Console
- Simulate JavaScript Key Events
In other words
Using AutoHotkey, you can easily make something like:
Down::
Send, {Up}
Then, if you press Down
arrow key, it will be triggered Up
. I just want to implement it with JS.