views:

77

answers:

1

An usual read statement in Fortran interrupts the execution of the program until the RETURN key was pressed. I am looking for a statement that reads any pressed key without waiting for the RETURN key. The program should not stop even if no key was pressed. Thank you for your answer.

Edit: Here is some source code that should clarify the question.

Program test1
  n=2
  do while (n==2)
    read (*,*) n
    write (*,*) 'Output'
  end do
end program test1

Program test2
  n=2
  do while (n==2)
    UnknownReadStatement (*,*) n
    write (*,*) 'Output'
  end do
end program test2

The program test1 will never write the word "Output" on the screen if no key is pressed.

Using the read statement I am looking for the program test2 should fill the screen with "Output" until a key different from "2" is pressed.

A: 

There is an example code for reading a single key from the terminal from Fortran without requiring that the input be terminated by a return key at http://home.comcast.net/~urbanjost/CLONE/GETKEY/getkey.html. I haven't tried this code, so can't vouch for it. His (John Ubran) solution mixes Fortran and C, using the C getkey. Assuming that your compiler supports it (most do), I suggest trying the ISO_C_BINDING method to combine the Fortran and C. This doesn't answer the part about the program proceeding even if no key is pressed -- for that you will have to add some sort of timeout, to give the person a chance to type something, but to timeout and proceed if they don't type by a deadline. Maybe you can modify the solution I linked to...

M. S. B.