简体   繁体   中英

J2ME Combo key press (multiple keys at once)

I am using keyboard_key variable from here:

    //overrides the function keyPressed from "lcdui.Canvas"
    protected void keyPressed(int keyCode){
        keyboard_key = keyCode;
    }

to detect if any key was pressed on a mobile phone.

But it returns only the key that is pressed most lately and it doesn't tell if any other key might be pressed. Please help!

Btw, I'm using NetBeans 7.0.1 as IDE.

...it returns only the key that is pressed most lately and it doesn't tell if any other key might be pressed

The way you use it in your code snippet, keyboard_key will always contain only the key that is pressed most lately - just because it "has no room" to hold anything more than that.

Consider using Vector to "memorize" different keys that were pressed.

    //define in your class:
    Vector keysPressed = new Vector(); // to keep track of keys pressed

    //overrides the function keyPressed from "lcdui.Canvas"
    protected void keyPressed(int keyCode){
        keysPressed.addElement(new Integer(keyCode));
    }

Side note given the question, you may benefit from studying Java language basics. There are many tutorials available online - just search the web for something like "Java getting started" .

Depending on your application requirements, consider overriding keyRepeated along with keyPressed.


If you intend to handle key presses in game-loop fashion ( "multiple keys at once" suggest that you may possibly have this in mind), consider another option provided by lcdui.game.GameCanvas API, method getKeyStates() :

Gets the states of the physical game keys. Each bit in the returned integer represents a specific key on the device. A key's bit will be 1 if the key is currently down or has been pressed at least once since the last time this method was called. The bit will be 0 if the key is currently up and has not been pressed at all since the last time this method was called. This latching behavior ensures that a rapid key press and release will always be caught by the game loop, regardless of how slowly the loop runs...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM