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

2017/5/3

Undo Key

ironphoenix20 ironphoenix20

2017/5/3

#
Hi, I have an undo and redo button in my simulation that undos and redos when clicked on? Is there a simple way or does anyone have code that I can use to let user click Ctrl+z or Ctrl+y to undo and redo as well? Thanks for any code or links you can provide. Also, as a request, if you are going to post any code also include any classes that I will need to import.
danpost danpost

2017/5/3

#
If you use the following fields to track the state of the 'y' and 'z' keys:
private boolean yDown;
// and
private boolean zDown;
in the classes you check for clicks on their respective buttons in, you can then use the following in the act method, or a method it calls (code is for 'undo' button):
if (Greenfoot.mouseClicked(this) || (!zDown && Greenfoot.isKeyDown("z") && Greenfoot.isKeyDown("control"))
{
    zDown = true;
    // button action code goes here
}
if (zDown && !Greenfoot.isKeyDown("z")) zDown = false;
ironphoenix20 ironphoenix20

2017/5/3

#
This code is not working (does not provide the functionality I need). Are you sure this is correct because everywhere else I searched it talked about Key Events and other complicated things.
danpost danpost

2017/5/3

#
ironphoenix20 wrote...
This code is not working (does not provide the functionality I need). Are you sure this is correct because everywhere else I searched it talked about Key Events and other complicated things.
Show the code you tried (with context -- show the entire class where used) and you say does not work. Explain how it is not working properly. BTW, greenfoot eliminates a lot of the nitty-gritty technical stuff by providing simple methods you can use from the Greenfoot class.
You need to login to post a reply.