Key != the pressed key when control is held

So I borrowed a lot from this old post on an older Processing forum, credit to @GoToLoop :

Check this out:

And I made some adjustments to fit what I think it is you are trying to do:

boolean controlPressed, shiftPressed, vPressed, cPressed;  //define all the booleans
String keysPressed = "";   //have a string that will compile all the strings pressed
String action = "";  //have a string that will display the end action 
 
void setup() {
  size(400,400);
}
 
void draw() {

  background(0);
  fill(255);
  textAlign(CENTER); 
  textSize(50); 
  text("Keys Pressed: ",width/2, 100);
  textSize(30);
  if(keysPressed.equals(" Control  C "))  //Check if the keysPressed String has both Control and C
     { action = "Control+C"; }  //if so, show "Control+C"
  if(keysPressed.equals(" Control  V ")) //Check if the keysPressed String has both Control and V
     { action = "Control+V"; }  //if so, show "Control+V"
  if(keysPressed.equals(" Shift  C "))  //Check if there is both Shift and C
     { action = "Shift+C"  ; }  //if so, show "Shift+C"
  if(keysPressed.equals(" Shift  V "))  //Check if there is both Shift and V
     { action = "Shift+V"  ; }  //if so, show "Shift+V"
  
  text(action, width/2, 150);  //display the ending action
}
 
void keyPressed() {
  keysPressed = ""; //clear keysPressed every time a new set of keys is pressed to not have conflict
  action = "";  // same with action, so that there is no extra conflict

  setKey(keyCode, true); //if a key is pressed, send that keyCode and true to the setKey() function
//check which key is being pressed, and add it to the keysPressed String
  if(controlPressed) { keysPressed+=" Control "; } 
  if(shiftPressed) { keysPressed+=" Shift "; } 
  if(vPressed) { keysPressed+=" V ";} 
  if(cPressed) { keysPressed+=" C "; }  
}
 
void keyReleased() {
 //when a key is released, it is now false
  setKey(keyCode, false);
}
 
boolean setKey(int k, boolean b) {

//Switch the keyCode through and check which key is being pressed. Set the corresponding boolean to true

  switch (k) {
  case CONTROL:
    return controlPressed = b;
 
  case SHIFT:
    return shiftPressed = b;
 
  case 67: //if the letter c is pressed
    return cPressed = b;
 
  case 86: //if the letter v is pressed
    return vPressed = b;
 
  default:
    return b;
  }
}

The most important part, is using the setKey() function to set a boolean to true or false. The function checks the keyCode being passed in, and depending on that, it will set the values of the corresponding booleans to true. This is different from what you were doing, which is trying to directly set a boolean checking which key is pressed, to true or false, directly within the void keyPressed() method.

Now this is quite limited because at least the way I chose to do it, you would have to define a variable for every single key that you want to have control for, but this is just a beginning to being able to see the multiple keys being pressed.

Also, I know there could possibly be a better way to do this, but again this is just a beginning to what it is I think you want to do. If you want to change what happens when a certain key binding is pressed, you would start by looking at this section of the code:

if(keysPressed.equals(" Control  C "))  //Check if the keysPressed String has both Control and C
     { action = "Control+C"; }  //if so, show "Control+C"
  if(keysPressed.equals(" Control  V ")) //Check if the keysPressed String has both Control and V
     { action = "Control+V"; }  //if so, show "Control+V"
  if(keysPressed.equals(" Shift  C "))  //Check if there is both Shift and C
     { action = "Shift+C"  ; }  //if so, show "Shift+C"
  if(keysPressed.equals(" Shift  V "))  //Check if there is both Shift and V
     { action = "Shift+V"  ; }  //if so, show "Shift+V"

Hopefully this little example gets you started, and if you have any more questions I would be glad to help out further,

EnhancedLoop7

1 Like