Thread overview | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
December 08, 2010 undefined identifier getch() | ||||
---|---|---|---|---|
| ||||
Hey guys, I'm writing a console based tool for windows. To receive the users input, I try to use getch() but the compiler always says "Error: undefined identifier getch". When I use getchar() it compiles successfully, but getchar() doesn't returns after a single input. Is there any equivalent version in D for getch? Thanks & regards... |
December 08, 2010 Re: undefined identifier getch() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nrgyzer | On Wed, 08 Dec 2010 06:06:18 -0500, Nrgyzer <nrgyzer@gmail.com> wrote: > Hey guys, > > I'm writing a console based tool for windows. To receive the users input, I > try to use getch() but the compiler always says "Error: undefined identifier > getch". When I use getchar() it compiles successfully, but getchar() doesn't > returns after a single input. > > Is there any equivalent version in D for getch? > > Thanks & regards... Just a note, dmd on windows does not use Microsoft's C library, it uses Digital Mars'. Therefore, any C functions that are not part of the platform SDK are not available in D. To see what DMC supports, look here: http://www.digitalmars.com/rtl/rtl.html -Steve |
December 08, 2010 Re: undefined identifier getch() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | Okay, but what function can I use to get the pressed key? kbhit/_kbhit seems to do what I want, but hwo can I use it in D? I always get the error, that kbhit (or similar functions) are not available. import std.stdio; import std.c.stdlib; import std.c.windows.windows; void main(string[] args) { kbhit(); _kbhit(); } |
December 08, 2010 Re: undefined identifier getch() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nrgyzer | On Wed, 08 Dec 2010 08:25:38 -0500, Nrgyzer <nrgyzer@gmail.com> wrote: > Okay, but what function can I use to get the pressed key? > kbhit/_kbhit seems to do what I want, but hwo can I use it in D? I > always get the error, that kbhit (or similar functions) are not > available. > > import std.stdio; > import std.c.stdlib; > import std.c.windows.windows; extern(C) int kbhit(void); > void main(string[] args) { > kbhit(); > } In order to call a C function you have to declare it. That's all std.c.windows.windows does. Just not all the functions are declared. Plus, I saw that getch is in that header as well... http://www.digitalmars.com/rtl/conio.html#_getch -Steve |
December 08, 2010 Re: undefined identifier getch() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Wed, 08 Dec 2010 08:57:40 -0500, Steven Schveighoffer <schveiguy@yahoo.com> wrote:
> extern(C) int kbhit(void);
Um... sorry, D doesn't support void args (copy-paste problem):
extern(C) int kbhit();
-Steve
|
December 08, 2010 Re: undefined identifier getch() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | Great, thanks :) |
December 08, 2010 Re: undefined identifier getch() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nrgyzer | On 08/12/2010 11:06, Nrgyzer wrote: > Hey guys, > > I'm writing a console based tool for windows. To receive the users input, I > try to use getch() but the compiler always says "Error: undefined identifier > getch". When I use getchar() it compiles successfully, but getchar() doesn't > returns after a single input. Under DMD 1, getch is declared in std.c.stdio. So import that. Under DMD 2, I don't know why it isn't there. But you just need to add this declaration: extern (C) int getch(); > Is there any equivalent version in D for getch? D doesn't have its own API for console operations besides stdin/stdout/stderr stuff. I guess it wasn't worth creating one partly because it would be a load of pointless wrappers around C functions, and partly because in these days where everyone uses GUI-based software they're not used as much. Stewart. |
December 08, 2010 Re: undefined identifier getch() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nrgyzer | On 08/12/2010 13:25, Nrgyzer wrote:
> Okay, but what function can I use to get the pressed key?
> kbhit/_kbhit seems to do what I want, but hwo can I use it in D? I
> always get the error, that kbhit (or similar functions) are not
> available.
<snip>
kbhit just tests whether there's a keystroke waiting in the keyboard buffer. It doesn't identify the key pressed.
If you want to wait for the user to press a key, use getch.
If you want to test whether the user has pressed a key, use kbhit, then use getch to determine what key was pressed.
For some reason, this seems to work reliably only on ASCII character keys, backspace, tab and carriage return. But this might be partly due to bugs in the DM implementation.
Stewart.
|
December 08, 2010 Re: undefined identifier getch() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | On 12/08/10 08:53, Stewart Gordon wrote:
> On 08/12/2010 11:06, Nrgyzer wrote:
>> Hey guys,
>>
>> I'm writing a console based tool for windows. To receive the users
>> input, I
>> try to use getch() but the compiler always says "Error: undefined
>> identifier
>> getch". When I use getchar() it compiles successfully, but getchar()
>> doesn't
>> returns after a single input.
>
> Under DMD 1, getch is declared in std.c.stdio. So import that.
>
> Under DMD 2, I don't know why it isn't there. But you just need to add this declaration:
>
> extern (C) int getch();
>
>> Is there any equivalent version in D for getch?
>
> D doesn't have its own API for console operations besides stdin/stdout/stderr stuff. I guess it wasn't worth creating one partly because it would be a load of pointless wrappers around C functions, and partly because in these days where everyone uses GUI-based software they're not used as much.
>
> Stewart.
Might it be useful to provide something a la conio.h? It doesn't have to be part of the standard, really (conio isn't a C standard, as I recall). I do a bit of console oriented work myself, as I mostly write tools, back-end programs, etc.
Yes, it'd be mostly C wrappers, but it's never stopped us before.
-- Chris N-S
|
Copyright © 1999-2021 by the D Language Foundation