Chris Dollin said:
Not /portably/, as in, not if you restrict yourself to the facilities
defined by the standard.
You need libraries not defined by the standard, with enough coverage that
you can run the code on Systems Of Your Choice.
In ISO/IEC 9899:1999 (E) there already are signal handlers
that might be used to implement such a feature.
Some implementations of C can detect a »break key«
(like »[Ctrl]-[C]«) to invoke a signal handler in
accordance with ISO/IEC 9899:1999 (E):
#include <stdio.h> /* printf */
#include <signal.h> /* signal, SIGINT, SIG_DFL, sig_atomic_t */
static volatile sig_atomic_t looping = 1;
static void h( int const sig ){ looping = 0; }
int main( void )
{ signal( SIGINT, h ); printf( "looping ...\n" );
while ( looping ); printf( "terminated.\n" );
signal( SIGINT, SIG_DFL ); }
stdout
looping ... terminated.
This might allow for some simple key-press games: For example,
the loop might emit random numbers, and the user has to press
the interrupt key as soon as he sees a certain number - then
his reaction time is determined.
ISO/IEC 9899:1999 (E) gives explicit permission to an
implementation to add custom signals:
»Additional signals and pointers to undeclarable
functions, with macro definitions beginning, respectively,
with the letters SIG and an uppercase letter or with SIG_
and an uppercase letter, may also be specified by the
implementation.«
Thus, a conforming implementation might add specific signals
for several keys, and these signals (due to their naming
scheme »SIG_...«) still will have partial standard semantics.
In this way, more elaborate games can be played using several
distinct »interactive attention« keys. (The standard semantics
of SIGINT is »receipt of an interactive attention signal«.)
But, admittedly, code using such additional signals might not
work in the intended way on every C implementation.