We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Multiple keys pressed simultaneously
Page Index Toggle Pages: 1
Multiple keys pressed simultaneously (Read 891 times)
Multiple keys pressed simultaneously
Nov 26th, 2007, 9:18pm
 
Hello,

Maybe I'm being dense, but I can't see a way to detect multiple key presses at the same time! I was going to write a little game, but it's going to be tricky if I can't do this...!

Thanks

- f.
Re: Multiple keys pressed simultaneously
Reply #1 - Nov 26th, 2007, 9:45pm
 
I've no idea if it's possible to detect multiple keypressed if they happen all at the same time, but most of the time they're actually multiple seperate (though concurrent) presses, and you can track that. Or processing is clever enough to run keyPressed once per key...


All you need to do is keep an array of boolean values, one per key, and set the value for a particular key to true when it's pressed (in keyPressed()) and false when it's released (in keyReleased())
Re: Multiple keys pressed simultaneously
Reply #2 - Nov 27th, 2007, 1:36am
 
and, unfortunately, new keyboards often can't handle more than 2-3 simultanious keypresses (+modifier keys).
Re: Multiple keys pressed simultaneously
Reply #3 - Nov 27th, 2007, 2:16am
 
Quote:


int nrKeys = 0;
PFont font;
boolean[] downKeys = new boolean[256];

void setup() {
 size(200,200);
 fill(255);
 font = createFont("Courier", 20);
 textFont(font);
}

void draw() {
 background(0);
 nrKeys = 0;
 for (int i=0; i<downKeys.length; i++) {
   if (downKeys[i]) {
     nrKeys++;
     text((char)i, 10*nrKeys,height/2);
   }
 }
 text("Nr. of Keys: " + nrKeys, 20,20);
}

void keyPressed() {
 println(key);
 if (key<256) {
   downKeys[key] = true;
 }
}

void keyReleased() {
 if (key<256) {
   downKeys[key] = false;  
 }
}


it's weird... on my macbook pro i can *sometimes* get 5 simultanious keys. it seems like this keyboard has some strange wiring. With some combinations I can't get over 3.
or is it my script?
Re: Multiple keys pressed simultaneously
Reply #4 - Nov 28th, 2007, 11:41pm
 
Thanks guys! Yeah, keyReleased makes a lot of sense.

Extrapixel, for what it's worth I've noticed this thing in the past of keyboards only registering a given number of keys at once, *and* that number depending on which keys are being pressed. Very weird, but I think I first noticed it on a ZX Spectrum back in the 80s!
Re: Multiple keys pressed simultaneously
Reply #5 - Nov 29th, 2007, 7:02pm
 
Wait, hold on. How do I know *which* key has been released? The key variable just tells us which key was most recently pressed, so if someone was holding down more than one key, it's not much use to us, right?
Re: Multiple keys pressed simultaneously
Reply #6 - Nov 29th, 2007, 7:29pm
 
In keyReleased 'key' is the most recently released key.
Re: Multiple keys pressed simultaneously
Reply #7 - Nov 30th, 2007, 12:57pm
 
Oh! Okay, thanks John. Is that documented anywhere?! I guess it'll work for keyCode too, right?
Re: Multiple keys pressed simultaneously
Reply #8 - Nov 30th, 2007, 2:16pm
 
http://processing.org/reference/key.html
Re: Multiple keys pressed simultaneously
Reply #9 - Nov 30th, 2007, 3:12pm
 
Unfortunately, I've just noticed that the reference for key say specifically "he system variable key always contains the value of the most recently pressed key" and I think that's not entirely true, since it's the most recently 'changed state' key I believe.
Re: Multiple keys pressed simultaneously
Reply #10 - Nov 30th, 2007, 3:37pm
 
yes, you're correct. i've updated the reference for the next release.
Page Index Toggle Pages: 1