www.webdeveloper.com
Results 1 to 5 of 5

Thread: How Do You Find Out WHICH Buttons have been pressed?

  1. #1
    Join Date
    Jan 2005
    Location
    Somewhere
    Posts
    121

    How Do You Find Out WHICH Buttons have been pressed?

    How Do You Find Out WHICH Buttons have been pressed in MSIE. Do you use the "onkeypress" event handeler or what else?
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

  2. #2
    Join Date
    Jul 2004
    Posts
    986

    Re: How Do You Find Out WHICH Buttons have been pressed?

    Originally posted by Hiya
    How Do You Find Out WHICH Buttons have been pressed in MSIE. Do you use the "onkeypress" event handeler or what else?
    Buttons are clicked... try onclick

  3. #3
    Join Date
    Nov 2003
    Location
    Aachen, Germany
    Posts
    4,090
    Hi!

    This is already your third thread about the same topic. Did you ever read the replies to the previous two threads?

    It seems, you didn't, so I assume my post doesn't need to contain any code.

    Pit
    The perfect website, which isn't one: http://www.pit-r.de (completely re-designed)

    Bad news for many "important" members here: I am still alive.

  4. #4
    Join Date
    Jan 2005
    Location
    USA
    Posts
    189
    you have a function called onkeypress, onkeydown or onkeyup. the event object is window.event. the property you want is keyCode. so in IE...

    function alertCode(e)
    {
    var e = e || window.event;
    var code = e.keyCode || e.which;
    alert(code);
    }

    <input type="text" onKeyDown="alertCode();">

    this should work in most browsers. e is put in as a parameter in Netscape, and it'll use the global event object in IE. I'm not sure if all browsers support keyCode, but which is basically the same (when it works)

    I think you also have to tell the browser to capture the event.

    just look at these tutorials...
    http://www.javascriptkit.com/dhtmltu...omevent1.shtml
    http://www.javascriptkit.com/dhtmltu...ventp2-1.shtml

  5. #5
    Join Date
    Oct 2004
    Posts
    1,118
    1) The key codes are stored in something called the Event object, which is automatically created when an event fires, e.g onkeypress. For a list of the various key codes a keyboard produces, see here(the characters in red are the keyboard characters and the key codes are on the far left under the heading 'Dec'):

    http://www.lookuptables.com/

    Moz automatically sends the Event object, which contains the key code, to the event handler function. So, all you have to do is 'catch' it by adding a parameter to your function:
    Code:
    function doSomething(e)
    {
    	....
    	....
    	
    }
    IE does not automatically send the event object to the function, so you have to summon it manually:
    Code:
    function doSomething(e)
    {
    	if (!e) var e = window.event;  //for IE
    	
    }
    After that, you will have the event object stored in the variable 'e'.

    2) There are also cross browser differences as to where the key code is stored in the event object 'e'. In IE, it is in the .keyCode property. In Moz, it can be the .which property. So, you need to do something like this:
    Code:
    function doSomething(e)
    {
    	if (!e) var e = window.event;  //for IE
    	
    	var code;
    	if(e.keyCode) code = e.keyCode; //for IE
    	if(e.which) code = e.which;  //for other browsers
    }
    After that, the variable 'code' will contain the key code.

    3) Then, all you have to do is test for the the key you are interested in, e.g. the Enter key, and if you want you can return false to cancel the keypress:
    Code:
    function doSomething(e)
    {
    	if (!e) var e = window.event;  //for IE
    	
    	var code;
    	if(e.keyCode) code = e.keyCode; //for IE
    	if(e.which) code = e.which;  //for other browsers
    
    	if (code == 13)
    	{
    		//do what you want here
    		
    		return false;  //cancels the keypress
    	}
    	else return true;
    }
    Some browsers allow the canceling of the onkeydown, onkeyup, and onkeypress events, and some don't, so that can be another problem. I believe onkeydown is not cancelable in FF1.0, where onkeypress is cancelable in IE6 and FF1.0.

    4) Now, how do you use that function? If you want to detect the Enter key on the whole page:
    Code:
    window.onload=function()
    {
    	document.onkeypress = doSomething;
    };
    </script>
    </head>
    If you only want to detect the Enter key on a form:
    Code:
    window.onload=function()
    {
    	document.my_form.onkeypress = doSomething;
    };
    </script>
    </head>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles