11 Replies - 13590 Views - Last Post: 01 February 2010 - 09:51 AM Rate Topic: -----

#1 diego_pmc  Icon User is offline

  • D.I.C Addict

Reputation: 81
  • View blog
  • Posts: 565
  • Joined: 13-May 09

Console cursor coordinates

Posted 01 February 2010 - 08:12 AM

I am curious if there is any function that would help me get the coordinates of the cursor in a text console app. By cursor, I mean the current line and column where cout, printf and other such functions will place the next piece of text they will be given.
Is This A Good Question/Topic? 0
  • +

Replies To: Console cursor coordinates

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: Console cursor coordinates

Posted 01 February 2010 - 08:34 AM

sure there is. However, such operations are not really platform independent.

Are you on windows? Then you can use the GetConsoleScreenBufferInfo function which will give you a CONSOLE_SCREEN_BUFFER_INFO object which you can use to get/set the current cursor coordinates.


#include <iostream>
#include <windows.h>

//This will set the position of the cursor
	void gotoXY(int x, int y) {
	//Initialize the coordinates
	COORD coord = {x, y};
	//Set the position
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
	return;
}

void getCursorXY(int &x, int&y) {
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	if(GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
		x = csbi.dwCursorPosition.X;
		y = csbi.dwCursorPosition.Y;

	}
}

using namespace std;

int main() {
	int x = 0, y = 0;
	cout << "Find the cursor test 1";
	getCursorXY(x, y);
	cout << endl;
	cout << "cursor was at: (" << x <<", " << y << ")" << endl;
	
	
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#3 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 109
  • View blog
  • Posts: 1,530
  • Joined: 25-January 07

Re: Console cursor coordinates

Posted 01 February 2010 - 08:34 AM

Not sure if you are working in C or C++, you are on windows or something else but here are my guesses.

GetConsoleScreenBufferInfo - In windows.
WhereX, WhereY - In C, but they are not standard (comes only with old Borland Compilers I guess.)

I hope this will help you. :)


