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

2011/11/18

Greenfoot.isKeyDown

bourne bourne

2011/11/18

#
If I have code somewhere that has something to do similar to Greenfoot.isKeyDown("5"), it becomes unwanted when this code is not reached for arbitrary reasons and when it is reached again and it returns true when it only does so because during the period of time the code is not reached the key is pressed but now is not pressed. So I have to do something like the following at the end of somewhere to clear up everything for (int i = 0; i < 10; i++) if (Greenfoot.isKeyDown(i + "")) {} But there are many other keys other than numbers and letters that can be traversed easily like the above loop. It would be nice if there was a method to dump all the states for this method. (Or for it to recognize it can clear it all after the current step)
bourne bourne

2011/11/19

#
This is what I have to do to include everything.
for (int i = 0; i < 10; i++)
    if (Greenfoot.isKeyDown(i + ""))
    {}
for (int i = 'a'; i <= 'z'; i++)
    if (Greenfoot.isKeyDown((char)i + ""))
    {}
for (int i = 1; i < 13; i++)
    if (Greenfoot.isKeyDown("F" + i))
    {}
for (String s = "; ' [ ] ` / \ , . - = up down right left control shift enter space tab escape backspace "; s.length() > 0; s = s.substring(s.indexOf(" ") + 1))
    if (Greenfoot.isKeyDown(s.substring(0, s.indexOf(" "))))
    {}
davmac davmac

2011/11/19

#
Yes, I can see that it is annoying. We'll work on a proper fix for the next release. An easier workaround for the moment is just to check the key status twice, and only treat the key as pressed if it returns true both times. I.e. instead of: if (Greenfoot.isKeyDown("a")) { Do: if (Greenfoot.isKeyDown("a") && Greenfoot.isKeyDown("a")) { It looks a bit funny of course.
bourne bourne

2011/11/19

#
Thanks davmac.
You need to login to post a reply.