Allegro.cc - Online Community

Allegro.cc Forums » Allegro Development » Keyboard Event Issue (aka: The Tales of a man who didn't knew how to code)

Credits go to Edgar Reynaldo for helping out!
This thread is locked; no one can reply to it. rss feed Print
Keyboard Event Issue (aka: The Tales of a man who didn't knew how to code)
mumu1000
Member #16,842
April 2018

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:

#SelectExpand
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
Major Reynaldo
May 2007
avatar

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.

mumu1000
Member #16,842
April 2018

Thank you alot (again ;p )

I think it will solve my problem ! :D

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

DanielH
Member #934
January 2001
avatar

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))

bamccaig
Member #7,536
July 2006
avatar

Niunio
Member #1,975
March 2002
avatar

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

-----------------
Current projects: Allegro.pas | MinGRo

mumu1000
Member #16,842
April 2018

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

Go to: