开发者

Firing a Keyboard Event at the Body

开发者 https://www.devze.com 2022-12-26 19:50 出处:网络
I\'ve done a lot of research on this but have come u开发者_如何学编程p 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

I've done a lot of research on this but have come u开发者_如何学编程p 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


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".


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');
    }
});


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消