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 › reading the keyboard
Page Index Toggle Pages: 1
reading the keyboard (Read 566 times)
reading the keyboard
Dec 15th, 2007, 2:25am
 
What are best practices in reading the keyboard?
Is there any way to poll to see if a particular key is currently down, rather than the "key has been pressed" event model, where key released doesn't even tell you which one?

I'm thinking in terms of games, where it would be nice to provide something like traditional WASD control... I know there are some complications with that, keyboards typically have arbitrary limits on how many keys they can chord etc, but I'm looking for a better solution than what I have now...
Re: reading the keyboard
Reply #1 - Dec 15th, 2007, 5:19am
 
So I came up with the following idea: use a HashSet to keep track of specific keydowns and keyups, and then a simple query function...

HashSet currentKeys = new HashSet();
void keyPressed(KeyEvent e){
 currentKeys.add(new Integer(e.getKeyCode()));
}
void keyReleased(KeyEvent e){
 currentKeys.remove(new Integer(e.getKeyCode()));
}
boolean isKeyDown(int c){
   return currentKeys.contains(new Integer(c));
}

Any comments? I know I might be glossing over the differences between different systems, what keycodes they generate, but for my quick and dirty purposes this seems to be doing the trick, without getting a key "stuck", though I don't know if keyreleases are guaranteed...
Re: reading the keyboard
Reply #2 - Dec 15th, 2007, 5:58am
 
this works for me
Code:

boolean [] keyDown = new boolean[255];

void setup() {
size(512,400);
frameRate(30);
}


void draw() {
background(0);
for(int i=0;i<255;i++) {
if(keyDown[i]==true) {
stroke(255);
line(i*2,0,i*2,height);
}
}
}


void keyPressed(){
println("key pressed "+key);
if (0 <= key && key < 255){
keyDown[key] = true;
}
}

void keyReleased(){
println("key released "+key);
if (0 <= key && key < 255){
keyDown[key] = false;
}
}


above code extended from this post
Code:

final static int NORTH = 1;
final static int EAST = 2;
final static int SOUTH = 4;
final static int WEST = 8;
int result;
float x,y;

void setup() {
size(512,400);
frameRate(30);
result = 0;
x = width/2;
y = height/2;
}


void draw() {
background(0);
switch(result) {
case NORTH: y--; break;
case EAST: x++; break;
case SOUTH: y++; break;
case WEST: x--; break;
case NORTH|EAST: y--; x++; break;
case NORTH|WEST: y--; x--; break;
case SOUTH|EAST: y++; x++; break;
case SOUTH|WEST: y++; x--; break;
}
fill(255);
rect(x,y,10,10);
}


void keyPressed(){
switch(key) {
case('w'):case('W'):result |=NORTH;break;
case('d'):case('D'):result |=EAST;break;
case('s'):case('S'):result |=SOUTH;break;
case('a'):case('A'):result |=WEST;break;
}
}

void keyReleased(){
switch(key) {
case('w'):case('W'):result ^=NORTH;break;
case('d'):case('D'):result ^=EAST;break;
case('s'):case('S'):result ^=SOUTH;break;
case('a'):case('A'):result ^=WEST;break;
}
}


best,
andi
Page Index Toggle Pages: 1