...

View Full Version : Using the enter key as an event handler.



wolf1728
10-10-2008, 08:50 AM
I wrote the game located here:
http://www.1728.com/digit.htm
It would seem easier to play if instead of clicking the "ENTER" button with the mouse, you could just press the enter key on the keyboard while the cursor is still in the input box.
I have attempted to read quite a bit on this but have never been able to get the enter key to work as an event handler.
Any help wpould be appreciated.
Thank you.

rangana
10-10-2008, 09:44 AM
window.onload=function(){
document.onkeydown=function(event)
{
var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
if(keyCode==13)
{
var nd=(document.dropbox.digits.options[document.dropbox.digits.selectedIndex].value);
calc();
calc2();
}
}
}

wolf1728
10-11-2008, 10:33 PM
Rangana
I've tried placing that code in various parts of the program and have not been able to get it to work. So, I decided to make a new version of the file (with the changes) at www.1728.com/digitB.htm

I put the code within the <body> tag. I suppose that might be part of the problem.

rangana
10-12-2008, 04:33 AM
That's a script, so you must surround it with the script tags:


<script type="text/javascript">
window.onload=function(){
document.onkeydown=function(event)
{
var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
if(keyCode==13)
{
var nd=(document.dropbox.digits.options[document.dropbox.digits.selectedIndex].value);
calc();
calc2();
}
}
}
</script>


See if it helps.

wolf1728
10-12-2008, 06:54 AM
Thanks Rangana.
JavaScript is so frustrating. I've been programming with it for about 10 years and it is aggravating to see that a relatively simple command such as detecting the enter key being pressed requires such weird, arcane commands.

wolf1728
10-12-2008, 09:30 AM
Geez, I spoke too soon. Javascript has shown its extremely ugly face once again. I tested the program on Firefox and even Netscape Navigator and it worked fine. However, when I run this on Internet Explorer, I get an error message "keyCode is null or not an object". I don't know if I should put this in a new thread but I might as well wait for an answer to this posting.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum