readchar/1

readchar(Char)

Read a single character

Remarks

readchar/1 reads a single character from the current input device (which is the keyboard, unless changed via see/1). When reading from the keyboard, a number of keys, including the function and cursor keys, will return two characters, where the first is ASCII 0.

In Windows and OS/2 PM programs, readchar/1 will only work if the current input device is redirected to a file.

Notice the different behavior of readchar in OS/2 textmode programs when the current input device is the keyboard. The first called readchar/1 does not obtains the first typed char immediately, it will wait until the Enter will be pressed. Only then OS/2 makes input stream accessible to readchar/1. From this moment all characters, typed before the Enter, become immediately accessible to consequently called readchar/1 predicates (if they are). When all typed (before the Enter) chars are taken away from the input stream, then readchar/1 (if one is called) will again waiting for the Enter before receiving char from input stream. For example, the below fragment

counter(0,99,I), % (i,i,o); every time add 1 to I;

write("Line %", I),nl,

readchar(_),

fail.

can have the following output:

Line 0 % is printed immediately

123<Enter> % user types 123 and then press <Enter>

Line 1 % Lines 1,2 and 3 will be printed

Line 2 % immedaiately after <Enter> is pressed

Line 3 %

% readchar again will waiting for <Enter>

...

Example

write("\nEnter a character: "),

readchar(CH1),

write("\nThe character was: ",CH1),

CH1=0,

write("\nIt was a function key"),

readchar(CH2),

writef("\nKey code1=%d:'%c', Key code2= %d:'%c'",CH1,CH1,CH2,CH2).