user
user

Reputation: 6797

Get pressed keys in J2ME with GameCanvas

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)

Upvotes: 3

Views: 3959

Answers (2)

somnus
somnus

Reputation: 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

Upvotes: -1

kgutteridge
kgutteridge

Reputation: 8981

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;
    }
}

Upvotes: 3

Related Questions