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 5 of 5
  1. #1
    New Coder
    Join Date
    Oct 2008
    Location
    Australia
    Posts
    32
    Thanks
    1
    Thanked 1 Time in 1 Post

    a function(event) problem

    hi all,

    i have to attach an event to the body, and in all eventuality it will be doing something completely different, but for the time being it is just bringing up an alert box for testing, but i'm attaching an event to the document onkeypress.

    Code:
    var test = new testFunction();
    function testFunction(){
    this.attachListener = function(){
    	if(document.addEventListener){
    		document.addEventListener('keypress',function(){test.onKeyPress(event)},false);
    	}
    	else if(document.attachEvent){
    		document.attachEvent(onkeypress,function(){test.onKeyPress(event)})
    	}
    	else{
    		//old way
    	}
    }
    this.onKeyPress = function(e){
    	if(window.event) // IE
    	{
    	keynum = e.keyCode;
    	}
    	else if(e.which) // Netscape/Firefox/Opera
    	{
    	keynum = e.which;
    	}
    	alert(keynum);
    }
    
    }
    but event returns undefined. Not sure why, as it's the only way I know how to get what key was pressed... but it's inaccessible

    thanks for your help

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    You need to define the e argument for IE
    Code:
    this.onKeyPress = function(e){
    var e=(!e)?window.event:e;//IE:Moz
    if(window.event) // IE
    	{
    	keynum = e.keyCode;
    	}
    	else if(e.which) // Netscape/Firefox/Opera
    	{
    	keynum = e.which;
    	}
    	alert(keynum);
    
    
    }
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New Coder
    Join Date
    Oct 2008
    Location
    Australia
    Posts
    32
    Thanks
    1
    Thanked 1 Time in 1 Post
    event still returns undefined, and so does e.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Try this:-

    Code:
    <script type = "text/javascript">   
    
    function handleKeyPress(evt) {   
    var key = (window.event)?event.keyCode:evt.which;   
    alert ("Keycode = " + key);
    
    which = String.fromCharCode(key).toUpperCase();
    if (key>=48 && key<=90) {
    alert ("You pressed the " + which + " key");  
    }
      
    return true;   
    }   
    
    document.onkeydown= handleKeyPress   
    </script>

  • #5
    New Coder
    Join Date
    Oct 2008
    Location
    Australia
    Posts
    32
    Thanks
    1
    Thanked 1 Time in 1 Post
    Thankyou very much, that last code worked fine.


  •  

    Posting Permissions

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