Forum Moderators: open

Message Too Old, No Replies

Use arrow keys to forward and go back

How do I setup keys to a weblink?

         

Mike

7:21 pm on Sep 26, 2009 (gmt 0)

10+ Year Member



I have a photo hosting site and I want to be able to use my keyboard to view the photos. Just hit the right arrow and I will see the next photo in the gallery; left button will bring me back.

I know there is a way to do this, but I have no idea how. Any help?

Miamicarpetpro

12:12 am on Sep 28, 2009 (gmt 0)

10+ Year Member



Hi Mike,
You will need to implement some user32 Api calls.

Fotiman

12:48 am on Sep 28, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome both of you to WebmasterWorld!
@Miamicarpetpro, huh? I've never heard of such a thing.

@Mike
You need to implement some sort of key listener. The Yahoo UI Library [developer.yahoo.com] has a KeyListener [developer.yahoo.com] class that you could use to capture the key event, and then call whatever function is appropriate. Hope that helps.

astupidname

1:08 pm on Sep 28, 2009 (gmt 0)

10+ Year Member



You wouldn't necessarily need to implement an entire YUI Library just for this, it's pretty straight forward to make your own key listener for specific (or any) key codes, such as:
<script type="text/javascript">

document.onkeydown = function (event) {
var key_code = (event && event.which) ? event.which : window.event.keyCode;
//keys we are looking for, and their corresponding key codes:
// < = 188, > = 190, left arrow = 37, right arrow = 39,
//number pad (4) left arrow = 100, number pad (6) right arrow = 102
var codesToWatchFor = {
"188":"previous",
"190":"next",
"37":"previous",
"39":"next",
"100":"previous",
"102":"next",
"previous":function () { alert("A left arrow key has been pressed"); },
"next":function () { alert("A right arrow key has been pressed"); }
};
if (key_code in codesToWatchFor) { //one of the desired keys listed in comment above has been pressed
var action = codesToWatchFor[key_code];
codesToWatchFor[action](); //invoke either of our codesToWatchFor.previous or codesToWatchFor.next functions
}
};

</script>