freelanceprogrammers.org Forum Index » ASP

Key Capture in Mozilla


View user's profile Post To page top
kindawords Posted: Fri Apr 15, 2005 7:51 pm


Joined: 11 Jan 2006

Posts: 46
Key Capture in Mozilla
OK,
I`m trying to capture a return key being pressed in a text box.

I capture the event successfully
onKeyPress="javascript:TrapKey();

and it calls this function...
function TrapKey(e)
{
alert(`Key Trap Entered`);
if(e.keyCode == 13)
{
e.keyCode == 0;
//do some stuff...
}
}

this works perfectly in IE

but in Mozilla the function gets called, but e is always undefined
and hence I cannot read the key code.

I`ve done some googling and come up with some variations

that try and set e, if it is not defined, but none of these work either, such as

if (!e) var e = window.event;

Any thoughts or hints would be appreciated.

Thanks


--
Dean Fiala
Very Practical Software, Inc
http://www.vpsw.com
Reply with quote
Send private message
View user's profile Post To page top
mk7306 Posted: Fri Apr 15, 2005 10:21 pm


Joined: 15 Apr 2005

Posts: 1
Key Capture in Mozilla
Hey Dean,

Try this:

http://www.severnsolutions.co.uk/twblog/archive/2003/09/14/keypressevents

Mark

>From: Dean Fiala <dfiala@...>
>Reply-To: AspNetAnyQuestionIsOk@yahoogroups.com
>To: AspNetAnyQuestionIsOk@yahoogroups.com
>Subject: [AspNetAnyQuestionIsOk] Key Capture in Mozilla
>Date: Fri, 15 Apr 2005 10:51:22 -0400
>
>
>OK,
>I`m trying to capture a return key being pressed in a text box.
>
>I capture the event successfully
>onKeyPress="javascript:TrapKey();
>
>and it calls this function...
>function TrapKey(e)
>{
> alert(`Key Trap Entered`);
> if(e.keyCode == 13)
> {
> e.keyCode == 0;
> //do some stuff...
> }
>}
>
>this works perfectly in IE
>
>but in Mozilla the function gets called, but e is always undefined
>and hence I cannot read the key code.
>
>I`ve done some googling and come up with some variations
>
>that try and set e, if it is not defined, but none of these work either,
>such as
>
>if (!e) var e = window.event;
>
>Any thoughts or hints would be appreciated.
>
>Thanks
>
>
>--
>Dean Fiala
>Very Practical Software, Inc
>http://www.vpsw.com
>
>
>
>Yahoo! Groups Links
>
>
>
>
>
>
>
Reply with quote
Send private message
View user's profile Post To page top
kindawords Posted: Fri Apr 15, 2005 11:28 pm


Joined: 11 Jan 2006

Posts: 46
Key Capture in Mozilla
Mark,
Thank you. That was a great help.

For those playing at home, here`s what I ended up with that works in
both IE and FireFox...

function TrapKey(evt){

evt = (evt) ? evt : (window.event) ? event : null;
if (evt)
{

var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ?
evt.keyCode :((evt.which) ? evt.which : 0));

if(charCode == 13)
{
//do stuff
return false;
}
}
}

Also, found it helpful to call the function properly - doh!

onKeyPress="javascript:TrapKey(event);"

Gotta love all those divergent DOMs!

Thanks again.

On 4/15/05, Mark L Keller <mk7306@...> wrote:
>
> Hey Dean,
>
> Try this:
>
> http://www.severnsolutions.co.uk/twblog/archive/2003/09/14/keypressevents
>
> Mark
>
> >From: Dean Fiala <dfiala@...>
> >Reply-To: AspNetAnyQuestionIsOk@yahoogroups.com
> >To: AspNetAnyQuestionIsOk@yahoogroups.com
> >Subject: [AspNetAnyQuestionIsOk] Key Capture in Mozilla
> >Date: Fri, 15 Apr 2005 10:51:22 -0400
> >
> >
> >OK,
> >I`m trying to capture a return key being pressed in a text box.
> >
> >I capture the event successfully
> >onKeyPress="javascript:TrapKey();
> >
> >and it calls this function...
> >function TrapKey(e)
> >{
> > alert(`Key Trap Entered`);
> > if(e.keyCode == 13)
> > {
> > e.keyCode == 0;
> > //do some stuff...
> > }
> >}
> >
> >this works perfectly in IE
> >
> >but in Mozilla the function gets called, but e is always undefined
> >and hence I cannot read the key code.
> >
> >I`ve done some googling and come up with some variations
> >
> >that try and set e, if it is not defined, but none of these work either,
> >such as
> >
> >if (!e) var e = window.event;
> >
> >Any thoughts or hints would be appreciated.
> >
> >Thanks
> >
> >
> >--
> >Dean Fiala
> >Very Practical Software, Inc
> >http://www.vpsw.com
> >
> >
> >
> >Yahoo! Groups Links
> >
> >
> >
> >
> >
> >
> >
>
> Yahoo! Groups Links
>
>
>
>
>


--
Dean Fiala
Very Practical Software, Inc
http://www.vpsw.com
Reply with quote
Send private message
Post new topic Reply to topic
Display posts from previous:   
 

All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
China Wholesale - Electronics Products
Character Studio - Tutorials and Help