0

I am wanting to write a function to ask the user input using Python curses. The problem I am having is I want to have the cursor hidden in other parts of the program except in the text-box. I intend to use this function in many cases and if the cursor is hidden before the function call, it should be returned to that state and vise versa. To do this I need to be able to read the state of the cursor. How do I do this? (Something like this?)

if curses.get_cursor_state() == 0:
    #Hidden
    hidden=True
else:
    hidden=False

#Textbox code here

if hidden:
    curs_set(0)
else:
    curs_set(1)
Enderbyte09
  • 409
  • 1
  • 5
  • 11

1 Answers1

0

The only way I'm aware of to check the cursor visibility state is to first set the visibility state:

curses.curs_set(visibility)

Set the cursor state. visibility can be set to 0, 1, or 2, for invisible, normal, or very visible. If the terminal supports the visibility requested, return the previous cursor state; otherwise raise an exception. On many terminals, the “visible” mode is an underline cursor and the “very visible” mode is a block cursor.

When you call curses.curs_set, if it works, it returns the previous cursor state. There doesn't seem to be any other way to check it. Fortunately, it sounds like you want to have the cursor visible during this section anyway, so you can do

prev_state = curses.curs_set(1)
# do stuff
curses.curs_set(prev_state)

to temporarily ensure the cursor is visible, and restore the old state afterward.

user2357112
  • 260,549
  • 28
  • 431
  • 505