linux下如何得到类似getch功能

limlzm 2004-07-23 03:26:22
我想要的功能无非就是把程序停一停,类似用看帮助,按一下空格或者其他键就

继续按我需要显示n行printf出来。

由于getch要initscr()才能起作用,而且效果太差,每次调用都要把屏幕

clear,感觉不好,现在暂时用了getpass()函数充当一下,但一定要按一下回车才

行,还有没有其他好用的函数实现这样的功能呢?
...全文
297 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
pacman2000 2004-08-02
  • 打赏
  • 举报
回复
个人认为getch()没什么不好的。
柯本 2004-08-02
  • 打赏
  • 举报
回复
//不回显的测试程序,在linux 7.2下通过!
#include <termio.h>
#include <stdlib.h>
#include <stdio.h>
int main (int argc, char* argv[])
{
struct termios new_settings;
struct termios stored_settings;
tcgetattr (0, &stored_settings);
new_settings = stored_settings;
new_settings.c_lflag &= (~ICANON);
new_settings.c_cc[VTIME] = 0;
tcgetattr (0, &stored_settings);
new_settings.c_cc[VMIN] = 1;
tcsetattr (0, TCSANOW, &new_settings);
printf("ok\n");
system("stty -echo");
getchar();
system("stty echo");
tcsetattr (0, TCSANOW, &stored_settings);
return 0;
}
loverP 2004-08-02
  • 打赏
  • 举报
回复
getch()不回显,而getchar()回显。但标准C中只对getchar()作了要求,因此Linux中的编译器没有提供getch()。我想这才是让搂主郁闷的原因吧?
我没有学过Linux,不晓得有什么系统调用能够实现getch()的功能,但个人认为,使用内嵌的汇编语言直接访问键盘应该是可以实现的。
limlzm 2004-07-26
  • 打赏
  • 举报
回复
to darkstar21cn(暗星):代码能用,已经不错的了,但还有少少的不好,就是你按任何键都会打出那个字符出来,能不能连那些符号也不打出来呢?
to hello_asong(蓝色的忧郁):这个函数我man不到
hello_asong 2004-07-23
  • 打赏
  • 举报
回复
bioskey(int cmd),说明在bios.h中
cmd==0,返回下一个键盘输入值
cmd==1,查询是否按下了一个键
cmd==2,返回键盘上档键状态
ttlb 2004-07-23
  • 打赏
  • 举报
回复
up
darkstar21cn 2004-07-23
  • 打赏
  • 举报
回复
我的程序在正常运行中,匆忙改的,我不可能把原来所有的代码都发上来,少了个头文件
#include <termio.h>
Linux下的,记得加上。

TO: ghtsao(月之暗面)

struct termios stored_settings;//不好意思上面打错了
struct termios new_settings;
tcgetattr (0, &stored_settings);
new_settings = stored_settings;
new_settings.c_lflag &= (~ICANON);
new_settings.c_cc[VTIME] = 0;
tcgetattr (0, &stored_settings);
new_settings.c_cc[VMIN] = 1;
tcsetattr (0, TCSANOW, &new_settings);

这一段就是解决你提出的问题的代码。

tcsetattr (0, TCSANOW, &stored_settings);
这是回复环境用的,运行结束之后要记得调用。

开个玩笑嘛,这个方法是谁给我出的我都忘了——因为记录被CSDN清了。他总不会也来找我吧?
ghtsao 2004-07-23
  • 打赏
  • 举报
回复
没细看,上面用的也是getch和getchar,恐怕还是不行。

可能只有用低级I/O函数来解决了。
ghtsao 2004-07-23
  • 打赏
  • 举报
回复
楼上的对不住,我已经声明是转贴的了,没有标明出处,下次补上。
darkstar21cn 2004-07-23
  • 打赏
  • 举报
回复
呵呵,楼上有盗用的嫌疑哦。你还没有把我后面的注释拿过来呢。
http://community.csdn.net/Expert/topic/3203/3203405.xml?temp=.918606
我自己来吧:)

呵呵,为了可以移植性,代码多了点。
这是我的一个程序里的部分。
如果定义WAIT_FOR_RETURN,那就得等待回车了。
ghtsao 2004-07-23
  • 打赏
  • 举报
回复
兄弟,另一个问题的回答,转给你用:

#ifndef _WIN32 //Linux platform
#include <iostream>
#define get_char getchar
#else //WIN32 platform
#ifndef WAIT_FOR_RETURN
#include "conio.h"
#define get_char getch
#else
#include <stdio.h>
#define get_char getchar
#endif
#endif

int main (int argc, char* argv[])
{
#ifndef WAIT_FOR_RETURN
#ifndef _WIN32
struct termios new_settings;
struct termios new_settings;
tcgetattr (0, &stored_settings);
new_settings = stored_settings;
new_settings.c_lflag &= (~ICANON);
new_settings.c_cc[VTIME] = 0;
tcgetattr (0, &stored_settings);
new_settings.c_cc[VMIN] = 1;
tcsetattr (0, TCSANOW, &new_settings);
#endif
#endif

char c;
c = get_char (); //这就可以了
printf ("%c", c);

#ifndef WAIT_FOR_RETURN
#ifndef _WIN32
tcsetattr (0, TCSANOW, &stored_settings);
#endif
#endif
return 0;
}
ghtsao 2004-07-23
  • 打赏
  • 举报
回复
那怕只有直接访问键盘缓冲区了。
limlzm 2004-07-23
  • 打赏
  • 举报
回复
to:ghtsao(月之暗面) getchar getwchar输入的内容是显示屏幕的,且要按回车才行,不符合要求。
geesun 2004-07-23
  • 打赏
  • 举报
回复
int getchar( void );
好像没有什么用啊!
zanchao 2004-07-23
  • 打赏
  • 举报
回复
up
Chuanyan 2004-07-23
  • 打赏
  • 举报
回复
int getchar( void );
limlzm 2004-07-23
  • 打赏
  • 举报
回复
补充:用system("pause")也不行。
ghtsao 2004-07-23
  • 打赏
  • 举报
回复
用其它几个ANSI函数:

int getchar( void );

wint_t getwchar( void );

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