keypressed
clovekx

I'd like to stop the program while there is a key pressed.

This freezes the program:

do{
  rest(1);
  poll_keyboard();    
}while(keypressed());

And this doesnt wait for the key to be released:

do{
  clear_keybuf();
  rest(1);
  poll_keyboard();    
}while(keypressed());

EDIT:
I've just done it this way (looping through key array):

1int done;
2
3do{
4 done=1;
5 
6 poll_keyboard();
7 for(int i=0;i<KEY_MAX;i++)
8 if(key<i>){
9 done=0;
10 break;
11 }
12
13 rest(5);
14}while(!done);
15
16clear_keybuf();

Is there some better way to do so?

CursedTyrant
int main()
{
  while (!keypressed())
  {
  }
  return 0;
}

Or...

int main()
{
  bool Running = true;
  
  while (Running)
  {
    if (keypressed()) { Running = false; }
  }
  return 0;
}

clovekx

The problem with your code is that it waits for a key to be pressed, but I want to wait for all keys to be released. Just adding '!' will not work.

Evert

Don't call poll_keyboard(); it's not nescessary.
Anyway, keypressed() returns TRUE as long as there is a key press waiting in the buffer. It's intended to be used with readkey(), which blocks until a key is pressed, it's not supposed to tell you if a key is pressed right now.
For that, use the key[] array.

clovekx

Wouldn't it be useful to add some key[KEY_ANY] to allegro, that will be set if any key is pressed?

Krzysztof Kluczek
Quote:

Wouldn't it be useful to add some key[KEY_ANY] to allegro, that will be set if any key is pressed?

I don't think it's necessary you can easily check entire array yourself. :)

do
{
  int i;
  for(i=0;i<KEY_MAX;i++)
    if(key<i>)
      break;
} while(i<KEY_MAX);

Also "KEY_ANY" should be probably reserved for "Any" key that might be added in future keyboards. ;)

CGamesPlay

Maybe al_keydown that does what Krzysztof just did?

clovekx
Quote:

Also "KEY_ANY" should be probably reserved for "Any" key that might be added in future keyboards. ;)

And what if there will be some "Max" key in the future?

BAF

Seems to me like setting KEY_ANY would be more optimal than scanning the entire array each time you want to know.

Thomas Harte

Future versions of Allegro have, if the plan hasn't quietly changed, the option of event based input. So if you're laying your code out like that you'd just do a loop like:

while(!KeyDown)
{
   al_WaitForEvent(&ev);
   switch(ev.Type)
   {
      case AL_KEYDOWN: KeyDown = true; break;
      /* check for other important stuff, like a quit message */
   }
}

Evert
Quote:

Future versions of Allegro have, if the plan hasn't quietly changed, the option of event based input.

Ithink that already works in 4.3

Thread #587596. Printed from Allegro.cc