Reputation: 527
I have been creating a test program to test how movement on an angle may possibly work, and even though this is not where my problem lies, it is the program to which my problem/confusion is in. This error(?) does not affect the way my program works unless a user wants to key mash every key to see what happens and that is exactly what I did...
I have a key listener which, on each key press and key release, sets a boolean in an array:
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
int a = KeyEvent.VK_LEFT; // int value of 37
int b = KeyEvent.VK_UP; // 38
int c = KeyEvent.VK_RIGHT; // 39
int d = KeyEvent.VK_DOWN; // 40
int aa = KeyEvent.VK_G; // 71
int bb = KeyEvent.VK_H; // 72
if(key == a || key == b || key == c || key == d) {
player.setKeyValue(key - 37, true); // keyReleased is exactly the same but with a false
}
if(key == aa || key == bb) {
player.setKeyValue((key - 71) + 4, true);
}
}
I had my panel draw which keys were getting pressed, and I noticed that not all the keys I were pressing were getting drawn onto the screen. I was majorly confused thinking that it had something to do with the buffered image being too small, so I double checked that and strangely it wasn't. I decided to print all booleans in the array to the console and I found out that, indeed, some keys were actually not getting pressed when physically they were.
Here is some of the output I had received when pressing different keys together:
// booleans are in order: Left, up, right, down, g, h
true, false, true, false, false, true
true, false, true, false, true, true
true, false, true, false, false, false
I'm not sure why this is happened, but I'm getting the feeling that it has something to do with the amount of memory the JVM can hold at a given time.
Though this poses no real obstacle for me, it would be nice to know how and why this occurs?
Upvotes: 0
Views: 164
Reputation: 30230
I seriously doubt it has anything to do with the amount of memory the JVM can hold at a given time.
It's hard to say what the best approach is to do what you want.
Consider something like:
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
int keyIndex = -1;
switch(key)
{
case KeyEvent.VK_LEFT: keyIndex = 0; break;
case KeyEvent.VK_UP: keyIndex = 1; break;
case KeyEvent.VK_RIGHT: keyIndex = 2; break;
case KeyEvent.VK_DOWN: keyIndex = 3; break;
case KeyEvent.VK_G: keyIndex = 4; break;
case KeyEvent.VK_H: keyIndex = 5; break;
default:
System.out.format("Unexpected KeyCode [%d]\n", key);
break;
}
if(keyIndex != -1){ player.setKeyValue(keyIndex, true); }
}
This approach gets rid of a lot of the cruft, and also attempts to notify you if you're getting unexpected keys. Without knowing more, it's hard to give you a better answer, but the one I proposed certainly isn't the best -- it's just a step in debugging. My guess is something like an ArrayList of KeyEvents or Integers (corresponding to KeyCodes) would probably be a better choice.
You could edit this to always display the keycode as well, so you could immediately see what keycode was generated.
Upvotes: 5