Chinaunix首页 | 论坛 | 博客
  • 博客访问: 84842
  • 博文数量: 28
  • 博客积分: 1430
  • 博客等级: 上尉
  • 技术积分: 240
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-30 23:34
文章分类

全部博文(28)

文章存档

2011年(2)

2006年(26)

我的朋友

分类: 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:");

阅读(1858) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~