Message from discussion Get Key Function?
From: Clive Page <use...@page2.eu>
Subject: Re: Get Key Function?
Date: Sun, 3 May 2009 10:52:20 +0100
X-Trace: individual.net x6Vpr8FIb5ccch1hD/AZggC6mjOyyZDW+kHYKRQ0uT8KhCz/8r
User-Agent: Turnpike/6.07-M (<JPumxV1hGhefXN7tiXnze3N1tV>)
In message <87zldw6ajc....@roark.xbeta.org>, Jason Blevins
>it mentions reading single keystrokes. Unfortunately, it says there
>isn't a portable way to do this in Fortran, but he offers a C function
>with some instructions on how to call it from Fortran:
As noted earlier, this works on some Unix-like systems including Linux,
but not on Windows.
>I would imagine there is a way to do this in Fortran 2003 using
>stream input from stdin, but I couldn't get anything working.
>Maybe someone else knows...
>If you don't mind using something non-standard, I know that
>GFortran has an FGET extension for this. It's likely that
>G95 has a similar extension.
I've tried this too, but can't get it to read single keystrokes - the
keystroke read still needs a RETURN at the end.
On Windows the nearest I've been able to find uses a function in the
GrWin library, which can be downloaded from:
This contains a function kbgetch(n) which returns the integer code of
the next single keystroke. Normally n=0 but if n=1 then the character
is echoed to stdout.
This can be linked using g95 or gfortran on Windows-XP using a command
>g95 testkbd.f90 -Wl,--subsystem,console -lGrWin -mwindows -lg2c
I have no idea what the switch "-Wl,--subsystem,console" is doing, it is
just a bit of magic I found in the GrWin files. If anyone understands
it, please post.
It is, as you say, a pity that this cannot be done in a way that is
portable across operating systems.