我正在开发一个小python脚本,它使用curses来读取终端的stdin。当用户按下键时,movement_smallstep
应递增,以便在x'重复'后更快地进行一般移动。每个按键都会调用此函数。由于移动和功能的UI变化不同步,用户通常会“超越”目标,因此我想要冲洗一些curses的标准输入。
def movement_manager(step,repeats,key):
global old_key #remembering the n-1 pressed key of stdin
global input_repeats #remembering how often the key was pressed
global movement_smallstep #movement for ui change
if key != old_key:
input_key = key
input_repeats = 0
movement_smallstep = 1
if (input_repeats > repeats):
movement_smallstep+=1
#Flush the newest x stdin inputs of the queue here
input_repeats+=1
return movement_smallstep
因此,例如用户按下“A”键23次。
Movement(repeats 0-10)=1 Stdin= Unflushed
Movement(repeats 10-20)=2 Stdin= flush oldest userinput
Movement(repeats 10-20)=3 Stdin= flush oldest 2 userinputs
...