| mygod 回复于:2001-12-29 13:57:32
|
getc(stdin)
|
| nico 回复于:2001-12-29 14:38:55
|
mygod错了!getc()虽然不要回车,但要让系统处理必须调皮个回车,不信你可以去试一下。 至于该怎样才能做到这一点,我也不知道。
|
| mygod 回复于:2001-12-29 14:47:21
|
那就是getch()了,反正是两者之一! 小问题
|
| fanyan1999 回复于:2001-12-29 16:50:03
|
用IOCTL还是FCNTCL的(记不清了)设置输入端口参数
|
| vt8000 回复于:2001-12-30 12:20:36
|
程序实现: #include <stdio.h> #include <termio.h>
static struct termio ttysave ; void restore() ; char getch() ;
main() { puts( "---------------------------"  ; puts( "enter a key : "  ; getch() ; puts( "\n--------------"  ; exit( 0  ; }
char getch() { static char ch ; static int total, flag = 1 ; struct termio tty ;
if( flag  { flag = 0 ; if( ioctl( 0, TCGETA, &tty  == -1  { perror( "ioctl"  ; exit( -1  ; } ttysave = tty ; tty.c_lflag &= ~( ICANON | ECHO | ISIG  ; tty.c_cc[VMIN] = 1 ; // MIN tty.c_cc{VTIME] = 0 ; // TIME if( ioctl( 0, TCSETAF, &tty  == -1  { restore(); perror( "ioctl"  ; exit( -2  ; } }
switch( total = read( 0, &ch, 1  ) {
case -1 : restore() ; perror( "read"  ; exit( 3  ; case 0: restore(): fputs( "EOF error!", stderr  ; exit( 4  ; default: ..... } restore(); return( ch  ; }
void restore() { if( ioctl( 0, TCSETAF, &ttysave  == -1  { perror( "ioctl"  ; exit( 5  ; } return ; }
|
| vt8000 回复于:2001-12-30 12:23:06
|
其说明可参见《深入学习UNIX》清华大学出版社 刘祖亮编著 1997年8月第一版
|
| tangram 回复于:2001-12-31 13:24:36
|
谢谢各位!! getch()的确可以,相信vt8000提供的代码也没有问题,因为我是看到类似的程序才提这个问题的~~
那么能否再讲讲这两者之间的异同呢?
|
| vt8000 回复于:2001-12-31 13:43:25
|
在UNIX里并没有直接的getch,能读入一个字符而不回车自行进行读取工作。 另外一种方法是利用curses库,再用getch(): #include <curses.h> #include <sys/select.h>
int Zinkey() { int k; fd_set f; struct timeval t;
FD_ZERO( &f  ; FD_SET( 0, &f  ; t.tv_sec = 0; t.tv_usec = 100000L; if( !select( 1, &f, 0, 0, &t    return( 0  ; if( ( k = getch()  == ERR  k = 0; return( k  ; }
|
| Allen1981 回复于:2002-11-14 16:49:11
|
getchar()可以读入int吗?
|
| 离了水的蛤蟆 回复于:2002-11-14 21:09:52
|
不可以
|
| millercn 回复于:2002-11-15 14:18:36
|
被蛤蟆吃到的天鹅不是好天鹅。
|
| Allen1981 回复于:2002-11-15 16:15:15
|
getchar()如若我强行打入一窜int, 是不是会转换成char?
|
| HopeCao 回复于:2002-11-15 16:21:24
|
???
|
| cpingyu1021 回复于:2002-11-15 18:49:29
|
我想问一下,linux下的getch()怎样使用,上面的程序抱错,说if( ( k = getch()  == ERR  中有问题?具体为undefine refer to stdscr undefine refer to wgetch, 具体怎么解决。快帮忙?本人非常感谢!!!
|
| beggar 回复于:2002-11-17 15:43:46
|
[quote][b]下面引用由[u]cpingyu1021[/u]在 [i]2002/11/15 06:49pm[/i] 发表的内容:[/b] 我想问一下,linux下的getch()怎样使用,上面的程序抱错,说if( ( k = getch()  == ERR  中有问题?具体为undefine refer to stdscr undefine refer to wgetch, 具体怎么解决。快帮忙?本 ... [/quote]
add param -lcurses when compile.
|
| 踢死牛 回复于:2002-11-26 11:19:36
|
while( 1) { if(kbhit()) *c++ = getch();
if( *c == '\n') dosomething(); } 我糊编的没准可以完美工作 ^.^
|
| route_998 回复于:2003-02-18 15:58:09
|
按VT8000提供的方法(include <curses.h>)使用函数编译不能通过!
怎么回事?
|
| route_998 回复于:2003-02-18 15:59:18
|
按VT8000提供的方法(include <curses.h>)使用函数编译不能通过!
怎么回事?
|
| route_998 回复于:2003-02-18 15:59:38
|
按VT8000提供的方法(include <curses.h>)使用函数编译不能通过!
怎么回事?
|
| ksc 回复于:2003-02-18 16:03:14
|
getch() not define
and
ERR not define
|
| tomken 回复于:2003-02-18 19:50:24
|
gcc -o [exe] [proc] -lcurses
[code:1:74ca2f5291]
int ch;
fcntl( 0, F_SETFL, O_NONBLOCK);
// 无阻塞状态
while(1)
{
ch = getch()
switch(ch)
{
case 27:
exit(0);
break;
.......
}
// 没有按键也到这里
// ........
}
[/code:1:74ca2f5291]
|