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

2012/5/21

Greenfoot.isKeyDown()

darkmist255 darkmist255

2012/5/21

#
Quick question, what String represents the numbers on the number pad, what represents the Alt key?
kartikitrak kartikitrak

2012/5/21

#
I'm wondering the same thing. For some reason Greenfoot.isKeyDown("1") doesn't work for me.
Loopy Loopy

2012/5/22

#
Along the same lines what represents the "enter" key?
davmac davmac

2012/5/22

#
darkmist255: those are multiple questions :p Numpad numbers should be either the number ("1", "2", "3" etc) or the arrow key ("left", "right", "up", "down") depending on numlock state. Numpad keys without an arrow (eg 9/PgUp) cannot be reliably detected when numlock is off. However, Java's key handling seems to have changed recently and so this mightn't work properly in the current Greenfoot version; I just committed a fix for a future version. There is no support for the alt key. See the documentation for the the Greenfoot class. Loopy: the "enter" key is represented by "enter". Again, see the documentation.
danpost danpost

2012/5/22

#
@darkmist255, "1" will work, but only for the "1" above the letter keys (not for the numberic keypad) @Loopy, you got it right: "enter" There are 10 keys (at least, as far as keys that there could possibly be of use in detecting) that cannot be detected. These are: "tab", "alt", "F10", "home", "end", "pgdn", "pgup", "ins", "del", and "\" The only keys that work on the numberic keypad are the direction arrow keys and the "enter" key; all which are duplicated keys (at least on my keyboard). The good news in all this is: with "shift" and "control" being detectable keys, they are, for almost any scenario, enough different key combinations to cover a multitude of events. The following is a list of all detectable keys: the letter keys: "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", and "z" the number keys (across the top of the keyboard): "1", "2", "3", "4", "5", "6", "7", "8", "9", and "0" the arrow keys: "up", "down", "left", and "right", the function keys (all but "F10"): "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F11", and "F12" the keys that produce, when "shift" is not pressed, these symbols: "-", "=", "", ";", "'", ",", ".", and "/" the following text entering keys: "space", "backspace", and "enter" the following key modifiers: "shift", and "control" and, finally, "escape"
danpost danpost

2012/5/22

#
@davmac, I did it again. I did not refresh before posting. Though, this one took a little while and I really did not want to just scratch it by editing it out! I do know that not all keyboards are the same; so, there might be some minor differences between what I can detect with mine and others can detect with theirs.
kartikitrak kartikitrak

2012/5/23

#
Could someone tell me why this doesn't work? I know this isn't my discussion, but it was very close to a problem I'm currently having. Quick Overview When the user scrolls their scroller on their mouse the weapons change(this works fine without numbers). I also want to include the feature to use the top row of numbers for users who don't have a scroller. I used a isKeyDown("1") method in an if statement that sets the weapon number to 1 but it does not register and if it is registering the 1st weapon isn't showing up and working.
 private void ChangeWeapon()
    {
        if (Mouse.getScroll()==0)
        {
            return;
        }
        previousWeaponNumber = weaponNumber;  //stores the weapon that was previous used.

        weaponNumber-=Mouse.getScroll();  //gets scrolling number
        Mouse.ResetScroll();  
        
        if (Greenfoot.isKeyDown("1"))
        {weaponNumber=1;}
        if (Greenfoot.isKeyDown("2"))
        {weaponNumber=2;}
        if (Greenfoot.isKeyDown("3"))
        {weaponNumber=3;}
       
        if (weaponNumber<=0)
        {
            weaponNumber = 1;
        }
        if (weaponNumber >= 3)
        {
            weaponNumber = 3;
        }

        if (weaponNumber == 1)
        {
            removePreviousWeapon();
            addObject(pistolWeapon,425, 605);
        }
        if (weaponNumber == 2)
        {
            removePreviousWeapon();	
            addObject(starbursterWeapon,425, 605);
        }
        if (weaponNumber == 3)
        {
            removePreviousWeapon();
            addObject(laserWeapon,425, 605);
        }
    }
Builderboy2005 Builderboy2005

2012/5/23

#
You are returning whenever the scroll number is zero. Consequently even when a key is being pressed, the routine decides to quit early because it sees the scroll number is zero.
kartikitrak kartikitrak

2012/5/23

#
Thanks. I moved the isKeyDown part before the returning of the zero and also moved the addObject part to the top and it works flawlessly. Thank you.
darkmist255 darkmist255

2012/5/23

#
Thanks, I had a feeling that Java just didn't like the numpad. I'll avoid these keys in the future.
You need to login to post a reply.