/    Sign up×
Community /Pin to ProfileBookmark

arrow key functions

EXAMPLE

[code]
left:
http://www.google.com

right:
http://www.yahoo.com
[/code]

when the user presses the right arrow key he should be redirected to the yahoo webpage, same goes for the left arrow key going to the google webpage.

does anyone know how to do this?;P

EXAMPLE

[code]
<script type=”text/javascript”>
var right_url=’http://www.yahoo.com’;
var left_url=’http://www.google.com’;

function right_arrowkey()
{
var url=right_url;

//function
}

function left_arrowkey()
{
var url=left_url;

//function
}
</script>
[/code]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@toicontienJan 02.2009 — You need to assign an "onkeypress" event handler to the some HTML tag that is editable, meaning you can type in it. Standards browsers will pass the event handler function an event object, and from that you can check the keyCode property of the event object. Internet Explorer uses a global event object called window.event.
Copy linkTweet thisAlerts:
@KryptonianJan 02.2009 — I could be wrong, but I didn't think "onkeypress" generated a keyCode value. I think you'll want something along the lines of...
[code=html]onkeyup="if(event.keyCode == ) right_arrowkey(); if(event.keyCode == [code]) left_arrowkey();"

That's the key portion. There may be more than one way to redirect the browser, but one way is to manipulate the window.location object.

That said, I don't know why you'd want to do it. :p A well placed link would almost certainly be more straight-forward.
Copy linkTweet thisAlerts:
@toicontienJan 02.2009 — onkeydown, onkeypress and onkeyup all generate keyCode's. They are only triggered by the user pressing a key.
Copy linkTweet thisAlerts:
@KryptonianJan 02.2009 — Thanks for the correction. My tests show that keyCode has a value of zero, for alpha-numeric keys, in Firefox and Navigator (out of Firefox, Navigator, IE, Opera, and Safari) onkeypress. I guess that's where I got the idea that it didn't return a keyCode. onkeyup (and onkeydown) returns correct values in all browsers.

That is to say, arrow keys return a valid keyCode for onkeypress in all of the above browsers. My mistake.
×

Success!

Help @ko0kie spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...