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.
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
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!
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;
}
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'>
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.
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".
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.
:)
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>
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...