Keyboard
vinze80

Hi,
I have a little problem with allegro with the keyboard.
I would like when I press a key on continue, that a circle move uniformly. For the moment my circle moves first one case, after it stops, and after it moves uniformly.

This is a short passage of how I manage the keyboard.

int touche;
while (valid==0)
{
  if(keypressed())
  {
    touche=readkey();
    if(touche>>8 == KEY_UP)
    {....
                }
        }
}

kazzmir

It has something to do with the repeat sensitivity of the keyboard. I forget how to change that, maybe you can look it up, but heres some code that might work better in the long run.

while ( valid == 0 ){
   if ( key[ KEY_UP ] ){
        ...
   }
}

The key[] array is automatically updated by allegro via interrupts so you dont need any other special code to detect when the keyboard is pressed.

vinze80

I try as you said me. but it is always the same. one foot, one stop and go on continue.

kazzmir

Can you paste the rest of the code that you use to move your circle?

vinze80
1int valid=0;
2int x, y;
3int touche;
4 
5...
6 
7while (valid==0)
8{
9 if(keypressed())
10 {
11 touche=readkey();
12 if(touche>>8 == KEY_UP)
13 {
14 y--;
15 rectfill(screen, 10*x, 10*(y+1), 10*(x+1), y+2),makecol(50, 50, 50));
16 circlefill(screen, x*10+5, y*10+5, 5, makecol(200,200,50));
17 }
18 if(touche>>8 == KEY_DOWN)
19 {
20 ...
21 }
22 }
23}

This is my code. 8-)

Michael Faerber

Using readkey() and keypressed() will have that effect. You need to replace readkey() and keypressed() with key[].

[EDIT]
Like this:

while (valid==0)
{
  if(key[KEY_UP])
        {
              y--;
              rectfill(screen, 10*x, 10*(y+1), 10*(x+1), y+2),makecol(50, 50, 50));
              circlefill(screen, x*10+5, y*10+5, 5, makecol(200,200,50));
        }
  if(key[KEY_DOWN])
  {
              ...
        }
}

kazzmir

Vinze, can you paste your code using the key[] array so we can be sure you are using it correctly?

Michael, no offense but thats almost exactly what I wrote above anyway :p.

vinze80

I tried with "key[KEY_UP]" exactly the same as Michael has written.
But with that, I lose completely the control of my keys.
If I press only one time KEY_UP, my circle reaches at the top of the screen. Same for the others keys.

Michael Faerber
Quote:

Michael, no offense but thats almost exactly what I wrote above anyway :p.

Yes, but I thought he didn't get it, because he said that it was still the same behaviour.

Quote:

But with that, I lose completely the control of my keys.
If I press only one time KEY_UP, my circle reaches at the top of the screen. Same for the others keys.

You have to divide your program into logic and draw parts. See this and search the forums.

vinze80

Tanks for the link, I searched how can I use timer with allegro.
I have already read some things about timer on French web (because I am French), and I didn't understood very well the timers.
I hope with this link I will understand timers and I will succeed of my keyboard problem.

vixinu

for Michael's code

Michael Faerber said:

while (valid==0)
{
  if(key[KEY_UP])
        {
              y--;
              rectfill(screen, 10*x, 10*(y+1), 10*(x+1), y+2),makecol(50, 50, 50));
              circlefill(screen, x*10+5, y*10+5, 5, makecol(200,200,50));
        }
  if(key[KEY_DOWN])
  {
              ...
        }
}

It probably should be

while (valid==0)
{
  if(key[KEY_UP])
        {
              y--;
              rectfill(screen, 10*x, 10*(y+1), 10*(x+1), y+2),makecol(50, 50, 50));
              circlefill(screen, x*10+5, y*10+5, 5, makecol(200,200,50));
        }
  while (key[KEY_DOWN])
        {
              ...
        }
}

Lucid Nightmare

check this out... its from the allegro keyboard routines- http://shamis.0nyx.com/download.html or http://shamis.0nyx.com/zips/kr.zip

Thread #585970. Printed from Allegro.cc