...

View Full Version : Key Event Preferes....



Nikon
05-10-2006, 06:22 PM
Hi you all:)

Im started to program JS 2 weeks ago and here is a code that asposed to be simple:


<html>
<head>
<script language="javascript">
<!--

/*B*/
function onKeyDownH(e)
{
var output_string;


switch (emod) {
case "IE4+":
e = window.event;
/*C*/
alt = (e.altKey) ? true : false;
ctrl = (e.ctrlKey) ? true : false;
shift = (e.shiftKey) ? true : false;

output_string = " alt: "+alt+"\n ctrl: "+ctrl+"\n shift: "+shift;

/*D*/
if ((e.keyCode<16)||(e.keyCode>18)) {
output_string += "\n 'keyCode' attribute: "+e.keyCode;
document.forms[0].elements[0].value = output_string;
alt = false;
ctrl = false;
shift = false; }
break;
case "NN4":
/*E*/
alt = ((e.modifiers & Event.ALT_MASK) == Event.ALT_MASK);
ctrl = ((e.modifiers & Event.CTRL_MASK) == Event.CTRL_MASK);
shift = ((e.modifiers & Event.SHIFT_MASK) == Event.SHIFT_MASK);

output_string = " alt: "+alt+"\n ctrl: "+ctrl+"\n shift: "+shift;

/*F*/
if ((e.which)&&(e.which!=0))
{
output_string += "\n 'which' attribute: "+e.which;
document.forms[0].elements[0].value = output_string;
}
break;
case "W3C":
alt = (e.altKey) ? true : false;
ctrl = (e.ctrlKey) ? true : false;
shift = (e.shiftKey) ? true : false;

output_string = " alt: "+alt+"\n ctrl: "+ctrl+"\n shift: "+shift;

if ((e.which<16)||(e.which>18))
{
output_string += "\n 'which' attribute: "+e.which;
document.forms[0].elements[0].value = output_string;
}
break; }

/*G*/
return false;
}

function onloadH(e)
{
/*get the event model*/
emod = (e) ? (e.eventPhase) ? "W3C" : "NN4" : (window.event) ? "IE4+" : "unknown";

/*A*/
if (emod == "NN4") {
document.captureEvents(Event.KEYDOWN); }

document.onkeydown = onKeyDownH;
return true;
}

/*define the event handler for the onload event*/
window.onload = onloadH;

/*global vars*/
var emod; /*the event model*/
var alt = false;
var ctrl = false;
var shift = false;

//-->
</script>

</head>

<body>
<form>
<textarea rows=14 cols=40 readonly></textarea>
</form>
<input type=text >
</body>

</html>



The problem is that when I try to write at the input its not allow me right
and presents on the TEXTAREA the predictable output.

How can I have both events:
1 - Writing in the input
2 - The Key event

thanks to the helpers :P
Ofir.

Kor
05-10-2006, 08:39 PM
you may use onfocus and onblur to change a Boolean. Here's a simplified example of preventing event capture while focusing on a text field control:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Captura key code</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
var b=true;// a Boolean
function capturekey(e){
if(b){
var key=(typeof event!='undefined')?window.event.keyCode:e.keyCode;
alert('keycode : '+key);
}
}
if(navigator.appName!= "Mozilla"){document.onkeyup=capturekey}
else{document.addEventListener("keypress",capturekey,true);}
</script>
</head>
<body>
<input type="text" onfocus="b=false" onblur="b=true">
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum