# I am having trouble with assigning a key press to a image and boolean variable

**URL:** <https://discourse.processing.org/t/i-am-having-trouble-with-assigning-a-key-press-to-a-image-and-boolean-variable/17144>\
**Category:** Coding Questions\
**Created:** [January 17, 2020, 5:04pm UTC](https://discourse.processing.org/t/i-am-having-trouble-with-assigning-a-key-press-to-a-image-and-boolean-variable/17144 "2020-01-17T17:04:34Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![NinjaKittens](https://avatars.discourse-cdn.com/v4/letter/n/97f17d/32.png) [@NinjaKittens](https://discourse.processing.org/u/NinjaKittens)\
**Post date:** [January 17, 2020, 5:04pm UTC](https://discourse.processing.org/t/i-am-having-trouble-with-assigning-a-key-press-to-a-image-and-boolean-variable/17144/1 "2020-01-17T17:04:34Z")

</div>

This is what I came up with on the spot.

```auto
void keyReleased() {
	if (keyPressed && ((key == 'a' || key == 'A')) {
			flagC1 =true
	}
}

void draw() { 
	if (keyPressed) {
		if (key == 'a' || key == 'A') {
			background(255,0,0);
		}
	}
	image(imgC1,350,350,300,50);
}

```

this is the rest of the code if you need it

```auto
PImage imgA,imgB,imgL; //images for beginning screens
PImage imgQ1,imgQ2,imgQ3,imgQ4; //images for the questions
PImage imgC1,imgC2,imgC3,imgC4; //answers for question 1

boolean flagA=true;
boolean flagB=true;
boolean flagL=false;
boolean flagQ1=false;
boolean flagC1=false;

void setup() {
size(1000,1000);
background(100,255,210); //background color
imgA = loadImage("https://www.openprocessing.org/sketch/813047/files/Screenshot%202020-01-08%20at%209.48.03%20AM.png"); //img link
imgB = loadImage("https://www.openprocessing.org/sketch/813047/files/Screenshot%202020-01-08%20at%209.44.31%20AM.png");
imgL = loadImage("https://www.openprocessing.org/sketch/813047/files/Screenshot%202020-01-09%20at%2010.10.31%20AM.png");
imgQ1 = loadImage("https://www.openprocessing.org/sketch/813047/files/Screenshot%202020-01-16%20at%208.50.12%20AM.png");
imgC1 = loadImage("https://www.openprocessing.org/sketch/813047/files/Screenshot%202020-01-16%20at%209.50.02%20AM.png");
noStroke();
}

void draw() {
if (mousePressed && (mouseButton == LEFT)) {
background(225,0,0);
	noLoop();
}
	else if (mousePressed && (mouseButton == RIGHT)) {
background(0,225,0);
	noLoop();
	}
  
	if (flagA)
    image(imgA, 500, 530, 100, 50);
  if (flagB)
    image(imgB, 400, 400, 400, 50);
	if (flagL)
		image(imgL,400,430,200,150);
	if (flagQ1)
		image(imgQ1,300,300,400,80);
	if (flagC1)
		image(imgC1,350,350,300,50);
		
}

void mousePressed() {
  if (mouseButton == LEFT) {
		flagA=false;
		flagB=false;
		flagL=true;
		flagQ1=false;
  }

  if (mouseButton == RIGHT) {
    flagA=false;
		flagB=false;
		flagL=false;
		flagQ1=true;
  }
}

```

Thanks in advace for the help.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 17, 2020, 5:08pm UTC](https://discourse.processing.org/t/i-am-having-trouble-with-assigning-a-key-press-to-a-image-and-boolean-variable/17144/2 "2020-01-17T17:08:21Z")

</div>

> [@NinjaKittens](#):
>
> void keyReleased() { if (keyPressed

Are we sure you should use keyPressed inside of keyReleased??

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 17, 2020, 5:09pm UTC](https://discourse.processing.org/t/i-am-having-trouble-with-assigning-a-key-press-to-a-image-and-boolean-variable/17144/3 "2020-01-17T17:09:33Z")

</div>

I won’t use noLoop(); …

---

<div class="post-metadata">

**Author:** ![NinjaKittens](https://avatars.discourse-cdn.com/v4/letter/n/97f17d/32.png) [@NinjaKittens](https://discourse.processing.org/u/NinjaKittens)\
**Post date:** [January 17, 2020, 5:09pm UTC](https://discourse.processing.org/t/i-am-having-trouble-with-assigning-a-key-press-to-a-image-and-boolean-variable/17144/4 "2020-01-17T17:09:54Z")

</div>

So switch keyReleased with keyPressed?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 17, 2020, 5:11pm UTC](https://discourse.processing.org/t/i-am-having-trouble-with-assigning-a-key-press-to-a-image-and-boolean-variable/17144/5 "2020-01-17T17:11:25Z")

</div>

Try this:  
void keyReleased() { if ( ((key == ‘a’

---

<div class="post-metadata">

**Author:** ![NinjaKittens](https://avatars.discourse-cdn.com/v4/letter/n/97f17d/32.png) [@NinjaKittens](https://discourse.processing.org/u/NinjaKittens)\
**Post date:** [January 17, 2020, 5:15pm UTC](https://discourse.processing.org/t/i-am-having-trouble-with-assigning-a-key-press-to-a-image-and-boolean-variable/17144/6 "2020-01-17T17:15:09Z")

</div>

It did work, but it showed up before everything else. I want it to show up when “imgQ1” shows up.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 17, 2020, 5:17pm UTC](https://discourse.processing.org/t/i-am-having-trouble-with-assigning-a-key-press-to-a-image-and-boolean-variable/17144/7 "2020-01-17T17:17:03Z")

</div>

> [@NinjaKittens](#):
>
> it showed up before everything else.

Define ‚it‘ please

Not understand

---

<div class="post-metadata">

**Author:** ![NinjaKittens](https://avatars.discourse-cdn.com/v4/letter/n/97f17d/32.png) [@NinjaKittens](https://discourse.processing.org/u/NinjaKittens)\
**Post date:** [January 17, 2020, 5:18pm UTC](https://discourse.processing.org/t/i-am-having-trouble-with-assigning-a-key-press-to-a-image-and-boolean-variable/17144/8 "2020-01-17T17:18:33Z")

</div>

“imgC1” or the image that I want to appear when “imgQ1” appears.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 17, 2020, 5:22pm UTC](https://discourse.processing.org/t/i-am-having-trouble-with-assigning-a-key-press-to-a-image-and-boolean-variable/17144/9 "2020-01-17T17:22:16Z")

</div>

Image C1 shouldn’t appear

when  
flagC1 is false

You never set flagC1 to true

Post your entire code

---

<div class="post-metadata">

**Author:** ![NinjaKittens](https://avatars.discourse-cdn.com/v4/letter/n/97f17d/32.png) [@NinjaKittens](https://discourse.processing.org/u/NinjaKittens)\
**Post date:** [January 17, 2020, 5:24pm UTC](https://discourse.processing.org/t/i-am-having-trouble-with-assigning-a-key-press-to-a-image-and-boolean-variable/17144/10 "2020-01-17T17:24:21Z")

</div>

```auto
PImage imgA,imgB,imgL; //images for beginning screens
PImage imgQ1,imgQ2,imgQ3,imgQ4; //images for the questions
PImage imgC1,imgC2,imgC3,imgC4; //answers for question 1

boolean flagA=true;
boolean flagB=true;
boolean flagL=false;
boolean flagQ1=false;
boolean flagC1=false;

void setup() {
size(1000,1000);
background(100,255,210); //background color
imgA = loadImage("https://www.openprocessing.org/sketch/813047/files/Screenshot%202020-01-08%20at%209.48.03%20AM.png"); //img link
imgB = loadImage("https://www.openprocessing.org/sketch/813047/files/Screenshot%202020-01-08%20at%209.44.31%20AM.png");
imgL = loadImage("https://www.openprocessing.org/sketch/813047/files/Screenshot%202020-01-09%20at%2010.10.31%20AM.png");
imgQ1 = loadImage("https://www.openprocessing.org/sketch/813047/files/Screenshot%202020-01-16%20at%208.50.12%20AM.png");
imgC1 = loadImage("https://www.openprocessing.org/sketch/813047/files/Screenshot%202020-01-16%20at%209.50.02%20AM.png");
noStroke();
}

void draw() {
if (mousePressed && (mouseButton == LEFT)) {
background(225,0,0);
}
	else if (mousePressed && (mouseButton == RIGHT)) {
background(0,225,0);
	}
  
	if (flagA)
    image(imgA, 500, 530, 100, 50);
  if (flagB)
    image(imgB, 400, 400, 400, 50);
	if (flagL)
		image(imgL,400,430,200,150);
	if (flagQ1)
		image(imgQ1,300,300,400,80);
	if (flagC1)
		image(imgC1,350,350,300,50);
		
}

void mousePressed() {
  if (mouseButton == LEFT) {
		flagA=false;
		flagB=false;
		flagL=true;
		flagQ1=false;
		flagC1=false;
  }

  if (mouseButton == RIGHT) {
    flagA=false;
		flagB=false;
		flagL=false;
		flagQ1=true;
		flagC1=true;
  }
}
/*
void keyReleased() {
	if ((key == 'a' || key == 'A')) {
			flagC1 =false
	}
}

void draw() { 
	if (keyPressed) {
		if (key == 'a' || key == 'A') {
			background(255,0,0);
		}
	}
	image(imgC1,350,350,300,50);
}
*/

```

The commented out code is the problem.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 17, 2020, 6:28pm UTC](https://discourse.processing.org/t/i-am-having-trouble-with-assigning-a-key-press-to-a-image-and-boolean-variable/17144/11 "2020-01-17T18:28:53Z")

</div>

> [@NinjaKittens](#):
>
> The commented out code is the problem.

You can’t have 2 draw() – AND imgC1 is shown there always without the if bit?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 17, 2020, 6:31pm UTC](https://discourse.processing.org/t/i-am-having-trouble-with-assigning-a-key-press-to-a-image-and-boolean-variable/17144/12 "2020-01-17T18:31:35Z")

</div>

> [@NinjaKittens](#):
>
> if (mousePressed && (mouseButton == LEFT)) { background(225,0,0); }

this background is not permanent but just during you hold the mouse

To make it permanent, use a boolean variable.

you should in fact use a background throughout to see what’s going on. Without background images just stay on the screen from the previous execution / frame of draw().

**Quiz**

Anyway. When you want to do a Quiz, you need a counter for the question and a means of **buttons** to select the answer with the mouse, right? See [Button / Examples / Processing.org](https://www.processing.org/examples/button.html)

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 17, 2020, 6:35pm UTC](https://discourse.processing.org/t/i-am-having-trouble-with-assigning-a-key-press-to-a-image-and-boolean-variable/17144/13 "2020-01-17T18:35:32Z")

</div>

I deleted this one because of reasons of discretion

---

<div class="post-metadata">

**Author:** ![NinjaKittens](https://avatars.discourse-cdn.com/v4/letter/n/97f17d/32.png) [@NinjaKittens](https://discourse.processing.org/u/NinjaKittens)\
**Post date:** [January 19, 2020, 12:33am UTC](https://discourse.processing.org/t/i-am-having-trouble-with-assigning-a-key-press-to-a-image-and-boolean-variable/17144/14 "2020-01-19T00:33:48Z")

</div>

Thanks for the help.
