View previous topic :: View next topic |
Author |
Message |
darthsight
Joined: 13 Nov 2007 Posts: 16
|
Posted: Tue Nov 20, 2007 2:25 pm Post subject: Get a character |
|
|
Hi,
I am working under Linux (Kubuntu 7.04) and I have to get a character from keyboard without enter.
The right function is getch(), but when I compile it returns this:
Code: |
pk-linux:~/prgd$ dmd moving3.d
gcc moving3.o -o moving3 -m32 -lphobos -lpthread -lm
moving3.o: In function `_Dmain':
moving3.d:(.gnu.linkonce.t_Dmain+0x16c): undefined reference to `getch'
collect2: ld returned 1 exit status
--- errorlevel 1
|
Searching with google, I found that getch() is a windows function and it is not implemented under Linux.
So, how I can get a character without enter key?
thanks |
|
Back to top |
|
|
pqnelson
Joined: 03 Jun 2007 Posts: 13 Location: Davis
|
Posted: Wed Nov 21, 2007 12:04 am Post subject: |
|
|
Use the std.c.stdio package, and use the getchar() method. It's the same getchar() from C.
That should work |
|
Back to top |
|
|
darthsight
Joined: 13 Nov 2007 Posts: 16
|
Posted: Wed Nov 21, 2007 12:58 am Post subject: |
|
|
Hi,
at the moment I am using getchar from std.c.stdio, but this command wait the enter key. What I want is get a character from keyboard without pressing the enter key.
Can you/someone help me? |
|
Back to top |
|
|
Dima-san
Joined: 25 Mar 2007 Posts: 33 Location: Almaty, Kazakhstan
|
Posted: Wed Nov 21, 2007 10:35 am Post subject: |
|
|
There was a function called kbhit(), which returned zero of no key was pressed and keynum otherwise. I haven't seen this function's prototype in std.c, though. |
|
Back to top |
|
|
csauls
Joined: 27 Mar 2004 Posts: 278
|
Posted: Wed Nov 21, 2007 2:47 pm Post subject: |
|
|
kbhit() is part of conio.h, not part of the standard. (Although, personally, I like conio.h)
Try fgetc(stdin) and see if it exhibits the same waiting behavior (I didn't think it was supposed to, but if getchar() is...).
I don't know whether anyone has tried making conio.h available to D, but if so that'd be a nifty way to go. _________________ Chris Nicholson-Sauls |
|
Back to top |
|
|
jcc7
Joined: 22 Feb 2004 Posts: 657 Location: Muskogee, OK, USA
|
Posted: Wed Nov 21, 2007 3:34 pm Post subject: |
|
|
Dima-san wrote: | There was a function called kbhit(), which returned zero of no key was pressed and keynum otherwise. I haven't seen this function's prototype in std.c, though. | Isn't kbhit() mentioned in std.c.stdio? |
|
Back to top |
|
|
darthsight
Joined: 13 Nov 2007 Posts: 16
|
Posted: Wed Nov 21, 2007 3:44 pm Post subject: |
|
|
kbhit() is another function avaible only for windows and not for linux. Anyway I tried to compile with kbhit() and it returns errors like getch.
fgetc(stdin) requires the enter key before processing, so I am at the starting point as same as getchar().
Maybe in linux there is some system variable (or constant?) that says me if there is data from keyboard. Does someone know something about that?
I am waiting for other helps with this trouble
Thanks to all |
|
Back to top |
|
|
ShprotX
Joined: 28 Aug 2007 Posts: 24 Location: Ukraine
|
Posted: Wed Nov 21, 2007 5:21 pm Post subject: Re: Get a character |
|
|
darthsight wrote: | So, how I can get a character without enter key? |
You need to change terminal mode into non-canonical. Here is an example in C:
Code: | #include <termios.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
struct termios term;
char buf;
tcgetattr(0, &term);
term.c_lflag &= ~(ECHO | ICANON | ECHONL);
tcsetattr(0, TCSANOW, &term);
while (1)
{
read(0, &buf, 1);
printf("%d\n", buf);
}
return 0;
}
|
Read man tcgetattr(3) for more details. |
|
Back to top |
|
|
ShprotX
Joined: 28 Aug 2007 Posts: 24 Location: Ukraine
|
Posted: Wed Nov 21, 2007 7:22 pm Post subject: |
|
|
I've ported the example to D and tango:
Code: | import tango.io.Stdout;
import tango.stdc.posix.unistd;
import termios;
int main(char[][] argv)
{
termios term;
ubyte buf;
tcgetattr(0, &term);
term.c_lflag &= ~(ECHO | ICANON | ECHONL);
tcsetattr(0, TCSANOW, &term);
while (1)
{
read(0, &buf, 1);
Stdout.formatln("{}", buf);
}
return 0;
} |
I can give module termios which is also ported from C (this is only partial port). |
|
Back to top |
|
|
darthsight
Joined: 13 Nov 2007 Posts: 16
|
Posted: Thu Nov 22, 2007 1:13 am Post subject: |
|
|
Okay, I tried to compile your source but it returns this
Code: |
pk-linux:~/prgd$ dmd try01.d
Unsupported environment; neither Win32 or Posix is declared
tango/io/DeviceConduit.di(48): function tango.io.DeviceConduit.DeviceConduit.toUtf8 function toUtf8 does not override any
|
where do I mistake? :-\ |
|
Back to top |
|
|
ShprotX
Joined: 28 Aug 2007 Posts: 24 Location: Ukraine
|
Posted: Thu Nov 22, 2007 2:26 am Post subject: |
|
|
darthsight wrote: | Okay, I tried to compile your source but it returns this
where do I mistake? :-\ |
Wrong compiler options. The easiest way to specify right is to use rebuild (It is included into DSSS). If you don't want to use it here is options:
Code: | gdc -fversion=Posix -I /usr/local/bin/../include/d -c terminal.d -o ./terminal.o
gdc -fversion=Posix -I /usr/local/bin/../include/d -c termios.d -o ./termios.o
gdc -rdynamic ./terminal.o ./termios.o -Wl,-lDG-tango-io -Wl,-lDG-tango-sys -Wl,-lDG-tango-stdc -Wl,-lDG-tango-sys-linux -Wl,-lDG-tango-stdc -Wl,-lDG-tango-io -Wl,-lDG-tango-text -Wl,-lDG-tango-io -o terminal |
where terminal.d is main application module, termios.d is ported C module and /usr/local/include/d is path to D interface modules. |
|
Back to top |
|
|
darthsight
Joined: 13 Nov 2007 Posts: 16
|
Posted: Thu Nov 22, 2007 7:22 am Post subject: |
|
|
Ok, I will try rebuild this night. But as I am a n00b, can you say me more about using rebuild, or give me some URL with some specifications?
Thank you very much |
|
Back to top |
|
|
ShprotX
Joined: 28 Aug 2007 Posts: 24 Location: Ukraine
|
Posted: Thu Nov 22, 2007 7:38 am Post subject: |
|
|
darthsight wrote: | Ok, I will try rebuild this night. But as I am a n00b, can you say me more about using rebuild, or give me some URL with some specifications?
Thank you very much |
man rebuild(1) will be more than enough. Just type "rebuild main_module_name.d" and wait for its completion. |
|
Back to top |
|
|
Dima-san
Joined: 25 Mar 2007 Posts: 33 Location: Almaty, Kazakhstan
|
Posted: Thu Nov 22, 2007 11:49 am Post subject: |
|
|
Dima-san wrote: | There was a function called kbhit(), which returned zero of no key was pressed and keynum otherwise. I haven't seen this function's prototype in std.c, though. |
std.c should be read as tango.stdc |
|
Back to top |
|
|
darthsight
Joined: 13 Nov 2007 Posts: 16
|
Posted: Thu Nov 22, 2007 5:11 pm Post subject: |
|
|
Okay, I downloaded termios.d and I try the source with rebuild but it returns this:
Code: |
pk-linux:~/prgd$ rebuild try01.d
WARNING: Module try01.d does not have a module declaration. This can cause problems
with rebuild's -oq option. If an error occurs, fix this first.
tango/io/DeviceConduit.di(48): function tango.io.DeviceConduit.DeviceConduit.toUtf8 function toUtf8 does not override any
|
Maybe I need some option with rebuild, but I don't understand the tango error.
Please help me |
|
Back to top |
|
|
|