5

I'm trying to click programmatically arrow keys using javascript or jquery. I did not find a proper solution for that. Guys any ideas ? any examples ?

Dalen
  • 8,856
  • 4
  • 47
  • 52
Eranga Perera
  • 908
  • 1
  • 11
  • 21
  • 3
    possible duplicate of [Trigger a keypress/keydown/keyup event in JS/jQuery?](http://stackoverflow.com/questions/3368578/trigger-a-keypress-keydown-keyup-event-in-js-jquery) – Bas van Dijk Dec 12 '13 at 08:35
  • 1
    http://stackoverflow.com/questions/832059/definitive-way-to-trigger-keypress-events-with-jquery – TMan Dec 12 '13 at 08:36
  • What is it you're *really* trying to do by "clicking" the keys? It's likely there's a better way than trying to simulate an event. – T.J. Crowder Dec 12 '13 at 08:41
  • i have a flash file.i load it inside html .it has few animations which are response to arrow keys. my intension is to programatically click arrow keys (according to logic) and animate . i don't have flash project so i can't change actionscript thats why im tring to do this. – Eranga Perera Dec 12 '13 at 09:40
  • @ErangaLakmalPerera You must use the allowScriptAccess="always" attribute to the object tag and use the script I've provided in my answer. – Rakesh Gopal Dec 12 '13 at 09:52

2 Answers2

2

I don't know how to trigger the events on the entire browser. But this is how you can trigger for individual elements on your page.

<body>
    <script>
       leftArrowKey = 37
       upArrowKey = 38
       rightArrowKey = 39
       downArrowKey = 40

       e = jQuery.Event("keydown");
       e.which = leftArrowKey;
       $("myInput").trigger(e);
    </script>

    <object allowScriptAccess="always" id="myInput" ... >
        ...
    </object>
</body>

To send this event to a flash animation, add allowScriptAccess="always" to the Object tag you are using.

Rakesh Gopal
  • 580
  • 5
  • 11
  • Sorry I didn't know that you were using this on a flash animation. I don't think this will work with flash. This was only meant for Html elements. – Rakesh Gopal Dec 12 '13 at 09:45
  • 1
    @ErangaLakmalPerera Please try out the new code I've edited my original code, to suit your scenario. – Rakesh Gopal Dec 12 '13 at 09:51
0

you can trigger click events by doing

$(selector).trigger('click');

Is that what you're looking for? http://api.jquery.com/trigger/

Lenny
  • 5,663
  • 2
  • 19
  • 27
  • 1
    The title may be confusing, but he's aking about keypresses. You might get some negative votes, even though it's an incorrectly asked question. – Dropout Dec 12 '13 at 08:37