Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » keypressed

This thread is locked; no one can reply to it. rss feed Print
keypressed
clovekx
Member #3,479
April 2003
avatar

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
Member #7,080
April 2006
avatar

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

Or...

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

---------
Signature.
----
[My Website] | [My YouTube Channel]

clovekx
Member #3,479
April 2003
avatar

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
Member #794
November 2000
avatar

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
Member #3,479
April 2003
avatar

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

Krzysztof Kluczek
Member #4,191
January 2004
avatar

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
Member #2,559
July 2002
avatar

Maybe al_keydown that does what Krzysztof just did?

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

clovekx
Member #3,479
April 2003
avatar

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
Member #2,981
December 2002
avatar

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

Thomas Harte
Member #33
April 2000
avatar

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
Member #794
November 2000
avatar

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

Go to: