# Game control plus buttonPressed

**URL:** <https://discourse.processing.org/t/game-control-plus-buttonpressed/6061>\
**Category:** Libraries\
**Created:** [November 29, 2018, 4:44am UTC](https://discourse.processing.org/t/game-control-plus-buttonpressed/6061 "2018-11-29T04:44:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bryan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bryan/32/2538_2.png) [@bryan](https://discourse.processing.org/u/bryan)\
**Post date:** [November 29, 2018, 4:44am UTC](https://discourse.processing.org/t/game-control-plus-buttonpressed/6061/1 "2018-11-29T04:44:53Z")

</div>

Hi! i’m using the library game control plus to interact with on sketch in processing. what i’m trying to do is that if i press a button, change his state to the contrary, like in the code below. **i want to create or use a function like the keyPressed() but with the keys of the controller**

```auto
import net.java.games.input.*;
import org.gamecontrolplus.*;
import org.gamecontrolplus.gui.*;

ControlIO control;
ControlDevice xbox;

boolean s = false;
boolean onAndOff = false;
long previousMillis = 0;
long startMillis = 0;

void setup() {
  size(500, 500);

  control = ControlIO.getInstance(this);
  xbox = control.getMatchedDevice("XboxController");
  if (xbox == null) {
    println("no esta conectado el control");
    System.exit(-1);
  }
}

void draw() {
  background(0);

  startMillis = millis();
  s = xbox.getButton("Start").pressed();

  if ( (startMillis - previousMillis) >= 20) {
  if (s == true) {
  onAndOff =! onAndOff;
    } 
      previousMillis = startMillis;
     }

  println(onAndOff);
}

```

i used the millis function because i assume that at pressing the key, it have to wait some time in order to read only 1 value.  
Greetings!

---

<div class="post-metadata">

**Author:** ![Whahahahaha](https://avatars.discourse-cdn.com/v4/letter/w/3be4f8/32.png) [@Whahahahaha](https://discourse.processing.org/u/Whahahahaha)\
**Post date:** [November 29, 2018, 9:02am UTC](https://discourse.processing.org/t/game-control-plus-buttonpressed/6061/2 "2018-11-29T09:02:53Z")

</div>

@bryan Do you mean something like this?

```auto
void draw() {
  background(0);

  boolean lastState = s;
  s = xbox.getButton("Start").pressed();

  if (s != lastState) {
    if (s == true) {
      onAndOff =! onAndOff;
    } 
  }
  println(onAndOff);
}

```

---

<div class="post-metadata">

**Author:** ![bryan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bryan/32/2538_2.png) [@bryan](https://discourse.processing.org/u/bryan)\
**Post date:** [November 29, 2018, 12:38pm UTC](https://discourse.processing.org/t/game-control-plus-buttonpressed/6061/3 "2018-11-29T12:38:21Z")

</div>

Yeah! i tested it and it works perfect! manny thanks 🙂

---

<div class="post-metadata">

**Author:** ![bryan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bryan/32/2538_2.png) [@bryan](https://discourse.processing.org/u/bryan)\
**Post date:** [November 30, 2018, 1:09pm UTC](https://discourse.processing.org/t/game-control-plus-buttonpressed/6061/4 "2018-11-30T13:09:53Z")

</div>

Hi again, after a better comprehension of the library examples, (and probably because i didn’t explained clear i wanted) i realized that i can use `xbox.getButton("Start").plug(this, "my function", ControlIO.ON_RELEASE);` which works exactly like the keyPressed function. Still the idea provided by @Whahahahaha was really helpful.

---

<div class="post-metadata">

**Author:** ![johnnydel](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@johnnydel](https://discourse.processing.org/u/johnnydel)\
**Post date:** [June 13, 2019, 3:12am UTC](https://discourse.processing.org/t/game-control-plus-buttonpressed/6061/5 "2019-06-13T03:12:14Z")

</div>

Thank you works well
