Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

KeyPress Event

Status
Not open for further replies.

stevejs

Programmer
May 9, 2000
6
0
0
EU
Hi, I am trying to write a KeyPress event that prevents certain keys from being accepted. The code I have works within Netscape but doesn't within IE.<br><br>&nbsp;&nbsp;&nbsp;&nbsp;function keyPress(evnt) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return (String.fromCharCode(evnt.which)!='¦')<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&lt;INPUT type=&quot;text&quot;..onKeyPress=&quot;keyPress(event)&quot;&gt;<br><br>The problem is that the &quot;event&quot; variable does not seem to set the which &quot;value&quot; in IE, but does in Netscape?? I have checked it and it says it is undefined, but the type is ok?<br><br>Anyone got any ideas??<br><br>&nbsp;
 
can you prevent them from typing the letters altogether, or just in that input box
 
Well, using Netscape you could use the procedure within the form or any other control with access to an OnKeyPress event to prevent the key from being pressed, but it won't work at all in Internet Explorer.<br><br>I tried to check the value of the text box on the key press, but of course the value hasn't been updated and won't be until the event finishes.
 
IE uses a different event model than Netscape.&nbsp;&nbsp;The Netscape handler passes the event object as a parameter, and uses the 'which' property.<br><br>In IE, you need to instantiate the event object yourself, and use the keyCode property:<br><br><br>&nbsp;&nbsp;&nbsp;&nbsp;function keyPress(evnt) { <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (document.layers)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;theKey = evnt.which;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else (if document.all)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;theKey = event.keyCode;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return (String.fromCharCode(theKey)!='¦')<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&lt;INPUT type=&quot;text&quot;..onKeyPress=&quot;keyPress(event)&quot;&gt;<br><br><br> <p>nick bulka<br><a href=mailto:nick@bulka.com>nick@bulka.com</a><br><a href= > </a><br>
 
Oops. Just saw a typo in my code:<br><br>else (if document.all)<br><br>should be<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>else if (document.all)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>sorry about that<br> <p>nick bulka<br><a href=mailto:nick@bulka.com>nick@bulka.com</a><br><a href= > </a><br>
 
Code:
<html>
<head>
	<title>Untitled</title>
<script>

function keyPress(evnt) { 
       if (document.layers)
          theKey = evnt.which;
       else if (document.all)
          theKey = event.keyCode;
     
       return (String.fromCharCode(theKey)!='a')
    
    }

</script>
</head>
<body>

    <INPUT type=&quot;text&quot; size=&quot;20&quot; maxlength=&quot;20&quot; onKeyPress=&quot;keyPress(event)&quot;>

</body>
</html>
I do not understand how to use this code to disallow a key press. Is it possible for an alert to be generated when a particular key is pressed. Apologies but could somebody explain that please.

Thanks

Tim [sig][/sig]
 
Tim,
Essentially, if you return false from an event handler, the event will not fire. That's how we're preventing keystrokes. If you want to generate an alert first, change the code at the end to something like this:

if (String.fromCharCode(theKey) == 'a'){
alert ('you pressed a');
return false;
} [sig]<p>nick bulka<br><a href=mailto: > </a><br>[/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top