bking
1
Hello.
I would like to switch case with a combination of two letter or letter, one of each.
I did like that, but it doesn’t work
void keyPressed() {
if (key=='5' && key=='T'){
println ("T55555TTTT5555");
}
if (key=='7'){
println ("CASE 7 PRRRRRRRRRRRRRRRREEEEEEEEEEEESSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSEDDDDDDDDDD");
}
if (keyCode == LEFT && key=='1') {
println ("LEFTLEFTLEFTLEFTLEFTLEFTLEFTLEFTLEFTLEFTLEFTLEFTLEFTLEFTLEFTLEFTLEFTLEFTLEFTLEFTLEFT11111111111111");
}
else if (keyCode == DOWN && key=='2') {/
println ("DOWNDOWNDOWNDOWNDOWNDOWNDOWNDOWNDOWNDOWNDOWNDOWNDOWNDOWNDOWNDOWNDOWNDOWNDOWNDOWN222222222222222222222");
}
}
Thanks
1 Like
You can’t use boolean operators with switch statements.
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
Use the if/else…
1 Like
bking
3
I have tried to adapt an example but it doesn’t work
int month = 1;
String monthString;
public void settings() {
size(600, 600);
}
void setup() {
}
void draw () {
keyPressed ();
}
void keyPressed () {
if ( key=='1' && key=='2' ) {
// if ( key=='2') {
month =12 ;
// }
}
switch (month) {
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid month";
break;
}
System.out.println(monthString);
}
keyPressed()
creates an event listener, so no need to call it in draw()
.
Not sure what you are going to do when the user needs to input November, but any case here is one way to accomplish what you are trying to do …
boolean[] keys;
boolean wasMultiPress = false;
final int NUM_KEYS = 2;
color col = color(3, 7, 11);
String monthString = null;
void setup() {
size(200, 200);
background(col);
keys = new boolean[NUM_KEYS];
for (int i = 0; i < NUM_KEYS; i++) {
keys[i] = false;
}
}
void draw() {
background(col);
if (keys[0] && keys[1]) {
wasMultiPress = true;
}
}
void updateMonth(int m) {
switch (m) {
case 1:
monthString = "January";
break;
case 2:
monthString = "February";
break;
case 3:
monthString = "March";
break;
case 4:
monthString = "April";
break;
case 5:
monthString = "May";
break;
case 6:
monthString = "June";
break;
case 7:
monthString = "July";
break;
case 8:
monthString = "August";
break;
case 9:
monthString = "September";
break;
case 10:
monthString = "October";
break;
case 11:
monthString = "November";
break;
case 12:
monthString = "December";
break;
default:
monthString = "Invalid month";
break;
}
println(monthString);
}
void keyPressed() {
if (key == '1') {
keys[0]=true;
}
if (key == '2') {
keys[1]=true;
}
}
void keyReleased() {
if (key == '1') {
keys[0]=false;
}
if (key == '2') {
keys[1]=false;
}
if (!keys[0] && !keys[1] && wasMultiPress) {
updateMonth(12);
wasMultiPress = false;
} else {
updateMonth(Character.getNumericValue(key));
}
}
1 Like
bking
5
Thank you Mister.
And How could I do a case with for example the keypad DOWN and the keypad 1.
Or the keypad A and the keypad 1 ?
You can up the number of array elements in keys
, then add those checks in keyPressed()/keyReleased()
…
then add to the if/else checking if multiple keys were selected or just single key.
At this point I would move that logic into it’s own function, ex. checkIfMultipleKeys()
and invoke that in keyReleased()
.
keep in mind most keyboards only detect 2 keys being pressed at same time.
bking
8
Ok it works with two keys together
static final String TABULAR = "\t";
static final color FG = #FF0000, BG = 255;
static final int KEYS = 0500, SMOOTH = 3;
static final float FPS = 120.0, WEIGHT = 1.0;
final boolean[] keysDown = new boolean[KEYS];
void setup() {
size(01000, 0500);
smooth(SMOOTH);
frameRate(FPS);
colorMode(RGB);
strokeWeight(WEIGHT);
stroke(BG);
}
void draw() {
final int p = frameCount % width;
line(p, 0, p, height);
for (int k = 0; k < KEYS; ) if (isKeyDown(k++)) set(p, k, FG);
processTwoKey();
}
void keyPressed() {
processKey(keyCode, true);
println(keyCode + TABULAR);
}
void keyReleased() {
processKey(keyCode, false);
}
void processKey(final int k, final boolean isDown) {
if (k < KEYS) keysDown[k] = isDown;
}
void processTwoKey (){//final int k, final boolean isDown) {
if (keysDown[65]== true && keysDown[40]== true) // && keysDown[49] = true )
println (" two keys together!!! ");
}
boolean isKeyDown(final int k) {
return keysDown[k];
}
1 Like