www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Non-blocking keyboard input

reply Joe bloow.edu <Joe bloow.edu> writes:
??????????????????????????????????????? Surely there there is a 
one liner library solution  for this?

I have a program that spawns a thread for debugging information 
and uses the keyboard input which allows me to display the 
information.

If I use getchar or readline then it blocks the thread. This is 
generally fine because that is all the thread does. The problem 
is that it also blocks the program termination as the main thread 
will not exit while the thread is running which is is because 
it's waiting on keyboard input stuck on getchar or fgetc or 
whatever.

If I terminate the threads using thread_term then it terminates 
the program but the program then does not return the return code 
that it was successfully finished(because it was prematurely 
terminated by thread_term.

Surely there is some type of peek for keyboards in D? I can't 
seem to get kbhit or because the library is not included(well, I 
tried to use one from dmc but it seemed to be invalid. snn.lib 
IIRC).


This shouldn't be hard... yet it is.
Dec 26 2023
next sibling parent reply Adam D Ruppe <destructionator gmail.com> writes:
On Wednesday, 27 December 2023 at 05:07:04 UTC, Joe wrote:
 ??????????????????????????????????????? Surely there there is a 
 one liner library solution  for this?
It is not one line because it needs a bit of setup (and teardown, but the objects' destructors do that for you) but it is close: http://arsd-official.dpldocs.info/arsd.terminal.html#single-key `input.getch` waits for a single line, but you can use `if(input.kbhit())` to see if it would block before calling it.
 This shouldn't be hard... yet it is.
Better be careful, the mods are out in force deleting posts this week that tell the hard truth. But yeah, the stdlib in D has very little activity: https://github.com/dlang/phobos/graphs/contributors? So you can't expect much from it. My arsd libs provide a broad set of functionality missing from it: stuff like this terminal/console stuff, window creation, basic guis, web servers, etc. If you want to try them, you can use it from the dub system, but I recommend just `git clone https://github.com/adamdruppe/arsd.git` in your working directory then import what you want and use `dmd -i` to automatically include them in the build.
Dec 27 2023
parent reply Joe bloow.edu <Joe bloow.edu> writes:
On Wednesday, 27 December 2023 at 13:27:53 UTC, Adam D Ruppe 
wrote:
 On Wednesday, 27 December 2023 at 05:07:04 UTC, Joe wrote:
 ??????????????????????????????????????? Surely there there is 
 a one liner library solution  for this?
It is not one line because it needs a bit of setup (and teardown, but the objects' destructors do that for you) but it is close: http://arsd-official.dpldocs.info/arsd.terminal.html#single-key `input.getch` waits for a single line, but you can use `if(input.kbhit())` to see if it would block before calling it.
 This shouldn't be hard... yet it is.
Better be careful, the mods are out in force deleting posts this week that tell the hard truth. But yeah, the stdlib in D has very little activity: https://github.com/dlang/phobos/graphs/contributors? So you can't expect much from it. My arsd libs provide a broad set of functionality missing from it: stuff like this terminal/console stuff, window creation, basic guis, web servers, etc. If you want to try them, you can use it from the dub system, but I recommend just `git clone https://github.com/adamdruppe/arsd.git` in your working directory then import what you want and use `dmd -i` to automatically include them in the build.
This does not actually work on my computer. It still blocks. int itr = 0; for(;;) { itr++; writeln("1. closeKBThread = ", closeKBThread, ", iter = ", itr); if (closeKBThread) return; string op = ""; string istr = ""; while (!closeKBThread) { writeln("2. ", input.kbhit(), " ", closeKBThread); if (input.kbhit()) { istr ~= input.getch(false); break; } } writeln("final istr = ", istr); 1. closeKBThread = false, iter = 1 2. false false 2. false false 2. false false 2. false false final istr = f 1. closeKBThread = false, iter = 2 2. false false < now blocking All this is just junk of me trying to figure out what was going on, but literally input.kbhit blocks after the first run(which really means it's always blocking) My original code was while(true) { if (input.kbhit()) { istr ~= input.getch(); break; } } and I've been trying all kinds of stuff to figure out what was going on but I believe it is the kbhit function itself. It calls getch(true) and blocks on that as if I were just using getch itself. The issue is the same, the code does not block then after I hit a key and it goes through an iteration of the outside loop it then blocks waiting for the next input.
Jan 14
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On Sunday, 14 January 2024 at 13:41:26 UTC, Joe wrote:
 This does not actually work on my computer. It still blocks.
Adam is no longer using mainstream D, and apparently not posting on this forum. I suggest you try to contact him via the arsd github page: https://github.com/adamdruppe/arsd -Steve
Jan 14
prev sibling parent reply Christian =?UTF-8?B?S8O2c3RsaW4=?= <christian.koestlin gmail.com> writes:
On Wednesday, 27 December 2023 at 05:07:04 UTC, Joe wrote:
 ??????????????????????????????????????? Surely there there is a 
 one liner library solution  for this?

 I have a program that spawns a thread for debugging information 
 and uses the keyboard input which allows me to display the 
 information.

 If I use getchar or readline then it blocks the thread. This is 
 generally fine because that is all the thread does. The problem 
 is that it also blocks the program termination as the main 
 thread will not exit while the thread is running which is is 
 because it's waiting on keyboard input stuck on getchar or 
 fgetc or whatever.

 If I terminate the threads using thread_term then it terminates 
 the program but the program then does not return the return 
 code that it was successfully finished(because it was 
 prematurely terminated by thread_term.

 Surely there is some type of peek for keyboards in D? I can't 
 seem to get kbhit or because the library is not included(well, 
 I tried to use one from dmc but it seemed to be invalid. 
 snn.lib IIRC).


 This shouldn't be hard... yet it is.
One option (not tested) should be to close stdin so that readln then returns null or something on eof. Shutting down threads is always tricky. It would be great if there would be one or two (perhaps one synchronous, one asynchronous) main io-frameworks for dlang (also as dub packages) that cover the most common use-cases. Kind regards, Christian
Dec 27 2023
parent Christian =?UTF-8?B?S8O2c3RsaW4=?= <christian.koestlin gmail.com> writes:
On Wednesday, 27 December 2023 at 14:41:05 UTC, Christian Köstlin 
wrote:
 One option (not tested) should be to close stdin so that readln 
 then returns null or something on eof.

 Shutting down threads is always tricky.

 It would be great if there would be one or two (perhaps one 
 synchronous, one asynchronous) main io-frameworks for dlang 
 (also as dub packages) that cover the most common use-cases.


 Kind regards,
 Christian
I tested this now, but it still blocks in readln ... Kind regards, Christian
Dec 27 2023