'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 functions I use work.
Solution 1:[1]
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.
Solution 2:[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.
Solution 3:[3]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Serge Ballesta |
Solution 2 | Clarus |
Solution 3 | bpinhosilva |