键盘操控

#include<iostream>
#include<windows.h>
#include<conio.h>//控制台输入输出头文件 
using namespace std;
int main(){
	HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);//获取标准输出的句柄 
	COORD coord={0,1};//保存光标位置
	HANDLE handle2=GetStdHandle(STD_OUTPUT_HANDLE);
	COORD coord2={0,0};
	SetConsoleCursorPosition(handle,coord);
	cout<<"BB";
	SetConsoleCursorPosition(handle2,coord2);
	cout<<"AA";
	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;
					default: break;
				}
				SetConsoleCursorPosition(handle,coord);
				cout<<"BB";	
				SetConsoleCursorPosition(handle2,coord2);
				cout<<"AA";
		}
	} 
	return 0;
}
//////////////////////////////////////////////////////////////////////////////////////
#include<iostream>
#include<windows.h>
#include<conio.h>//控制台输入输出头文件 
using namespace std;
int main(){
	HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);//获取标准输出的句柄 
	COORD coord={0,1};//保存光标位置
	HANDLE handle2=GetStdHandle(STD_OUTPUT_HANDLE);
	COORD coord2={0,0};
	SetConsoleCursorPosition(handle,coord);
	cout<<"BB";
	SetConsoleCursorPosition(handle2,coord2);
	cout<<"AA";
	while(1){
		if(kbhit()){
			int key=getch();
			if(key==72||key==75||key==77||key==80){
				SetConsoleCursorPosition(handle,coord);
				cout<<"  ";
				switch(key){
					case 72: coord.Y--;break;
					case 75: coord.X--;break;
					case 77: coord.X++;break;
					case 80: coord.Y++;break;
					default: break;
				}
				SetConsoleCursorPosition(handle,coord);
				cout<<"BB";	
			}else if(key==87||key==65||key==68||key==83){
				SetConsoleCursorPosition(handle2,coord2);
				cout<<"  ";
				switch(key){
					case 87: coord2.Y--;break;
					case 65: coord2.X--;break;
					case 68: coord2.X++;break;
					case 83: coord2.Y++;break;
					default: break;
				}
				SetConsoleCursorPosition(handle,coord);
				cout<<"AA";	
			}
			
		}
	} 
	return 0;
}

  

posted @ 2024-04-13 15:56  陈若麟  阅读(5)  评论(0编辑  收藏  举报