Enter Home Page

GETKEY(3F/3C): read single character from keyboard in hot (raw I/O) mode

John S. Urban

GETKEY(3F) is a C/Fortran combination that (hopefully) lets Fortran read one character at a time in raw I/O mode on most platforms.

Unfortunately (as of this writing), there is no universal standard method using the programming languages Fortran or C for reading hot (raw I/O) keystrokes. In this document "Hot" or "Raw" mode, also sometimes called "immediate mode" means each keystroke is detected immediately, without requiring a carriage return.

The best way to know if this will work on your platform is to try it.

How it works

The getkey(3c) C routine uses commonly available routines to set to raw mode, read a keystroke, and reset to normal mode. Once this is working, it is typically easy to make a Fortran routine that calls the C routine. This example uses the five most common methods for calling C from Fortran, since how to make Fortran/C interfaces still varies if you do not have a Fortran 2003 compiler.

getkey.c is the core C routine that you must get working. Once this functions then you can try the three simple Fortran test programs ...

A bash shell script that shotguns for potential solutions

Alternatively, ksh(1) or bash(1) shell users can try getkey.ksh. This script generates all the above files and then tries to compile and load them.

The getkey.ksh script uses a "shotgun" approach to try to help you identify which compiler versions might work on your machine. If it gets far enough to build executables it will create a list of executable files like:

getkey+cc+f77+f77
getkey+cc+f90+f03
getkey+cc+f90+f77
getkey+cc+f90+f90
getkey+gcc
getkey+gcc+g95+f03
getkey+gcc+g95+f77
getkey+gcc+g95+f90
getkey+gcc+gfortran+f77
getkey+gcc+gfortran+f90
getkey+icc+ifort+f03
getkey+icc+ifort+f77
getkey+icc+ifort+f90

A name like getkey+icc+ifort+f03 means the Intel icc(1) and ifort(1) compilers were used with the Fortran 2003 test case to make this test executable.

Look at this log file; if you can read it you probably want the script.

MANUAL METHOD ...

Even without the getkey.ksh script, the steps to test the GETKEY(3F) procedure are relatively simple.

Test C program

First you need to make sure the C routine will work by itself ...

########################################
cc -DTESTPRG -DLinux getkey.c -o testit
   or
cc -DTESTPRG getkey.c -o testit
   or
cc -DBSD -DTESTPRG  getkey.c -o testit
   then
./testit
########################################

Test common Fortran/C interface style

Once the C program works; just make an object file and then load it with one of the Fortran programs, depending on which compilers you have

########################################
cc -DLinux getkey.c -o 
# call simple integer C function from Fortran
g95 f90.f90 getkey.o -o testit
./testit
########################################

Test using Fortran 2003 ISO_C_BINDING

If you have a Fortran 2003 compiler, there is a standard-based method for the Fortran-to-C interface that is preferable that uses the ISO_C_BINDINGS modules. Note that most f90+ compilers now support this 2003 feature as an extension ...

########################################
cc -DLinux getkey.c -o 
g95 M_getkey.F90 getkey.o -o testit
./testit
########################################

Running the Test Program

The sample program reads one character at a time until the letter "q" is entered. If the C program works in stand-alone mode but none of the Fortran examples work you will have to find out how your programming environment allows Fortran to call C routines. For this intentionally simple routine you usually just need to add an underscore to the C name (ie "_getkey" or "getkey_" or make it uppercase "GETKEY") to make getkeyC(3c) a Fortran-callable procedure.

Alternatives: