Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

getch() function

Status
Not open for further replies.

qUarC

Computer
Joined
Jan 14, 2005
Messages
3
Location
RO
I have written a simple game with a ball which bounces off the margins of the screen and at the bottom of the screen is my pad which is controlled by 'a' and 'd' buttons on my keyboard.:(simplified)
int c = 1;
while(1)
{
if(kbhit())
{
c = getch();
if(c == 27) exit(1);
if(c == 'a' || c == 'A') movepadleft();
if(c == 'd' || c == 'D') movepadright();
}
movetheball();
}
The thing that i don't like is: when i press a or d the pad moves for a short period of time, then stops and then moves constantly as long as i keep the button pressed.(it is the same thing just if i open notepad and press a letter: the letter i press apears, then there is a pause and then it prints that letter al long as i keep the button pressed).
This function(getch()) is not suitable for this problem? What alternatives i have to solve the problem?
 
I don't understand. What do you want it to do instead?

TTFN
 
I want the pad to move constantly as long as i keep the buton pressed. That's all.
 
You're bumping up against the keyboard repeat rate. You'll need accessto the raw data from the keyboard specifying when a key-up/key-down action has occured.
 
So you need to find the KeyPressed and KeyReleased or equivalent functions.

TTFN
 
Or you can perhaps change the typemetric(?) rate of your keyboard from BIOS settings.

Ciao.
 
And do you know what are the equivalent KeyPressed and KeyReleased functions because i have bc5.0 and i don't have them?
 
What OS are you writing this in? To get beyond the limitations of getch(), scanf(), etc., you may need to use an additional library. On *nix systems ncurses comes to mind.

-10d
 
Preseumably you are using Windows (kbhit doesn't exist on any other OS). You can change the typemetric rate from keyboard properties. The default is about .75 way down the scale. If you change it to .25 way down, it might achieve what you want.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top