--EDIT--
Damn... Lost to NickDMax by a minute or less. :(

This post has been edited by AmitTheInfinity: 01 February 2010 - 08:37 AM

Was This Post Helpful? 0
  • +
  • -

#4 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Console cursor coordinates

Posted 01 February 2010 - 08:38 AM

There is a set of functions:
Console Functions

* AddConsoleAlias
* AllocConsole
* AttachConsole
* CreateConsoleScreenBuffer
* FillConsoleOutputAttribute
* FillConsoleOutputCharacter
* FlushConsoleInputBuffer
* FreeConsole
* GenerateConsoleCtrlEvent
* GetConsoleAlias
* GetConsoleAliases
* GetConsoleAliasesLength
* GetConsoleAliasExes
* GetConsoleAliasExesLength
* GetConsoleCP
* GetConsoleCursorInfo
* GetConsoleDisplayMode
* GetConsoleFontSize
* GetConsoleHistoryInfo
* GetConsoleMode
* GetConsoleOriginalTitle
* GetConsoleOutputCP
* GetConsoleProcessList
* GetConsoleScreenBufferInfo
* GetConsoleScreenBufferInfoEx
* GetConsoleSelectionInfo
* GetConsoleTitle
* GetConsoleWindow
* GetCurrentConsoleFont
* GetCurrentConsoleFontEx
* GetLargestConsoleWindowSize
* GetNumberOfConsoleInputEvents
* GetNumberOfConsoleMouseButtons
* GetStdHandle
* HandlerRoutine
* PeekConsoleInput
* ReadConsole
* ReadConsoleInput
* ReadConsoleOutput
* ReadConsoleOutputAttribute
* ReadConsoleOutputCharacter
* ScrollConsoleScreenBuffer
* SetConsoleActiveScreenBuffer
* SetConsoleCP
* SetConsoleCtrlHandler
* SetConsoleCursorInfo
* SetConsoleCursorPosition
* SetConsoleDisplayMode
* SetConsoleHistoryInfo
* SetConsoleMode
* SetConsoleOutputCP
* SetConsoleScreenBufferInfoEx
* SetConsoleScreenBufferSize
* SetConsoleTextAttribute
* SetConsoleTitle
* SetConsoleWindowInfo
* SetCurrentConsoleFontEx
* SetStdHandle
* WriteConsole
* WriteConsoleInput
* WriteConsoleOutput
* WriteConsoleOutputAttribute
* WriteConsoleOutputCharacter

that exist in kernel32.dll.

GetConsoleScreenBufferInfo will return the screen coordinates of the cursor position.

I hope this information helps
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: Console cursor coordinates

Posted 01 February 2010 - 08:45 AM

lol... could have just given a link to MSDN which would include a description of all of the functions...


If you are not on windows ( or need a platform independent solution)you can look at ncurses library
Was This Post Helpful? 0
  • +
  • -

#6 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Console cursor coordinates

Posted 01 February 2010 - 08:58 AM

View PostNickDMax, on 1 Feb, 2010 - 07:45 AM, said:

lol... could have just given a link to MSDN which would include a description of all of the functions...

I almost did :D
Was This Post Helpful? 0
  • +
  • -

#7 diego_pmc  Icon User is offline

  • D.I.C Addict

Reputation: 81
  • View blog
  • Posts: 565
  • Joined: 13-May 09

Re: Console cursor coordinates

Posted 01 February 2010 - 09:03 AM

Thanks for the replies. Unfortunately I'm not on Windows -- I'm on Wii Hombrew. The Wii console understands VT terminal codes. I found this list of codes which seems to contain some commands that may help me:

Quote

Save Cursor <ESC>[s

* Save current cursor position.

Unsave Cursor <ESC>[u

* Restores cursor position after a Save Cursor.

Save Cursor & Attrs <ESC>7

* Save current cursor position.

Restore Cursor & Attrs <ESC>8


But I don't really understand terminal codes (I've just been copy-pasting them from other examples). For example, "\x1b[2;0H" seems to be the "Cursor Home" command according to the list above, so I would assume that "\x1b" is the escape command for Wii. (Correct me if I'm wrong.)

So is this what I have to do in order to save and then restore the cursor to its saved coords (I can't test it right now)?
printf("\x1b[s"); // save cursor coords
printf("some text");
printf("\x1b[u"); // restore


Was This Post Helpful? 0
  • +
  • -

#8 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: Console cursor coordinates

Posted 01 February 2010 - 09:04 AM

Quote

I almost did :D


lol, assembly language programmers never do things the *easy way* -- nothing lazy about them. Why write one line when you can do it 63 instead?
Was This Post Helpful? 0
  • +
  • -

#9 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: Console cursor coordinates

Posted 01 February 2010 - 09:13 AM

Quote

Unfortunately I'm not on Windows -- I'm on Wii Hombrew.

note to self: It might have been good to mention that AT THE TOP of the thread...

I wonder if ncurses would work on the Wii.. I bet if escape codes do then it probably would work...
Was This Post Helpful? 0
  • +
  • -

#10 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Console cursor coordinates

Posted 01 February 2010 - 09:19 AM

Hombrew? Is that the same as homebrew or moonshine? If it is I'll drink to that!
Was This Post Helpful? 0
  • +
  • -

#11 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: Console cursor coordinates

Posted 01 February 2010 - 09:36 AM

homebrew refers to unofficial APIs/SDKs that have been developed for a platform by users of the platform. Basically a bunch of old-school hackers figure out how to build a basic API and the the opensource spirit kicks in and other contribute until you have a nice unofficial platform for writing programs to run on devices that were not intended to have such a public API.

Basically it is DIY game console programming.
Was This Post Helpful? 0
  • +
  • -

#12 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Console cursor coordinates

Posted 01 February 2010 - 09:51 AM

@NickDMax ty :)

See ... never to old to learn summat new!

This post has been edited by Martyn.Rae: 01 February 2010 - 09:52 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1