Closed Bug 139421 Opened 22 years ago Closed 22 years ago

Function Keys : Unable to catch onKeyDown events and to cancel default action

Categories

(Core :: DOM: Events, defect)

defect
Not set
normal

Tracking

()

VERIFIED INVALID

People

(Reporter: xavier.loup, Assigned: joki)

References

Details

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.
-> DOM Events
Assignee: Matti → joki
Component: Browser-General → DOM Events
QA Contact: imajes-qa → vladimire
Nearly a dup of bug 96086 (in fact that bug was filed by someone writing a
terminal emulator as well).
Depends on: 96086
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.
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 !
> 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 ... :-)
> 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 ?
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?
> 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>

-----------------------------
> 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.
Status: UNCONFIRMED → NEW
Ever confirmed: true
> > 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.
Status: NEW → RESOLVED
Closed: 22 years ago
Resolution: --- → INVALID
verifying. This is invalid.
Status: RESOLVED → VERIFIED
You need to log in before you can comment on or make changes to this bug.