社区
Linux/Unix社区
帖子详情
【请教】linux下如何直接获取键盘输入而不需要以回车作为结束符
longhiram
2008-03-18 10:56:24
想要直接读取键盘的输入,但是libc中提供的函数getchar,sscanf都需要以回车作为结束符。
请问,用什么函数能够在按下键盘的时候程序立刻读取。类似于windows下的hitkb函数功能。
linux应该有这样的东西吧,比如man,more命令,在键入q的时候程序就回响应退出,而不需要回车。
...全文
1420
7
打赏
收藏
【请教】linux下如何直接获取键盘输入而不需要以回车作为结束符
想要直接读取键盘的输入,但是libc中提供的函数getchar,sscanf都需要以回车作为结束符。 请问,用什么函数能够在按下键盘的时候程序立刻读取。类似于windows下的hitkb函数功能。 linux应该有这样的东西吧,比如man,more命令,在键入q的时候程序就回响应退出,而不需要回车。
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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);这句屏蔽整行缓存。那就只能单个了。
michaelscofielddong
2009-12-08
打赏
举报
回复
求助,等啊等
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对于键盘输入控制究竟是如何设计的?
Excel办公软件灵活妙用技巧笔记集锦
第19节:不
需要
截图工具
直接
使用excel复制图片 l 第20节:excel粘贴也可以多种方式选择了 l 第21节:巧为excel单元格画双划线 l 第22节:为excel的文字或数字添加单划线或双划线 l 第23节:...
Linux
键盘输入
buf,【
请教
】
linux
下如何
直接
获取
键盘输入
而不
需要
以
回车
作为
结束符
...
想要
直接
读取键盘的输入,但是libc中提供的函数getchar,sscanf都
需要
以
回车
作为
结束符
。请问,用什么函数能够在按下键盘的时候程序立刻读取。类似于windows下的hitkb函数功能。
linux
应该有这样的东西吧,比如man,...
Linux
精简教程
ROS 依赖
Linux
提供底层支持,一般基于
Linux
系统开发,玩转 ROS 机器人操作系统
需要
具备基本的
Linux
知识。本篇对
Linux
进行基本讲解,并讲解常用
Linux
操作命令。如果您对
Linux
熟练掌握,请自行跳过该篇。 ...
linux
下 vi o命令,
linux
下VI编辑器命令大全(超级完整版)
NOTICE:把这里提到的命令牢记心间,行走
LINUX
才能轻松自如!这是基本功,练就此身本领后,方可学习上乘功夫……如若不按此步骤修练,结果会经脉尽断,真气逆行,哈哈哈!XP交换两个字符位置ddp上下两行调换J上下两...
Linux
配置
第3章 对
Linux
系统管理员的建议... 1 第4章 安装
Linux
操作系统... 2 第5章 初步认识
Linux
. 5 第6章
Linux
系统的远程登陆... 13 第7章
Linux
文件与目录管理... 19 第8章
Linux
系统用户及用户组管理... 37 ...
Linux/Unix社区
23,121
社区成员
74,508
社区内容
发帖
与我相关
我的任务
Linux/Unix社区
Linux/Unix社区 应用程序开发区
复制链接
扫一扫
分享
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章