Screenshot with CTRL + S

Hi, I’ve been struggling with this for a long time today. I’m trying to make screenshots with ctrl and s but it doesn’t work. Can anyone help me? Thanks.

This is what I tried:

boolean ctrlPressed;

void keyPressed() {
    if (key == BACKSPACE)  // this is to restart the program
      setup();                         
    else {
      if (keyCode == CONTROL) {
       ctrlPressed = true; 
      }
      else {
        if (ctrlPressed && key == 's' || key == 'S')
          saveFrame("drawing-######.jpg");
      }
    }
}  

here is my solution. You need to press ‘s’ within “allow” milliseconds from releasing ctrl key

boolean key1 = false, key2 = false;
int start = 0, allow = 300;
void setup() {}
void draw() {if(frameCount % 50 == 0) println(key1,key2,millis());}
void keyPressed() {
  if(keyCode == CONTROL) key1 = true;
  if(key == 's') key2 = true;
  if(key2 && millis() > start && millis() < start + allow) { println("take screenshot!"); }
}
void keyReleased() {
  if(keyCode == CONTROL) {key1 = false; start = millis();}
  if(key == 's') key2 = false;
}

I find allow 300 to be the best. You can obviously configure it to your liking

The problem with your attempt is that keyPressed() function can only recognise 1 key at the time. I solved it by adding a delay between pressing the keys

Edit: seems I was wrong

source: Processing 1.0 - Processing Discourse - CONTROL + char commands.

boolean ctrlPressed = false;

void setup() {
  size(400, 400);
}

void draw() {
  background(200);
}

void keyPressed() {
  if (key == CODED) {
    if (keyCode == CONTROL) {
      println("control");
      ctrlPressed = true;
    }
  } else {
    if (ctrlPressed && keyCode == 83) { // 83 is keyCode for "s"
      println("ctrl + s");
      save("test.png");
    }
  }
}

void keyReleased() {
  if (key == CODED) {
    if (keyCode == CONTROL) {
      ctrlPressed = false;
    }
  }
}
1 Like

Well, actually my code was right. I just write 83 instead of ‘s’ and it worked. Thank you!