How to Get the Mouse Position in a Console Program

Get Mouse Cursor position in a C# console app

This program will get X, Y position of mouse on screen each 1 second

using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Threading;

namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
while (true)
{
// New point that will be updated by the function with the current coordinates
Point defPnt = new Point();

// Call the function and pass the Point, defPnt
GetCursorPos(ref defPnt);

// Now after calling the function, defPnt contains the coordinates which we can read
Console.WriteLine("X = " + defPnt.X.ToString());
Console.WriteLine("Y = " + defPnt.Y.ToString());
Thread.Sleep(1000);
}
}

// We need to use unmanaged code
[DllImport("user32.dll")]

// GetCursorPos() makes everything possible
static extern bool GetCursorPos(ref Point lpPoint);
}
}

Source

How can I get the mouse position in a console program?

You'll need to use the *ConsoleInput family of methods (peek, read, etc). These operate on the console's input buffer, which includes keyboard and mouse events. The general strategy is:

  1. wait on the console's input buffer handle (ReadConsoleInput)
  2. determine the number of waiting events (lpNumberOfEventsRead)
  3. handle them as you see fit (i.e. MOUSE_EVENT and MOUSE_EVENT_RECORD)

You'll have to indicate that you want to retrieve mouse input using SetConsoleMode first though, as illustrated in this MSDN article.

How can I get the position of the cursor in a console app?

Look at the various methods and properties of the Console class. Specifically, using Console.CursorLeft and Console.CursorTop, you can get and set the position of the cursor.

How do I find the coordinates of the cursor in a console window?

As per the documentation for the SetConsoleCursorPosition function:

To determine the current position of the cursor, use the GetConsoleScreenBufferInfo function.

In general, if you know how to get or set something, the MSDN documentation for that function will hint at how to do the opposite. That is certainly true in this case.

If we look up the GetConsoleScreenBufferInfo function, we see that we've struck paydirt. It fills in a CONSOLE_SCREEN_BUFFER_INFO structure that, among other things, contains a COORD structure that indicates the current column and row coordinates of the cursor.

There is even an example. Package it up into a function if you want to make it convenient:

COORD GetConsoleCursorPosition(HANDLE hConsoleOutput)
{
CONSOLE_SCREEN_BUFFER_INFO cbsi;
if (GetConsoleScreenBufferInfo(hConsoleOutput, &cbsi))
{
return cbsi.dwCursorPosition;
}
else
{
// The function failed. Call GetLastError() for details.
COORD invalid = { 0, 0 };
return invalid;
}
}

As Michael mentioned already in a comment, GetCursorPos doesn't work because it is for the mouse cursor (the arrow), not the cursor (insertion point) in a console window. It is returning valid values, just not the values you are looking for. Lucky that the return types are different, otherwise they'd be easy to mix up. Calling it the "cursor" for a console window is sort of misleading, it probably should be called the caret.

How do I set the position of the mouse cursor from a Console app in C#?

Inside your console application, add a reference to System.Windows.Forms.dll and use the other techniques you've read about. The choice of console vs windows exe only impacts the PE header (and maybe the default code template, but you can hack that trivially); you can still use the full framework in a console exe.

The mouse you want to control is in windows, not the console.

Get console's cursor position

If you are on Windows, you can do this (error handling omitted for brevity):

HANDLE hConsoleOutput = GetStdHandle (STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi = { };
BOOL ok = GetConsoleScreenBufferInfo (hConsoleOutput, &csbi);

The current cursor position should then be in csbi.dwCursorPosition;

Documentation here and here.

On Unix platforms, you would use the ncurses library.

Is there a way to get the mouse position within the terminal?

I have solved my problem.

To get events, the bash escape load ESC[?100Xh, replacing X with a number. The events will then be logged to the console.

Getting mouse position in c#

You should use System.Windows.Forms.Cursor.Position: "A Point that represents the cursor's position in screen coordinates."



Related Topics



Leave a reply



Submit