views:

73

answers:

3

Hi,

I would like to store all currently-pressed keycodes in a single variable by using bitwise operations when the key is pressed and when the key is released.

I'm not sure how to properly use bitwise operations, but I know this will be very simple to someone who does.

Once complete, it should be simple to see which key is currently depressed by asking "is this key's code in the variable?"

Thanks in advance!

+5  A: 

This is technically impossible to do in a single variable, no datatype in javascript can store the 256 bits required to hold the bitmask (which supports bitwise operations), you will need to use an array instead.

Also, unless you have text to speech software which you've macroed to magically execute js functions for you, asking your computer: "is this key's code in the variable?" won't do squat.

The way you would do it would be to initialize an array with 256 indexes, and then when a key is pressed, you find the relevant index and set it to true and when a key is released, you set it to false

It's the only way to do it. There actually isn't any other.

Andrew Dunn
Well, you could technically have an array of 9 numbers and still use bitwise operations... it'd be silly though.
Matti Virkkunen
You could, but RAM isn't exactly a precious resource nowadays :S
Andrew Dunn
A: 

javascript bit operations are generally reliable for the first 31 bits, and things go downhill from there. So, you can 31 different keys which you have given a value to act as a flag. for instance, if you wanted to track the four arrows and A and B, you would do something like this.

var KEYS = { 
    LEFT:  1 << 0,
    UP:    1 << 1,
    RIGHT: 1 << 2,
    DOWN:  1 << 3,
    A:     1 << 4,
    B:     1 << 5
}

var flags = 0;

myElement.addEventListener ('keydown', function (e) { 

    switch (e.keyCode) {

        case 37: // left
        flags = flags | KEYS.LEFT;
        break;
        case 38: // up
        flags = flags | KEYS.UP;
        break;

        ... etc ...

    }

}

} , false);

function checkKeys () {

    if ( (flags & KEYS.LEFT) === KEYS.LEFT)
        alert('LEFT key pressed');
    }

    if ( (flags & KEYS.UP) === KEYS.UP )
        alert('UP key pressed');
    }

    ... etc ...

}
lincolnk
+3  A: 

I don't think that it is possible the way you want to do it. Have a look at the available keycodes. There you see, that e.g. backspace is 8 and tab is 9.

In binary, that would be 1000 and 1001. Using binary operators, you would use OR | to "combine" the values, which would result in 1001.

You would check if a value is set via AND &, e.g. 1001 & 1000 to see, if the backspace key was pressed. Unfortunately, this would also evaluate to true if only the tab key was pressed (as its value is 1001).

That said, you can only use such bitwise comparison techniques, if the different values you want to test are powers of 2 only, i.e. 1, 2, 4, 8, 16, 32 and so on, as this represents in binary 1, 10, 100, 1000,...

For example if we can have a status variable and possible statuses would be OPEN = 2, LIGHT ON = 4 and ALARM ON = 8.
Assume that it is OPEN and LIGHT ON, i.e.

  0010
| 0100
------- 
  0110

Here we can easily check whether the ALARM is on, be using AND: 0110 & 1000 = 0. But if we would encode ALARM ON with 6 = 0110, we could not check this.


What you could do is, to map the key codes to a some power of 2 value and apply binary operations there. The Wikipedia article about bitmasks might be worth reading.

I hope my explanation was somehow clear.

Felix Kling