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.
Page Index Toggle Pages: 1
multiple keys (Read 756 times)
multiple keys
Jun 30th, 2005, 5:36pm
 
I know there have been some threads regarding the subject, but couldn't quite find what I'm looking for.

I want to be able to do something when certain keys and key combination are pressed. Like, if A is pressed, do A(), if B is pressed do B(), and if both are pressed, do AB().
The problem is that whenever a second key is pressed, the first one becomes false, although I'm still pressing it. It's the same with "key" and "keyCode". I guess I need some kind of sniffer that doesn't get fooled by a second keypress ...

I looked here ( http://java.sun.com/j2se/1.3/docs/api/java/awt/event/KeyEvent.html )  but couldn't find what I need.

Any ideas ?
Re: multiple keys
Reply #1 - Jun 30th, 2005, 8:02pm
 
Check out:
http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Contribution_Responsive;action=display;num=1117955653

It has a couple of methods to give you the information you want.
Re: multiple keys
Reply #2 - Jul 1st, 2005, 3:02pm
 
Yup, it was what I wanted. So, for the sake of sharing, here's what I did with it. I wan to control something with two sets of sensors on the floor. The problem was that somebody could step on two adjacent "keys" (the sensors are actually a keyboard board rewired) or step on a key and tap the other etc.

Anyway, here is my solution. You'll notice that almost everything is in pairs, that's because I have 2 sets of sensors.

Code:

String stringP1 = "1234567890-=";
String stringP2 = "qwertyuiop[]";
PFont font;
String st;
int[] keysPressed1 = new int[13];
int[] keysPressed2 = new int[13];
int num;

void setup(){
size(640,480,P3D);
background(255);
fill(0);
}



void draw(){
int temp1=0, temp2=0, tempNum=0;
background(255);
for (int i=0;i<stringP1.length();i++) {
if (keysPressed1[i] != 0){
temp1 += keysPressed1[i];
tempNum++;
}
}
if (tempNum>0) temp1 = temp1 / tempNum;

tempNum=0;
for (int i=0;i<stringP2.length();i++) {
if (keysPressed2[i] != 0){
temp2 +=keysPressed2[i];
tempNum++;
}
}
if (tempNum>0) temp2 = temp2 / tempNum;

rect(temp2, height-10, 110, 10);
rect(temp1, 0, 110, 10);
}



void keyPressed() {
num = stringP1.indexOf(key);
st = str(key);
if (num >= 0) keysPressed1[num] = getValue(stringP1, st, 1, 540);

num = stringP2.indexOf(key);
st = str(key);
if (num >= 0) keysPressed2[num] = getValue(stringP2, st, 1, 540);
}


void keyReleased() {
num = stringP1.indexOf(key);
if (num >= 0) keysPressed1[num] = 0;

num = stringP2.indexOf(key);
if (num >= 0) keysPressed2[num] = 0;

}


void mousePressed(){
}



int getValue(String myString, String myKey, int mapMin, int mapMax){
float pos = myString.indexOf(myKey);
float maxPos = myString.length()-1;
return round( mapMin+(pos/maxPos)*(mapMax-mapMin) );
}
Page Index Toggle Pages: 1