Reputation: 21
This code runs for 1 million iterations (about a few seconds on my machine), yet when I hold down a button, the # iterations where I hold down does not increase substantially.
import curses
stdscr = curses.initscr()
curses.cbreak()
curses.noecho()
stdscr.nodelay(1)
num_iters_nochar = 0
num_iters_char = 0
for i in range(10**6):
if stdscr.getch() == -1:
num_iters_nochar += 1
else:
num_iters_char += 1
curses.nocbreak()
curses.echo()
curses.endwin()
print num_iters_nochar , 'iterations with no input'
print num_iters_char , 'iterations with input'
Why doesn't the nodelay getch() accurately capture the button press?
Upvotes: 2
Views: 3333
Reputation: 2067
Because nodelay
is literally no delay. And unless your keyboard repeat rate is really high, there will be some iterations of the loop when the getch
times out and yields no input, which would be normal.
For me I get:
999742 iterations with no input
258 iterations with input
Which seems reasonable for 11 seconds. There is no way my keyboard would repeat a key 1 million times in 11 seconds, and if it did it would be impossible to use the keyboard for anything but hitting all getches in this program, since it would have to count ~99 000
keys per second, which would make typing really painful. So, in short, your numbers are normal.
Upvotes: 1