Simulate a trigger as switch/case with the keyboard

Hi ,
I would like my program switch on the same case automatically, in order to actualise data from void draw.
For the moment, I have to tap on the shift key to actualise the case ‘default’. In this way, each time I tap on the shift key, I have my new datas parameterised in my case ‘default’.
I would like to do the same think, but rather than I tap on the keyboard, I would like my program trig by itself the case ‘default’ or the case ‘1’ … , with ???, an order, a function, I don’t know.

Actually, i want my program automatise the switch to the same case or an other case, to actualise my data which come from void draw ().
Thanks for yours lights!

I put a part of my program below

// in void draw 
  kickSize = constrain(kickSize * 0.95, 16, 32);
  snareSize = constrain(snareSize * 0.95, 16, 32);
  hatSize = constrain(hatSize * 0.95, 16, 32);
  
    k = map (kickSize, 16, 32, 1,2); //beat
  
    s = map (snareSize, 16, 32, 1,2); // kick
  
    h = map (hatSize, 16, 32, 1,2);// snare
  
/*  
  println (k);
  println (s);
  println (h);
  println();
*/

   if ( k > 1.5 ) { // if the beat is louder 1.5 switch on case 7 with doit();
   
   doit();    
  float v= frequencyEnergy(k);

    }   
}

float frequencyEnergy (float dVal) {
  dVal= dVal *1;
  return dVal;
  
}

void  doit()  {
       
    if (k>1.7){
   
      print ("Frequency_ENERGY");println (k); println(frequencyEnergy(k));
      key='7';
        
      }
  }

void keyPressed() {
    
     doit (); //The program does = each time I push the shift key AND kick is upper
               //  than 1.7--> go on case 7
              // otherwise program switch on the case 0 == if (k<1.7) AND i push on the 
             //  shift key, it actualise case 0 with new value of k, s, h (kick, snare, hat) .
      
  switch (key) {
        default:
      
       
  
   //    noiseDetail(mouseX/30, mouseY/300);
     
     
      net.naturalFrequency[0] = frequencyEnergy(k)*TWO_PI * noise(t);
      t += dt; 
      net.naturalFrequency[1] = 1.0*s*TWO_PI * noise(t);
      t += dt;
      net.naturalFrequency[2] = 1.0*h*TWO_PI * noise(t);
      t += dt;
      net.naturalFrequency[3] = 0.9*TWO_PI * noise(t);
      t += dt;
      net.naturalFrequency[4] = 1.1*TWO_PI * noise(t);
      t += dt;
     
       println ("DEFAULT");
       print ("Kick"); println (k);
       print ("snare"); println (s);
       print ("hat"); println (h);
       println ();
        
       print ("Freq Energy Kick"); println(frequencyEnergy(k));;
        
      break;

      case '7':
      
      
      doit();

    
   
       noiseDetail(mouseX/30, mouseY/300);
       
         
    
   
     
      net.naturalFrequency[0] = 1.0f*k*TWO_PI * noise(t);
      t += dt; 
      net.naturalFrequency[1] = 1.0f*s*TWO_PI * noise(t);
      t += dt;
      net.naturalFrequency[2] = 1.0f*h*TWO_PI * noise(t);
      t += dt;
      net.naturalFrequency[3] = 0.9f*TWO_PI * noise(t);
      t += dt;
      net.naturalFrequency[4] = 1.1f*TWO_PI * noise(t);
      t += dt;
     
      
     
      
       print ("Frequency-Energy inside case 7: "); println(frequencyEnergy(k));
       print ("Kick"); println (k);
        
          println(s);
          println (h);
          println ();
          println ();
          
      break;
      
     
   
  }
}
1 Like

Hey there !
From what I understand is that you want different events to occur in your switch statement based on different inputs?

Would something like this work ?

void pressed(int k){
	switch(k){
		case UP: System.out.println("Something happens") break;
		case DOWN: System.out.println("Something next happens") break;
		default: System.out.println("This happens as coded events in the statement weren't entered");
	}
}

You can change each case to be triggered by whatever button you want to be. Just change it to the character you want it to be triggered by.

Yes that what I want but I don’t want to tap on any button. I would like to trig a case “as if” I tap on the keyboard. For example I would like the program send “UP” when K>1.5. Is it possible?

Thanks InferNova, it almost works. :grin:
But I have to stay on the key UP or DOWN (I can’t see any difference) to actualise the case the case ‘default’.
And when k>1.5 it switch on case 7.
Is it possible to not stay on any keys to actualise a case only when k >1.5?? Thanks again!!!

Hi there!
Ok I think I get what you mean. So basically you want your program to simulate a key press ? When a key is pressed on your computer that’s send a number which represents a certain key. The numbers correspond to their particular symbols through the ASCII table

From what you have said here’s the code:

int keySim;
void setup(){
	keySim = UP;
}
void draw(){
	pressed(keySim);
}
void pressed(int k){
	switch(k){
		case UP: System.out.println("Something happens") break;
		case DOWN: System.out.println("Something next happens") break;
		default: System.out.println("This happens cause the proper stuff wasn't clicked");
	}
}

Is this what you are trying to achieve ? Also you don’t have to use cases such as UP or DOWN or any of the ASCII values you can have your program call and simulate any number within the case !

To make the default case to be entered you can just pass a value which hasn’t been initialized within the ‘switch’ case so for example if you just passed in a value LEFT to the switch statement I gave you default: would be entered

You can just send the value to your switch statement after k > 1.5 and as long as k > 1.5 that will always be executed.

1 Like

There are two different possible cases, here.

  1. You want the computer to simulate actual input events – commonly, this is to move the mouse around. For that, you can create your own events, or generate Java Events and pass them to the builtin functions, or you can use Java Robot.
  2. There are some actions – you want either the usee or the computer to perform those actions. ( I think this is what you want ). For this, make functions that are called either by input functions or by draw, like:
float k;
void draw() {
  k = random(1);
  if (frameCount%100==0) {
    myAction('1');
  }
}
void keyPressed() {
  myAction(key);
}
void myAction(char arg) {
  switch(arg) {
  case '1': 
    println(frameCount + ": 1"); 
    break;
  case '2': 
    println(frameCount + ": 2"); 
    break;
  }
}

In this example the computer is calling “1” every 120ms, but you can also call either the “1” or “2” whenever you want by pushing those buttons.

This design works the same with mouse or other input, or with multiple user and multiple computer methods of access a function. point all your potential input methods at common functions.