Getting mouse position in console window

Hello All, i have a question on how to get the cursor/mouse from the console window. i am making a game and i need to get the mouse position in pixels

using the folowing example https://msdn.microsoft.com/en-gb/library/windows/desktop/ms685035(v=vs.85).aspx i was able to het keyboad and mouse input but the x and y position is the size in the buffer.
"dwMousePosition
A COORD structure that contains the location of the cursor, in terms of the console screen buffer's character-cell coordinates."

so i was wondering do any of you know of a better way to get the actual pixel position of the cursor\mouse for the console window.
You need to tell the OS to give you mouse information along with textual input.

1
2
HANDLE hstdin = GetStdHandle( STD_INPUT_HANDLE );
SetConsoleMode( hstdin, GetConsoleMode( hstdin ) | ENABLE_MOUSE_INPUT );

Your main loop should be using ReadConsoleInput() to get input.

http://www.google.com/search?btnI=1&q=msdn+SetConsoleMode
http://www.google.com/search?btnI=1&q=msdn+ReadConsoleInput
http://www.google.com/search?btnI=1&q=msdn+INPUT_RECORD+structure

Hope this helps.

[edit]
You might find it worth your time to check out PDCurses (which is the Windows port of NCurses). Both make this kind of stuff a lot easier than dinking directly with the OS API.
Last edited on
Duoas: thanks, what you explained is exactly what i am doing ... it works its just the data i am getting from it is not in the format i wanted.

as for PDCurses i will have a look if it is something that i can use. thanks for the tip.

Edit: i think PDCurses might be able to help me its jut i don't know it might be a little overkill. and i already have all my input working other than this. so if all else fails i will try it out.
Last edited on
At the center of your game you should have a loop which polls the OS for input, like this:

1
2
3
4
5
6
7
8
9
10
11
12
  while (!quit && wait_for_input( my_delay_time ))
  {
    get input
    switch (input type)
    {
      case key:   do_key_input( keydata ); break;
      case mouse: do_mouse_input( mousedata ); break;
      case timer: do_timer_input( timerdata ); break;
    }
    if (refresh needed)
      refresh;
  }

The INPUT_RECORD tells you the kind of input you have. Timer events you have to create on your own, based on an elapsed timer or something.

Use one of the 'wait' functions for the loop. If you want, my iskeypressed() function will do what you want:
http://www.cplusplus.com/forum/general/64802/#msg350595

Hope this helps.
Duoas: thanks for the help but its not what i needed to know. I am so sorry for not being direct its my fault, if you woult let me let me explain my problem again and i will also show you my code.

so i am making a simple game in the console window, its not to fancy hehe ok maybe a little bit. here is a actual screenshot of one of the maps i made in tiled for my game http://s17.postimg.org/p6asy0cbj/Window.png well ok then enough of showing off what i a making.

Console_Mouse.cpp is where i do all my mouse event stuff

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
111
112
113
114
115
#include "Console_Mouse.h"
#include <iostream>
#include <Windowsx.h>

Console_Mouse Console_Mouse::GetMouse()
{
	static Console_Mouse m;
	return m;
}

Console_Mouse::Console_Mouse()
{
	mMouseState = Mouse::State();
}

void Console_Mouse::ProcessEvent(const MOUSE_EVENT_RECORD& msg)
{
	switch (msg.dwEventFlags)
	{
	case MOUSE_MOVED:
		GetMouse().Motion(msg);
		break;
	case 0: //button click
	case DOUBLE_CLICK:
		GetMouse().Button(msg);
		break;
	case MOUSE_HWHEELED:
	case MOUSE_WHEELED:
		GetMouse().Wheel(msg);
		break;
	}
}

void Console_Mouse::Reset()
{
	Console_Mouse* m = &GetMouse();

	m->mMouseState->LeftButton = ButtonState::Released;
	m->mMouseState->MiddleButton = ButtonState::Released;
	m->mMouseState->RightButton = ButtonState::Released;
	m->mMouseState->XButton1 = ButtonState::Released;
}

int OldX = 0;
int OldY = 0;

void Console_Mouse::Motion(const MOUSE_EVENT_RECORD& msg)
{
	mMouseState->Position.X = msg.dwMousePosition.X;
	mMouseState->Position.Y = msg.dwMousePosition.Y;

	mMouseState->ReletivePos.X = mMouseState->Position.X - OldX;
	mMouseState->ReletivePos.Y = mMouseState->Position.Y - OldY;

	if (OldX != mMouseState->Position.X || OldY != mMouseState->Position.Y)
		mMouseState->StateChanged = true;

	OldX = msg.dwMousePosition.X;
	OldY = msg.dwMousePosition.Y;
}


