分类: C/C++
2006-10-13 09:02:39
/*
* determine which key is pressed
* you can use this to get the password function
* I have realize it
* and you can use this to test the key you have pressed
*/
/*
* determine which key is pressed
* you can use this to get the password function
* I have realize it
* and you can use this to test the key you have pressed
*/
#include
#include
#include
char* win_getpass(char* prompt)
{
int i;
int tmp;
static char buf[20+1];
printf("%s",prompt);
for(i = 0; i < 20; i++)
{
tmp = getch();
if(tmp == 13)
{
buf[i] = '\0';
break;
}
buf[i] = tmp;
}
putchar('\n');
return buf;
}
int main()
{
int tmp;
//system("tty -cho raw");
for(;;)
{
printf("press q(Q) to quit\n");
tmp = getch();
if((tmp == 'q') || (tmp == 'Q'))
return 0;
if (tmp == 224)
{
tmp = getch();
if(tmp == 72)
{
printf("you press up arrow\n");
}
else if(tmp == 75)
{
printf("you press left arrow\n");
}else if(tmp == 77)
{
printf("you press right arrow\n");
}else if(tmp == 80)
{
printf("you press down arrow\n");
}
}else{
printf("Please press the up down left right key\n");
}
}
return 0;
}
main函数中的是我用来测试up left right down按下的
要使用win_getpass函数很简单你需要这样子调用
char *password;
password = win_getpass("Password:");