javamobilejava-memidlet

Java - Is it possible to make it listen for specific keypress order?


I'm trying to implement a cheat code for a simple paddle game, the cheat should ONLY activate when the specific order of key is pressed: UP, UP, DOWN, DOWN, LEFT, LEFT, RIGHT, RIGHT. I'm trying, but I can't get it!!

    if(up >= 2){
        if(down >= 2){
            if(left >= 2){
                if(right >= 2){
                    cheat = true;
                    g.setColor(0x00FF0000);
                    g.fillRect(x, y, canvas.getWidth(), 5);
                    g.fillRect(x, y - 5, 5, 5);
                    g.fillRect(canvas.getWidth() - 5, y - 5, 5, 5);
                    x = 0;
                    width = canvas.getWidth();
                    height = canvas.getHeight();
                }else{
                    g.setColor(0x00FF0000);
                    g.fillRect(x, y, width, height);
                    g.fillRect(x, y - height, height, height);
                    g.fillRect(x + width - height, y - height, height, height);
                    System.out.println(up + " " + down + " " + left + " " + right + "right");
                }
            }else{
                g.setColor(0x00FF0000);
                g.fillRect(x, y, width, height);
                g.fillRect(x, y - height, height, height);
                g.fillRect(x + width - height, y - height, height, height);
                right = 0;System.out.println(up + " " + down + " " + left + " " + right + "left");
            }
        }else{
            g.setColor(0x00FF0000);
            g.fillRect(x, y, width, height);
            g.fillRect(x, y - height, height, height);
            g.fillRect(x + width - height, y - height, height, height);
            left = 0; right = 0;System.out.println(up + " " + down + " " + left + " " + right + "down");
        }
    }else{
        g.setColor(0x00FF0000);
        g.fillRect(x, y, width, height);
        g.fillRect(x, y - height, height, height);
        g.fillRect(x + width - height, y - height, height, height);
        down = 0; left = 0; right = 0;System.out.println(up + " " + down + " " + left + " " + right + "up");
    }

Program is being updated every 50ms. The up, left, down, right are the keylisteners, when those keys are pressed, it gets added like a counter. Ignore the whole g.fillRect and g.setColor and the System.out(was trying to see how it acted and how to solve the problem on my own). When the user doesn't enter the correct order of keys, the counters should reset, but since the update is every 50ms, it screws up the counter.

Is there a way to detect a specific order of keypresses or it's just not possible?


Solution

  • You can do something like this

    int cheat = 0; //keeps track of how many were pressed in order
    
    if (cheat == 0 && upButtonPressed)
        cheat++;
    elseif (cheat == 1 && upButtonPressed)
        cheat++;
    elseif(cheat == 2 &&  downButtonPressed)
        cheat++;
    //rest of the order
    else
        cheat = 0;
    
    if (cheat == 8) //or however many buttons need to be pressed
        //do whatever you want to happen when the cheat is activated