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

2011/7/5

Greenfoot.isKeyDown(any) ???

1
2
3
manster2008 manster2008

2011/7/5

#
?? Please check the last page for a new question... I am making a program which allows me to type a string in a box and I plan to do it as following:
public String msg = "";
...
...
if(Greenfoot.isKeyDown(any**)) {
    msg = msg + Greenfoot.getKey();
}
** This is the problem: I do not know if there is a any in Greenfoot which allows me to know if any key is being pressed. I would greatly appreciate to know if this is possible. Thank you...
manster2008 manster2008

2011/7/5

#
Is it possible to remove questions? I have figured out how to do this by looking at a program made by another member. I have realized that the Greenfoot.getKey() only returns a string, if a key is pressed. I believed that it would always return the key that was last pressed. I have used the following code:
String key = Greenfoot.getKey();
if(key != null) {
    if(key == "space") {
        key = " ";
    }
    msg = msg + key;
}
GameCode GameCode

2011/7/5

#
You have to write:
Greenfoot.isKeyDown(null)
In this case you can use null instead of 'any'
mjrb4 mjrb4

2011/7/5

#
Is it possible to remove questions?
Just because a question is solved doesn't mean it should be removed, it's good to have an archive of such threads for other people who might be stuck on similar issues! Also bear in mind that the above code won't account for the arrow keys and the function keys which will put "up", "down", etc. (the name of the key) in the box unless you change the functionality to something else.
manster2008 manster2008

2011/7/5

#
Thanks, I have made a "backspace" command, and I will now fix it so it does not read the cursor keys and etc.
manster2008 manster2008

2011/7/5

#
Also thanks GameCode, for that: I didn't even think of using null...
davmac davmac

2011/7/5

#
I don't think it's correct that using null as a parameter will tell you if any key is down. The API documentation is pretty clear: you have to provide the name of a key.
GameCode GameCode

2011/7/5

#
@davmac Just tested it. You're Right @manster2008 Sorry, I for my mistake, this will work:
 if(Greenfoot.getKey()!=null)
 {
            ...
 }
danpost danpost

2011/7/5

#
I had just written some code a few days ago; it is generalized here.
public final String LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public final String letters = "abcdefghijklmnopqrstuvwxyz";
public final String Numbers = "0123456789";
public final String Symbols = "`~!@#$%^&*()_+-=[]{}|;':<>?,./";
public String txtString = "";

public void act()
{
    String myKey = Greenfoot.getKey();
    if (myKey == null) { return; }
    String myText = "";
    if (myKey == "space") { myKey = " "; } // If spaces are not wanted, change 'myKey = " ";' to 'return;'
    if (myKey == "backspace")
    {
        // Code for when 'backspace' is pressed
        if (txtString.length() > 0)
        {
            txtString = txtString.substring(0, txtString.length() - 1);
        }
        return;
    }
    if (myKey == "enter")
    {
        // Code for when 'enter' is pressed
        return;
    }
    // You can continue for other keys: arrow keys, function keys, 'tab' and 'escape' (and any others I may have missed)

    // Change the concatenation in the following statement to only wanted characters
    String goodChars = LETTERS + letters + Numbers + Symbols + " ";
    int myIndex = goodChars.indexOf(myKey.charAt(0));
    if (myIndex > -1)
    {
        myText = goodChars.substring(myIndex, myIndex + 1);
        txtString = txtString + myText;
    }
}
Hope it helps!
mjrb4 mjrb4

2011/7/6

#
I haven't run the above code - but even if nothing else is wrong you're comparing strings with == which is wrong (as in it's highly likely to return false even when they are equal.) So instead of:
 if (myKey == "backspace")  
(and every other occurrence like this) it should be:
 if ("backspace".equals(myKey))  
danpost danpost

2011/7/6

#
Now, that I was NOT aware of! Though, I did know that sometimes (usually with Strings of length only of one) the comparison seems to fail. I will go through my work and make the appropriate corrections.
mjrb4 mjrb4

2011/7/6

#
It's to do with the underlying difference between == and .equals(), the former compares whether the two objects are exactly the same object whereas the latter says whether they're meaningfully equal (even if they're two different objects.) You can have two separate string objects that have exactly the same contents - in this case .equals() will return true (almost always what you want) and == will return false. Sometimes == does appear to work, but if it does it's just a side effect of some of the optimisation Java makes under the hood (there's all sorts of rules you can look up if you like; all you really need to know though is that .equals() should always be used with strings.) One other point, it's generally good habit (well, I think it is anyway) to put the literal first in such comparisons, i.e:
"str".equals(myStr);
not:
myStr.equals("str");
Why? If myStr happens to be null then the first will return false, the second will throw an exception at runtime (since you're trying to call a method on null, not a string object.)
danpost danpost

2011/7/6

#
@mjrb4: Your input was appreciated. Thank you for pointing out that which you consider a good habit. I will try to remember that, and follow in your footsteps in that regard. (though, in the code above, it has been dealt with by line # 10
if (myKey == null) { return; }
which ensures that 'myKey' is not null when making further comparisons, which, of course, should still be written with '.equals()' and not '=='). Thanks, again.
mjrb4 mjrb4

2011/7/6

#
which ensures that 'myKey' is not null when making further comparisons, which, of course, should still be written with '.equals()' and not '==').
No problem! This is the one exception - if you're comparing to null you can safely use ==. In fact you can't safely use equals() here since if myKey is indeed null, you'll be trying to call a method on it which will cause a NullPointerException.
danpost danpost

2011/7/6

#
Out of curiousity: Is there an 'isNull()' method? to return a boolean value. if (!myKey.isNull()) { ...code here... }
There are more replies on the next page.
1
2
3