3

I would like to get whether (for example) the 3 key is pressed (KEY_NUM3).
I have tried getKeyStates but it only detects the game action keys.
How could I get the states of non-game action keys?
(I have overridden the keyPressed and keyReleased functions of Canvas and storing the key states in an array (I'm using a Vector for storing but I think could store them in an array too, if that's the problem), but this does not seem to be very nice)

gnat
  • 6,213
  • 108
  • 53
  • 73
user
  • 6,567
  • 18
  • 58
  • 85

2 Answers2

3

in your keypressed use the keyCode passed in like so

protected void keyPressed(int keyCode)
{
    //try catch  getGameAction as can legally throw an exception
    int gameAction = getGameAction(keyCode);

    switch(gameAction)
    {
        case UP:
            break;
        case DOWN:
            break;
        case LEFT:
            break;
    }

    switch(keyCode)
    {
        case KEY_NUM1:
            break;
        case KEY_NUM2:
            break;
        case KEY_NUM3;
            break;
    }
}
user
  • 6,567
  • 18
  • 58
  • 85
kgutteridge
  • 8,727
  • 1
  • 18
  • 23
-1

I suppose that can be something like the code below

int key=getKeyStates();
// i mean keyStates();
if((key&down_pressed)!=0)
{
//do movements
}

but can be

if((key & Canvas.key_num3)!=0)
{
//do something
}

//you can set the super() to true in the constructor
gnat
  • 6,213
  • 108
  • 53
  • 73
somnus
  • 1