1.单方移动
#include <iostream> #include <windows.h> #include <conio.h> using namespace std; int main(){ HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); COORD coord = {0,0}; SetConsoleCursorPosition(handle,coord); cout<<"zs"; HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE); COORD Coord = {0,1}; SetConsoleCursorPosition(Handle,Coord); cout<<"sz"; int key; while(1){ if(kbhit()){ key = getch(); if(key==72 || key==75 || key==77 || key==80){ SetConsoleCursorPosition(handle,coord); cout<<" "; } if(key==87 || key==65 || key==68 || key==83){ 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; case 87: Coord.Y--;break; case 65: Coord.X--;break; case 68: Coord.X++;break; case 83: Coord.Y++;break; default: break; } if(key==72 || key==75 || key==77 || key==80){ SetConsoleCursorPosition(handle,coord); cout<<"zs"; } if(key==87 || key==65 || key==68 || key==83){ SetConsoleCursorPosition(Handle,Coord); cout<<"sz"; } } } return 0; }
2.双方移动
#include <iostream> #include <windows.h> #include <conio.h> using namespace std; int main(){ HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); COORD coord = {0,0}; SetConsoleCursorPosition(handle,coord); cout<<"zs"; HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE); COORD Coord = {0,1}; SetConsoleCursorPosition(Handle,Coord); cout<<"sz"; int key; while(1){ if(kbhit()){ key = getch(); if(key==72 || key==75 || key==77 || key==80){ SetConsoleCursorPosition(handle,coord); cout<<" "; } if(key==87 || key==65 || key==68 || key==83){ 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; case 87: Coord.Y--;break; case 65: Coord.X--;break; case 68: Coord.X++;break; case 83: Coord.Y++;break; default: break; } if(key==72 || key==75 || key==77 || key==80){ SetConsoleCursorPosition(handle,coord); cout<<"zs"; } if(key==87 || key==65 || key==68 || key==83){ SetConsoleCursorPosition(Handle,Coord); cout<<"sz"; } } } return 0; }
标签:std,main,handle,coord,键盘,C++,HANDLE,操作,include From: https://www.cnblogs.com/wangyihang-xh/p/18132990