中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > C/C++
unix c中有没有类似ms c 中_kbhit()的函数呀,谢谢了
作者:未知 时间:2005-09-13 19:19 出处:ChinaUnix.net 责编:chinaitpower
              摘要:unix c中有没有类似ms c 中_kbhit()的函数呀,谢谢了

_kbhit():检验最后一次按键

 zhutr 回复于:2003-04-18 16:17:52
termcap之类的东东应该可以用。应该能man到。
或curses。

 admwby1hy 回复于:2003-04-18 16:50:58
能具体说说笑笑吗

 zhutr 回复于:2003-04-18 17:40:40
具体做法好像是设置一下终端属性,
改成无缓冲输入的方式,等待按键,
之后再改回原方式。
不记的具体的函数名了,
很早以前在linux下做过一个小程序,
刚才找了半天没找到,
还是看看manpage吧,等会儿我再找找看。

 admwby1hy 回复于:2003-04-18 17:55:49
zhutr 谢谢你了

 zhutr 回复于:2003-04-18 18:27:12
[code:1:ba5da560cd]
#include <termios.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
int getpassword(char *passwd);
main()
{
char passwd[64];
if(getpassword(passwd)==-1)
{
printf("error");
exit(1);
}
printf("%s\n",passwd);
return(0);
}
int getpassword(char *passwd)
{
int fd = -1, i=0, termpid;
struct termios term, termsave;
char str[200];
char tmp;
strcpy(str,"/dev/tty");
if ((fd = open(str, O_RDWR | O_NOCTTY )) < 0) return -1;
sprintf(str, "Please input the password:");
write(fd, str, strlen(str));
tcgetattr(fd, &term);
tcgetattr(fd, &termsave);
term.c_lflag &= ~(ICANON|ECHO|ISIG);
tcsetattr(fd, TCSANOW, &term);
read(fd, &tmp, 1);
while(tmp!='\n')
{
passwd[i] = tmp;
i++;
tcsetattr(fd, TCSANOW, &termsave);
write(fd,"*",1);
tcsetattr(fd, TCSANOW, &term);
read(fd, &tmp, 1);
}
tcsetattr(fd, TCSANOW, &termsave);
passwd[i]='\0';
write(fd,"\n",1);
return 0;
}
[/code:1:ba5da560cd]

不谢不谢!
找到一个以前做的在linux下输入密码回显‘*’的小程序。
呵呵,用tcsetattr()、tcsetattr()系列的函数可以实现,
linux下肯定是可以支持的,别的unix我不敢肯定,你可以试试!
具体的属性按你的需求怎么设置还是去查一下manpage吧!

 晴天羽 回复于:2003-05-16 11:12:00
写的好,不过好象显示慢了点,要停顿的,这是怎么回事。

 zhutr 回复于:2003-05-16 11:19:31
啊?不会吧,我当时测的时候没觉得停顿呀?
有可能是属性切换时的短暂停顿,
输入时应该没有吧?

 fightxxx 回复于:2003-05-16 11:22:45
很好,能用!!

在linux下输入密码回显‘*’的小程序,能讲讲实现的原理吗??

 superhoo 回复于:2003-05-16 11:27:55
[code:1:04f1a3a3a6]
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <sys/time.h>

static struct termios inital_settings , new_settings;
static int peek_character=-1;

void init_keyboard(void);
void close_keyboard(void);
int kbhit(void);
int readch(void);
void delayusec(int usec);

int main()
{
int ch=0;
init_keyboard();
while(ch != 'q' ){
//printf("looping \n");
delayusec(100);
if(kbhit()){
ch=readch();
printf("You hit %x\n",ch);
}
}
close_keyboard();
exit(0);
}

void init_keyboard(void)
{
tcgetattr(0,&inital_settings);
new_settings = inital_settings;
new_settings.c_lflag &= ~ICANON;
new_settings.c_lflag &= ~ECHO;
new_settings.c_lflag &= ~ISIG;
new_settings.c_cc[VMIN]=1;
new_settings.c_cc[VTIME]=0;
tcsetattr(0,TCSANOW,&new_settings);
}

void close_keyboard(void)
{
tcsetattr(0,TCSANOW,&inital_settings);
}

int kbhit(void)
{
char ch;
int nread;

if(peek_character != -1) 
return 1;
new_settings.c_cc[VMIN]=0;
tcsetattr(0,TCSANOW,&new_settings);
nread=read(0,&ch,1);
new_settings.c_cc[VMIN]=1;
tcsetattr(0,TCSANOW,&new_settings);

if(nread==1)
{
peek_character=ch;
return 1;
}
return 0;
}

int readch(void)
{
char ch;

if(peek_character != -1)
{
ch=peek_character;
peek_character =  -1;
return ch;
}

read(0,&ch,1);
return ch;
}
void delayusec(int usec)
{
struct timeval tv;

tv.tv_sec = 0;
        tv.tv_usec = usec;
        select(0, NULL, NULL, NULL, &tv);
        
}
[/code:1:04f1a3a3a6]

 chdonald 回复于:2003-05-16 11:45:31
1.open(... 那一句是将终端设置为非控制终端,为什么要这样做?
2.ISIG和ICANON选项是什么意思,哪里可以得到这些帮助(MAN里无)

 zhutr 回复于:2003-05-16 13:34:52
1.不记得当时为什么加这个了,等我想想。
2.man里有的,直接man tcsetattr找不到吗?

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有