views:

75

answers:

3

I've done a lot of research on this but have come up empty handed. What I would like to do is invoke or create an event for the right arrow key (Key Code 39). I was planning on using an onclick event inside an tag to go to a function where this event could be 'fired'. Any help would be appreciated.

If anyone needs context, I have a jQuery content slider that's functionality is very hard to alter and currently the functionality I need to emulate only occurs when a user hits the right arrow on their keyboard. What I would like to do is tie an onclick() event to a image that when clicked on fires the right keyboard event and the content will slide. Please let me know if I can provide further details or explanation.

My working demo for this is: http://greenplanet3d.com

A: 

I'm going to go with "it's not possible to mimic keyboard input". It would open a gaping security hole anyways.

Fix your script instead of going for an ugly kludge. That slider thing looks so simple that it's not going to take a lot of time to rewrite it from scratch anyways, if its functionality is really "very hard to alter".

Matti Virkkunen
I believe this is probably the best idea, time to learn some jQuery...Thanks for all the responses everyone!
A: 

I'm not sure if you meant this, but you can bind 'keydown' event on document like this:

$(document).bind('keydown', function fn(event) 
{ 
    if (event.keyCode == 39)
    {
        alert('event');
    }
});
Vertigo
A: 

I think you're going about this the wrong way. Instead of tieing the action to the keyboard event, I would write a function to handle the transition code.

This way both the keyboard event and the click event can call the same function.

If you absolutely need to invoke the event, you can do this with jquery's trigger.

var keydown_event = jQuery.Event("keydown");
keydown_event.which = 39;
$('#my-image').trigger(keydown_event);
digitaldreamer