Error in keyboard events in Allegro

Asked

Viewed 201 times

6

I’m doing a work of my course using Allegro as indicated.

I need to capture the events of loose keystrokes on the keyboard, so I used the following:

al_wait_for_event(evento, &ev);
if(ev.type == ALLEGRO_EVENT_KEY_UP){
   if (ev.keyboard.keycode==ALLEGRO_KEY_A || ev.keyboard.keycode==ALLEGRO_KEY_LEFT)
       cmd='a';
   else if (ev.keyboard.keycode==ALLEGRO_KEY_W || ev.keyboard.keycode==ALLEGRO_KEY_UP)
            cmd='w';
   else if (ev.keyboard.keycode==ALLEGRO_KEY_D || ev.keyboard.keycode==ALLEGRO_KEY_RIGHT)
            cmd='d';
   else if (ev.keyboard.keycode==ALLEGRO_KEY_S || ev.keyboard.keycode==ALLEGRO_KEY_DOWN)
            cmd='s';
}

But the event returns the key value 6 times instead of just one!

In the code I had still included (previously) these clauses:

al_install_keyboard();
[...]
al_register_event_source(evento, al_get_keyboard_event_source());

What am I missing?

  • Did you find a solution? Poste as an answer to help other people.

  • Which operating system? You are authorized to use an alternative method?

2 answers

0

I used this in a little game I made, I had to type the name, then I concatenated the string with the typed key:

//FUNÇÃO PARA INSERIR NOMES
void LE_TECLADO() {
  int tam;

  readkey();
  if (strlen(nome) < 13){
    if(key[KEY_A])
      strcat(nome,"A");
    else if(key[KEY_B])
      strcat(nome,"B");
    else if(key[KEY_C])
      strcat(nome,"C");
    else if(key[KEY_D])
      strcat(nome,"D");
    else if(key[KEY_E])
      strcat(nome,"E");
    else if(key[KEY_F])
      strcat(nome,"F");
    else if(key[KEY_G])
      strcat(nome,"G");
    else if(key[KEY_H])
      strcat(nome,"H");
    else if(key[KEY_I])
      strcat(nome,"I");
    else if(key[KEY_J])
      strcat(nome,"J");
    else if(key[KEY_K])
      strcat(nome,"K");
    else if(key[KEY_L])
      strcat(nome,"L");
    else if(key[KEY_M])
      strcat(nome,"M");
    else if(key[KEY_N])
      strcat(nome,"N");
    else if(key[KEY_O])
      strcat(nome,"O");
    else if(key[KEY_P])
      strcat(nome,"P");
    else if(key[KEY_Q])
      strcat(nome,"Q");
    else if(key[KEY_R])
      strcat(nome,"R");
    else if(key[KEY_S])
      strcat(nome,"S");
    else if(key[KEY_T])
      strcat(nome,"T");
    else if(key[KEY_U])
      strcat(nome,"U");
    else if(key[KEY_V])
      strcat(nome,"V");
    else if(key[KEY_X])
      strcat(nome,"X");
    else if(key[KEY_Z])
      strcat(nome,"Z");
    else if(key[KEY_SPACE])
      strcat(nome," ");
    else if(key[KEY_0_PAD])
      strcat(nome,"0");
    else if(key[KEY_1_PAD])
      strcat(nome,"1");
    else if(key[KEY_2_PAD])
      strcat(nome,"2");
    else if(key[KEY_3_PAD])
      strcat(nome,"3");
    else if(key[KEY_4_PAD])
      strcat(nome,"4");
    else if(key[KEY_5_PAD])
      strcat(nome,"5");
    else if(key[KEY_6_PAD])
      strcat(nome,"6");
    else if(key[KEY_7_PAD])
      strcat(nome,"7");
    else if(key[KEY_8_PAD])
      strcat(nome,"8");
    else if(key[KEY_9_PAD])
      strcat(nome,"9");
    else if(key[KEY_0])
      strcat(nome,"0");
    else if(key[KEY_1])
      strcat(nome,"1");
    else if(key[KEY_2])
      strcat(nome,"2");
    else if(key[KEY_3])
      strcat(nome,"3");
    else if(key[KEY_4])
      strcat(nome,"4");
    else if(key[KEY_5])
      strcat(nome,"5");
    else if(key[KEY_6])
      strcat(nome,"6");
    else if(key[KEY_7])
      strcat(nome,"7");
    else if(key[KEY_8])
      strcat(nome,"8");
    else if(key[KEY_9])
      strcat(nome,"9");
  }
  if(key[KEY_BACKSPACE]) {
    tam = strlen(nome);
    if (tam > 0)
      nome[tam-1] = '\0';
  }

}

0


Apparently the problem was a bond that surrounded that code region or the region that I called printf("%c", cmd);. I can’t remember, it’s been three years.

Who had a similar problem: take care of your ties (I don’t understand how you got so many up’s this question).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.