Original Post
How can I move the cursor in console mode using the arrow keys? [Edited by - kingpinzs on March 22, 2005 11:41:04 AM]
void gotoXY(int x, int y){ HANDLE screen_buffer_handle = GetStdHandle(STD_OUTPUT_HANDLE); COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(screen_buffer_handle, coord);}void clrscr(){ cout.flush(); static HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); COORD coord = {0, 0}; DWORD dw; CONSOLE_SCREEN_BUFFER_INFO csbi; DWORD dwSize; GetConsoleScreenBufferInfo(hConsole,&csbi); dwSize = csbi.dwSize.X * csbi.dwSize.Y; FillConsoleOutputCharacter(hConsole, ' ', dwSize, coord, &dw); FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwSize, coord, &dw); SetConsoleCursorPosition(hConsole, coord);}void setForeColor(int c){ static HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); WORD wAttrib = 0; CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo(hConsole,&csbi); wAttrib = csbi.wAttributes; wAttrib &= ~(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); if (c & 1) wAttrib |= FOREGROUND_BLUE; if (c & 2) wAttrib |= FOREGROUND_GREEN; if (c & 4) wAttrib |= FOREGROUND_RED; if (c & 8) wAttrib |= FOREGROUND_INTENSITY; SetConsoleTextAttribute(hConsole, wAttrib);}void setBackColor(int c){ static HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); WORD wAttrib = 0; CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo(hConsole,&csbi); wAttrib = csbi.wAttributes; wAttrib &= ~(BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY); if (c & 1) wAttrib |= BACKGROUND_BLUE; if (c & 2) wAttrib |= BACKGROUND_GREEN; if (c & 4) wAttrib |= BACKGROUND_RED; if (c & 8) wAttrib |= BACKGROUND_INTENSITY; SetConsoleTextAttribute(hConsole, wAttrib);}clrscr() -- Clears the ScreengotoXY(int x, int y) -- Moves to x,y on consolesetForeColor(int) -- Sets the Text ColorsetBackColor(int) -- Sets the Background Color#define BLACK 0#define BLUE 1#define GREEN 2#define CYAN 3#define RED 4#define PURPLE 5#define BROWN 6#define WHITE 7#define GRAY 8#define LBLUE 9#define LGREEN 10#define LCYAN 11#define LRED 12#define LPURPLE 13#define YELLOW 14#define BWHITE 15 void game(){ system("CLS"); cout<<"\t\t | | "<<endl <<"\t\t | | "<<endl <<"\t\t________|_________|_______"<<endl <<"\t\t | | "<<endl <<"\t\t | | "<<endl <<"\t\t | | "<<endl <<"\t\t________|_________|_______"<<endl <<"\t\t | | "<<endl <<"\t\t | | "<<endl <<"\t\t | | "<<endl; }num{ KEY_ESC = 27, ARROW_UP = 256 + 72, ARROW_DOWN = 256 + 80, ARROW_LEFT = 256 + 75, ARROW_RIGHT = 256 + 77};static int get_code ( void ){ int ch = getch(); if ( ch == 0 || ch == 224 ) ch = 256 + getch(); return ch;} int main(int argc, char *argv[]){ int ch; while ( ( ch = get_code() ) != KEY_ESC ) { switch ( ch ) { case ARROW_UP: printf ( "UP\n" ); break; case ARROW_DOWN: printf ( "DOWN\n" ); break; case ARROW_LEFT: printf ( "LEFT\n" ); break; case ARROW_RIGHT: printf ( "RIGHT\n" ); break; } } system("PAUSE"); return 0;}This topic has been locked by a moderator. New replies are not allowed.
GameDev.net uses cookies to ensure you have the best experience on our platform. Learn more