3

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 functions I use work.

Jon Taylor
  • 7,865
  • 5
  • 30
  • 55
  • possible duplicate of [Detecting keystrokes](http://stackoverflow.com/questions/3571212/detecting-keystrokes) – Chris Dodd May 06 '15 at 18:41
  • possible duplicate of [Implementing a KeyPress event in C](http://stackoverflow.com/questions/21091191/implementing-a-keypress-event-in-c/21101030#21101030) – Chris Dodd May 06 '15 at 18:44

3 Answers3

3

Yes and no.

C language has no notion of input and output. It relies on a standard library (essentially written in C) that in turn relies on system calls.

Neither the standard library, nor the set of system calls common to Unix-like systems and Windows deal with non blocking system calls, so you have to call system specific ones.

But again, you can call them easily from C language.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
2

It depends.

On windows stdio (standard io, like stdin/stdout) is always blocking and thus you need to use os specific system calls to avoid a blocking call like read.

On Linux you can change stdio to be non blocking using fcntl thus avoiding the need for specialized function calls.

Clarus
  • 2,259
  • 16
  • 27
1

Yes, it is possible.

That's how it works:

It returns a non-zero integer if a key is in the keyboard buffer. It will not wait for a key to be pressed.

Basically you check from stdin (assumed to be default input data from keyboard in C language).

There is an implementation here where you can start from.

bpinhosilva
  • 692
  • 1
  • 5
  • 15