Get single keystroke?
Lars Holowko
lars.holowko at gmail.com
Mon Mar 21 14:28:24 PDT 2011
On Mon, Mar 21, 2011 at 1:33 PM, Sean Eskapp <eatingstaples at gmail.com> wrote:
> Is there a way to get a single keystroke in D2? Any method I've tried requires
> pushing Enter before the stroke is registered.
>
Hi Sean,
what you want to do is OS dependent.
I needed something similar ('press key to continue')
e.g.: for Windows
import core.sys.windows.windows;
bool kbHit() {
// inspired by
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1045691686&id=1043284392
HANDLE stdIn = GetStdHandle(STD_INPUT_HANDLE);
DWORD saveMode;
GetConsoleMode(stdIn, &saveMode);
SetConsoleMode(stdIn, ENABLE_PROCESSED_INPUT);
bool ret = false;
if (WaitForSingleObject(stdIn, INFINITE) == WAIT_OBJECT_0) {
uint num;
char ch;
ReadConsoleA(stdIn, &ch, 1, &num, cast(void *) 0L);
ret = true;
}
SetConsoleMode(stdIn, saveMode);
return ret;
}
void wait_for_key() {
writeln("\nPress any key to continue");
while (!kbHit())
{}
}
for Linux/Unix something like
http://www.linuxquestions.org/questions/programming-9/kbhit-34027/
should work.
Hope that helps,
Lars
More information about the Digitalmars-d-learn
mailing list