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

2017/6/14

When keypress not after keypress?

Sylva Sylva

2017/6/14

#
Is there a method to register a keypress when the key is pressed (for example an 'up' key) and not after the key is released?
lookaround lookaround

2017/6/14

#
Greenfoot.isKeyDown("up") Do you mean this ?
danpost danpost

2017/6/14

#
You will need to add a boolean field to the class to retain the last state of the "up" key. Comparing that to the current state will catch any change in the state of the key:
// instance field
private boolean upDown;

// in act
if (upDown != Greenfoot.isKeyDown("up")) // was there change in state of key
{
    upDown = ! upDown; // record new state
    if (upDown) // was state changed from 'up' to 'down'
    {
        // do what you do when "up" key is pressed
    }
}
Sylva Sylva

2017/6/15

#
Thanks for the help danpost. It works and feels much better now :D.
You need to login to post a reply.