I’m looking for a predicate (or a way to implement it) for emulating the behavior of the C function _kbhit
. This function returns a nonzero value if a key has been pressed (corresponding to the key code) and otherwise, it returns 0. Reading the docs, I did not find it and thought that the following code might implement it:
go :-
current_input(Input),
character_count(Input, C),
loop(Input, C).
loop(Input, C) :-
repeat,
character_count(Input, C0),
(C == C0
-> % Do not wait for user input, do some stuff
% and repeat
fail
; with_tty_raw(get_single_char(_Code)),
% Do some other stuff with _Code
% and loop again
character_count(Input, C1),
loop(Input, C1)
).
But maybe I’m missing something because the character count increases even when there is no user input in the default current input stream (keyboard).
This was tested in the swipl-win console of SWI-Prolog 8.5.11 running on Windows 10.