Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Delayed Keyboard Input in Allegro 5.0.8

Credits go to Edgar Reynaldo and Thomas Fjellstrom for helping out!
This thread is locked; no one can reply to it. rss feed Print
Delayed Keyboard Input in Allegro 5.0.8
ph03nix
Member #15,028
April 2013
avatar

I'm porting a game to C++ using Allegro 5.0.8, and I noticed that the keyboard input is a bit delayed. It is not consistently delayed, but rather it occasionally delays input for maybe around 10 ms or so (subtle but noticeable).

I'm using al_get_keyboard_state once per iteration to get the key info, and then using al_key_down for each key that my game needs. I can't use events for the keys since they seem to only be able to read one press and release per iteration, meaning if two keys are released at the same time it will assume one was never released.

I'm new to Allegro, but I've looked through the online manual extensively before coming here and couldn't find any alternatives to capturing key presses. Is this a known problem with allegro, or is there a proper way to capture key presses that I'm not aware of?

Thomas Fjellstrom
Member #476
June 2000
avatar

You can actually read more than one key press/release per iteration. What you should be doing is handling/draining ALL events in the queue each time through your loop.

There is no other way to do it properly. Otherwise your queue gets backed up and you start getting WAY behind.

check the wiki's tutorials.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

ph03nix
Member #15,028
April 2013
avatar

Thanks for the quick replies. I have seen this example before, but when I look at the event properties I see that it only stores one key code. They even use a switch for the key code in the example. If you release up and right at the exact same time for example, only one of these will be sent to the switch and the other will not be considered released. I don't see how it's possible for one event with one key code to act multiple times ???

Thomas Fjellstrom
Member #476
June 2000
avatar

ph03nix said:

I don't see how it's possible for one event with one key code to act multiple times

Check out the last if(). that makes sure it doesn't do any drawing till the event queue is empty. So every single frame, it processes ALL events before continuing.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

ph03nix
Member #15,028
April 2013
avatar

I see, you are absolutely right. I've tried it out and it seems to be working perfectly, although I'll need extensive testing before I can be sure. Sorry about the dumb question (although to be fair the event system is just a little confusing)

Thomas Fjellstrom
Member #476
June 2000
avatar

It's something to get used to. But a big benefit is you never lose key presses. Where as with polling you can, and probably will.

I'm pretty sure that method in the tutorial works fine. A lot of people use it (and I wrote that variant of it for the tutorial).

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Just track the state of the keys you wanna know about :

#SelectExpand
1 2int keysdown[ALLEGRO_KEY_MAX]; 3for (int i = 0 ; i < ALLEGRO_KEY_MAX ; ++i) {keysdown[i] = 0;} 4 5do { 6do { 7 ALLEGRO_EVENT ev; 8 al_wait_for_event(queue , &ev); 9 10 if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { 11 keysdown[ev.keyboard.keycode] = 1; 12 } 13 if (ev.type == ALLEGRO_EVENT_KEY_UP) { 14 keysdown[ev.keyboard.keycode] = 0; 15 } 16 17 /// Here's where you put what you had before 18 if (ev.type == ALLEGRO_EVENT_TIMER) { 19 // do logic, check keystates here 20 if (keysdown[ALLEGRO_KEY_RIGHT]) { 21 vx = 100.0f; 22 } 23 } 24 25} while (!al_is_event_queue_empty()); 26 27Display(); 28} while (!quit);

Thomas Fjellstrom
Member #476
June 2000
avatar

Just track the state of the keys you wanna know about :

Guess what that tutorial I linked to does ;)

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

ph03nix
Member #15,028
April 2013
avatar

So I've been testing it with the events rather than the keyboard state, and I've noticed that it's almost the same, although maybe a little better. Most of the time it feels pretty responsive when I jump for example, however sometimes I notice a very slight delay, and occasionally a noticeable one. My code is now virtually identical to that example in regards to input. Because this is a game I'm porting, I compared it to the original and there isn't such a delay.

Go to: