找回密码
 注册
查看: 1393|回复: 4

请问linux有没有象getche那样的函数~~~

[复制链接]
发表于 2004-4-7 21:24:17 | 显示全部楼层 |阅读模式
要求是如果键盘有输入那么立即输入,不会象cin等那样需要 enter才正式输入。
用tc的时候可以包涵头文件 conio,但在LINUX下没有这个头文件。郁闷啊~~!!
发表于 2004-4-7 22:43:54 | 显示全部楼层
没有现成的函数,我有一个函数能实现这点,(ncurse有现成,但俺不喜欢那东东),明天给你一个例子
回复

使用道具 举报

 楼主| 发表于 2004-4-8 09:06:48 | 显示全部楼层
谢斑竹~~~~
回复

使用道具 举报

发表于 2004-4-8 12:50:27 | 显示全部楼层
[code:1]
#include <stdio.h>
#include <termios.h>
#include <sys/ioctl.h>
                                                                                                                                                
int Get_KeyValue()
{
        struct termios stored_settings;
        struct termios new_settings;
        int key_value;
        int key_press=0;
                                                                                                                                                
        tcgetattr(0,&stored_settings);
        new_settings = stored_settings;
        new_settings.c_lflag &= (~ICANON);
        new_settings.c_cc[VTIME] = 0;
        new_settings.c_cc[VMIN] = 1;
        tcsetattr(0,TCSANOW,&new_settings);
                                                                                                                                                
        ioctl(0,FIONREAD,&key_press);
        if(key_press)
        {
                key_value=getc(_IO_stdin);
        }
        else
        {
                key_value=0;
        }
        return key_value;
                                                                                                                                                
        tcsetattr(0,TCSANOW,&stored_settings);
                                                                                                                                                
}
main()
{
        int new_char=0;
        while(1)
        {
                new_char=Get_KeyValue();
                if(new_char)
                {
                        if(new_char=='q')
                                break;
                        printf("you press key:'%c'\n",new_char);
                }
                usleep(10000);
        }
}



[/code:1]
回复

使用道具 举报

发表于 2007-3-1 19:10:42 | 显示全部楼层
getch在windows中是在conio.h里的,不可移植的。

在linux下 :
#include <stdio.h>
#include <curses.h>

int main()
{
    initscr(); /* 开始curses模式 */  
    getch();
    endwin(); /* 结束curses模式 */
}

gcc -o 1.out 1.c -lcurses[/code]
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2025-5-19 20:37 , Processed in 0.043760 second(s), 16 queries .

© 2001-2025 Discuz! Team. Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表