Search code examples
java-mekeypressmidplcdui

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)


Solution

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