0

I am making a game which has a character moves in 4 directions: up, down, left, right corresponding to W,S,A,D on the keyboard. The problem is when using getch() to get input from buffer, it always has a pause time after the first single keypress. For instance, when I hold 'A' button, it acts like: A(a short period of time)AAAAAAAAA.

How do I get rid of that delay time?

Any help would be appreciated.

(Answers in either C or C++ are all acceptable, since I am using graphics.h for this program, which requires C++ to run, but I mainly code in C).


I am using windows 10 64 bits.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Becker
  • 147
  • 7
  • 1
    https://stackoverflow.com/questions/448944/c-non-blocking-keyboard-input https://stackoverflow.com/questions/21294713/non-blocking-i-o-in-c-windows – Franck May 12 '22 at 06:48
  • 1
    It depends on your unknown OS. – Jabberwocky May 12 '22 at 06:59
  • 4
    If what you really want is "key up/key down" events, then you should look at something like SDL (https://www.libsdl.org/) – Frodyne May 12 '22 at 07:05
  • Does this answer your question? [Non-Blocking i/o in c? (windows)](https://stackoverflow.com/questions/21294713/non-blocking-i-o-in-c-windows) – irowe May 12 '22 at 12:44
  • @irowe I tried the solution in that topic but it act like: when a key is pressed, then the character keeps moving until it hits the wall, in that time, I still can change the direction. – Becker May 12 '22 at 13:37

2 Answers2

1

For a non-blocking getch method I personally use this piece of code (in C):

#include <conio.h>

int getch_noblock(){
   return _kbhit() ? _getch() : -1;
}

the _kbhit() method returns 0 if no key are pressed, otherwise returns a number if the keyboard input buffer is not empty.

the _getch() method read a char from the keyboard buffer.

It works only for Windows.

Documentation:

  1. _khbit(): https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/kbhit?view=msvc-170
  2. _getch(): https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getch-getwch?view=msvc-170

By the way, surfing on the web I found an interesting method, but I've never tried or seen it: https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getch-nolock-getwch-nolock?view=msvc-170

volpoh
  • 169
  • 1
  • 9
  • I have tried to use `getch_noblock()` instead of `getch()` but the program crash when I hit the first button. Do I need to re-config anything? – Becker May 12 '22 at 07:41
  • @Becker sorry for the stupid question, but did you include the conio.h library? – volpoh May 12 '22 at 07:42
  • Yes, I did. I tried to rebuild several times but doesn't work – Becker May 12 '22 at 07:43
  • Mh.. I can suggest you to flush the standard input keyboard buffer (stdin) with `fflush(stdin);` – volpoh May 12 '22 at 07:45
  • I tried it, but it still crash. The rolling circle of the mouse cursor start rolling immediately when the program run – Becker May 12 '22 at 07:49
  • @Becker could I see the complete code if not a problem? – volpoh May 12 '22 at 07:59
  • The complete code is pretty long for a fast review. But I only call this "key handling" function one time in a while loop. And I think the functions contain underscore prefix does not work on my machine. (Sorry for late repply). – Becker May 12 '22 at 13:25
  • @Becker I don't know what you could try, I'm sorry :( – volpoh May 12 '22 at 13:34
0

getch() already bypasses buffer:

 Prototype
         int _getch(void);  

 Description
         _getch obtains a character  from stdin. Input is unbuffered, and this
         routine  will  return as  soon as  a character is  available  without 
         waiting for a carriage return. The character is not echoed to stdout.
         _getch bypasses the normal buffering done by getchar and getc.

And for your problem, as someone already said, you can use the kbhit() method

al366io
  • 87
  • 8