Game control plus buttonPressed


#1

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

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!


#2

@bryan Do you mean something like this?

void draw() {
  background(0);

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

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


#3

Yeah! i tested it and it works perfect! manny thanks :slight_smile:


#4

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.