Key != the pressed key when control is held

I’m trying to implement copy and pasting into a node based programming language I’m making. An abreviated version of my code is something like this:

void keyPressed() {
if(key == 'c' || key == 'C'){
    print("\nC pressed");
    if(isCtrlPressed)
      CopySelectedToBuffers();
  }
  if(key == 'v' || key == 'V'){
    print("\nV pressed");
    if(isCtrlPressed)
      PasteFromBuffers();
  }
  if(key == CODED){
    if(keyCode == CONTROL)
      isCtrlPressed = true;
    if(keyCode == SHIFT)
      isShiftPressed = true;
  }
}

for some reason, when i hold control and press c or v, nothing gets printed. But when i hold shift, it seems like it prints out fine. this leads me to believe it has nothing to do with coded keys, as shift is a coded key. is there some special way i need to handle ctrl in particular?

1 Like

You are working with node and you are using p5.js? What are you trying to do? Node is for backend. you need to provide more details about your setup. Also…

Please format your code :blush:

It consist on these two steps:

  1. In your code editor (PDE, VS code, Eclipse, etc) ensure you execute the beautifier function. This function automatically indents your code. Auto-indenting makes your code easier to read and helps catching bugs due to mismatch parenthesis, for instance. In the PDE, you use the key combination: ctrl+t
  2. You copy and paste your code in the forum. Then you select the code and you hit the formatting button aka. the button with this symbol: </>

That’s it! Please notice you do not create a new post in case you need to format something you already posted. You can edit your post, copy the code to the PDE, indent the code properly there and then past it back here, format the code and >> save << the edits.

Extra info:

Formatting your code makes everybody’s life easier, your code looks much better plus it ensures your code integrity is not affected by the forum’s formatting (Do you know the forum processes markup code?) Please visit the sticky posts or the FAQ section/post to learn about this, other advantages and super powers you can get in this brand new forum.

Kf

1 Like

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

you are a champion. thanks so much

1 Like

Glad I could help :grin:

Thanks for the tip, I just switched it

Apparently it’s not necessary to keep track of the CTRL key yourself, Processing does that for you:

void draw() {}

void keyPressed(KeyEvent event) { 
  if (event.isControlDown()) {
    if (keyCode == 'C') {
      println("CTRL+C");
    } else if (keyCode == 'V') {
      println("CTRL+V");
    }
  }
}

// Also available:
//event.isAltDown()
//event.isShiftDown()
//event.isMetaDown();

But the question headline is right: key is not the pressed key when control is held. The key is found in keyCode as an upper case character (at least on Linux, I didn’t try in other OSes).

1 Like