How to test if a key has been hit w/o waiting for a key ?

Martin Weitzel martin at mwtech.UUCP
Wed May 2 20:48:52 AEST 1990


In article <1990Apr30.182959.18254 at cti-software.nl> pim at cti-software.nl (Pim Zandbergen) writes:
[original q&a deleted]
>
>There is also way to find out if a key has been pressed in System V,
>but it is an *ugly* way.

If all keyboard input flows thru a central function of your program,
it's rather clean - just read a character with timeout set to 0
seconds and store that character in a static variable. Use some
impossible value in the static, if no "look ahead" character is
available. It's not extremly hard to extend the look ahead to a
buffer and count of the available characters or have an additional
function to purge them.

BTW: If someone request the above information from me, my first reaction
is to ask back: "Why do want to do this"? If the one who asks comes from
DOS-world, there's a good probability that it's rather a 'timed out
read' what he or she wants, and if you let them you can bet they will
write an idle loop until some input is available.

A: Ohhh, how *incredibly slow* is this UNIX system compared to good old
   DOS I used before ...
B: Let's see ... hmm <typing ps ... etc.>; Ehhm, what does this
   program called "humpfelgrumpf" that is running on the other virtual
   screen?
A: Oh, not much really, gathers some statistical information, not much
   work at all, some few tenths of a second every thirty seconds.
B: And in the meantime.
A: Just looks if a key has been pressed.
-- 
Martin Weitzel, email: martin at mwtech.UUCP, voice: 49-(0)6151-6 56 83



More information about the Comp.lang.c mailing list