Reputation: 122458
I have some python code that is not using curses. Maybe at some point I will change that and use curses throughout all the code. However, now this is not an option.
I was looking for how to read a single keypress in linux and the only feasible solution I found was using curses. However, I don't know if it is possible to use curses only to read a key, but otherwise not interfere with my terminal.
I tried this
def get_key_via_ncurses_impl(win):
return win.getkey()
def get_key_via_ncurses():
x = curses.wrapper(get_key_via_ncurses_impl)
print(x)
return x
..but when called it clears the screen and only after the key was pressed I get to see the original screen again (with the output for which I didnt use curses).
I also tried this:
def get_key_via_ncurses():
stdscr = curses.initscr()
x = get_key_via_ncurses_impl(stdscr)
print(x)
return x
in the hope that curses.initscr()
will not clear the screen, but it does and of course not properly cleaning up will leave my terminal in a messy state.
Is it possible to use curses to read a single key, but keep the rest of the output "uncursed"? If yes, what am I doing wrong?
Upvotes: 2
Views: 652
Reputation: 54515
The filter
function (called before initscr
) tells curses to limit its updates to a single line. The python binding includes filter
.
Here's an example:
import curses
curses.filter()
stdscr = curses.initscr()
curses.noecho()
foo = stdscr.getkey()
print("result:" + foo)
Upvotes: 3