質問

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!");  
    }  
  }  
役に立ちましたか?

解決

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

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top