Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Aug 2008
    Location
    Boston, MA
    Posts
    24
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Using the enter key as an event handler.

    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.

  • #2
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Code:
    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();
    		}
    	}
    }
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #3
    New Coder
    Join Date
    Aug 2008
    Location
    Boston, MA
    Posts
    24
    Thanks
    9
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    That's a script, so you must surround it with the script tags:
    Code:
    <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.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • Users who have thanked rangana for this post:

    wolf1728 (10-12-2008)

  • #5
    New Coder
    Join Date
    Aug 2008
    Location
    Boston, MA
    Posts
    24
    Thanks
    9
    Thanked 0 Times in 0 Posts
    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.

  • #6
    New Coder
    Join Date
    Aug 2008
    Location
    Boston, MA
    Posts
    24
    Thanks
    9
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •