/////////////////////////////////////////////////////////////////////////////// // Datum : 200-10-01 // Programmerare : Patrik Rosengren, Göteborg // Filnamn : moveCursor.cpp // Uppgift : Styra cursorn med piltangenterna. Programmet avslutas med [Esc]. // Kompilator : Microsoft Visual C++ 6.0 // OS : XP Home Edition /////////////////////////////////////////////////////////////////////////////// #include #include #include using namespace std; // Textplacering på skärmen void gotoxy(int x, int y) { COORD coordScreen={x,y}; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coordScreen); } //Huvudprogrammet int main() { int c1, c2; int x = 0, y = 0; do { while( !kbhit() ) //Känner av tangenttrycks-händelse { c1 = _getch(); //Tar emot den första tecknet ( 224 ) if( c1 == 224 ) { c2 = _getch(); //Tar emot den andra tecknet ( 72, 80, 75, 77 ) system("cls"); if( c2 == 72 ) if( y != 0) gotoxy(x,--y); else gotoxy(x,y); if( c2 == 80 ) if( y != 50 ) gotoxy(x, ++y ); else gotoxy(x,y); if( c2 == 75 ) if( x != 0 ) gotoxy(--x, y); else gotoxy(x,y); if( c2 == 77 ) if( x != 74 ) gotoxy(++x,y); else gotoxy(x,y); cout << x << "," << y; } else if( c1 == 27) // ESC-knappen, avsluta programmet. break; } if( kbhit() ) _getch(); } while( c1 != 27 ); return 0; }