24

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:


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.

10
  • This might sound silly, but did you import jQuery in your HTML? Commented Feb 2, 2016 at 1:51
  • 11
    AFAIK triggering a keydown event will just make any keydown handlers process. You can't actually make the browser scroll, since scrolling is not done by a JavaScript keydown event. Why can't you directly scroll the document using scrollTop manipulation? Commented Feb 2, 2016 at 1:55
  • 2
    I agree with @Amadan, this just seems silly, and is a classic X/Y problem. You're trying to trigger a keypress on an arrow key, just to scroll the page down a bit, when you can do that with just $(window).scrollTop(100) etc. Commented Feb 2, 2016 at 2:08
  • 1
    What do you mean by "web file manager", and what do you mean by "extension"? Also, please read about XY problems and why they are a bad idea. If you want to ask about extending a web file manager (whatever that is), ask about that. Commented Feb 2, 2016 at 3:35
  • 2
    It won't answer your question directly, but it might shine some light in your search for a solution stackoverflow.com/a/13821309/1949694 Commented Feb 3, 2016 at 12:23

3 Answers 3

12
+50

As @rfsbsb pointed out from: Jquery script for simulated key press down not running keyboard shortcut

If you're trying to fire some browser or system wide keyboard shortcut then it's a dead end - it can't be done for security reasons. If it would be possible, you would have pages all over the Internet that would (for example) add themself to your bookmarks without even asking (by firing CTRL+B shortcut using Javascript).

Sign up to request clarification or add additional context in comments.

1 Comment

Arigato! this info helped
5

Using this answer, I managed to change the code a bit and I think I got what you are looking for?

here is my jsfiddle

Code:

jQuery(document).ready(function($) {
    $('body').keypress(function(e) {
        if(e.which == '40') 
            $('body').animate({scrollTop: '100px'});
    });
});
jQuery.fn.simulateKeyPress = function(character) {
    jQuery(this).trigger({
        type: 'keypress',
        which: character
    });
};

 setTimeout(function() {
    $('body').simulateKeyPress(40);
 }, 1000);

1 Comment

and what is that 40, could you put any minimal explanation?
3

Here is a sample starting from @Haring10's example:

https://jsfiddle.net/po33vfb4/4/

$('body').keydown(function(e) {
  e.preventDefault();
  e.stopImmediatePropagation();

  if(e.which == 40) {
    window.scrollBy(0, -100);
  } else {
    window.scrollBy(0, 100);
  }
});

Triggering keydown-keyup-keypress programatically does not seem to have the efect of scrolling. The above sample can be customized to add animations.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.