Get keystrokes in J2ME with GameCanvas

I would like to get, for example, pressing the 3 ( KEY_NUM3) key .
I tried getKeyStates, but it only detects game action keys.
How can I get key states without a game?
(I redefined the functions keyPressedand the keyReleasedCanvas and saved the key states in the array (I use Vectorfor storage, but I think I could store them in the array if this is a problem), but this does not seem very nice)

+3
source share
2 answers

when a key keyCodeis pressed, it is transmitted like this

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;
    }
}
+3
source

, -

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

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

//you can set the super() to true in the constructor
-1

Source: https://habr.com/ru/post/1726596/


All Articles