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

2013/4/2

isKeyDown problem

Game/maniac Game/maniac

2013/4/2

#
how do you check if any key is down
JetLennit JetLennit

2013/4/2

#
I'm guessing
if (Greenfoot.isKeyDown())
Game/maniac Game/maniac

2013/4/2

#
You have to have a string
Gevater_Tod4711 Gevater_Tod4711

2013/4/2

#
if (Greenfoot.getKey() != null) {
   //a key is pressed;
}
JetLennit JetLennit

2013/4/2

#
I was close... (I was just guessing)
danpost danpost

2013/4/2

#
You have to check each key individually:
if (Greenfoot.isKeyDown("a") // code for 'a' down
if (Greenfoot.isKeyDown("b") // code for 'b' down
// etc.
You can check for ANY key being released with
if (Greenfoot.getKey() != null)
Game/maniac Game/maniac

2013/4/2

#
Gevater_Tod4711 wrote...
if (Greenfoot.getKey() != null) {
   //a key is pressed;
}
It works but I need to check if a key is down not pushed
Gevater_Tod4711 Gevater_Tod4711

2013/4/2

#
I think if this doesn't work you unfortunatly will have to check every key using Greenfoot.isKeyDown.
JetLennit JetLennit

2013/4/2

#
would an array list that uses a for loop work?
danpost danpost

2013/4/2

#
I guess you could condense the code a little:
String keys = "abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()_-+={[}]|\\<,>.?/`~";
String[] specialKeys = { "enter", "escape", "space", "backspace", "tab", "shift", "control",  "up", "down", "left", "right", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12" };
int i = 0;
for (i=0; i<keys.length(); i++) if (Greenfoot.isKeyDown(keys.substring(i, i+1))) break;
boolean keyFoundDown =  i < keys.length();
if (!keyFoundDown)
{
    for (i=0; i<specialKeys.length; i++) if (Greenfoot.isKeyDown(specialKeys[i])) break;
    keyFoundDown = i < specialKeys.length;
}
if (keyFoundDown) // do whatever.
Game/maniac Game/maniac

2013/4/2

#
thanks :)
You need to login to post a reply.