键盘控制2
#include<iostream> #include<windows.h> #include<conio.h>//控制台输入输出头文件 using namespace std; int main(){ HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);//获取标准输出的句柄 COORD coord={0,2};//保存光标位置 SetConsoleCursorPosition(handle,coord); cout<<"HH"; HANDLE handle2=GetStdHandle(STD_OUTPUT_HANDLE); COORD coord2={1,2}; SetConsoleCursorPosition(handle2,coord2); while(1){ if(kbhit()){ int key=getch(); SetConsoleCursorPosition(handle,coord); cout<<" "; SetConsoleCursorPosition(handle2,coord2); cout<<" "; switch(key){ case 72: coord.Y--;coord2.Y--;break; case 75: coord.X--;coord2.X--;break; case 77: coord.X++;coord2.X++;break; case 80: coord.Y++;coord2.Y++;break; case 119: while(coord2.Y>0){ SetConsoleCursorPosition(handle,coord); cout<<"HH"; coord2.Y--; SetConsoleCursorPosition(handle2,coord2); cout<<"H"; Sleep(30); SetConsoleCursorPosition(handle2,coord2); cout<<" "; } coord2.Y=coord.Y; break; case 115: while(coord2.Y<30){ SetConsoleCursorPosition(handle,coord); cout<<"HH"; coord2.Y++; SetConsoleCursorPosition(handle2,coord2); cout<<"H"; Sleep(30); SetConsoleCursorPosition(handle2,coord2); cout<<" "; } coord2.Y=coord.Y; break; case 97: while(coord2.X>0){ SetConsoleCursorPosition(handle,coord); cout<<"HH"; coord2.X--; SetConsoleCursorPosition(handle2,coord2); cout<<"H"; Sleep(30); SetConsoleCursorPosition(handle2,coord2); cout<<" "; } coord2.X=coord.X; break; case 100: while(coord2.X<30){ SetConsoleCursorPosition(handle,coord); cout<<"HH"; coord2.X++; SetConsoleCursorPosition(handle2,coord2); cout<<"H"; Sleep(30); SetConsoleCursorPosition(handle2,coord2); cout<<" "; } coord2.X=coord.X; break; default: break; } SetConsoleCursorPosition(handle,coord); cout<<"HH"; } } return 0; }