How To Control Cursor On Output Screen With KeyBoard And Mouse

so this is a code im writing, and i what i wish is that when i press a specific key a specific task is performed, i was doing perfectly fine until the mouse hit me (not literally :v), now what i want is that when i left click on the screen the program should get coordinates of the screen and then the cursorrow and cursorcolumn should be updated. the function im using is glitching a bit, i gets stuck usually, help me get a better way,
P.S : trying to make a text editor. these are just the initial steps.
also please suggest me features that the program shall have, ty

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <windows.h>
#include <WinUser.h>
#include <fstream>
#include <string>
#include <math.h>
#include <stdio.h>
#include <conio.h>
using namespace std;
struct Line
{
	int Size;
	char* Cs;
};
struct Document
{
	int NOfLines;
	Line* Lines;
};
struct Word
{
	char* Cs;
};
struct Sentence
{
	int WSize;
	Word *Ws;
};
struct Paragraph
{
	int SenSize;
	Sentence *Ss;
};

HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
void GetRowColByLeftClick(int &rpos, int &cpos)
{
	HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE);
	DWORD Events;
	INPUT_RECORD InputRecord;
	SetConsoleMode(hInput, ENABLE_PROCESSED_INPUT | ENABLE_MOUSE_INPUT | ENABLE_EXTENDED_FLAGS);
	//do
	//{
		ReadConsoleInput(hInput, &InputRecord, 1, &Events);
		if (InputRecord.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
		{
			cpos = InputRecord.Event.MouseEvent.dwMousePosition.X;
			rpos = InputRecord.Event.MouseEvent.dwMousePosition.Y;
			break;
		}
	//} while (true);
}
void gotoRowCol(int rpos, int cpos)
{
	int xpos = cpos, ypos = rpos;
	COORD scrn;
	HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE);
	scrn.X = xpos; scrn.Y = ypos;
	SetConsoleCursorPosition(hOuput, scrn);
}

int main()
{
	int CursorRow = 0, CursorColumn = 0;
	Document *File;

	while (true)
	{
		if (_kbhit())
		{
			char Key = _getch();
			cout << Key;
			if (GetAsyncKeyState(VK_UP))
			{
				CursorRow--;
			}
			if (GetAsyncKeyState(VK_DOWN))
			{
				CursorRow++;
			}
			if (GetAsyncKeyState(VK_LEFT))
			{
				CursorColumn--;
			}
			if (GetAsyncKeyState(VK_RIGHT))
			{
				CursorColumn++;
			}
			if (GetAsyncKeyState(VK_RETURN))
			{
				CursorColumn = 0;
				CursorRow++;
			}
			if (GetAsyncKeyState(VK_BACK))
			{
				CursorColumn--;
			}
			if (GetAsyncKeyState(VK_LBUTTON))
			{
				GetRowColByLeftClick(CursorRow, CursorColumn);
				cout << ".";
			}
			if (GetAsyncKeyState(VK_RBUTTON))
			{
				
			}
		}
		gotoRowCol(CursorRow, CursorColumn);
	}
}
Last edited on
Topic archived. No new replies allowed.