This site requires JavaScript, please enable it in your browser!
Greenfoot back
jeromesam
jeromesam wrote ...

2013/1/28

Detecting multiple isKeyDown

jeromesam jeromesam

2013/1/28

#
I've created a simple Actor that needs to detect multiple keys depressed at the same time. Namely these keys are "down", "left", "up", "right", and "space". I'm finding that Greenfoot won't detect all combinations of these keys being depressed at the same time. Is there some reason it does not? I tried JDK 6 and JDK 7 on Windows XP. Both exhibit this odd behavior.
moobe moobe

2013/1/28

#
I think the problem is your code, can you show it please?
danpost danpost

2013/1/28

#
Greenfoot will detect as many keys down as you put in your code. The problem is (1) you dealing with each key seperately and are not using 'else' between the checks, (2) your checks are not in the proper order, or (3) you are not being specific enough in your checks. The best way to handle multiple checks is to put them in groups as far as their function. If the arrow keys all control the movement of the actor, then do something like the following:
int dx=0, dy=0;
if(Greenfoot.isKeyDown("left") dx--;
if(Greenfoot.isKeyDown("right") dx++;
if(Greenfoot.isKeyDown("up") dy--;
if(Greenfoot.isKeyDown("down") dy++;
When execution reaches the end of this snippet, if both 'left' and 'right' are down or if both 'up' and 'down' are down, then the result in 'dx' or 'dy' will be zero; if only one in either set are down, then the results will reflect the direction of movement along its respective axis. There are eight possible directions that can arise from this (not including both being zero, giving no movement -- or change thereof). If only four-way movement is to be permitted, then follow the code with this:
if(dx * dx == 0 && dx + dy != 0) setLocation(getX() + dx, getY() + dy);
I am guessing that the 'space' key is to do some special action for this actor (possibly to shoot), and this can be dealt with seperately.
danpost danpost

2013/1/28

#
BTW, if the keys are being used for something other than what I believed they were for, please specify what the keys will be used for so an appropriate code can be formulated.
jeromesam jeromesam

2013/1/28

#
public class KeyDownDebug extends Actor
{
    private Message message = null;
    
    public void act() 
    {
        if (message == null)
        {
            message = new Message("");
            getWorld().addObject(message, 100, 535);
        }
        
        List<String> keysDown = new ArrayList<String>();
        
        if (Greenfoot.isKeyDown("left"))
        {
            keysDown.add("left");
        }
        if (Greenfoot.isKeyDown("right"))
        {
            keysDown.add("right");
        }
        if (Greenfoot.isKeyDown("up"))
        {
            keysDown.add("up");
        }
        if (Greenfoot.isKeyDown("down"))
        {
            keysDown.add("down");
        }
        if (Greenfoot.isKeyDown("space"))
        {
            keysDown.add("space");
        }
        
        String text = "";
            
        for (String keyDown : keysDown)
        {
            if (text.isEmpty())
                text = keyDown;
            else
                text = text + "," + keyDown;    
        }
        
        message.setText( text );
    }    
}
For example, if I press and hold "up" followed by "left" followed by "space" my Message only says "up,left" rather than the expected "up,left,space". However if I press and hold in this order "right,up,down,space" my message shows the expected "right,up,down,space". Message is an Actor that I got from this discussion group. I can include it's code if you need it. -Jerome
davmac davmac

2013/1/29

#
It's your keyboard. Put simply, keyboards can only handle a limited number of keys pressed at the same time. After a certain point, they won't register new keypresses. Certain key combinations will work, and others won't. Better keyboards can handle a larger number.
jeromesam jeromesam

2013/1/29

#
OK, I was afraid of that. It's an oldish Dell laptop. Thanks davmac for your answer.
jeromesam jeromesam

2013/1/29

#
I'm using 4 keys for direction and 1 for shoot. Turns out the 'w','a','s','d','space' keys work well for all combinations I need. My son says these keys are standard with many games such as Minecraft. They work on my laptop. So I've replaced using the arrow keys with these instead. 'w' = up, 'a' = left, 's' = down ,'d' = right ,'space' = shoot
You need to login to post a reply.