int OldButton = 0;
void Console_Mouse::Button(const MOUSE_EVENT_RECORD& msg)
{
	ButtonState bState; //http://msdn.microsoft.com/en-us/library/windows/desktop/ms645610(v=vs.85).aspx

	int CurrentButton = msg.dwButtonState;

	//set the type of button event
	if (CurrentButton) // 0 = button release note we don't know what was released so store last button
		bState = ButtonState::Pressed;
	else
		bState = ButtonState::Released;

	if (CurrentButton == FROM_LEFT_1ST_BUTTON_PRESSED || OldButton == FROM_LEFT_1ST_BUTTON_PRESSED) //left
	{
		mMouseState->LeftButton = bState;
	}
	else if (CurrentButton == RIGHTMOST_BUTTON_PRESSED || OldButton == RIGHTMOST_BUTTON_PRESSED) //right
	{
		mMouseState->RightButton = bState;
	}
	else if (CurrentButton == FROM_LEFT_2ND_BUTTON_PRESSED || OldButton == FROM_LEFT_2ND_BUTTON_PRESSED) //middle
	{
		mMouseState->MiddleButton = bState;
	}

	OldButton = CurrentButton;
}


int OldWheelX = 0;
int OldWheelY = 0;

void Console_Mouse::Wheel(const MOUSE_EVENT_RECORD& msg)
{
	switch (msg.dwEventFlags) 
	{
		case MOUSE_HWHEELED: // horizontal
			mMouseState->ScrollWheel.X = (short)HIWORD(msg.dwButtonState);
			mMouseState->ScrollWheel.Y = 0;
			break;
		case MOUSE_WHEELED: // vertical
			mMouseState->ScrollWheel.X = 0;
			mMouseState->ScrollWheel.Y = (short)HIWORD(msg.dwButtonState);
			break;
	}
	
	if (OldWheelX != mMouseState->ScrollWheel.X || OldWheelY != mMouseState->ScrollWheel.Y)
		mMouseState->StateChanged = true;

	OldWheelX = mMouseState->ScrollWheel.X;
	OldWheelY = mMouseState->ScrollWheel.Y;
}


not all of the code is tested but what i want to look at is line 49 and 50, where i get the moue position. i am using the MOUSE_EVENT_RECORD but it returns a cursor position as in 8x12 blocks so

screen buffer = 80x32
0x1--------------------------------------- 0x79
|**************************|
|**************************|
|**************************|
|**************************|
|**************************|
|**************************|
0x31--------------------------------------- 31x79

but i need the pixel position. i hope this explains it a little more.
Last edited on
Ah. Sorry. You can't get the pixel position without installing a system-wide event hook.

Why would you need the pixel position for a console position? The finest resolution you have on the console is a character glyph.

Are you sure you shouldn't be using a library like SDL or SFML for what you are doing?
Hehehe yes i is beter to use SDL , SFML or any other graphics engine. but that takes the fun out of doing things the console id not designed to do. This is one of my learning project. in this case i am trying to make a game in the console using setpixel. on my first try i got a whole 1 frame every 3 sec after a lot of work i got my code efficient enough to get 60fps. so i guess i will lock my gui to the cursor grid ;} anyways thanks for thinking and trying to help.
If you really want to go all out, you know you can get the console's GDI surface and draw on it, right? (The only issue is that you would have to refresh the entire surface every time you do an update, since there is no graphics buffering for it.)

Ah, here it is. https://www.daniweb.com/software-development/c/code/216431/put-a-bitmap-image-on-your-console-c
The modern Windows API has a function to get the Console's window handle, but you are just as well-off using the one there. (His code is a little crufty, so you'll have to massage it a bit, but the mechanics are correct.)

LOL.
Hehe yess i managed to get somthing like that to work with BitBlt https://msdn.microsoft.com/en-us/library/dd183370.aspx witch let me copy the buffer over directly instead of set pixel. now just for fun i have directx running in the console window. just fun thing you should never do ;}

but i actually have a game idea that would fit well with this setup. so thats why i am trying so hard ;} and its just plane fun.
Last edited on
Topic archived. No new replies allowed.