Desiring bool any_key_pressed()

Adam D Ruppe destructionator at gmail.com
Sat Mar 4 12:44:06 UTC 2023


On Friday, 3 March 2023 at 03:38:56 UTC, Daren Scot Wilson wrote:
> Here is a very simple version of the program I'm working on.  
> Is there a way to write is_any_key_pressed() that doesn't 
> block, doesn't require the Enter key, and doesn't require 
> dragging in any complex libraries or dealing with low-level 
> stuff like ioctl()?

You have to do something since the normal behavior is the 
operating system holds on to the input until the user presses 
enter. You must tell it not to do that somehow.

My terminal.d lib at least stands alone (for now, the next 
version will probably require two files instead of just one) and 
has things for this:

http://arsd-official.dpldocs.info/arsd.terminal.html#single-key

It is `input.kbhit()` just to test without getting the thing. 
Though... testing for input like this is a kinda iffy thing to do 
too, you might be able to adjust the program to be more event 
driven.

Anyway, it is the "RealTimeConsoleInput" object in the library 
that tells the OS to stop buffering and send things directly. Its 
code isn't too complex - calls to tcgetattr and tcsetattr and 
similar for Windows - but still.


More information about the Digitalmars-d-learn mailing list