Program not recognising key presses
Hey guys, new on here but I did post an introduction in the new members place :)
OK, so my problem, I'm working on what is to be a bit of a basic game engine, not really for much in particular, just an assignment I have for my final year at university. A lot of what I have so far works OK, but for some reason key presses are not being recognised, maybe because I'm doing it in a silly way I realise or I just made a simple error, and its one of those things I've sat staring at for so long now that I cant see what's wrong.
This is the keyAdapter I made, I used it as a bit of an interface so I could add different control schemes later on
Code :
@Override
public void keyPressed(KeyEvent e)
{
keys.add(e);
}
@Override
public void keyReleased(KeyEvent e)
{
remove(e);
}
private void remove(KeyEvent e)
{
for(int i = 0; i < keys.size(); i++)
{
if(keys.get(i).getKeyCode() == e.getKeyCode())
{
keys.remove(i);
}
}
}
Here is a part of a controls class that actually states what keys are and what they do, I did try using if(keys.contains(KeyEvent.VK_MINUS)) to just check if that keyEvent is stored but that didn't work for some reason
Code :
if(isPressed(KeyEvent.VK_MINUS))
{
GlobalVariables.numBirds--;
}
private boolean isPressed(int e)
{
ArrayList<KeyEvent> keys = Input.get().getKeys();
for(int i = 0; i < keys.size(); i++)
{
if(keys.get(i).getKeyCode() == e)
{
return true;
}
}
return false;
}
hmm, cant think what else may be the problem, if anyone else has any ideas please say and Ill get whatever code you wish to see
Thank you for your time regardless of whether you know what my problem is or not :)
Re: Program not recognising key presses
Please make a small, complete program that compiles, executes and shows the problem. What you posted can not be compiled and executed to show the problem.
Using key listeners can have a problem with which component has the focus. Key binding is a better way to catch key presses.
Re: Program not recognising key presses
OK I think rather stupidly I left out the part where I actually add the key listener... I'm working on it now because that's what I know and with lack of time it may take a while to change things but Ill get around to it at a later date, while it was my stupidity causing this error, thank you norm for sorta making me remember somehow :)