trapping the enter key? 
Author Message
 trapping the enter key?

How can I trap a particular key?

thanks!

--
Anthony Sullivan MCP, GMT, GST
Analyst/Programmer
Ajilon Services, Inc

Check out the ASP FAQ! http://www.*-*-*.com/
"From the mouths of babes comes ... slobber."



Mon, 21 Jul 2003 00:17:44 GMT  
 trapping the enter key?
On keypress event say :

if (event.keycode == 13)
    event.returnvalue = false ;

Quote:

> How can I trap a particular key?

> thanks!

> --
> Anthony Sullivan MCP, GMT, GST
> Analyst/Programmer
> Ajilon Services, Inc

> Check out the ASP FAQ! http://www.aspfaq.com
> "From the mouths of babes comes ... slobber."



Mon, 21 Jul 2003 01:00:49 GMT  
 trapping the enter key?
This seemed to work in IE 5.5; naturally this is a skeleton upon which you
should improve things dramatically.
-------------
<head>
.
<script type="text/javascript">
<!--
  function trapKey(theEvent, keyToTrap)
  {
     if (theEvent.keyCode == keyToTrap.charCodeAt(0))
       return (true);
     return (false);
  }
// -->
</script>
</head>
<body>
.
.
.
<form>
<input type="text"  onKeyPress="if (trapKey(event, 'd') == true)
alert('gotcha!')">
</form>
.
.
</body>
------------------------

Quote:
> How can I trap a particular key?

> thanks!

> --
> Anthony Sullivan MCP, GMT, GST
> Analyst/Programmer
> Ajilon Services, Inc

> Check out the ASP FAQ! http://www.aspfaq.com
> "From the mouths of babes comes ... slobber."



Mon, 21 Jul 2003 01:17:59 GMT  
 trapping the enter key?
Thanks!

I tried as you said and it seemsto work except...

<input type="text" name="mytextbox" onkeydown="if (event.keyCode == 13)
{return false;}">

If I hit enter in this field it still submit's the form.

Any idea why?

Thanks

Anthony Sullivan MCP, GMT, GST
Analyst/Programmer
Ajilon Services, Inc

Check out the ASP FAQ! http://www.aspfaq.com
"From the mouths of babes comes ... slobber."

Quote:

>On keypress event say :

>if (event.keycode == 13)
>    event.returnvalue = false ;


>> How can I trap a particular key?

>> thanks!

>> --
>> Anthony Sullivan MCP, GMT, GST
>> Analyst/Programmer
>> Ajilon Services, Inc

>> Check out the ASP FAQ! http://www.aspfaq.com
>> "From the mouths of babes comes ... slobber."



Mon, 21 Jul 2003 02:50:42 GMT  
 trapping the enter key?

Quote:

> How can I trap a particular key?

+++++++
IE only
+++++++

<body onkeydown="if (event.keyCode==13) {{{ YOUR ACTION HERE }}}">

+++++++++++++++
IE and Netscape
+++++++++++++++

First you have to prevent form submission via [Enter]:
============================================================================
<FORM ... onsubmit="return ButtonPressed" ...>

where ButtonPressed is some global variable set to false at page load, and
set to true by some deliberate action of submission

Then you have to define an event handler:
============================================================================
    function evalKeyPressed(e) {
      var keyStroked
      if (document.all) {
        var e = window.event
        keyStroked = e.keyCode
       } else keyStroked = e.which

       if ((keyStroked == 3)||(keyStroked == 13)) {
         {{{ YOUR ACTION HERE }}}
       }
    }

Next, assign it to your form elements:
============================================================================
    var fm = document.myFormName
    for (var i=0; i<fm.length; i++)
      if (fm.elements[i].type == 'text')
        fm.elements[i].onKeyPress = evalKeyPressed

Fin.

--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms.



Mon, 21 Jul 2003 04:03:04 GMT  
 trapping the enter key?
This works on IE

<form action=someurl.asp method=post>
<input type="text" name="mytextbox" onkeydown="return event.keyCode != 13">
<p>
<input type="submit">
</form>

-PK


Quote:
> How can I trap a particular key?

> thanks!

> --
> Anthony Sullivan MCP, GMT, GST
> Analyst/Programmer
> Ajilon Services, Inc

> Check out the ASP FAQ! http://www.aspfaq.com
> "From the mouths of babes comes ... slobber."



Mon, 21 Jul 2003 08:57:21 GMT  
 trapping the enter key?
This is probably related to non-standard behavior in the IE browser that
when you hit the enter key with a form present and with a focus on any form
input element, the action is equivalent to a form submission.  (I believe
with other browsers that follow the standard, user is required to interact
with a submit control, such as a pressing a button of type="submit").

In that case, you need an
    onSubmit="myFormSubmitHandler()"
as an attribute in the appropriate place, within a  'form' start tag,
according the specs I read.
Since the onKeyPress event might be handled before the onSubmit event, you
could set a global variable from within its handler that could be referenced
later as the thread of execution passes to the onSubmit handler
[myFormSubmitHandler()] so as to know whether to proceed with the form
submission (return true) or not (return false).  Alternatively, you might
pass the event object as an arg to myFormSubmitHandler(), see what
information you could extract from it, if that is enough for deciding
whether to proceed.


Quote:
> Thanks!

> I tried as you said and it seemsto work except...

> <input type="text" name="mytextbox" onkeydown="if (event.keyCode == 13)
> {return false;}">

> If I hit enter in this field it still submit's the form.

> Any idea why?

> Thanks

> Anthony Sullivan MCP, GMT, GST
> Analyst/Programmer
> Ajilon Services, Inc

> Check out the ASP FAQ! http://www.aspfaq.com
> "From the mouths of babes comes ... slobber."


> >On keypress event say :

> >if (event.keycode == 13)
> >    event.returnvalue = false ;


> >> How can I trap a particular key?

> >> thanks!

> >> --
> >> Anthony Sullivan MCP, GMT, GST
> >> Analyst/Programmer
> >> Ajilon Services, Inc

> >> Check out the ASP FAQ! http://www.aspfaq.com
> >> "From the mouths of babes comes ... slobber."



Mon, 21 Jul 2003 17:49:36 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Trapping Enter Key

2. Trap the <enter> key

3. trapping enter key

4. Trapping Function Keys and Alt-Key Combinations

5. how to make enter key act like tab key

6. Make the Enter key act as the Tab key

7. Make Enter Key Act Like Tab Key

8. How to make the ENTER key act like a TAB key

9. trapping the "Enter" event

10. trap pf keys

11. Trapping Control Keys

12. trapping F5 key and cancelling

 

 
Powered by phpBB® Forum Software