#include <conio.h>
#include <iostream>
int GetDirection()
{
int ret = 0;
do
{
int ch = _getch();
if(isascii(ch))
continue;
//这里必须要两次,非ascii的第一次输入是 虚拟码(224),第二次输入才是
ch = _getch();
switch(ch)
{
case 72:
ret = 2; // top
break;
case 75:
ret = 1; // left
break;
case 77:
ret = 3; // right
break;
case 80:
ret = 4; // down
break;
default:
break;
}
} while (ret == 0);
return ret;
}
int main(int argc, char* argv[])
{
for(int k = 0; k < 10; k++)
std::cout<<GetDirection();
system("pause");
return 0;
}
conio.h 不是ISO c 和 POSIX的标准, Console Input/Output 简写。windows linux均可用。
包含的接口有:
cgets(char*);
cputs(constchar*);
cscanf(constchar*,...);
inpw(unsignedshort);
getch(void);
getche(void);
kbhit(void);//键盘响应,是否被按下。
outp(unsignedshort,int);
outpw(unsignedshort,unsignedshort);
putch(int);
ungetch(int);
void_Cdeclclreol(void);
void_Cdeclclrscr(void);
void_Cdecldelline(void);
int_Cdeclgettext(intleft,inttop,intright,intbottom,void*destin);
void_Cdeclgettextinfo(structtext_info*r);
void_Cdeclgotoxy(intx,inty);
void_Cdeclhighvideo(void);
void_Cdeclinsline(void);
void_Cdecllowvideo(void);
int_Cdeclmovetext(intleft,inttop,intright,intbottom,intdestleft,intdesttop);
void_Cdeclnormvideo(void);
int_Cdeclputtext(intleft,inttop,intright,intbottom,void*source);
void_Cdecltextattr(intnewattr);
void_Cdecltextbackground(intnewcolor);
void_Cdecltextcolor(intnewcolor);
void_Cdecltextmode(intnewmode);
int_Cdeclwherex(void);
int_Cdeclwherey(void);
void_Cdeclwindow(intleft,inttop,intright,intbottom);
char*_Cdeclcgets(char*str);
int_Cdeclcprintf(constchar*format,...);
int_Cdeclcputs(constchar*str);
int_Cdeclcscanf(constchar*format,...);
int_Cdeclgetch(void);
int_Cdeclgetche(void);
char*_Cdeclgetpass(constchar*prompt);
int_Cdeclkbhit(void);
int_Cdeclputch(intc);
int_Cdeclungetch(intch);