0

I'm currently working on a "game", in which I'm encountering trouble with the controls. I'm currently using numerical keyCode values (because they seem more efficient and pretty to me), although nothing seems to happen when trying to bind the following keys with the following values: & with 49, é with 50, " with 222.

I got the codes from http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes and these are the first problems I encountered with these.

As a side note, I'm using a Mac and an AZERTY-keyboard.

Thanks in advance,

Actual code:

    void keyPressed() {  
  if (mode == "azerty") {  
    if (key == CODED) {  
      if (keyCode == 38) {  
        keybool[0] = true;  
      }  
      else if (keyCode == 40) {  
        keybool[1] = true;  
      }  
      if (keyCode == 37) {  
        keybool[2] = true;  
      }  
      else if (keyCode == 39) {  
        keybool[3] = true;  
      }  
      if (keyCode == 16) {  
        keybool[4] = true;  
      }  
      if (keyCode == 49) {  
        keybool[5] = true;  
      }  
      if (keyCode == 50) {  
        keybool[6] = true;  
      }  
      if (keyCode == 222) {  
        keybool[7] = true;  
      }  
    }  
  }  
}  
void keyReleased() {  
  if (mode == "azerty") {  
    if (key == CODED) {  
      if (keyCode == 38) {  
        keybool[0] = false;  
      }  
      else if (keyCode == 40) {  
        keybool[1] = false;  
      }  
      if (keyCode == 37) {  
        keybool[2] = false;  
      }  
      else if (keyCode == 39) {  
        keybool[3] = false;  
      }  
      if (keyCode == 16) {  
        keybool[4] = false;  
      }  
      if (keyCode == 49) {  
        keybool[5] = false;  
      }  
      else if (keyCode == 50) {  
        keybool[6] = false;  
      }  
      else if (keyCode == 222) {  
        keybool[7] = false;  
      }  
    }  
  }  
}  
void keyFunc() {  
  if (keybool[0]) {  
    player.move(1);  
  }   
  else if (keybool[1]) {  
    player.move(-1);  
  }  
  if (keybool[2]) {  
    player.turn(-0.5);  
  }  
  else if (keybool[3]) {  
    player.turn(0.5);  
  }  
  if (keybool[4]) {  
  }  
  if (keybool[5]) {  
    player.attack(0);  
  }  
  else if (keybool[6]) {  
    player.attack(1);   
  }  
  else if (keybool[7]) {  
    player.attack(2);   
  }  

}  


void attack(int attackNum) {  
    if (attackNum == 0) {  
     println("SLASH!");  
    } else if (attackNum == 1) {  
     println("STAB!");  
    } else if (attackNum == 2) {  
     println("PUMMEL!");  
    }  
  }  
stinodes
  • 1,269
  • 10
  • 15

2 Answers2

0

Those codes you linked are javascript key codes. Why aren't the Key Event descriptions pretty? VK_QUOTEDBL, VK_AMPERSAND, etc. Readability is important, but not as important as functionality.

dev_feed
  • 689
  • 2
  • 7
  • 25
  • I did try these, but I got the error "*insert key* was not found". – stinodes Feb 05 '14 at 19:08
  • Really? It's in the list: http://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyEvent.html#VK_INSERT – dev_feed Feb 05 '14 at 19:10
  • "Since 2007, Apple Macintosh computers have replaced the insert key with a Fn key to allow for usage of the top row both as "media keys" and as traditional function keys" http://en.wikipedia.org/wiki/Insert_key – dev_feed Feb 05 '14 at 19:17
  • (with insert key, I did not mean the actual "insert" key) – stinodes Feb 05 '14 at 22:31
  • Ahhh ok haha. Well, back to your original question. Does the problem continue when you bind '&', 'é', and ' " ' with values other than the ones you listed? – dev_feed Feb 06 '14 at 11:26
  • P.S. are all of your java installations up to date (including the desktop's version for other programs)? which version does your project build path target? – dev_feed Feb 06 '14 at 11:56
  • I'll try just using the (key == &), I guess? And I'll also check my Java, didn't think about that one. – stinodes Feb 07 '14 at 11:35
0

A much more elegant way to play with keyCodes which also reduces bug-probability (forgotten break:P) is this:

switch (keyCode) {
case 38: // Up
    break;
case 40: // Down
    break;
case 37: // Left
    break;
case 39: // Right
    break;
default:
    System.out.println("case " + keyCode + ": // " + KeyEvent.getKeyText(keyCode) + "\nbreak;");
}

Example output:

case 112: // F1
break;
case 113: // F2
break;
case 69: // E
break;
case 10: // Enter
break;
case 18: // Alt
break;
case 115: // F4
break;

You might want to put that line into a method.

You could also be interested in this method (which uses reflection) that obtains the scancode, a keyboard language layout independent key value, but this only works on Windows. It's quite sad that we don't get simple "This is the hardware key, and that is the number that identifies it." information in Java. :(

https://stackoverflow.com/a/26688875/3500521

Community
  • 1
  • 1
Dreamspace President
  • 1,060
  • 13
  • 33