Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » ALLEGRO_EVENT_KEY_DOWN repeating

This thread is locked; no one can reply to it. rss feed Print
ALLEGRO_EVENT_KEY_DOWN repeating
wdtracy
Member #15,680
July 2014

Hey guys, I'm new to Allegro and have been following some tutorials. I'm not new to game programming so I understand the concept of ALLEGRO_EVENT_KEY_DOWN and that it registers the initial keypress and not the hold. When I run my program, it registers the key being held down as continuous ALLEGRO_EVENT_KEY_DOWN events. I'm not using custom KB software that would turn trigger key presses when I hold the key down, so I am not sure what is going on. The guy in the video specifically says his doesn't repeat when the key is held down. Any ideas? Here is the code:

#SelectExpand
1#include <string> 2#include <allegro5\allegro.h> 3#include <allegro5\allegro_native_dialog.h> 4#include <allegro5\allegro_primitives.h> 5 6int main(int argc, char **argv) 7{ 8 int width = 640; 9 int height = 480; 10 11 bool done = false; 12 int pos_x = width / 2; 13 int pos_y = height / 2; 14 ALLEGRO_EVENT ev; 15 16 ALLEGRO_DISPLAY *display = NULL; 17 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 18 19 if(!al_init()) { 20 al_show_native_message_box(NULL, "Allegro Error", "Error", "Failed to initialize Allegro!", NULL, NULL); 21 return -1; 22 } 23 24 display = al_create_display(width, height); 25 if(!display) { 26 al_show_native_message_box(NULL, "Allegro Error", "Error", "Failed to create display!", NULL, NULL); 27 return -1; 28 } 29 30 al_init_primitives_addon(); 31 al_install_keyboard(); 32 33 event_queue = al_create_event_queue(); 34 al_register_event_source(event_queue, al_get_keyboard_event_source()); 35 al_register_event_source(event_queue, al_get_display_event_source(display)); 36 37 int x = 1, y = 1; 38 39 while(!done) 40 { 41 42 al_wait_for_event(event_queue, &ev); 43 44 if(ev.type = ALLEGRO_EVENT_KEY_DOWN) 45 { 46 x > width ? x = 1 : ++x; 47 y > height ? y = 1 : ++y; 48 49 switch(ev.keyboard.keycode) 50 { 51 case ALLEGRO_KEY_UP: 52 pos_y -= 10; 53 break; 54 case ALLEGRO_KEY_DOWN: 55 pos_y += 10; 56 break; 57 case ALLEGRO_KEY_RIGHT: 58 pos_x += 10; 59 break; 60 case ALLEGRO_KEY_LEFT: 61 pos_x -= 10; 62 break; 63 case ALLEGRO_KEY_ESCAPE: 64 done = true; 65 break; 66 } 67 } 68 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 69 { 70 done = true; 71 } 72 73 al_clear_to_color(al_map_rgb(0, 0, 0)); 74 al_draw_circle(pos_x, pos_y, 30, al_map_rgb(100, 180, 50), 5); 75 al_draw_filled_circle(x, y, 10, al_map_rgb(100, 180, 0)); 76 al_flip_display(); 77 } 78 79 al_destroy_display(display); 80 81 return 0; 82}

Arthur Kalliokoski
Second in Command
February 2005
avatar

You need to enable warnings. You have an assignment on line 44 when you're trying to test for equality.

if(ev.type = ALLEGRO_EVENT_KEY_DOWN)

should be

if(ev.type == ALLEGRO_EVENT_KEY_DOWN)

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

wdtracy
Member #15,680
July 2014

Wow, I can't believe I missed that one. Sheesh! Thanks for the catch!

Arthur Kalliokoski
Second in Command
February 2005
avatar

I didn't see it myself either, the compiler told me when I habitually use the -Wall switch. (Warn all errors)

t.c:44:3: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   if(ev.type = ALLEGRO_EVENT_KEY_DOWN)

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

Thomas Fjellstrom
Member #476
June 2000
avatar

Confusingly -Wall doesn't actually enable all warnings. You need -Wextra as well I think.

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

Elias
Member #358
May 2000

Indeed. (In the past -Wextra was called just -W.) I always use this:

gcc -Wall -Wextra -Werror

--
"Either help out or stop whining" - Evert

Thomas Fjellstrom
Member #476
June 2000
avatar

I tend to only use -Werror in special cases. I hate getting stuck in unimportant warnings.

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

Arthur Kalliokoski
Second in Command
February 2005
avatar

Actually for this program gcc was complaining about al_show_native_message_box() too, it doesn't like NULL for the flags.

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

Go to: