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
1
vote
1 answer

Alternative function for _kbhit in C++?

I am currently trying to develop a simple snake game on ios. The tutorial requires me to use however this does not work on my mac. What would be an alternative for _kbhit ? I am new to c++ so would appreciate any help!
Curly22
  • 11
  • 1
1
vote
1 answer

Why would this exemple only works with a breakpoint

I'm creating a basic console application in C/C++. In the following example I'm repeatedly writing some char to the console with a 50ms delay and I want it to exit the program when I hit a key. #include "pch.h" #include #include…
Tom23
  • 37
  • 1
  • 6
1
vote
1 answer

No relevant answers on the actual behavior of kbhit() on characters such as ", %, ~ in Windows 10 when keyboard and locale are US (not international)

Windows 10 with latest updates installed on a Dell XPS13. US keyboard layout and US locale selected (not international). Still a call to kbhit() or _kbhit() with specific characters such as ", ~, % does not return the key hit, at least mot until a…
Patrick
  • 21
  • 4
1
vote
1 answer

How do I use kbhit in C?

#include #include #include #define Ukey 87 #define ukey 119 #define Dkey 115 #define dkey 83 #define Lkey 97 #define lkey 65 #define Rkey 100 #define rkey 68 int main(){ int x=0; int y=0; int prev=rkey; …
dormer
  • 23
  • 4
1
vote
1 answer

Using kbhit in C to control a BGI drawing of a robot

I have been trying various combinations of if and do while statements but can't get it to work correctly. We use Visual Studio 2015 and work with C code. The general aim of the code is to use BGI graphics to simulate a 2D robot (a circle and a line…
EllieC
  • 33
  • 1
  • 5
1
vote
3 answers

How do I make your program increment repeatedly until another key is pressed in a switch statement

I have been working on one of those snake games and I have a switch statement that says if a key is pressed to move the snake in a direction by incrementing/decrementing, but it will only do that if I hold it. I am looking for a way to have the…
Ryan Shesler
  • 119
  • 8
1
vote
1 answer

Can I use windows.h on OS X?

Im studying computer science and we work with visual studio in calls. I prefer to work with my mac and the windows.h is not supported. Is there any alternate? can i use sleep or _khbit somehow?
Gal Zaken
  • 21
  • 2
1
vote
3 answers

How to make a loop in C++ that runs a time counter and allows for user input simultaneously (non-blocking user input)

So, what I am trying to do is create a function in C++ that allows the user to input anything and everything into a string for a set amount of time (say, ten minutes (or even one)) (and KEEP all the stuff they inputted, btw. I'm trying to design a…
1
vote
1 answer

How to use kbhit() with delay without waiting

I am making a program in which i have an infinite loop. I used kbhit() to detect whether a key is pressed or not. I am also using delay in the loop. So the problem is if i press the key i have to wait for the time given in delay function to perform…
Linkin
  • 11
  • 2
0
votes
1 answer

Implement Threading in C

I'm currently working on a mini game project, and I have a problem with this specific mechanic: void monsterMove(){ //monster moves randomly } void playerMove(){ //accepting input as player movement using W, A, S, D } However, the project…
bolakecil
  • 66
  • 1
  • 7
0
votes
1 answer

Cannot get _kbhit to work anymore on windows, issue with conio

Compiling Microsoft's sample program in C for using _kbhit thows me this error: fatal error: conio.h: No such file or directory I use Code::Blocks and GNU GCC compiler, and Windows 10. The weird thing about that is, that this code worked 2 months…
0
votes
0 answers

Holding a key, then pressing another, stops original key being pressed

(No, not a hardware issue) I have coded kbhit() in standard C as follows (obtained from here; another function "setTerm()" sets (non)canonical mode): #include int kbhit() { int bytesWaiting; …
Luke Dunn
  • 39
  • 5
0
votes
3 answers

Can a variable declared inside a function stays in the stack even after the function call gets over in C/C++?

Below is my code in C++. Here in the function "temp", 1st, the value of 'ch' is declared & then printed(at the first time 'ch' is not initialized so 'ch' has Undefined value).Then "if" condition is satisfied(Inside "if" no work is done). Then…
0
votes
0 answers

How can I use kbhit function in Mingw64?

I'm developing some game in Mingw, (It's school assignment so that i cannot change the development environment). I have to consider keypress event, so i used kbhit(), included in conio headerfile. However, it doesn't normally work in Mingw64…
Flower
  • 1
0
votes
1 answer

How can I use kbhit() and getch() on Linux? C++

I'm wrinig a simple snake game on C++. But I have a problem: I need to use kbhit() and getch() for read what the user enters. For use it I need conio.h but there're no this library on Linux. I tryed use this, but there're have a trouble: code is…
h4cktivist
  • 73
  • 3
  • 13