• Advertisement
Sign in to follow this  

[web] Javascript Question (Solved)

This topic is 3492 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been playing around with javascript for the last few days and I've run into an issue with IE. I'm trying to capture the keyboard state using the following code:
myKeyboard = function(){
    var self = this;
    this.keyspressed = [];
    this.i = [[0]];
    
    document.onkeydown = function(event){
        self.keyPress(event);
    };
    
    document.onkeyup = function(event){
        self.keyUp(event);
    };
};

myKeyboard.prototype.keyPress = function(event){
    var code;
        
    if (event.keyCode === [[0]] || event.keyCode == "undefined"){
        code = event.charCode;
    } else {
        code = event.keyCode;
    }
        
    this.keyspressed = [[1]];
};

myKeyboard.prototype.keyUp = function(event){
    var code;
        
    if (event.keyCode === [[0]] || event.keyCode == "undefined"){
        code = event.charCode;
    } else {
        code = event.keyCode;
    }
    
    this.keyspressed = [[0]];
};

myKeyboard.prototype.isKeyDown = function(/*int*/ keyCode){
    if (typeof this.keyspressed[keyCode] == "undefined" || this.keyspressed[keyCode] === [[0]]){
        return false;
    } else {
        return true;
    }
};


This code works fine in Firefox, Safari, and Opera, but it fails in IE. I did some testing and "event" isn't being passed to the keyPress and keyUp functions. Edit: fixed source tags. [Edited by - Avictus on July 27, 2008 11:43:44 PM]

Share this post


Link to post
Share on other sites
Advertisement
I'm not sure, but I have a hunch that the this keyword does not represent the right object in IE. I'm trying to find reference material to support my suspicion, but I can't find it right now :(

Share this post


Link to post
Share on other sites
in IE, event is not defined. you should always use
event = event || window.event
to work cross browers.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement