Thread overview | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 21, 2011 Get single keystroke? | ||||
---|---|---|---|---|
| ||||
Is there a way to get a single keystroke in D2? Any method I've tried requires pushing Enter before the stroke is registered. |
March 21, 2011 Re: Get single keystroke? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Eskapp | On Mon, Mar 21, 2011 at 1:33 PM, Sean Eskapp <eatingstaples@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 |
March 21, 2011 Re: Get single keystroke? | ||||
---|---|---|---|---|
| ||||
Here's something simpler: import std.stdio : writefln; extern(C) int kbhit(); extern(C) int getch(); void main() { while(!kbhit()) { // keep polling // might use thread.sleep here. } writefln("Key hit was %s.", getch()); } |
March 21, 2011 Re: Get single keystroke? | ||||
---|---|---|---|---|
| ||||
Replace that writefln with this: writefln("Key hit was %s.", cast(char)getch()); You should get the right key displayed then. |
March 21, 2011 Re: Get single keystroke? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lars Holowko | Lars Holowko:
> what you want to do is OS dependent.
But the need to get a keystroke is simple and not so uncommon, so I think Phobos needs a function to do that that works on both Windows/Linux (and Mac too).
Bye,
bearophile
|
March 21, 2011 Re: Get single keystroke? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | == Quote from Andrej Mitrovic (andrej.mitrovich@gmail.com)'s article
> Here's something simpler:
> import std.stdio : writefln;
> extern(C) int kbhit();
> extern(C) int getch();
> void main()
> {
> while(!kbhit())
> {
> // keep polling
> // might use thread.sleep here.
> }
> writefln("Key hit was %s.", getch());
> }
What extra linker dependencies does this add?
|
March 21, 2011 Re: Get single keystroke? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 03/21/2011 02:42 PM, bearophile wrote: > Lars Holowko: > >> what you want to do is OS dependent. > > But the need to get a keystroke is simple and not so uncommon, so I think Phobos needs a function to do that that works on both Windows/Linux (and Mac too). If D sticks to the C (and C++) heritage, there is no guarantee that there is a keyboard around. The interface is stdin, stdout, and stderr; which are just character streams. But I agree: some subset of ncurses would be nice when a keyboard is available. > > Bye, > bearophile Ali |
March 21, 2011 Re: Get single keystroke? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | > Lars Holowko:
> > what you want to do is OS dependent.
>
> But the need to get a keystroke is simple and not so uncommon, so I think Phobos needs a function to do that that works on both Windows/Linux (and Mac too).
Assuming that it could be done cleanly, it would be a good function to add, but honestly, I dispute that it's a common need in this day and age. If all you need is something like "press any key to continue," you can easily do that with the current functions by having "press enter to continue," and if you need to do something other than that, odds are you need something more powerful like ncurses anyway. Not to mention, I don't think that interactive console apps are all that common these days. They definitely exist, but most console apps essentially run a single command for you and then quit. Most of the types of applications which would have have been interactive console apps in the past are now GUI apps.
So, while it certainly wouldn't hurt to add a function like this to Phobos (assuming that it could be done cleanly), I really don't think that it's a common need anymore.
- Jonathan M Davis
|
March 21, 2011 Re: Get single keystroke? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Eskapp | On 3/21/11, Sean Eskapp <eatingstaples@gmail.com> wrote:
> == Quote from Andrej Mitrovic (andrej.mitrovich@gmail.com)'s article
>> Here's something simpler:
>> import std.stdio : writefln;
>> extern(C) int kbhit();
>> extern(C) int getch();
>> void main()
>> {
>> while(!kbhit())
>> {
>> // keep polling
>> // might use thread.sleep here.
>> }
>> writefln("Key hit was %s.", getch());
>> }
>
> What extra linker dependencies does this add?
>
snn.lib
|
March 21, 2011 Re: Get single keystroke? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | == Quote from Andrej Mitrovic (andrej.mitrovich@gmail.com)'s article > On 3/21/11, Sean Eskapp <eatingstaples@gmail.com> wrote: > > == Quote from Andrej Mitrovic (andrej.mitrovich@gmail.com)'s article > >> Here's something simpler: > >> import std.stdio : writefln; > >> extern(C) int kbhit(); > >> extern(C) int getch(); > >> void main() > >> { > >> while(!kbhit()) > >> { > >> // keep polling > >> // might use thread.sleep here. > >> } > >> writefln("Key hit was %s.", getch()); > >> } > > > > What extra linker dependencies does this add? > > > snn.lib What about on Linux? |
Copyright © 1999-2021 by the D Language Foundation