0

enter image description here

Hello. I am making checkers with C++ for a console window. Currently the only method of interacting is by entering the coordinates of the piece you want to move and then the the coordinates of the place you want to move it. This gets very tedious very fast. I was thinking of somehow getting the coordinates of the selected letter (the 'w' in the image) from the console. I have tried Googling this but I don't know how to word this query properly and haven't found anything yet. Is this possible? If not can you point me some alternative?

mrf1freak
  • 71
  • 2
  • 8
  • 1
    This will be a lot easier with a library such as [NCurses](https://en.wikipedia.org/wiki/Ncurses). – Arnav Borborah Feb 20 '18 at 17:54
  • @ArnavBorborah Its for a project so I cant use any libraries other than the default ones in windows :( – mrf1freak Feb 20 '18 at 17:59
  • Possibly: https://stackoverflow.com/questions/6423729/get-current-cursor-position – NathanOliver Feb 20 '18 at 18:01
  • 1
    Why not use the mouse? Windows can do mouse interaction in console windows. – Zan Lynx Feb 20 '18 at 18:03
  • Here is some Microsoft sample code that may help you: https://learn.microsoft.com/en-us/windows/console/reading-input-buffer-events – Zan Lynx Feb 20 '18 at 18:06
  • But to add to this, of course this isn't portable to other non-Windows machines. There is no portable C++ standard for anything beyond very simple text input and output. – Zan Lynx Feb 20 '18 at 18:07

1 Answers1

2

You can use BOOL GetConsoleSelectionInfo(PCONSOLE_SELECTION_INFO lpConsoleSelectionInfo) from windows.h:

CONSOLE_SELECTION_INFO selectionInf;
GetConsoleSelectionInfo(&selectionInf);

members of structure CONSOLE_SELECTION_INFO :

  1. dwFlags: CONSOLE_NO_SELECTION, CONSOLE_SELECTION_IN_PROGRESS, CONSOLE_SELECTION_NOT_EMPTY, CONSOLE_MOUSE_SELECTION, CONSOLE_MOUSE_DOWN

  2. dwSelectionAnchor: structure COORD with x and y coordinates of selection (exactly what you need)

  3. srSelection: structure SMALL_RECT with coordinates of selection's rectangle (Left and Top, Right and Bottom)

Alexander
  • 34
  • 4