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
0
votes
2 answers

how to clear the key buffer while using kbhit() and getch()

heu so I'm using the above stated windows functions which luckily are for windows 2000 and up, but in making a game on the console I've run into a problem: as soon as a key is pressed the console gets passed the kbhit() function no matter if a key…
Griffin
  • 2,399
  • 7
  • 48
  • 83
0
votes
0 answers

Implementing 'q' interrupt enabled loop exit

I'm trying to implement a loop that is exited solely by pressing the 'q' key, how do I go about it. This is for a shell I'm making, I've tried various input methods like getchar(), getch() and the expanded code of kbhit() {as kbhit() is not…
0
votes
1 answer

Memory leaks with kbhit for Linux

The code is here. When I run my program (I saved kbhit as a header file and kept it in my program folder), I get an uninitialized read access on the first instance of using kbhit (I am using DrMemory for memory debugging). I included sys/ioctl.h as…
0
votes
1 answer

How to detect without delay if a button is pressed continuously?

When you hold down a key on Windows, it presses it once, then there is a small delay of about 0.3 seconds, and only after that delay it starts pressing it continuously. Something like this: x (0.3 second delay) xxxxxxxx. If I use kbhit() to detect…
Firefighter123
  • 59
  • 1
  • 1
  • 4
0
votes
1 answer

Function kbhit to move object in C

This program is detecting right keyboard keys, but when I am trying to move object by pressing arrow on my keyboard, but when i do this it goes in the same line, no matter which arrow i am pressing. I am asking for help to move this object in…
0
votes
1 answer

Insert an empty space at the end of a string

I was wondering how can I insert an empty space in the string text (defined in char *text = argv[1];) For example, if a write: ./mar "Hello how are you" I would like to see/ Hello how are you Hello how are you Hello how are you Hello how are…
Cesar8489
  • 1
  • 1
  • 1
  • 1
0
votes
1 answer

Observing activites of running program from other C program. [Reporting Key Strokes]

I want to monitor a interactive C program (say program1), which is running on one terminal window. and takes input as number (0-9) As in monitoring I expect : when I will provide input to program1(running on separate terminal) my observer should…
Pert8S
  • 582
  • 3
  • 6
  • 21
0
votes
2 answers

How to reset kbhit()?

I am making typing game in which random alphabets falls down from top of screen to the bottom and user needs to press that key to get score. There are two nested loops use to make this falling effect. The outer while loop generates random alphabet…
0
votes
1 answer

Why my keyboard hit is evaluating false

I'm a C++ noob coding the snake game. The whole program draws the board, fruit and snake head perfectly, however I cannot seem to make the snake head coordinates change using a keyboard hit function. When going through the input function which gets…
0
votes
1 answer

kbhit and multiple calls on function

While making a asteroid shooter, I came around using _kbhit() and kbhit(). I'm no expert, but here is the problem I think I'm having: int run = 1; int main() { while(run){ if(GetUserInput() == 2) printf("W"); …
0
votes
0 answers

What is a not so complicated was to get a kbhit() equivalant on mac?

I am making a program on mac but I need a kbhit() equivalent and all the ones I see are too complicated, too long or I just can't use them in my code because I don't get it or I don't know how to add it in. Please note I have curses.h…
Asra
  • 151
  • 1
  • 11
0
votes
1 answer

Reset GetAsyncKeyState() to default

I am learning to use combination of kbhit(), GetAsyncKeyState() and getch() to build my console game. I'm using visual studio 2010 express, c/c++ headers are ok for me. Compability issues(only run at windows platform) does not matter for me. Take a…
mhythes
  • 11
  • 1
0
votes
1 answer

Why do I get different outputs by using different loops?

I made a code that inputs four letters, and prints an '*' in place of each one, as in a password. The working code is this: #include #include int main() { int c = 0; char d[4]; printf("Enter a character:"); while…
0
votes
2 answers

move cursor in c++ using gotoXY and kbhit

I want to move the position of symbol "A" in the terminal via the following code in c++, but the terminal closes and seems it does not enter the for loop. I don't know where I am wrong. I will be grateful if you help me: 'w' should move it up 's'…
SRYZDN
  • 307
  • 5
  • 13
0
votes
4 answers

Is there a way to replace the kbhit() and getch() functions in Standard C?

I'm trying to get a quick time event type of interaction with the console and I managed to obtain it using the conio library. Sadly the project I'm working on requires the code to be compilable on both Windows and Linux and I can't figure out a way…
user3481786
  • 1
  • 1
  • 1