0

I'm working on an RTS style webapp in processingJS, meaning that there is a little minimap that represents a larger map, which the user can only see a small part of at any given moment. I just added the ability to use arrow keys to navigate the map, i.e.:

    void keyPressed(){
    if(key == CODED){
        switch(keyCode){
            case(UP): //go up
            case(DOWN): //go down , etc

However, I'd like to be able to let a user move diagonally by pressing a combination of two arrow keys. Right now, it seems like this is impossible to do, seeing as how "keyCode" can only seem to hold one value at a time.

Does anybody know any workarounds to this issue?

Best,
Sami

thisissami
  • 15,445
  • 16
  • 47
  • 74
  • An effective approach is outlined here: http://stackoverflow.com/questions/5203407/javascript-multiple-keys-pressed-at-once – Xenethyl Apr 13 '12 at 22:48

3 Answers3

0

As @Xenethyl pointed to in his link in the comments, one way to get around this is by keeping tracks of when a key is pressed and then watching for when the key is released. It is safe to assume that a key is held down in the period of time in between those two events.

thisissami
  • 15,445
  • 16
  • 47
  • 74
0

I would just do the following in javascript:

    document.onkeydown = keydown; 

    function keydown(evt) { 

    if (!evt) evt = event; 

    if (evt.ctrlKey && evt.altKey && evt.keyCode==115) {

        alert("CTRL+ALT+F4"); } 

    else if (evt.shiftKey && evt.keyCode == 9) { 

        alert("Shift+TAB"); }  

    } 
-1

I haven't tested this - but have you tried:

 void keyPressed(){
      if(key == 'a' && key == 'b'){
           println("this just happened");
      }
 }

If this works you can find the ascii values for the arrow keys instead of using key CODED.

Robbie
  • 563
  • 7
  • 19