0

Is it possible to create an auto key press event? I want to flip a book once automatically after refreshing the site?

Thanks in advance!

Destruktor
  • 463
  • 1
  • 4
  • 19
jkr137
  • 51
  • 1
  • 6

2 Answers2

5

This will do it:

$(document).ready(function() {
    var e = $.Event('keydown', { keyCode: 39 });// right arrow key
    $(document).trigger(e);
});

References:

Community
  • 1
  • 1
Ted Whitehead
  • 1,731
  • 10
  • 18
  • thank's for the fast reply! unfortunately it doesn't work... here is the link: [link](http://goo.gl/RsBTDh) Am I doing something wrong? – jkr137 Feb 12 '16 at 20:23
  • You probably just need to trigger the event on the specific element that’s listening for it. Will take a look at your site. – Ted Whitehead Feb 12 '16 at 20:30
  • @jkr137 Just updated my answer for your specific case. Worked for me in the browser console. – Ted Whitehead Feb 12 '16 at 20:46
  • wow! thanks for your fast answer! it works, but is it possible to put a delay on it? I tried something but it doesn't work :) `$(document).delay (1000).ready(function() { var e = $.Event('keydown', { keyCode: 39 });// right arrow key $(document).trigger(e);` – jkr137 Feb 12 '16 at 21:48
  • Glad that worked! To add a delay, you’ll need to use `window.setTimeout()`, see https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setTimeout – Ted Whitehead Feb 12 '16 at 22:09
0

This solved the problem:

$(window).load(function()  {
    var e = $.Event('keydown', { keyCode: 39 });// right arrow key
    setTimeout(function() {
    $(document).trigger(e);
             }, 1000);
});

Many thanks!

George Kagan
  • 5,913
  • 8
  • 46
  • 50
jkr137
  • 51
  • 1
  • 6