|
Capturing a keypress in an event loop |
nshade
Member #4,372
February 2004
|
I'm trying to capture if any key was pressed, however my event queue is kind of all over the place. (It's spanned across several c files) It's not detecting if I have a key pressed... Because my C files are getting on the heavy side, I'll cut/paste the relevant info. From main.c 1ALLEGRO_TIMER *timer = NULL;
2ALLEGRO_EVENT_QUEUE *event_queue = NULL;
3ALLEGRO_EVENT event;
4
5al_install_keyboard();
6
7int main(int argc, char **argv)
8{
9 init_gfx();
10
11 while (1)
12 {
13
14 al_wait_for_event(event_queue, &event);
15
16 if if ((event.type == ALLEGRO_EVENT_TIMER) || (event.type == ALLEGRO_EVENT_KEY_DOWN))
17 {
18 logics();
19 need_redraw = true;
20 }
21
22 if (need_redraw && al_event_queue_is_empty(event_queue))
23 {
24 al_clear_to_color(al_map_rgb_f(0, 0, 0));
25 drawing();
26 al_flip_display();
27
28 }
29
30 }
31
32}
gfx.c 1
2extern ALLEGRO_TIMER *timer
3extern ALLEGRO_EVENT_QUEUE *event_queue
4extern ALLEGRO_EVENT event;
5
6void logics(void)
7
8{
9 if (event.type == ALLEGRO_EVENT_TIMER) { printf("timer\n"); }
10 if (event.type == ALLEGRO_EVENT_KEY_DOWN) { printf("Keyboard!\n"); }
11 switch (title_state)
12 {
13 case scrolling:
14 { ......}
15 }
16}
The keyboard events don't seem to be be coming though. I just see it printing "timer" on my screen. Did I for forget to get something? |
beoran
Member #12,636
March 2011
|
It is hard to say from your code but did you initialize your event queue with al_create_event_qyeue and register the keyboard event source with al_register_event_source? |
nshade
Member #4,372
February 2004
|
Whoops found my event initialization function.. Guess what was missing.. Thanks. 1void init_events(void)
2{
3 // Start a timer to regulate speed. 60bps
4 timer = al_create_timer(ALLEGRO_BPS_TO_SECS(60));
5 al_start_timer(timer);
6
7 // Start the event queue to handle keyboard input and the timer
8 event_queue = al_create_event_queue();
9 al_register_event_source(event_queue, al_get_timer_event_source(timer));
10
11 // I forgot this
12 al_register_event_source(event_queue, al_get_keyboard_event_source());
13}
|
beoran
Member #12,636
March 2011
|
Well, it happens to everyone at times. Glad I could be of help. |
|