Getch() Problem: C vs D

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jan 10 06:48:39 PST 2017


On Tuesday, 10 January 2017 at 02:37:31 UTC, LouisHK wrote:
> So, it will stay inside the case KEY_EVENT: and will check the 
> if KEY status is DOWN, otherwise will return null event.

So, the newest version of terminal.d already has a check for 
this... but you actually want to REQUEST key release events so 
kbhit doesn't clash with the system's release events.

Try this code on your version:

import arsd.terminal;
void main() {
	auto terminal = Terminal(ConsoleOutputType.linear);
	auto input = RealTimeConsoleInput(&terminal, 
ConsoleInputFlags.allInputEventsWithRelease);
	while(true) {
		if(input.kbhit()) {
			terminal.write(input.getch());
		} else terminal.write(".");
		terminal.flush();

		import core.thread;
		Thread.sleep(dur!"msecs"(50));
	}
}


It should give a steady series of dots unless you press a key. It 
should never stop giving dots (that means kbhit returned true 
when getch wouldn't actually return) and not print extra on 
release (though note that key repeat may give several down events 
as you hold it).


I might provide a helper function for that input thing 
eventually... but idk how yet, that RealTimeConsoleInput thing 
uses RAII to change terminal state so it cannot be copied - you 
always must pass by pointer, and I can't do that when returning 
from a function! Maybe I'll do a mixin to make the setup a bit 
simpler.


More information about the Digitalmars-d-learn mailing list