Click to See Complete Forum and Search --> : keybord


Justin
04-29-2003, 05:47 PM
how do you make javascript react to keyboard strikes, like if you type in something of press something it would activate a function or something?

khalidali63
04-29-2003, 05:52 PM
To do that you need to capture certain events,like
onkeyup,
this event is triggered when a key goes back up after being pressed.
you can catch this event..in this manner


<script type="text/javascript">

function keyUP(e){
alert("keyUp ewvent is triggered");
}

if(!document.all){
document.captureEvents(Event.KEYUP);
}
document.onkeyup = keyUP;
<script>

put the above code in between the <head> tags and open the html page in a browser(IE or NS6+)

Justin
04-29-2003, 05:56 PM
can you give me an example like if h was pressed then
Alert("h was pressed")

sorry, thanks

khalidali63
04-29-2003, 06:08 PM
Here you go..


<script type="text/javascript">

function keyUP(e){
var trigger = (document.all)?event:e;
keyValue = String.fromCharCode(trigger.keyCode);
alert(keyValue+" was pressed")
}

if(!document.all){
document.captureEvents(Event.KEYUP);
}
document.onkeyup = keyUP;
</script>


I have tested the above with NS6+ and IE6+

Justin
04-30-2003, 06:48 PM
it now works, it was the
<script type="text/javascript">
that was throwing it off i replaved with
<script language="javascript">
and it worked