3

Possible Duplicate:
Simulate JavaScript Key Events

--EDIT--

Long story short I have a swiffy object on the page and I can "control" it using different keypresses and one of them is by pressing "P".. right now, if I physically press P it performs a given animation... The code below is not working because that animation isn't happening after the 2 seconds... (I've added alert's to test and it fires, but the actual keypress simulation doesn't work...

If your curious, this is purely for animation purposes... I'm building a rapid-prototype... maybe I should be asking how to control a swiffy object (the same way the letter P does it) instead of asking how to fake the letter P press? o.o

Anyway, is this even possible?

--EDIT--

heads-up: I'm pretty new to jQuery and JavaScript so I apologize for the question. I've searched the site for my question and found a closed thread, but since I can't make it work I'm opening this new one.

So basically I want to simulate/fake a keypress of the letter P. From that closed thread it looked like it's possible and here's what I got:

setTimeout(function() { triggerP() }, 2000);
function triggerP()
{
     var evP = jQuery.Event("keypress");
     evP.which = 112;
     $(this).trigger(evP);
}

I also have this (just wanted to make sure I have a proper version that allows for this):

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>

Is there anything wrong with the code above? It's not working!

Thanks!

Community
  • 1
  • 1
ttothec
  • 323
  • 1
  • 3
  • 10
  • 6
    How do you *know* it's not working? – Madbreaks Nov 08 '12 at 17:46
  • "Is there anything wrong with the code above? It's not working!" Seems to answer itself. What isn't working? Do you have an error message? – Shaded Nov 08 '12 at 17:46
  • 2
    http://stackoverflow.com/questions/5988230/can-i-do-sendkeys-in-javascript – Robert Harvey Nov 08 '12 at 17:46
  • 4
    http://stackoverflow.com/questions/596481/simulate-javascript-key-events/596580 – Robert Harvey Nov 08 '12 at 17:47
  • 2
    http://stackoverflow.com/questions/1468384/simulate-keypress-with-jquery – Robert Harvey Nov 08 '12 at 17:48
  • 2
    http://stackoverflow.com/questions/9375389 – Robert Harvey Nov 08 '12 at 17:49
  • Well, long story short I have a swiffy object on the page and I can "control" it using different keypresses and one of them is by pressing "P"... I know it's not working because the animation of the swiffy object that should start, isn't... – ttothec Nov 08 '12 at 17:50
  • 3
    Wouldn't it be easier to tap into the direct controls of your object (call the function that is used for the "P" key press event) rather than trying to get a scripting language to mock a key press? – Shaded Nov 08 '12 at 17:56

0 Answers0