Questions tagged [kbhit]

kbhit is a C/C++ function used to determine if a key has been pressed or not

Prototype: int kbhit(void);
Header File: conio.h

If a key has been pressed then it returns a non zero value otherwise returns zero.

Declaration : int kbhit();

How to use:

#include <stdio.h>
#include <conio.h>

main()
{
   while (!kbhit())
      printf("You haven't pressed a key.\n");

   return 0;
}
51 questions
19
votes
3 answers

PyCharm: msvcrt.kbhit() and msvcrt.getch() not working?

I've tried to read one char from the console in PyCharm (without pressing enter), but to no avail. The functions msvcrt.getch() stops the code, but does not react to key presses (even enter), and msvcrt.kbhit() always returns 0. For example this…
user4953886
  • 191
  • 1
  • 1
  • 3
17
votes
5 answers

Using kbhit() and getch() on Linux

On Windows, I have the following code to look for input without interrupting the loop: #include #include #include int main() { while (true) { if (_kbhit()) { if (_getch() == 'g') …
Boxiom
  • 2,245
  • 6
  • 40
  • 51
10
votes
1 answer

How to wait for a key press in Clojure

I'd like to break out of a loop when the user presses a key. In C I'd use kbhit(). Is there a Clojure (or Java) equivalent?
justinhj
  • 11,147
  • 11
  • 58
  • 104
5
votes
2 answers

C++ loop until keystroke

If I want to loop until a keystroke there is a quite nice Windows solution: while(!kbhit()){ //... } But this is neither an ISO-Function nor works on other Operating Systems except MS Win. I found other cross-plattform solutions but they are…
NaN
  • 3,501
  • 8
  • 44
  • 77
4
votes
1 answer

can I display realtime feedback in a terminal game?

I'm trying to write a simple terminal-based dungeon-style game in C, but I want to give the user real-time feedback on various processes that they affect. I remember using kbhit() years ago, but I also remember the cpu going to 100% during the…
Korgan Rivera
  • 495
  • 3
  • 9
3
votes
1 answer

How to detect any keyboard button pressed in Ada language

I am a beginner in Ada programming. I am trying to make a "2048" game using Ada. I did "2048" in C++, and I used "kbhit()" to detect that is there any keyboard button pressed. I want to know that is there any similar function available like…
3
votes
3 answers

Is it possible to program your own kbhit() in C?

I took a class of programming at my university and I am working on some program. I want to know if it is possible to program my own kbhit() function. And if it is possible to look, how kbhit() is coded. The purpose is that I need to know how…
user4870780
3
votes
2 answers

How to run program whilst listening for user input in C?

I'm trying to make a game that continues running until a key is pressed and then it should take that key in and do something with it then continue running as per normal. How do I do this? I'm on MAC so even though I've come across a windows library…
Ikki
  • 33
  • 1
  • 5
2
votes
1 answer

Is separate thread a good strategy to terminate application with background tasks by keypress?

I've a console Java application, which does some tasks in the background. Java doesn't support console kbhit() for testing if there is something in keyboard buffer and as I know all console reads and tests are blocking. What I'm going to do is to…
Vladislav Rastrusny
  • 29,378
  • 23
  • 95
  • 156
2
votes
1 answer

portable alternative to kbhit() and getch() and system("cls")

I need a way to use kbhit and getch functionality in a portable way. I'm currently developing a simple ascii game and I need to detect if a key is pressed. If it is I need to read it and if it isn't I need to continue without waiting for input. I…
2
votes
1 answer

Why does kbhit() always return 0 when input is entered in the Eclipse console?

I am running Eclipse CDT (Helios) for C/C++ on Windows 7 x64. At first I was having the problem of output not appearing when run in the Eclipse console until the program exited, even though it did while running in a Windows console. I discovered…
Jeff Lockhart
  • 5,379
  • 4
  • 36
  • 51
2
votes
1 answer

Use of kbhit() with while loop

Here is the program. void main( ) { int h, v; h = 1; v = 10; while ( !kbhit( ) || h <= 80 ) { gotoxy( h, v ); printf( "<--->" ); delay( 200 ); clrscr( ); h = h + 1; } getch( ); } I am…
UmairKhan
  • 108
  • 1
  • 10
2
votes
2 answers

C Strange kbhit() behavior in delayed infinite loop

I have been testing some things with kbhit() and have found an odd behavior with a delayed infinite loop. In this code sample I have the loop delayed to run 30 times every second. #include #include #include #include…
NAME__
  • 625
  • 1
  • 7
  • 17
2
votes
7 answers

kbhit() as escape trigger

I want to use kbhit() for "Press any key to continue" function. However, after I used the kbhit() in a loop, the key-pressed is stored in the stdin. So in the next scanf(), the key-pressed from before, appears in the input. int x,b=0; …
Raam Kumar
  • 127
  • 2
  • 11
1
vote
1 answer

C kbhit() returns 1 even when key is not pressed

#include #include int main() { while(1) { printf("%d", kbhit()); Sleep(100); } return 0; } I noticed the kbhit() function was going weird in the game I was making so I tried this code. It…
CTTG
  • 27
  • 1
  • 7
1
2 3 4