Discussion:
Non-blocking Input
(too old to reply)
Blair Vidakovich
2020-03-02 01:25:21 UTC
Permalink
Hello all,

I am writing a little tamagotchi-like virtual pet that sits in your
terminal.

You can find the code here:

https://github.com/bootlicker/uwu

I am having some issues. I want the little program to scan the keyboard
for input every game loop cycle, and if there is no key pressed, I want
the game to continue marching on in execution.

However, all of my attempts at creating a scanning function halt until
the user presses enter, even if they do not press a key.

I have looked at (read-char-no-hang), (listen), and this really cool
little package that someone wrote called 'terminal-keypress':

https://github.com/eudoxia0/terminal-keypress

I am using SBCL, and SLIME with EMACS.

Can someone point me in the right direction? I need a little help.

I have tried to code literately, so I hope that my code makes sense.

Thanks in advance,

Vidak
t***@google.com
2020-03-02 18:30:51 UTC
Permalink
Post by Blair Vidakovich
I am having some issues. I want the little program to scan the keyboard
for input every game loop cycle, and if there is no key pressed, I want
the game to continue marching on in execution.
However, all of my attempts at creating a scanning function halt until
the user presses enter, even if they do not press a key.
I don't have anything too specific to help you with, but the important thing
is to make sure that the program is reading directly from an unbuffered input
stream.

Emacs/Slime will certainly not work, and a lot of times even running SBCL
directly from the terminal will have a layer of buffering or editing software
that stops reading until an entire line has been entered.

But here are some links you could try:
https://stackoverflow.com/questions/20276738/reading-a-character-without-requiring-the-enter-button-pressed
http://coding.derkeiler.com/Archive/Lisp/comp.lang.lisp/2008-06/msg00246.html
Blair Vidakovich
2020-03-03 07:09:01 UTC
Permalink
Thanks for your suggestions :-)

Much appreciated :-)

Vidak.
Blair Vidakovich
2020-03-03 01:35:08 UTC
Permalink
Just for everyone's reference, I have decided to go with an ncurses
library for Common LISP which disables TTY buffering :-)

This little piece of code I found on cliki:

(defun y-or-no ()
(with-screen (scr :input-buffering nil :input-blocking t)
(clear scr)
(princ "Do you want to continue? [Y/N]" scr)
(refresh scr)
(event-case (scr event)
((#\Y #\y) (return-from event-case t))
((#\N #\n) (return-from event-case nil)))))

It uses the croatoan library.

This should allow me to get instantaneous key presses :-)

Loading...