瑞星卡卡安全论坛技术交流区系统软件 【求助】C++中的获取键盘的ASCII的函数是什么?

1   1  /  1  页   跳转

【求助】C++中的获取键盘的ASCII的函数是什么?

【求助】C++中的获取键盘的ASCII的函数是什么?

在C++中,怎么实现按一个键然后根据按键进行操作?
比如我按一个X,就输出"你按的是X"
按一个Y,就输出"你按的是Y"
最后编辑2006-08-18 15:48:40.217000000
分享到:
gototop
 

可以直接接用C函数,getch或者getchar
好像在stdlib.h中
gototop
 

<io.h>&<stdlib.h>
getchar();
gototop
 

具体?举个例子吧,并说明一下!
gototop
 

#include "stdio.h"
#include "conio.h"
int main(void)
{
    char key;
    key=getch();
    if(key="a"){
      printf("你好帅!\n");
    }
    else{
      printf("你好笨!\n");
    }
}


运行时报错
gototop
 

再问一个.void main()和int main(void)不相同吗?
gototop
 

把#include "stdio.h"
#include "conio.h"
int main(void)
{
char key;
key=getch();
if(key="a"){
printf("你好帅!\n");
}
else{
printf("你好笨!\n");
}
}
改成
#include "stdio.h"
#include "conio.h"
int main(void)
{
char Key;
int key;
Key=getch();
key=Key;
switch(key)
{
  case 97:
  printf("你好帅!\n");
  break;
  case 98:
  printf("厉害\n");
  break;
  default:
  printf("顶\n");
}
}
就行了,为什么?难道IF语句用错了吗?
gototop
 

引用:
【1糖果的贴子】把#include "stdio.h"
#include "conio.h"
int main(void)
{
char key;
key=getch();
if(key="a"){
printf("你好帅!\n");
}
else{
printf("你好笨!\n");
}
}
改成
#include "stdio.h"
#include "conio.h"
int main(void)
{
char Key;
int key;
Key=getch();
key=Key;
switch(key)
{
  case 97:
  printf("你好帅!\n");
  break;
  case 98:
  printf("厉害\n");
  break;
  default:
  printf("顶\n");
}
}
就行了,为什么?难道IF语句用错了吗?
………………

if里面的改成key=='a'
gototop
 
1   1  /  1  页   跳转
页面顶部
Powered by Discuz!NT