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

2013/8/20

'Nother Q.

8bitcarrotjuice 8bitcarrotjuice

2013/8/20

#
How do you use key_pressed in a if?
Gevater_Tod4711 Gevater_Tod4711

2013/8/20

#
if (Greenfoot.isKeyDown("space")) {
    //key is pressed;
}
Also any other key name will work.
Kartoffelbrot Kartoffelbrot

2013/8/20

#
Another possibility is the getKey() method:
if("space".equals(Greenfoot.getKey())){}
This method works only one time per act. If you want to check more often on it in one act you have to do it this way:
String key = Greenfoot.getKey();
if("space".equals(key)){
//do something
}else{if("enter".equals(key)){

}
SPower SPower

2013/8/21

#
you have a { too much between else and if in the fourth line :)
8bitcarrotjuice 8bitcarrotjuice

2013/8/21

#
I ment the Java KeyListener keyPressed... I'm not a complete noob to not know how to use the greenfoot's one. :}
Gevater_Tod4711 Gevater_Tod4711

2013/8/21

#
To use a java.awt.event.KeyListener in an if clause is not so easy like in Greenfoot. When you add a KeyListener to a JFrame (or any other gui) this KeyListener can override the methods keyPressed(KeyEvent), keyReleased(KeyEvent) and keyTyped(KeyEvent). Now this methods will be called when a key is pressed, released or typed. But you can't check whether there is a key currently pressed (not directly). You can check which key is pressed because the key can be identified by the KeyEvent (using the methods getKeyChar() or getKeyCode()). But to check it like in greenfoot you need to create some supporting stuff. I created such an support in my Greenfoot Full Screen Demo scenario (in the classes GreenfootKeyListener and FullScreenWindow) if you want to see an example. But probably ther is an easyer way to do help you with your problem. What exactly do you want to do with the KeyListener?
8bitcarrotjuice 8bitcarrotjuice

2013/8/21

#
I was just wondering, since I am new to java I want as much information as I can get. Thanks tod!
8bitcarrotjuice 8bitcarrotjuice

2013/8/21

#
TOD, can I use your code(below) instead of the isKeyDown("") method in Greenfoot?
public int getButton() {
        if (mouseEvent != null) {
            return mouseEvent.getButton();
        }
        return -1;
    }
Gevater_Tod4711 Gevater_Tod4711

2013/8/21

#
Well this method doesn't work the same way as the isKeyDown method. This method returns the button number of a mouse event. So it returns 1 if the left mouse button is pressed (in this mouseEvent) 2 if the middle button is clicked ... So the funktion of this method is an other. For a method that works like the Greenfoot.isKeyDown method you should have a look at the isKeyDown method in FullScreenWindow and at the class FullScreenKeyInfo. Also there is a method getKey() in the FullScreenWindow which you could use. It works like the Greenfoot.getKey() method.
8bitcarrotjuice 8bitcarrotjuice

2013/8/21

#
Thanks Tod, I wish I had your coding skills :}
Kartoffelbrot Kartoffelbrot

2013/8/21

#
me too
You need to login to post a reply.