Hey, throwing a quick question in here:

Is there a way to get the value at a cursor's location?

For instance, the cursor is at {0,20} and I decide to do
cout<<"Foo"
then I move my cursor to {0,10} do something else and then come back to {0,20}, and I want to know what I wrote earlier, because I want to use that char/string in my next function.

So, to reiterate, is there a way to get the value written at a cursor's location?

It's a console program.
I know how to do it in an application though. (Using Windows API, which I find annoying, but since I'm having a hard time learning DirectX...)

Ah, I couldn't find it on MSDN, thanks for showing me! :)

EDIT:
Alright, I found the function, but I cannot interpret the MSDN documentation of it (as usual)

Anyone want to try explaining it to me?
http://msdn.microsoft.com/en-us/library/ms684969(VS.85).aspx
Here's what I tried:

#include <iostream>

using namespace std;

int main(){
	cout<<"abc";
	char buffer[10];
	int count = 0;
	COORD loc = {0,0};
	ReadConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), (LPWSTR)buffer, 3, loc, (LPDWORD)count);
	cout<<buffer;
	return 0;
}

Of course, that didn't work, since using casts always scramble everything anyway, and I can't make anything of the hungarian notation... Help!

LongPointerWideString? lousy freggin' hungarians...! Trying to make everything logical, I say they messed it up even more! :P
I have no idea how to declare such a variable...

// #include <windows.h> in stdafx.h (VC++)

struct CurPos
{
    CurPos():x(-1),y(-1) {}
    int x, y;
    operator bool() const { return x >= 0 && y >= 0; }
};

CurPos getCursorPos()
{
    CurPos pos;
    CONSOLE_SCREEN_BUFFER_INFO con;
    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
    if (hcon != INVALID_HANDLE_VALUE &&
        GetConsoleScreenBufferInfo(hcon,&con)) 
    {
        pos.x = con.dwCursorPosition.X;
        pos.y = con.dwCursorPosition.Y;
    }
    return pos;
}

int main()
{
    cout << "1234\n"; // must be y = 1, x = 0 (started from IDE)
    CurPos pos = getCursorPos();
    if (pos)
        cout << pos.y << ':' << pos.x << endl;
    return 0;
}

Ehm, no, that was not what I was asking for.
I wanted a function that returns the value at a position in the console using the cursor, read the previous comment.

What's a problem?

char getCursorChar()
{
    char c = '\0';
    CONSOLE_SCREEN_BUFFER_INFO con;
    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
    if (hcon != INVALID_HANDLE_VALUE &&
        GetConsoleScreenBufferInfo(hcon,&con)) 
    {
        DWORD read = 0;
        if (!ReadConsoleOutputCharacterA(hcon,&c,1,
            con.dwCursorPosition,&read) || read != 1
           )
           c = '\0';
    }
    return c;
}

It's so easy ;) and looks like a needless pastime...

// helloworld.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>

CONSOLE_SCREEN_BUFFER_INFO csbi;

void gotoxy ( short x, short y )
{  COORD coord = {x, y};
   SetConsoleCursorPosition ( GetStdHandle ( STD_OUTPUT_HANDLE ), coord );
}

COORD getxy (  CONSOLE_SCREEN_BUFFER_INFO * csbi)
{
	
	COORD coord = csbi->dwCursorPosition;
	return coord;
}

char getCursorChar(){    
	
	char c = '\0';

    CONSOLE_SCREEN_BUFFER_INFO con;
    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
    if (hcon != INVALID_HANDLE_VALUE && 
		GetConsoleScreenBufferInfo(hcon,&con)) {
			DWORD read = 0;
			if (!ReadConsoleOutputCharacterA(hcon,&c,1,
				con.dwCursorPosition,&read) || read != 1 
				)  
				c = '\0';
    }    
	return c;
}

int main(int argc, char* argv[])
{	
	char ch;
	COORD coord;
		
	gotoxy( 10,5);
	printf("Hello World!\n");

	//Read the Cursor Position from CONSOLE_SCREEN_BUFFER_INFO
	GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
	coord = getxy( &csbi);

	gotoxy(10,6);
	printf ("X:%d", coord.X);
	printf (" Y:%d",coord.Y);


	gotoxy(16,5);
	ch = getCursorChar();
	gotoxy(0,7);
	printf("Cursor Char:%c\n",ch);
	return 0;
}

Edited 7 Years Ago by technology: Change title

This article has been dead for over six months. Start a new discussion instead.