Keyboard Event Issue (aka: The Tales of a man who didn't knew how to code)

Hello everybody !
I'm having trouble using Allegro's Event system, and since i'm quite new to programming i can't figure out what's going wrong...

I'm trying to use a really simple test code:

1#include <iostream> 2#include "allegro5/allegro.h" 3 4int main(int argc, char **argv) 5{ 6 al_init(); 7 ALLEGRO_DISPLAY * mainDisplay = al_create_display(1200,700); 8 if(!al_install_keyboard()) 9 { 10 cout << "Keyboard Installation Failed"; 11 return 1; 12 } 13 ALLEGRO_EVENT_SOURCE * keyboardEventSource = al_get_keyboard_event_source(); 14 ALLEGRO_EVENT_QUEUE * keyboardEventQueue = al_create_event_queue(); 15 ALLEGRO_EVENT * keyboardEvent; 16 al_register_event_source(keyboardEventQueue,keyboardEventSource); 17 do 18 { 19 while (!al_get_next_event(keyboardEventQueue,keyboardEvent)) 20 { 21 } //I know this while loop is ugly, but thats just a test code OK ? ;) 22 //It crashes here (when i press a key down)... :-/ 23 }while (keyboardEvent->type != ALLEGRO_EVENT_KEY_DOWN); 24 25 int key = keyboardEvent->keyboard.keycode; 26 cout << key; 27 return 0; 28}

Everything works fine, until i press a key, then my program crashes with this error code: 0xC0000005 (no more comments)

Where did I make a mistake ? :(

Thanks for your help ! :D

PS: I'm on Win10 and i'm using C::B

Edgar Reynaldo
mumu1000 said:

 ALLEGRO_EVENT * keyboardEvent;

That should be an ALLEGRO_EVENT, not an ALLEGRO_EVENT*. A pointer is an address, and in this case you didn't initialize the value either. So when al_wait_for_event returns and tries to fill in the data at the address of the ALLEGRO_EVENT you passed into it, it crashes, with an access violation.


Thank you alot (again ;p )

I think it will solve my problem ! :D

Edgar Reynaldo

That should fix it.

Bump if you have questions.

ALLEGRO_EVENT keyboardEvent;


// also change this to pass the address of the event object

while (!al_get_next_event(keyboardEventQueue, @keyboardEvent))

while (!al_get_next_event(keyboardEventQueue, &keyboardEvent))

DanielH said:


FTFY. The address-of ('&') operator in C returns the address of its operand.


Looks like DanielH is using Pascal or some other Wirth-lesque language. ;)


Thanks alot for your replies !
I shall ask you if I have any questions ! (I will definitely have some later ;p)

Thread #617396. Printed from