undefined identifier getch()

2010-12-08 Thread Nrgyzer
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...


Re: undefined identifier getch()

2010-12-08 Thread Steven Schveighoffer

On Wed, 08 Dec 2010 06:06:18 -0500, Nrgyzer nrgy...@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


Re: undefined identifier getch()

2010-12-08 Thread Nrgyzer
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();
}


Re: undefined identifier getch()

2010-12-08 Thread Steven Schveighoffer
On Wed, 08 Dec 2010 08:57:40 -0500, Steven Schveighoffer  
schvei...@yahoo.com wrote:



extern(C) int kbhit(void);



Um... sorry, D doesn't support void args (copy-paste problem):

extern(C) int kbhit();

-Steve


Re: undefined identifier getch()

2010-12-08 Thread Nrgyzer
Great, thanks :)


Re: undefined identifier getch()

2010-12-08 Thread Stewart Gordon

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.


Re: undefined identifier getch()

2010-12-08 Thread Stewart Gordon

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.