|
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: |
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. --- |
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> --- |
Thomas Fjellstrom
Member #476
June 2000
|
if you want to stick to the key array, something like this should work:
-- |
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. --- |
James Stanley
Member #7,275
May 2006
|
If you only want it to play once when a key is pressed use readkey(). |
gnolam
Member #2,030
March 2002
|
... no. That's wrong. To quote X-G: stop giving shoddy advice. -- |
|