[web] Javascript Question (Solved)

Started by
2 comments, last by Avictus 15 years, 9 months ago
I've been playing around with &#106avascript 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]
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 :(
in IE, event is not defined. you should always use
event = event || window.event
to work cross browers.
TTT Art Game http://ttt-games.com:8080/ttt/art-game.html
huoyangao,

Thank you for your help. Your advice worked perfectly.

This topic is closed to new replies.

Advertisement