Last Comment Bug 139421 - Function Keys : Unable to catch onKeyDown events and to cancel default action
: Function Keys : Unable to catch onKeyDown events and to cancel default action
Status: VERIFIED INVALID
:
Product: Core
Classification: Components
Component: DOM: Events (show other bugs)
: Trunk
: All All
: -- normal (vote)
: ---
Assigned To: joki (gone)
: Vladimir Ermakov
Mentors:
Depends on: 96086
Blocks:
  Show dependency treegraph
 
Reported: 2002-04-23 01:44 PDT by xavier.loup
Modified: 2002-07-02 18:34 PDT (History)
2 users (show)
See Also:
Crash Signature:
(edit)
QA Whiteboard:
Iteration: ---
Points: ---
Has Regression Range: ---
Has STR: ---


Attachments

Description xavier.loup 2002-04-23 01:44:09 PDT
I try to develop a 3270 HTML emulatora and in order to mimic the behaviour of a
terminal, we want to use the function Keys (F1, F2, F3, ...)
However the default action of the F5, F7, F9 and F11 keys can't be cancelled
with javascript.

The following javascript has solved the problem for me on ie5 :

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

function getKeyCode(eventObj)
{
	var iKC = eventObj.keyCode;
	if ((iKC>=113) && (iKC<=123) && (!(eventObj.altKey))) {
		try
		eventObj.keyCode = 0;
		eventObj.cancelBubble = true;
		eventObj.returnValue = false;
		varTempTouche = iKC - 111;
		if (eventObj.shiftKey) {
			varTempTouche = varTempTouche + 12;
		}
		alert("F" + varTempTouche);
	}
}
function getKeyCodeHelp(eventObj)
{
	eventObj.keyCode = 0;
	eventObj.cancelBubble = true;
	eventObj.returnValue = false;
	varTempTouche = 1;
	if (eventObj.shiftKey) {
		varTempTouche = varTempTouche + 12;
	}
	alert("F" + varTempTouche);
}

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

Mozilla doesn't accept this script and throws an error for this command
"eventObj.keyCode = 0;".
--> Error: setting a property that has only a getter

This bug could depend on bug 57700 (changing event.keycode has no effect).
However I don't think this is a duplicate : I don't need to use the function
"event.keycode", I just need to be able to catch onKeyDown events and to cancel
default action for all Function Keys.
Comment 1 Matthias Versen [:Matti] 2002-04-23 08:47:02 PDT
-> DOM Events
Comment 2 Boris Zbarsky [:bz] (Out June 25-July 6) 2002-04-23 09:53:36 PDT
Nearly a dup of bug 96086 (in fact that bug was filed by someone writing a
terminal emulator as well).
Comment 3 Christian Schmidt 2002-04-23 10:15:14 PDT
A way to make it work is to call eventObj.preventDefault() (see 
http://www.mozilla.org/docs/dom/domref/dom_event_ref31.html) on
the onkeypress event.

I don't know whether calling preventDefault on the onkeydown event
is supposed to have the same effect (it currently hasn't). If not, 
I think this bug is INVALID.
Comment 4 xavier.loup 2002-04-23 10:38:34 PDT
Thanks for the help ! Using preventDefault works with onKeyPress events.
However this function is still buggy with onKeyDown events.

Moreover ie5 doesn't catch onKeyPress events for function keys.
I'll have to handle onKeyDown events for ie5 and onKeyPress events for mozilla !
Comment 5 Christian Schmidt 2002-04-24 02:21:20 PDT
> However this function is still buggy with onKeyDown events.
Buggy?

Are you sure that cancelling keyDown but not keyPress is _supposed_ to cancel
the default events of the function keys?

> Moreover ie5 doesn't catch onKeyPress events for function keys.
I suppose this is a bug in IE.

> I'll have to handle onKeyDown events for ie5 and onKeyPress events for mozilla !
That's life ... :-)
Comment 6 xavier.loup 2002-04-24 05:36:44 PDT
> Are you sure that cancelling keyDown but not keyPress is _supposed_ to cancel
> the default events of the function keys?

I think keyDown events should be cancellable, as KeyPress events are.

I have read some documentation on DOM events
(http://www.brainjar.com/dhtml/events/):
1. The DOM2 Event Model does not include specifications for key events.
2. The function preventDefault() can be used to cancel the event, if it is
cancelable.
3. keyPress in cencellable
Conclusion --> Why aren't keyDown events cancellable ?
Comment 7 Boris Zbarsky [:bz] (Out June 25-July 6) 2002-04-24 07:40:25 PDT
Well... keydown is cancellable, but the sidebar is triggered by keypress instead
of keydown...

So cancelling keydown makes no difference, since keypress is still fired.

Out of curiousity, does cancelling keydown prevent firing of keypress or keyup
in IE?
Comment 8 xavier.loup 2002-04-24 09:38:01 PDT
> Out of curiousity, does cancelling keydown prevent firing of keypress or keyup
> in IE?

Yes cancelling keydown prevent firing of keypress or keyup in IE !

The following script doesn't have the same action on ie and on mozilla.
-> keyDown and KeyPress events are fired on Mozilla
-> keyDown events are fired on IE and no keyPress events

-----------------
<html>
<head>
<script language=JavaScript>
<!--
function getKeyDown(eventObj)
{
	var iKC = eventObj.keyCode;
	eventObj.cancelBubble = true;
	eventObj.returnValue = false;
	alert("onKeyDown event: " + iKC);
}
function getKeyPress(eventObj)
{
	var iKC = eventObj.keyCode;
	alert("onKeyPress event: " + iKC);
}

-->
</script>
</head>
<body onKeyDown="getKeyDown(event)" onKeyPress="getKeyPress(event)">
dsflsfs
</body>
</html>

-----------------------------
Comment 9 Boris Zbarsky [:bz] (Out June 25-July 6) 2002-04-24 10:18:06 PDT
> Yes cancelling keydown prevent firing of keypress or keyup in IE !

That's what I seemed to recall.... I searched for a bug on that, but we don't
seem to have one (that includes resolved bugs).  Not sure whether we should
support that behavior (it seems broken to me) but confirming bug so joki can decide.
Comment 10 Christian Schmidt 2002-04-29 11:26:59 PDT
> > Yes cancelling keydown prevent firing of keypress or keyup in IE !
>
> [...] Not sure whether we should support that behavior (it seems  broken to 
> me)

According to <http://lists.w3.org/Archives/Public/www-dom/2002AprJun/0086.html>
IE's behaviour actually /is/ broken, and Mozilla's is right.

Marking INVALID. Please reopen, if you disagree.
Comment 11 Vladimir Ermakov 2002-07-02 18:34:10 PDT
verifying. This is invalid.

Note You need to log in before you can comment on or make changes to this bug.