Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Keyboard

This thread is locked; no one can reply to it. rss feed Print
Keyboard
vinze80
Member #7,072
March 2006

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
Member #1,786
December 2001
avatar

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
Member #7,072
March 2006

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

kazzmir
Member #1,786
December 2001
avatar

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

vinze80
Member #7,072
March 2006

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
Member #4,800
July 2004
avatar

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])
  {
              ...
        }
}

--
"The basic of informatics is Microsoft Office." - An informatics teacher in our school
"Do you know Linux?" "Linux? Isn't that something for visually impaired people?"

kazzmir
Member #1,786
December 2001
avatar

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
Member #7,072
March 2006

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
Member #4,800
July 2004
avatar

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.

--
"The basic of informatics is Microsoft Office." - An informatics teacher in our school
"Do you know Linux?" "Linux? Isn't that something for visually impaired people?"

vinze80
Member #7,072
March 2006

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
Member #7,343
June 2006
avatar

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
Member #5,982
July 2005
avatar

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

Click Here For My Website

Two Golden Rules of life- Firstly, I'm always right and secondly, if you think otherwise, slap your face and read rule number one again!

Go to: