Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Slow down the keyboard

This thread is locked; no one can reply to it. rss feed Print
Slow down the keyboard
DavyKager
Member #8,299
February 2007

Hi!

I want my program to play a sound when a certain key is pressed. But the problem is that this happens too rapidly when a key is hold down.

I know that I can use set_keyboard_rate, but I also read that this only affects readkey() while I use key[KEY_NAME]. Can anyone give me some advice on this?

Here a little example:
while (!key[KEY_ESC]) {
if (key[KEY_UP]) {
play_sample(sample, 255, 128, 1000, 0);
}
}

Kauhiz
Member #4,798
July 2004

The key array only tells you if a key is up or down. If you want to time that somehow, you'll have to do it yourself. That shouldn't be too hard, though.

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

DavyKager
Member #8,299
February 2007

So, with rest(time); everywhere? I think that's impossible, because I want to disable repeatment.

Kauhiz
Member #4,798
July 2004

Quote:

So, with rest(time); everywhere?

Of course not. That wouldn't really work, would it? I meant, do something like:

if(key[KEY_UP] && can_register_keypress)
{
  play_sample(sample, 255, 128, 1000, 0);
  keypress_countdown = 60; //Waits for 60 loops
  can_register_keypress = false;
}

if(keypress_countdown > 0)
  keypress_countdown--;
else if(!can_register_keypress)
  can_register_keypress = true;

</code>

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

Thomas Fjellstrom
Member #476
June 2000
avatar

if you want to stick to the key array, something like this should work:

int up_pressed = 0;
if(key[KEY_UP] && !up_pressed) {
   up_pressed = 1;
   do stuff;
}
else if(!key[KEY_UP] && up_pressed) {
   up_pressed = 0;
}
...

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

DavyKager
Member #8,299
February 2007

That looks great! I'll try something this weekend...

Kauhiz
Member #4,798
July 2004

Yeah, if you only want the sound to play once when a key is pressed, do what Thomas said.

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

James Stanley
Member #7,275
May 2006
avatar

If you only want it to play once when a key is pressed use readkey().

gnolam
Member #2,030
March 2002
avatar

... no. That's wrong.

To quote X-G: stop giving shoddy advice. :P

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Go to: