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

2012/12/24

Is there a "keyPressed" method in Greenfoot?

1
2
Minion1 Minion1

2012/12/24

#
I've been looking through the API, and I can't seem to find a method that checks to see if a key has been pressed. Sure there's the obvious "isKeyDown" method, but that doesn't serve. I just need the computer to register the press of a particular key one time, not check to see if it's down. This seems like a silly thing to bring up here, but I can't seem to find anything in the API (by the way, my search skills suck).
actinium actinium

2012/12/24

#
Your looking for the getKey() method in the Greenfoot class i think.
Minion1 Minion1

2012/12/24

#
getKey checks the last key that was pressed. Not sure If I can use that the way I want to. It doesn't take any parameters, it merely returns the last key pressed, so... Let's see.. getKey will return "M" if that was the last key I pressed, then I have to set up a boolean to check if "M" was pressed, and if true then implement the statement.. nope. Didn't work..
Minion1 Minion1

2012/12/24

#
Here is the code. I'm trying to make it so that the user can turn the music in the game on or off by pressing the "M" key. if(Greenfoot.getKey() == "M" && music.isPlaying()) { music.pause(); } else if(Greenfoot.getKey() == "M" && !music.isPlaying()) { music.playLoop(); }
actinium actinium

2012/12/24

#
if you want any key check if its not null
Minion1 Minion1

2012/12/24

#
I'm looking for a way to have the program check for the pressing of the "M" key specifically. It kind of blows my mind that a way to check if a single key has been pressed doesn't already exist in Greenfoot.
nooby123 nooby123

2012/12/24

#
This is a function that returns a true or false if a specific key has been pressed or not.
Greenfoot.isKeyDown("m");
You can replace "m" with any other letter or action key such as "up", "space", "down", "right", "left".
Minion1 Minion1

2012/12/24

#
Dang it, this is the problem with programming. Lol. Everything is moving along swimmingly and then "BAM!" ..some miniscule problem halts everything. It's frustrating as all heck. So why am I still having fun?
nooby123 nooby123

2012/12/24

#
If you want to put it in to use :
if(Greenfoot.isKeyDown("m")) //what ever code you want to put in here
;
Minion1 Minion1

2012/12/24

#
nooby123. Thanks, but the "isKeyDown" method only checks to see if the key is currently down. It doesn't do anything else. I want my music to stop and start at the touch of a key, not continue playing only if the "M" key is still down.
nooby123 nooby123

2012/12/24

#
Oh then here. Make a boolean that shows if the music is playing or not.
boolean music_playing = true;
GreenfootSound music = new GreenfootSound("what ever sound you're playing");

if(Greenfoot.isKeyDown("m")) music_playing = false;

if(music_playing) music.play();
else music.stop();
Minion1 Minion1

2012/12/24

#
Well, thanks again, but it doesn't seem to be working. Truth is, I can solve this problem easily by just using two different keys instead of one. I guess for the time being I'll use that route. I googled the problem,and for the most part the results were somewhat difficult to understand, and they all used methods I'm unfamiliar with. I really need to take some time to work out how to go "cold turkey" without Greenfoot. Thanks everyone.
actinium actinium

2012/12/24

#
Your using the getKey() method wrong, getKey() returns a string therefore you should do,
String key = Greenfoot.getKey()

if (key == "m")
ect
actinium actinium

2012/12/24

#
 public void checkKey()
    {
        String key = Greenfoot.getKey();
        
        if (key != null)
        {
            if (key.equals("m"))
            {
                System.out.println("here");
                sound.stop();
            }
        }
    }
This seems to work
danpost danpost

2012/12/25

#
This will also work:
 public void checkKey()
{
    String key = Greenfoot.getKey();
    if ("m".equals(key))
    {
        System.out.println("here");
        sound.stop();
    }
}
As long you are not calling 'equals' on a null, you will not get an error. It matters not what is returned from inside the parenthesis '( )'. In other words, no error will ensue with
"any valid string".equals(null)
There are more replies on the next page.
1
2