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 › pressing two keys at the same time
Page Index Toggle Pages: 1
pressing two keys at the same time? (Read 1073 times)
pressing two keys at the same time?
Feb 6th, 2006, 9:00pm
 
hi

i have here a small codeexample to make my problem clear.

void setup()
{
 size(200, 200);
 framerate(5);
}
void draw() {
 if(keyPressed) {
   if( key == 'a') {
     print("1");
   }
   if( key == 's') {
     print("2");
   }
 }
}

if you press "a" and "s" at the same time, the programm is only printing the "2" but not the "1". is it possible to change this?

mfg willy
Re: pressing two keys at the same time?
Reply #1 - Feb 6th, 2006, 9:22pm
 
because Java is in a sandbox, direct routines that would allow such spiffy hacks as "multiple simultaneous keypresses" would have to be written, possibly with low-level assembler routines, which just aren't available in standard Processing.

A better way to do this is to use an array [] of keys and toggle them realtime as they are pressed with a multi-threaded solution.  then in the software, reference the keys.  that's the sort of 'hot keyboard' effect achieved by most videogames released since Mode-X in 1990.
Re: pressing two keys at the same time?
Reply #2 - Feb 6th, 2006, 9:55pm
 
can you explane this a bit preciser?
my english is not very good, please try to phrase simply.
Re: pressing two keys at the same time?
Reply #3 - Feb 7th, 2006, 12:46pm
 
You need to keep track of the key presses and releases yourself if you want ot chek on multiple keys.

This is a simple example for the two specific keys. You can extend on this to have any number you want...

Code:
boolean[] keys;

void setup()
{
size(200, 200);
framerate(5);
keys=new boolean[2];
keys[0]=false;
keys[1]=false;
}
void draw()
{
if( keys[0])
{
print("1");
}
if( keys[1])
{
print("2");
}
}

void keyPressed()
{
if(key=='a')
keys[0]=true;
if(key=='s')
keys[1]=true;
}

void keyReleased()
{
if(key=='a')
keys[0]=false;
if(key=='s')
keys[1]=false;
}
Re: pressing two keys at the same time?
Reply #4 - Feb 7th, 2006, 2:04pm
 
great, thank you

mfg willy
Page Index Toggle Pages: 1