IE Not Stopping Default Event

B

blaine

Hello,

I'm currently overriding function keys (F1 to F4) to perform other
actions. In order to do this the default popup windows of Help (F1),
Seach(F3) etc must be turned off. In FF it's easy enought to do using
the preventDefault and stopPropagation event functions.

IE's equivalent is supposed to be cancelBubble and returnValue,
however I can not seem to get them to stop no matter what I try.

Can someone please point out my error? The test code is below.

------
var classKeyPressOverride = function(){}
classKeyPressOverride.prototype.toString = function(){ return " Class
Key Press Override "; }
classKeyPressOverride.prototype.attachListener = function( type,
functionCode ){
//Register the listener
var obj = window.document;
if ('addEventListener' in obj) {
obj.addEventListener( type, functionCode, true);
} else if ('attachEvent' in obj) {
obj.attachEvent('on' + type, functionCode );
} else {
alert("Could not add listener");
}
}


//Create an instance and add attach a lister tpe of keydown
var classKP = new classKeyPressOverride();

classKP.attachListener('keydown', function(event){
var ev = event || window.event;

var keyCode = ev.keyCode || ev.which;
alert("aatrying " + keyCode);
var fKeyPressed = false;

//IE won't like the regular ev.DOM_VK_F1 so use keyCode values.
if (keyCode == 112) {
fKeyPressed = true;
alert("ev F1 Pressed");
}else if (keyCode == 113) {
fKeyPressed = true;
alert("F2 Pressed");
}else if (keyCode == 114) {
fKeyPressed = true;
alert("F3 Pressed");
}else if (keyCode == 115) {
fKeyPressed = true;
alert("F4 Pressed");
}

if (fKeyPressed){
//Prevent help menu and other default F1 key functions
if ('stopPropagation' in ev) {
ev.stopPropagation();
ev.preventDefault();
}else{
// Trying to stop the popup windows in IE
//Does not seem to work...
ev.cancelBubble = true;
ev.returnValue = false;
}
}

}
);
 
B

blaine

Hey Gérard,

Thanks for your concern.

If you would have thought about it a little longer you may have
realized that this if for a web application that the general public
does not have access to.

Since users a trained to press the K-Keys for help. It makes perfect
sense to override the Help menu of the browser with the Help Menu of
the application.

Sincerely,
Blaine
 
L

Lee

(e-mail address removed) said:
Hey G=E9rard,

Thanks for your concern.

If you would have thought about it a little longer you may have
realized that this if for a web application that the general public
does not have access to.

What clues did you provide that might have helped anyone to
realize that? The vast majority of posts in this newsgroup
are about internet sites, putting the onus on you few others
to identify your unusual situation.

It doesn't make sense to over-ride browser functionality.
Even if your current users are used to some legacy system,
it makes much more sense to have them adapt to web browser
standards than to try to pervert the browser to fit them.


--
 
B

blaine

I just wanted to post the solution for anyone that is interested.

1) If you want to override the 'F1' button in IE you need to attach an
event listener to stop
the default action.
classKP.attachListener('help', classKP.stopPropagation );

2) In IE cancelBubble and returnValue were not stopping the default
actions as specified in the docs, setting the keyCode to 0 then
running the actions stops all the defaults
ev.keyCode = 0;


Below is a working example.


var classKeyPressOverride = function(){}
classKeyPressOverride.prototype.toString = function(){ return " Class
Key Press Override "; }
classKeyPressOverride.prototype.attachListener = function( type,
functionCode ){
//Register the listener

var obj = window.document;
obj.eventParent = this;
if ('addEventListener' in obj) {
obj.addEventListener( type, functionCode, true);

} else if ('attachEvent' in obj) {
obj.attachEvent('on' + type, functionCode );
} else {
alert("Could not add listener");
}
}
classKeyPressOverride.prototype.stopPropagation = function(ev){
if ('stopPropagation' in ev) {
ev.stopPropagation();
ev.preventDefault();
}else{
ev.keyCode = 0;
ev.cancelBubble = true;
ev.returnValue = false;
}
}

//Create an instance and add attach a lister tpe of keydown
var classKP = new classKeyPressOverride();
classKP.attachListener('help', classKP.stopPropagation );
classKP.attachListener('keydown', function(event,parentClass){
var ev = event || window.event;

var keyCode = ev.keyCode || ev.which;
var fKeyPressed = false;

//ev.DOM_VK_F1
if (keyCode == 112) {
fKeyPressed = true;
//alert("ev F1 Pressed");
}else if (keyCode == 113) {
fKeyPressed = true;
//alert("F2 Pressed");
}else if (keyCode == 114) {
fKeyPressed = true;
//alert("F3 Pressed");
}else if (keyCode == 115) {
fKeyPressed = true;
//alert("F4 Pressed");
}

if (fKeyPressed == true){
this.eventParent.stopPropagation(event);
}

}
);
 
B

blaine

Ups I problem with the code above..

The line
this.eventParent.stopPropagation(event)
should be:
classKP.stopPropagation(event);



var classKeyPressOverride = function(){}
classKeyPressOverride.prototype.toString = function(){ return " Class
Key Press Override "; }
classKeyPressOverride.prototype.attachListener = function( type,
functionCode ){
//Register the listener

var obj = window.document;

if ('addEventListener' in obj) {
obj.addEventListener( type, functionCode, true);

} else if ('attachEvent' in obj) {
obj.attachEvent('on' + type, functionCode );
} else {
alert("Could not add listener");
}
}

classKeyPressOverride.prototype.addListener = function(obj, evt, fun,
b) {
var eventPhase = (typeof(b)=='boolean') ? b : false;

if ('addEventListener' in obj) {
obj.addEventListener(evt, fun, eventPhase);
} else if ('attachEvent' in obj) {
obj.attachEvent(evt, fun);
} else {
alert("Could not add listener");
}
}

classKeyPressOverride.prototype.stopPropagation = function(ev){
if ('stopPropagation' in ev) {
ev.stopPropagation();
ev.preventDefault();
}else{
ev.keyCode = 0;
ev.cancelBubble = true;
ev.returnValue = false;
}
}

//Create an instance and add attach a lister tpe of keydown
var classKP = new classKeyPressOverride();
classKP.attachListener('help', classKP.stopPropagation );
classKP.attachListener('keydown', function(event){
var ev = event || window.event;

var keyCode = ev.keyCode || ev.which;
var fKeyPressed = false;

//ev.DOM_VK_F1
if (keyCode == 112) {
fKeyPressed = true;
//alert("ev F1 Pressed");
}else if (keyCode == 113) {
fKeyPressed = true;
//alert("F2 Pressed");
}else if (keyCode == 114) {
fKeyPressed = true;
//alert("F3 Pressed");
}else if (keyCode == 115) {
fKeyPressed = true;
//alert("F4 Pressed");
}

if (fKeyPressed == true){
//new classKeyPressOverride().stopPropagation(event);

//Use the variable name of the class instance
//Since it is within the scopt of this function code
// "this" and "parent" refer to the object that the key lister is
run on ie window.document
classKP.stopPropagation(event);
}
}
);
 

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,818
Messages
2,569,727
Members
45,664
Latest member
Phil79581

Latest Threads

Top