Sign in to follow this  
Avictus

[web] Javascript Question (Solved)

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[code] = [[1]];
};

myKeyboard.prototype.keyUp = function(event){
    var code;
        
    if (event.keyCode === [[0]] || event.keyCode == "undefined"){
        code = event.charCode;
    } else {
        code = event.keyCode;
    }
    
    this.keyspressed[code] = [[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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this