頭文件中conio.h是控制檯輸入輸出,即 console IO
_kbhit() 是key board hit的縮寫。這個函數是一直在等待鍵盤輸入,所以我們需要用循環來持續等待輸入
#include<stdio.h>
#include<conio.h>
int main()
{
char ch;
while(1)
if (_kbhit())
ch = _getch(), printf("%d\n", ch);
return 0;
}
但是這裏輸出總是會輸出-32再輸出相應的碼,不知道爲什麼
通過下面程序驗證可以知道那個-32不是這四個方向的數據
#include<stdio.h>
#include<conio.h>
int main()
{
char ch;
while(1)
if (_kbhit()) {
ch = _getch();
switch(ch) {
case 72:printf("向上\n"); break;
case 80:printf("向下\n"); break;
case 75:printf("向左\n"); break;
case 77:printf("向右\n"); break;
}
}
return 0;
}