Detect keypress of 'ENTER'
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Detect keypress of 'ENTER'

  1. #1
    Join Date
    May 2005
    Posts
    195

    Detect keypress of 'ENTER'

    I need to basically detect the key press of ENTER on a asp.net web form, using javascript of course, whenever the user presses the ENTER button, no matter what part of the screen has focus.

    I've written code, and added its function to a text field so when they press enter some javascript code works, but thats only if the focus is set to the label. How can I detect and run code if the user presses ENTER no matter when or where the user is currently at on the form?

    Any insight will be very helpful, thanks!!

    Brandon.

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Detect keypress of 'ENTER'

    Maybe you could do something like this (it seems to work on IE6.0, but not tested with other browsers).
    Code:
    <HEAD>
        <SCRIPT>
        function checkKey()
        {
            if (window.event.keyCode == 13)
            {
                alert("you hit return!");
            }
        }
        </SCRIPT>
    </HEAD>
    <BODY onkeypress="checkKey()">
    Empty page.
    </BODY>
    - petter

  3. #3
    Join Date
    May 2005
    Posts
    195

    Re: Detect keypress of 'ENTER'

    Putting the function to handle the enter button in the body worked. I would have never thought of doing that. However, that didn't solve my problem.

    Let me explain fully why I need to capture the enter button (this is all in asp.net). I have a web form which applys security to several buttons when they click an item on a grid. The security enables and disables the buttons related to their security, one of the buttons is a cancel button, which pops up a confirm box "are you sure...."

    My problem is, when the user selects an item from the grid, and security is applied, it looks like focus is on the cancel button, so if they press ENTER it will fire the confirm, and an accidental cancel might happen (with a second Enter press to confirm it).

    I have tried moving the focus to other buttons, or other controls with no luck. I was hoping that if I called a function on the ENTER button, no matter what I could redirect what was happening on the form, but that didn't seem to work, because now when the user presses ENTER, it will fire off that code in the javascript, but then right after that, will press the cancel button (what had focus on previously).

    Is there a way through javascript to prevent this from happening?

    Thanks!

  4. #4
    Join Date
    Sep 2004
    Location
    Tehran(Ir)
    Posts
    469

    Re: Detect keypress of 'ENTER'

    well,I am not sure exactly what you told,but for every control on your page you can have onKeyPress attribute for traping & handling enter key
    Code:
    //if enter pressed,disable it
    	if(event.keyCode==13){
    		event.keyCode = null;
    	}
    //if enter pressed,change the focus
    	if(event.keyCode==13){
    		event.keyCode = 9;
    	}

  5. #5
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,877

    Re: Detect keypress of 'ENTER'

    While sending another keystroke will work, I do not believe that it is the best resourceful idea. I believe denying the return is the best option. See the example below.

    Code:
    <input type='button' value='Cancel' onkeypress='return false'>
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  6. #6
    Join Date
    Sep 2004
    Location
    Tehran(Ir)
    Posts
    469

    Re: Detect keypress of 'ENTER'

    Quote Originally Posted by peejavery
    While sending another keystroke will work, I do not believe that it is the best resourceful idea. I believe denying the return is the best option. See the example below.

    Code:
    <input type='button' value='Cancel' onkeypress='return false'>
    well,your code terminates all the keyboard events

    BTW as OP mentioned ASP.NET,he could call a recurssive function on the Page for adding the onKeyPress attribute to the child controls.

  7. #7
    Join Date
    Feb 2005
    Location
    Denmark
    Posts
    742

    Re: Detect keypress of 'ENTER'

    If you want your code to be FireFox compatible, then I'd recommend doing something along this line

    Code:
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;
    if (keycode == 13)
    {	
    	//do stuff
    }
    FireFox and IE uses different event models, which is the reason for the "if".

  8. #8
    Join Date
    May 2005
    Posts
    195

    Re: Detect keypress of 'ENTER'

    Thanks for the input. I believe all of your replies have helped me not only in this solution, but overall in my career of javascript/app development.


  9. #9
    Join Date
    Jul 2004
    Location
    Holy Land
    Posts
    306

    Re: Detect keypress of 'ENTER'

    Here's what I always use... and it always works...

    HTML Code:
    <script language="Javascript">
    	document.onkeydown = function() {
    		if (event.keyCode == 13) {
    			// do stuff
    		}
    	}
    </script>
    Rate this post if you found it useful!
    10X, gilly914

  10. #10
    Join Date
    Jun 2004
    Posts
    142

    Re: Detect keypress of 'ENTER'

    hello

    I know this is an old post but since I think this is the best solution to people hitting the Enter key in a form I though I'd share it with yous...

    also because I just finished using it and want to kill the last 5 minutes of my shift....

    I dont remember where I got this, but I'm not the original author of this. I just like it because once you post that in the header of the html page you do not need to call it anywhere else, not in the form, nowhere but once like posted here

    Code:
    <script type="text/javascript">
    
    function checkCR(evt) {
    
        var evt  = (evt) ? evt : ((event) ? event : null);
    
        var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
    
        if ((evt.keyCode == 13) && (node.type=="text")) {
    	return false;
    	}else if ((evt.keyCode == 13) && (node.type=="file")) {
        return false;
      }}
    
      document.onkeypress = checkCR;
    
    </script>
    the enter key will do a return for the text, and not submit the form...
    Last edited by bobo; December 20th, 2007 at 07:03 AM.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center