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.
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)
// }
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