Javascript Cancel OnKeyPress prob on Safari

F

Fred Brown

Hi,
I want to cancel a certain key in JavaScript.
To do so, I catch the event in OnKeyPress and cancel the default:

<head>
....
function f(evt)
{
var evt = (evt) ? evt : ((window.event) ? window.event : "")
...
evt.returnValue = false; // IE
evt.preventDefault(); // dom 2 (also tried without this here)
return false; // netscape
}
....
<body>
....
<TEXTAREA Name=demoTextarea ROWS=10 COLS=50 OnKeyPress="f(event)">
</TEXTAREA>

IE works fine.
Safari will not cancel the key.
I also checked to see if the textArea value already had received the
key before this handler. It had not.

Any ideas?

Thanks in advance,
Fred Brown
 
G

Guest

I want to cancel a certain key in JavaScript.

Why?

I really hate it when folks use JavaScript to screw up
what I expect my browser to do.

I really can't think of a valid reason for you to be able to
do this. Just don't.
 
F

Fred Brown

I did want to waste the forum's bandwidth with unnecessary details of
my app,
but in short, I am not actually just canceling the key, I am replacing
the
inputted keys with other keys. This makes sense in my app. It is the
reason that this textarea exists. My problem is that both the
replaced key and the original one end up in the textarea on Safari.
Does anybody know how to achieve this replacement?
 
P

Peter Hurford

I can't remember the exact details but I seem to recall having problems
trapping keys - esp. the tab key - with onkeypress.

My solution was to look at onkeydown instead.

I can think of lots of reasons why you'd want to do this, by the way.
 
F

Fred Brown

Peter,
I checked if I could cancel the onKeyDown on Safari and I could not.
I am no JavaScript expert but I think that onKeyPress is better for me
because it has already translated from virtual key to unicode. Also,
I believe if you hold down a key, you will get many onKeyPress events
for each
char that is processed but only one keyDown. I think you are right
though
that keyPress does not get called for all keys - I saw that it did not
get called on IE for a backspace (which makes sense since I assume
there is no
unicode defined for keys like a backspace.)

Thanks anyhow,
Fred Brown
 
J

Jules

Hi Fred,

Did you ever find a solution for this? I am now encountering the same
problem. We want to cancel non-numeric characters and have found a way to
do it that is supported by both IE6 and NS7 - onKeyPress event with a
handler that returns false (or sets event.returnVal) if not numeric. Now
I'm trying to get it to work with Safari and it just won't. The onKeyPress
event fires, the handler is called, returns false appropriately, but doesn't
cancel the key. Since it has a window.event like IE, I tried setting
returnVal to false - nada. I tried setting event.keyCode to something
else - nada. I tried overriding the value of the object - nada.

Anyone have any ideas? Your help is greatly appreciated!

Julia Allen
(e-mail address removed)
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top