Hi I'm writing a program in python curses and I need to get just one char from a long keypress. (In other words what I need is that if i keep pressing down a key my program just has to get the char with the function getchar() just once). I need that because I want to prevent curses refreshing many times the windows. I'm not giving the code cause it is in italian and it is very long. Thank you.
Asked
Active
Viewed 399 times
1 Answers
0
One normally solves this problem by
- setting a variable which will record the fact that a character was read
- reading the character
- if no character is available (within a timeout, for instance), stop reading
- if no character was read yet, record the fact that one was read (as well as remembering which character)
- go back to step 2, to read again
Simply reading from the terminal will not cause curses to do extra work (it does a refresh on each read, anyway). It would only have work to do if your program modifies the screen. So (assuming you have turned echo
off), there is no need to worry about the number of calls to refresh
.
Regarding the comment "it has to clear and refresh the box": there are choices (per documentation):
- window.erase() clears a window, but
- window.clear() is like erase(), but also causes the whole window to be repainted upon next call to refresh().
Avoid using window.clear() unless you intend repainting the whole window, because it is much slower, and is visually distracting.
By the way, the curses function is called getch
, not getchar
. The latter may not work properly with curses.

Thomas Dickey
- 51,086
- 7
- 70
- 105
-
What I have is a menu that gets records from a sqlite db and every time i press key up or down it has to clear and refresh the box: I just wanted to prevent that the box flashes because of too many refreshes at the same time. Tomorrow I'll try what you suggested thanks :) – Alessio Ragno May 31 '15 at 23:57
-
Clearing the box is a design problem. In curses, you can either *clear* or *erase*. The latter is less distracting. – Thomas Dickey Jun 01 '15 at 00:09
-
Thanks that was really what i needed! Do I have to close the post or something similar? – Alessio Ragno Jun 01 '15 at 09:07
-
Marking it "accepted" is enough. – Thomas Dickey Jun 01 '15 at 09:14