|
|
_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找不到吗?
| |
|