Skip to main content
GameDev.net gamedev.net
🔒 Locked

how to move cursor in windows console mode [fixed]

Started by kingpinzs Mar 21, 2005 at 4:30 PM 5 replies 7.3k views
Original Post
kingpinzs
kingpinzs
How can I move the cursor in console mode using the arrow keys? [Edited by - kingpinzs on March 22, 2005 11:41:04 AM]
izzo
izzo
You'll have to read keypresses from the user, and then position the cursor based on what keys the user presses. E.g. if they press "up" then move the cursor up one row. You'll want to keep track of the current cursor position.

Have a look here for some console programming tips (including how to position the cursor).

cheers
sam.
Radagar
Radagar
Here is a function that will set the cursor position for you.

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);}


And here is a function that will clear a console screen for you

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);}


And finally, these 2 functions will allow you to set the Foreground and Background colors

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);}


use these functions like this...

clrscr() -- Clears the ScreengotoXY(int x, int y) -- Moves to x,y on consolesetForeColor(int) -- Sets the Text ColorsetBackColor(int) -- Sets the Background Color


PS: here is a list of colors that you can define in a Header file to make the SetForeGround and SetBackground functions easier.

#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   
WyrmSlayer RPG - In Early Development
kingpinzs
kingpinzs
Sweet that is what I needed to know. Now one more question.

I am trying to have the user put a letter in only in serten spots and that I want it to over wright what is already there.

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; }


So what happens is the plsyer will move the cursor to the place he wants but it cant go out of the place and when He puts in a X or an O then it will over wright what is there so it wont make it look mess up the game board.

So how do you insert or over wright a charicter in the console?

kingpinzs
kingpinzs
And also how do you capture assci keys? I need to use the arrow keys in console mode but I cant find any info on getting the assci for the arrow keys
kingpinzs
kingpinzs
Thanks for the help that was half of what I was looking for and the rest I was looking for I found

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;}


Now I can use the arrow keys with out using windows.h or dos.h

Thanks for every ones help.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.