# How to write code to press multiple Radiobuttons at once (button grid)

**URL:** <https://discourse.processing.org/t/how-to-write-code-to-press-multiple-radiobuttons-at-once-button-grid/42263>\
**Category:** Libraries\
**Created:** [June 21, 2023, 2:07am UTC](https://discourse.processing.org/t/how-to-write-code-to-press-multiple-radiobuttons-at-once-button-grid/42263 "2023-06-21T02:07:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hyunwoo](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@Hyunwoo](https://discourse.processing.org/u/Hyunwoo)\
**Post date:** [June 21, 2023, 2:07am UTC](https://discourse.processing.org/t/how-to-write-code-to-press-multiple-radiobuttons-at-once-button-grid/42263/1 "2023-06-21T02:07:23Z")

</div>

Hello everyone

I have created 128 switches as radiobuttons and I want them to work only one at a time, and I want to be able to press SHIFT and select a switch and have multiple alternatives, how should I code it?

Below is the code created with the Radiobutton.

```auto
import controlP5.*;

ControlP5 cp5;
RadioButton radio;

final int _numCols = 16;
final int _numRows = 8;

void setup() {
  size(750, 350);
  background(209);
  cp5 = new ControlP5(this);
  radio = cp5.addRadioButton("radio");
  for (int x = 0; x < _numCols*_numRows; x++) {
    radio.setItemsPerRow(_numCols);
    radio.setPosition(30, 30);
    radio.setSize(40, 30);
    radio.addItem(str(x), x);
  }
}

void draw() {
}

void radio(int a) {
  println("radio id: ", a);
}

void controlEvent(ControlEvent theEvent) {
  if (theEvent.isGroup()) {
    println("event from group "
      +theEvent.getGroup().getName()
      +", isOpen? = "+theEvent.getGroup().isOpen()
      );
  }
}
//Below are the functions I need to add
//void keyPressed (){
 // if (keyCode==SHIFT) 

//}

//void keyReleased()
  {
  // if (keyCode==SHIFT) 
   
// } 

```

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [June 21, 2023, 3:00am UTC](https://discourse.processing.org/t/how-to-write-code-to-press-multiple-radiobuttons-at-once-button-grid/42263/2 "2023-06-21T03:00:44Z")

</div>

The function of a radiobutton is to allow only one selection, which was your initial question. Think of selecting a radio station on your car radio; each button goes to a single station. Now you want to use radiobuttons for multiple selections. In order to have that behavior it would likely require a custom control. If you want multiple selections then checkboxes would probably be a better choice. In summary, radiobuttons for a single choice and checkboxes for multiple selections for normal situations. It could be difficult to combine the two without using custom code. [https://en.wikipedia.org/wiki/Radio\_button](https://en.wikipedia.org/wiki/Radio_button)

---

<div class="post-metadata">

**Author:** ![Hyunwoo](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@Hyunwoo](https://discourse.processing.org/u/Hyunwoo)\
**Post date:** [June 21, 2023, 6:05am UTC](https://discourse.processing.org/t/how-to-write-code-to-press-multiple-radiobuttons-at-once-button-grid/42263/3 "2023-06-21T06:05:09Z")

</div>

Thanks for the answer!  
I’ll try to define a user function to combine checkboxes and radio buttons!

---

<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:** [June 22, 2023, 2:00pm UTC](https://discourse.processing.org/t/how-to-write-code-to-press-multiple-radiobuttons-at-once-button-grid/42263/4 "2023-06-22T14:00:18Z")

</div>

you can also write your own

```auto
// https://discourse.processing.org/t/how-to-write-code-to-press-multiple-radiobuttons-at-once/42263/3

/*
 Select one button
 Hold shift key to select multiple keys
 Esc key to reset
 i to inverse grid
 To Do: store the matrix and make a replay
 */

ArrayList<Button> buttons = new ArrayList ();

boolean shiftIsDown = false;

// ---------------------------------------------------------------------------------------

void setup () {
  size (1000, 600);

  // init grid
  for (int x = 0; x<8; x++) {
    for (int y = 0; y<8; y++) {
      Button btn = new Button (x*40+30,
        y*40+30);
      buttons.add(btn);
    }//for
  }//for

  background(111);
}//func

void draw () {
  background(255);

  // show yellow box
  showYellowBox("Demo for Button Grid \n\nMouse select\nshift with mouse - multi select\ni - inverse\nEsc - reset\nX - quit ");

  // show button grid
  for (Button btn : buttons) {
    btn.display();
  }//for

  // show as text
  fill(0);
  int i=0;
  for (int x = 0; x<8; x++) {
    for (int y = 0; y<8; y++) {
      Button btn=buttons.get(i);
      text(btn.selectedToString(),
        x*7+width/2-110, y*7+height/2-110);
      i++;
    }//for
  }//for
}//func

// ---------------------------------------------------------------------------------------
// Inputs

void mousePressed() {
  // when we don't have shift
  if (!shiftIsDown) {
    // all off
    for (Button btn : buttons) {
      btn.selected=false;
    }
  }//if

  //search
  for (Button btn : buttons) {
    if (btn.inside()) {
      btn.selected=true; // or toggle
    }//if
  }//for
}//func

void keyPressed() {
  if (keyCode==SHIFT) {
    shiftIsDown=true;
  } else if (key==ESC) {
    // reset all
    for (Button btn : buttons) {
      btn.selected=false;
    }
  } else if (key=='i') {
    // inverse all
    for (Button btn : buttons) {
      btn.selected= ! btn.selected; // inverse
    }
  } else if (key=='X') {
    // Quit
    exit();
  }// else if

  key=0;//kill Esc
} // func

void keyReleased() {
  if (keyCode==SHIFT) {
    shiftIsDown=false;
  } //if
} //func

// ------------------------------------------------------------------
// Other Tools

void showYellowBox(String text_) {
  // yellow box
  fill( #F1F743 );//YELLOW
  rect ( width - 297, 19,
    262, 366);

  // black frame
  noFill();
  stroke(0);
  float a1 = 4;
  rect ( width - 297+a1, 19+a1,
    262-a1*2, 366-a1*2);
  noFill();

  //text
  fill(0);
  textSize(14);
  text (text_,
    width - 290+2, 19+5,
    270, 900);
  fill(255);
} //func

// ====================================================================

class Button {

  final float buttonSize = 35;

  float x, y;

  boolean selected=false;

  // constr
  Button (float x_, float y_) {
    x=x_;
    y=y_;
  }// constr

  void display() {
    if (selected)
      fill(222);
    else
      fill(111);

    stroke(0);
    rect(x, y,
      buttonSize, buttonSize);
  }

  boolean inside() {
    return
      mouseX>x &&
      mouseX<x+buttonSize &&
      mouseY>y &&
      mouseY<y+buttonSize;
  }//method

  String selectedToString() {
    if (selected)
      return "x";
    else
      return ".";
  }
  //
}//class

// =================================================================

```

_keywords: select buttons button grid multi select on off buttons multiple radiobuttons_

---

<div class="post-metadata">

**Author:** ![Hyunwoo](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@Hyunwoo](https://discourse.processing.org/u/Hyunwoo)\
**Post date:** [June 23, 2023, 7:56am UTC](https://discourse.processing.org/t/how-to-write-code-to-press-multiple-radiobuttons-at-once-button-grid/42263/5 "2023-06-23T07:56:32Z")

</div>

thank you! i will try to fix my code using your code 😀
