【请教】linux下如何直接获取键盘输入而不需要以回车作为结束符

longhiram 2008-03-18 10:56:24
想要直接读取键盘的输入,但是libc中提供的函数getchar,sscanf都需要以回车作为结束符。
请问,用什么函数能够在按下键盘的时候程序立刻读取。类似于windows下的hitkb函数功能。
linux应该有这样的东西吧,比如man,more命令,在键入q的时候程序就回响应退出,而不需要回车。
...全文
1420 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiaohhp 2012-07-16
  • 打赏
  • 举报
回复
更详细的参考 man 3 tcgetattr

ICANON
Enable canonical mode. This enables the special characters EOF, EOL, EOL2, ERASE, KILL, LNEXT, REPRINT, STATUS, and WERASE, and buffers by lines.

可以看出ICNAON标志位启用了整行缓存。

所以,new_settings.c_lflag &= (~ICANON);这句屏蔽整行缓存。那就只能单个了。
  • 打赏
  • 举报
回复
求助,等啊等
longhiram 2008-03-19
  • 打赏
  • 举报
回复
还有哪位高手再指点一下?
cceczjxy 2008-03-18
  • 打赏
  • 举报
回复
有两种方式,一种是用curses库,
#include <curses.h>
int main()
{
unsigned char c,b,a,buf[32];
initscr();
while(1)
{
a=b;
b=c;
c=getch();
//clear();
printf("\n%x\n",c);
memset(buf,0,sizeof(buf));
if(a==27&&b==91)
{
switch(c)
{
case 65:sprintf(buf,"up\n");break;
case 66:sprintf(buf,"down\n");break;
case 67:sprintf(buf,"right\n");break;
case 68:sprintf(buf,"left\n");break;
}
move(0,0);
addstr(buf);
}
memset(buf,0,sizeof(buf));
refresh();
}
endwin();
}
gcc cursestest.c -o cursestest -l curses

一种是把终端设置成非加工摸设。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <termio.h>
#include <fcntl.h>
#include <curses.h>
int tty_mode(int how)
{
static struct termios original_mode;
if(how==0)
tcgetattr(0,&original_mode);
else return tcsetattr(0,TCSANOW,&original_mode);
}
void set_crmode()
{
struct termios ttystate;
tcgetattr(0,&ttystate);
ttystate.c_lflag &= ~ICANON;
ttystate.c_lflag &=~ECHO;
ttystate.c_cc[VMIN]=1;
tcsetattr(0,TCSANOW,&ttystate);
}
int main()
{
char passwd[256];
char name[32];
int flag1=1,flag2=0,len,i,j;
memset(passwd,0,sizeof(passwd));
while(flag1)
{
memset(name,0,sizeof(name));
printf("input your name:");
fflush(stdout);
scanf("%32s",name);
len=strlen(name);
if(len<4||len>8)
{
system("clear");
printf("用户名字必须在4--8个字符之间\n");
continue;
}

for(j=0;j<len;j++)
for(i=j+1;i<len;i++)
{
if(name[i]==name[j])
{
i=len;
j=len;
flag2=1;
}
}
if(flag2)
{
flag2=0;
printf("用户名字不能包含同一个字符\n");
}
else break;


}
printf("Input passwd:");
fflush(stdout);
getchar();
i=0;
char ch;
fflush(stdin);
tty_mode(0);
set_crmode();
while(ch=(char)getchar())
{
if(ch=='\n')
{
if(i<8)
{
printf("\n密码长度要求8位以上\n");
memset(passwd,0,sizeof(passwd));
i=0;
continue;
}
else break;
}
passwd[i++]=ch;
printf("*");
fflush(stdout);
fflush(stdin);
if(i>16)
{
printf("passwd very long\n");
}
}
tty_mode(1);
printf("\n用户注册成功!! 用户名为 : %s \n\n", name);
}

longhiram 2008-03-18
  • 打赏
  • 举报
回复
to xiaoelem
你的这段代码跟cceczjxy的第二段代码实现原理是一样的,都是通过tcsetattr函数设置terminal的属性来控制需不需要回车来结束输入。
xiaoelem 2008-03-18
  • 打赏
  • 举报
回复
#include <termio.h>
#include <stdio.h>


int scanKeyboard()
{
int in;
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);

in = getchar();

tcsetattr(0,TCSANOW,&stored_settings);
return in;
}

这个方法就可以,返回值是该键的ASCII码值,不需要回车的,
具体为什么我现在还不是很清楚,呵呵
在网上找的,看看吧
如果看懂了,可以给我讲讲啊
longhiram 2008-03-18
  • 打赏
  • 举报
回复
感谢指教。
是不是libc中从标准终端读入跟terminal的设计有关,由terminal中的某些属性来控制。
还有没有其他办法可以实现,想了深入解一下linux对于键盘输入控制究竟是如何设计的?

23,121

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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