onKeyPress event Mozilla

L

LilAndy23

How can I use the onKeyPress event handler in Firefox? onKeyPress
doesn't seem to fire in Firefox.
 
R

Robert Mark Bram

Hi Andy,
How can I use the onKeyPress event handler in Firefox? onKeyPress
doesn't seem to fire in Firefox.

It does! This works fine on Firefox.

<input type="text" name="search" onkeypress="alert('Hi!');" />
<input type="text" name="search" onKeyPress="alert('Hi!');" />

Perhaps you could post some code to show us exactly what you are trying
to do?

Rob

:)
 
L

LilAndy23

The following seems to work in IE but not in firefox... (I changed the
event from onKeyPress to onKeyDown

HTML

<input name="strWnum" type="text" value="W" size="9"
onBlur="checkInput();" onKeyDown="checkEnter(event);">
..
..
..
<script language="javascript" type="text/javascript">
document.frmCheckOut.strWnum.onkeydown = checkEnter;
</script>
.........
then in my linked .js file I have.....

function checkEnter(e) {
if(!e) var e = window.event;

if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
//code contains the pressed key in all browsers

//alert("A) code is: " + code)

if(code==13) {
if (e.keyCode) e.keyCode = 9;
else e.which = 9;

}

}
////////////////////////////////////////////////////////////////////////////////////////////////////////
function checkInput() {
var strValue = document.frmCheckOut.strWnum.value;

if (String(strValue).length == 26 || String(strValue).length == 16) {
document.frmCheckOut.strWnum.value = String(strValue).substring(1,
10)

} else if (String(strValue).length == 13) {
document.frmCheckOut.strWnum.value = "W" +
String(strValue).substring(2, 10)
}

}

--------------------------------------------

I want to switch the focus to the next form element once I hit enter.
Like i said, it works in IE and not in Firefox.

Thanks
Andy
 
M

Martin Honnen

LilAndy23 wrote:

if(code==13) {
if (e.keyCode) e.keyCode = 9;
else e.which = 9;

Well check the JavaScript console if something does not work to find out
where the problem is, the attempt to set e.keyCode is probably giving a
script error in Mozilla that setting that property is not allowed.

It is not allowed to change the code of a key event in Mozilla and most
other browsers so that approach works in IE/Win but for other browsers
you need a different approach. You could look for the next control and
call its focus method.
 
L

LilAndy23

Thanks, I'll going to try to switch the focus instead of
programmatically trying to fire off keycode 9.

Andy
 
L

LilAndy23

I modifed the code. It now looks like:

if(code==13) {
//I want to be able to set e.keyCode to 9 at this point
if (e.keyCode) {
document.frmCheckOut.ddlLID.focus();
} else {
document.frmCheckOut.ddlLID.focus();
}

this code fire within a form and so when enter is pressed, it keeps
trying to send/submit the form. Which is what I don't want to happen.


Does anyone know of an other approach to simulate a tab (or a switch of
focus) key by pressing the enter key (that is compatable with Mazilla
and IE)???

Thanks
Andy
 
R

Randy Webb

LilAndy23 said the following on 10/13/2005 12:25 AM:
I modifed the code. It now looks like:

if(code==13) {
//I want to be able to set e.keyCode to 9 at this point
if (e.keyCode) {
document.frmCheckOut.ddlLID.focus();
} else {
document.frmCheckOut.ddlLID.focus();
}

this code fire within a form and so when enter is pressed, it keeps
trying to send/submit the form. Which is what I don't want to happen.

Then cancel the submission.
Does anyone know of an other approach to simulate a tab (or a switch of
focus) key by pressing the enter key (that is compatable with Mazilla
and IE)???

Why are you trying to re-engineer the way the browser works?
 
R

RobG

LilAndy23 said:
I modifed the code. It now looks like:

if(code==13) {
//I want to be able to set e.keyCode to 9 at this point
if (e.keyCode) {
document.frmCheckOut.ddlLID.focus();
} else {
document.frmCheckOut.ddlLID.focus();
}

this code fire within a form and so when enter is pressed, it keeps
trying to send/submit the form. Which is what I don't want to happen.


Does anyone know of an other approach to simulate a tab (or a switch of
focus) key by pressing the enter key (that is compatable with Mazilla
and IE)???

Thanks
Andy

The craziness here is that there are two active threads attempting to
modify the browsers default behaviour to achieve opposing results: this
one is trying to stop submission and make the enter key navigate to the
input, the another is trying to make it submit the form.

The result is the deliberate creation of confusion for users. Leave the
default behaviour of the browser alone.
 
L

LilAndy23

Well, the reason why I need the enter key to simulate the tab key is
because I'm using a magnetic stripe reader to input the data directly
into the input form. When you swipe the plastic card with the mag
reader it automatically sends the enter key signal. This keeps trying
to submit the form. Therefore, I'm trying to figure a way to cancel
the submission and instead switch the focus to the next input field.

Any suggestions on how I can do this? Hopefully my explaination above
is enough to get some help or suggestions.

Andy
 
P

pipe

LilAndy23 said:
Well, the reason why I need the enter key to simulate the tab key is
because I'm using a magnetic stripe reader to input the data directly
into the input form. When you swipe the plastic card with the mag
reader it automatically sends the enter key signal. This keeps trying
to submit the form. Therefore, I'm trying to figure a way to cancel
the submission and instead switch the focus to the next input field.

Any suggestions on how I can do this? Hopefully my explaination above
is enough to get some help or suggestions.

Andy

so, if that comes from a codebar or something, we could assume that the
length is fixed right?

so you could say something like:
<input
onkeypress='if(this.length==20){document.formnamr.input.focus()}'>

so the key here is really the:
document.formname.inputname.focus()

look it up.




/f3l
http://www.geocities.com/kyoosho/
 
R

RobG

LilAndy23 said:
Well, the reason why I need the enter key to simulate the tab key is
because I'm using a magnetic stripe reader to input the data directly
into the input form. When you swipe the plastic card with the mag
reader it automatically sends the enter key signal. This keeps trying
to submit the form. Therefore, I'm trying to figure a way to cancel
the submission and instead switch the focus to the next input field.

The assumption here is that things are for general web use, the advice
you have received so far has been in that context. If you have a need
for a special purpose, it should be explained up front.
Any suggestions on how I can do this? Hopefully my explaination above
is enough to get some help or suggestions.

Have you tried loading the data into a textarea element? That will
keep the return/linefeed in its value, you can then parse it onsubmit
(or earlier if you wish) to remove the return or just leave it there.

Once the data is posted, subsequent processes have not idea whether it
was sent by a textarea or text input.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top