# Problems with true false

**URL:** <https://discourse.processing.org/t/problems-with-true-false/10348>\
**Category:** Beginners\
**Created:** [April 16, 2019, 12:53pm UTC](https://discourse.processing.org/t/problems-with-true-false/10348 "2019-04-16T12:53:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jensemand](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@jensemand](https://discourse.processing.org/u/jensemand)\
**Post date:** [April 16, 2019, 12:53pm UTC](https://discourse.processing.org/t/problems-with-true-false/10348/1 "2019-04-16T12:53:40Z")

</div>

Hi, I have some troubles. I want to make a boolean true when I press a bottom and then stay true but it always changes back to false

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [April 16, 2019, 1:02pm UTC](https://discourse.processing.org/t/problems-with-true-false/10348/2 "2019-04-16T13:02:21Z")

</div>

depends on the use, a toggle with one key would be

```auto
boolean myBoolean=false;

void setup(){}
void draw(){}
void keyPressed(){
 if ( key == 't' ) {
   myBoolean = ! myBoolean;
   println("see key t "+myBoolean); 
 }
}

```

or use two keys for set true resp. false.

---

<div class="post-metadata">

**Author:** ![jensemand](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@jensemand](https://discourse.processing.org/u/jensemand)\
**Post date:** [April 16, 2019, 1:13pm UTC](https://discourse.processing.org/t/problems-with-true-false/10348/3 "2019-04-16T13:13:49Z")

</div>

is it possible to make it in draw?

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [April 16, 2019, 1:17pm UTC](https://discourse.processing.org/t/problems-with-true-false/10348/4 "2019-04-16T13:17:38Z")

</div>

idea is to change it by keyboard? then do like i show you.

but use it in draw… that is the idea.

but a very common use would be

```auto
boolean myBoolean=false;

void setup(){}
void draw(){
//...
  if ( myBoolean ) {
    // do something on keypress but only one time
    myBoolean=false;
  }
}
void keyPressed(){
 if ( key == 't' ) {
   myBoolean = true;
   println("see key t "+myBoolean); 
 }
}

```

---

<div class="post-metadata">

**Author:** ![jensemand](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@jensemand](https://discourse.processing.org/u/jensemand)\
**Post date:** [April 16, 2019, 1:21pm UTC](https://discourse.processing.org/t/problems-with-true-false/10348/5 "2019-04-16T13:21:48Z")

</div>

Well I can’t figure out how to make it stay as true. What i wanted to make is i want to press a bottom once and then the boolean stays at true until i say otherwise

---

<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:** [April 16, 2019, 1:22pm UTC](https://discourse.processing.org/t/problems-with-true-false/10348/6 "2019-04-16T13:22:53Z")

</div>

```auto
boolean myBoolean=false;

void setup(){}
void draw(){
//...
  if ( myBoolean ) {
    // do something on keypress 
  }
}
void keyPressed(){
 if ( key == 't' ) {
   myBoolean = true;
   println("see key t "+myBoolean); 
 }
}

```

---

<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:** [April 16, 2019, 1:32pm UTC](https://discourse.processing.org/t/problems-with-true-false/10348/7 "2019-04-16T13:32:15Z")

</div>

Since you don’t show your code:

Using the variable keypressed as opposed to the function keypressed () can often cause that a key is registers multiple times.

So, when you use a toggle on the Boolean variable (using … = ! …) it will uncontrollably reach one of its two states - bad.

Chrisir
