社区
C语言
帖子详情
linux下如何得到类似getch功能
limlzm
2004-07-23 03:26:22
我想要的功能无非就是把程序停一停,类似用看帮助,按一下空格或者其他键就
继续按我需要显示n行printf出来。
由于getch要initscr()才能起作用,而且效果太差,每次调用都要把屏幕
clear,感觉不好,现在暂时用了getpass()函数充当一下,但一定要按一下回车才
行,还有没有其他好用的函数实现这样的功能呢?
...全文
330
18
打赏
收藏
linux下如何得到类似getch功能
我想要的功能无非就是把程序停一停,类似用看帮助,按一下空格或者其他键就 继续按我需要显示n行printf出来。 由于getch要initscr()才能起作用,而且效果太差,每次调用都要把屏幕 clear,感觉不好,现在暂时用了getpass()函数充当一下,但一定要按一下回车才 行,还有没有其他好用的函数实现这样的功能呢?
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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 );
linux
运行get
ch
吗,在
linux
中使用get
ch
()函数
本文介绍在
Linux
环境中如何实现get
ch
()函数的
功能
。由于
Linux
系统中不存在conio.h,通常采用curses库来实现
类似
get
ch
()的
功能
。文章提供了具体的代码示例及编译运行步骤。
linux
终端实现get
ch
函数
本文介绍如何在
Linux
系统中实现
类似
Windows的get
ch
函数
功能
,提供了具体代码实现及使用示例。
linux
中
类似
get
ch
的函数,c-
Linux
中的get
ch
()和get
ch
e()等效于什么?
本文探讨了
Linux
中get
ch
()和get
ch
e()函数的替代方案,尤其是在找不到conio.h头文件的情况下。着重介绍了使用curses.h或ncurses库实现
类似
功能
的方法,以及如何通过curses库修改get
ch
行为。
liunx 下实现windouw中 conio.h中get
ch
()
功能
本文介绍如何在
Linux
环境中实现
类似
Windows下conio.h中get
ch
()的
功能
,即读取键盘输入但不显示,使用stty命令进行设置。
在
linux
中使用get
ch
()函数
本文详细介绍了get
ch
()函数在
Linux
环境中的实现方式,包括如何通过修改终端属性来实现字符的即时读取,避免了标准输入的行缓冲。文章提供了具体的代码示例,展示了如何使用tcgetattr()和tcsetattr()函数来临时更改终端设置,以及如何使用curses库来实现
类似
get
ch
()的
功能
。
C语言
70,038
社区成员
243,247
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章