Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Keyboard event queue not really empty when it should be

This thread is locked; no one can reply to it. rss feed Print
Keyboard event queue not really empty when it should be
Aleksa
Member #14,874
January 2013

I made function that gets changes of keys from queue. Problem in this case is that when i press and hold single key that event is registered once more in next if even after al_get_next_event (which should remove that event from queue). It only happens on key DOWN, not UP.

#SelectExpand
1void keyboard_receive(void) { 2 /* Checks if there is anything in keyboard_event_queue */ 3 if (!al_is_event_queue_empty(keyboard_event_queue)) { 4 5 /* Get whats on queue */ 6 al_get_next_event(keyboard_event_queue, &keyboard_event); 7 8 /* Reads event and acts accordingly */ 9 // some code 10 11 /* Checks if there is anything left in queue - if there is, receive it */ 12 if (!al_is_event_queue_empty(keyboard_event_queue)) { 13 al_peek_next_event(keyboard_event_queue, &keyboard_event); 14 printf("\n key = %i", keyboard_event.keyboard.keycode); 15 keyboard_receive(); 16 } 17 } 18}

torhu
Member #2,727
September 2002
avatar

If that is your actual code, you are probably getting an ALLEGRO_EVENT_KEY_CHAR event. You need to check for the event types you are interested in, and ignore the others.

http://liballeg.org/a5docs/trunk/events.html#allegro_event_key_down

Aleksa
Member #14,874
January 2013

It's mine code. I am checking for event type in "//some code" part. Only source that is registered to keyboard_event_queue is keyboard.

I attached whole .c file.

torhu
Member #2,727
September 2002
avatar

Allegro can add events to the queues at any time, it uses a separate thread for it. You have to get all events, then ignore the ones you don't care about.

If you print the type of the unexpected events, you will probably see that it is ALLEGRO_EVENT_KEY_CHAR.

Edgar Reynaldo
Member #8,592
May 2007
avatar

Recursion is not really called for here. Just loop over the queue's events.

while (!al_is_event_queue_empty(queue)) {
   al_get_next_event(queue , &ev);
   if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
      keys[ev.keyboard.keycode] = true;
   }
   else if (ev.type == ALLEGRO_EVENT_KEY_UP) {
      keys[ev.keyboard.keycode] = false;
   }
}

torhu
Member #2,727
September 2002
avatar

Yeah, don't use recursion in C unless it simplifies your code significantly. There's no tail call elimination, it's slow, and you risk running out of stack space :P

Aleksa
Member #14,874
January 2013

I used recursion as i just wanted to make it work for now (was first thing on my mind). Implementing while loop is giving me expected results which is that system is simulating key rapid key press when key is held down. I'm just not sure why first solution was not giving same result :)

torhu
Member #2,727
September 2002
avatar

The system is not simulating key repeat, it's a basic keyboard feature that goes back decades ;)

By the way, why was using recursion the first thing on your mind? Did you learn a functional programming language first?

Aleksa
Member #14,874
January 2013

Because i was like lets do this over and over until there is no stuff in queue, which should work if I call same function again. Not sure what you mean by second question. First and almost only programming language I started learning (and still trying to learn) is C, which i learn on my own from book and internet :)

Go to: