# Undo and Redo Button

**URL:** <https://discourse.processing.org/t/undo-and-redo-button/27301>\
**Category:** Coding Questions\
**Created:** [January 23, 2021, 5:35am UTC](https://discourse.processing.org/t/undo-and-redo-button/27301 "2021-01-23T05:35:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prxt.3k](https://avatars.discourse-cdn.com/v4/letter/p/9e8a1a/32.png) [@Prxt.3k](https://discourse.processing.org/u/Prxt.3k)\
**Post date:** [January 23, 2021, 5:35am UTC](https://discourse.processing.org/t/undo-and-redo-button/27301/1 "2021-01-23T05:35:09Z")

</div>

```auto
// Press CTRL+Z to undo, CTRL+SHIFT+Z to redo 
// We need these to know if CTRL/SHIFT are pressed
boolean controlDown = false;
boolean shiftDown = false;
 
Undo undo;
 
void setup() {
 size(500, 500);
  background(255);
  undo = new Undo(10);
}
void draw() {
  // Our two line drawing program
  if (mousePressed)
    line(mouseX, mouseY, pmouseX, pmouseY);
}
void mouseReleased() {
  // Save each line we draw to our stack of UNDOs
  undo.takeSnapshot();
}
 
void keyPressed() {
  // Remember if CTRL or SHIFT are pressed or not
  if (key == CODED) {
    if (keyCode == CONTROL)
      controlDown = true;
    if (keyCode == SHIFT)
      shiftDown = true;
    return;
  }
  // Check if we pressed CTRL+Z or CTRL+SHIFT+Z
  if (controlDown) {
    if (keyCode == 'Z') {
      if (shiftDown)
        undo.redo();
      else
        undo.undo();
    }
    return;
  }
  // Check if we pressed the S key
  if (key=='s') {
    saveFrame("image####.png");
  }
}
 

```

I found this code for Undo and Redo… But redo doesn’t seem to work and when I put them as a button, and none of them seem to work which is really confusing. If some as tried them as buttons and they both work please let me know what you did!!

Thanks

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [January 23, 2021, 5:52am UTC](https://discourse.processing.org/t/undo-and-redo-button/27301/2 "2021-01-23T05:52:44Z")

</div>

Your code, as posted, never sets `controlDown` or `shiftDown` back to false when they are released. That’s probably the problem.

---

<div class="post-metadata">

**Author:** ![Prxt.3k](https://avatars.discourse-cdn.com/v4/letter/p/9e8a1a/32.png) [@Prxt.3k](https://discourse.processing.org/u/Prxt.3k)\
**Post date:** [January 23, 2021, 5:54am UTC](https://discourse.processing.org/t/undo-and-redo-button/27301/3 "2021-01-23T05:54:27Z")

</div>

can you show me what you mean if possible, this is a fairly new code to me since I was given this?

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [January 23, 2021, 5:58am UTC](https://discourse.processing.org/t/undo-and-redo-button/27301/4 "2021-01-23T05:58:18Z")

</div>

It’s very concerning that you do not understand code you were given.

When a key is pressed, the `keyPressed()` function runs once.  
The value in `keyCode` is the name of the key pressed.  
That value does not change inside `keyPressed()`!

If I press down the Shift key, what does `keyPressed()` do?  
If I press down the Control key, what does `keyPressed()` do?  
Answer: That’s right - it sets the appropriate boolean to `true`.

Does that change ever get reverted?!? That is, when is `controlDown` or `shiftDown` ever set back to `false`?  
Hint: It’s never set back to `false`!

You might think about adding a `keyReleased()` function…

---

<div class="post-metadata">

**Author:** ![Prxt.3k](https://avatars.discourse-cdn.com/v4/letter/p/9e8a1a/32.png) [@Prxt.3k](https://discourse.processing.org/u/Prxt.3k)\
**Post date:** [January 23, 2021, 5:59am UTC](https://discourse.processing.org/t/undo-and-redo-button/27301/5 "2021-01-23T05:59:57Z")

</div>

oh Alright I will defenitly look into this code.

Thanks, Much appreciated
