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 & HelpPrograms › Smooth keyboard input
Page Index Toggle Pages: 1
Smooth keyboard input? (Read 478 times)
Smooth keyboard input?
Aug 29th, 2006, 3:15pm
 
Hey,

I was wondering how you could make the keyboard input smooth. By that I mean not needing to hold the key and it still repeatedly inputs it. My code:

int player_size = 16;
float player_x = 100;
float player_y = 100;

void setup()
{
 size(200,200);
 stroke(#FF0000);
 fill(#FF0000);
 framerate(30);
}

void keyPressed()
{
 if (keyCode == UP)
 {
   player_y--;
 }
 if (keyCode == DOWN)
 {
   player_y++;
 }
 if (keyCode == LEFT)
 {
   player_x--;
 }
   if (keyCode == RIGHT)
 {
   player_x++;
 }
}

void draw()
{
 background(0);
 rect(player_x,player_y,player_size,player_size);
}

The player movements arent very smooth, and only one key work at a time, so its 4 directional.
Re: Smooth keyboard input?
Reply #1 - Sep 3rd, 2006, 4:17am
 
The problem is the method keypressed only have the last key that you press, but If you have to combine keypressed and release to know what key remain pressed.

Here have a new code. Enjoy
Code:

int player_size = 16;
float player_x = 100;
float player_y = 100;

boolean _up, _down, _left, _right;
void setup()
{
size(200,200);
stroke(#FF0000);
fill(#FF0000);
framerate(30);
}

void keyReleased()
{
if (keyCode == UP)
{
_up = false;
}
if (keyCode == DOWN)
{
_down = false;
}
if (keyCode == LEFT)
{
_left = false;
}
if (keyCode == RIGHT)
{
_right = false;
}
}

void draw()
{
if(keyPressed){
println(keyCode);
if (keyCode == UP){ _up = true; }
if (keyCode == DOWN){_down = true; }
if (keyCode == LEFT){_left = true;}
if (keyCode == RIGHT){_right = true;}
}
if (_up == true){ player_y--; }
if (_down == true){player_y++; }
if (_left == true){player_x--; }
if (_right == true){player_x++; }
background(0);
rect(player_x, player_y, player_size, player_size);
}


Mar
Re: Smooth keyboard input?
Reply #2 - Sep 6th, 2006, 1:02am
 
Thanks mcanet, im also working on a similar code and was trying ot fix this problem.
Page Index Toggle Pages: 1