日期:2014-05-16  浏览次数:20802 次

【请教】linux下如何直接获取键盘输入而不需要以回车作为结束符
想要直接读取键盘的输入,但是libc中提供的函数getchar,sscanf都需要以回车作为结束符。
请问,用什么函数能够在按下键盘的时候程序立刻读取。类似于windows下的hitkb函数功能。
linux应该有这样的东西吧,比如man,more命令,在键入q的时候程序就回响应退出,而不需要回车。

------解决方案--------------------
有两种方式,一种是用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);
}


------解决方案--------------------
#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码值,不需要回车的,
具体为什么我现在还不是很清楚,呵呵
在网上找的,看看吧
如果看懂了,可以给我讲讲啊