PDA

View Full Version : Keys?



Nile
02-03-2008, 02:02 AM
Hey how would I make something like if I pressed the up kep it would make an alert and down key would make an imag.. etc.:confused::confused::confused::confused::confused:

BYK
02-03-2008, 09:13 AM
Look for the window.onkeydown, window.onkeyup events

Nile
02-03-2008, 01:45 PM
No but what I want is if you press the up, down, right, left keys to make an alert or something.

BYK
02-03-2008, 01:50 PM
Ok, I thought so and posted the above message. You should assign a function to the window.onkeyup or window.onkeydown events and then handle the key code inside that function and respond to the keycodes you want.

Nile
02-03-2008, 01:59 PM
How do I do that:confused::confused::confused:

BYK
02-03-2008, 04:25 PM
A simple example ;)


window.onkeydown=function(e)
{
var key = e ? e.which : window.event.keyCode;
switch(key)
{
case 27:
alert("You cannot escape from me!");
break;
case 13:
alert("You have pressed enter, right?");
break;
default:
alert("Pressed key's code is: " + key);
}
};

jackbenimble4
02-03-2008, 04:28 PM
Here's some resources:

w3cschools onkeyup event information (http://www.w3schools.com/jsref/jsref_onkeyup.asp)

Table of key codes (http://www.cambiaresearch.com/c4/702b8cd1-e5b0-42e6-83ac-25f0306e3e25/Javascript-Char-Codes-Key-Codes.aspx)

Nile
02-03-2008, 04:32 PM
BYK: Thanks man!