PDA

View Full Version : Firing a Keyboard Event


demeyer2
04-07-2010, 11:55 PM
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 <a> 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. Please let me know if I can provide further details or explanation. I'm sort of a JS newb but all the research I've gone into always takes me to DOM so I figured this would be the best place to post this problem.

Dormilich
04-08-2010, 08:39 AM
you can of course create and fire any event you like yourself, but I’m you want that … using the event that naturally occurs when a user hits a key is far easier.

demeyer2
04-08-2010, 05:40 PM
Thanks for the response. So are you saying there is a way to naturally create the keyboard event without having to fire the event at the window?

Dormilich
04-08-2010, 06:51 PM
sure. press a key.

demeyer2
04-08-2010, 07:33 PM
sure. press a key.
Hah thanks, but my users don't know that pressing the right arrow key will slide the content slider and having a graphic that tells them to do so is a bit much. My idea was to have a graphic saying 'Click here to ....' and when they clicked it I would use the onclick() function to fire the right arrow keypress event (which I can't figure out how to do).

If it helps, here is the website with the content slider I'm talking about - http://greenplanet3d.com

Dormilich
04-08-2010, 09:05 PM
then simply assign the right arrow handler (that from the keypress event) also to the click event.

rnd me
04-08-2010, 09:44 PM
then simply assign the right arrow handler (that from the keypress event) also to the click event.

he can't: it's buried in a jQuery plugin, most likely assigned using $().click()...

here is a firefox tool that will help, not sure about other browsers:

function simKey() {
var A=[].slice.call(arguments),
a1=A[0]||"",
base= document.body;
if(a1.nodeName){ base=a1;A.splice(0,1);a1=A[0]}
if(a1 && a1.split && a1.length>1){A=a1.split("");}
if(a1.splice){ A=a1; }
A.map(function(key){
var ev = document.createEvent("KeyEvents");
ev.initKeyEvent("keypress",
true,true,window,false,false,false,false,key.toFixed?key:key.charCodeAt(0),key.toFixed?key:key.charC odeAt(0));
base.dispatchEvent(ev);
});
}



use: simKey(element, key(s) );
right arrow: simKey(document.body,39);

demeyer2
04-08-2010, 09:52 PM
then simply assign the right arrow handler (that from the keypress event) also to the click event.

Thanks, this is what I've been trying to do but I can't figure out the coding (I'm a JS newb of sorts).
Here's what I've been trying

onclick="event.keyCode=39;"
and
onclick="KeyEvent.DOM_VK_RIGHT;"
and
onclick="handleKeyDown(39);"
and
onclick="handleKeyDown(KeyEvent.DOM_VK_RIGHT);"


Any suggestions would be awesome.

demeyer2
04-08-2010, 10:20 PM
Thanks for the in depth look rnd me. The code you provided looks sound but something must be interfering with it because I implemented the code with the usage you listed and it didn't work in Firefox. Back to the drawing board for me but thanks again!

he can't: it's buried in a jQuery plugin, most likely assigned using $().click()...

here is a firefox tool that will help, not sure about other browsers: