2016-11-17 46 views
0

我正在开发一个小型python脚本,它使用curses来读取终端的stdin。当用户按下该键时,movement_smallstep应该递增,以便在x'重复'之后更快的总体移动。每个按键都会调用这个函数。由于用户的UI变化和功能不同步,用户通常会“超调”目标,因此我想要刷新一些诅咒的标准输入。Python诅咒刷新最新x stdin

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 
... 

回答

0

您可以使用flushinp功能丢弃输入,或typeahead丢弃输出(当落后)。对于后者,文件描述符将为1(用于标准输出)。