br0kenpixel
br0kenpixel

Reputation: 105

How can I get the current position of the cursor (curses)?

I'm trying to get the current location of the cursor and I can't seem to be able to do that. Apparently, the stdscr.getxy() is supposed to return the current position, however it does not seem to return the correct location.

Returned values by getmaxyx and getyx

As you can see, getyx() returns a very similar value to getmaxyx(). The cursor is on line 2 at character 24, so the Y coordinate (27) makes sense, however the X coordinate (99) doesn't. Shouldn't it be something like 76 (100-24)?

Upvotes: 1

Views: 1374

Answers (2)

Thomas Dickey
Thomas Dickey

Reputation: 54475

curses uses a virtual screen and a physical screen. The virtual screen contains the information that your program sets, while the physical screen is what actually appears on the terminal after optimizing the cursor movement.

You're seeing the latter (the physical screen), which will only correspond to the virtual screen after doing a refresh. Normally (when your program reads a character from the keyboard), those are equivalent (because getch does a refresh).

Upvotes: 0

kids code
kids code

Reputation: 5

I use a module called pyautogui to trace the cursor:

import pyautogui

var = pyautogui.position()

Upvotes: -2

Related Questions