×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

getch() function

getch() function

getch() function

(OP)
  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?
   

RE: getch() function

I don't understand.  What do you want it to do instead?

TTFN

RE: getch() function

(OP)
  I want the pad to move constantly as long as i keep the buton pressed. That's all.

RE: getch() function

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.

RE: getch() function

So you need to find the KeyPressed and KeyReleased or equivalent functions.

TTFN

RE: getch() function

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

Ciao.

RE: getch() function

(OP)
  And do you know what are the equivalent KeyPressed and KeyReleased functions because i have bc5.0 and i don't have them?

RE: getch() function

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

RE: getch() function

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources