2013-03-13 114 views
1

我想拥有它,这样每次我点击空格键时,终端中的数字就会增加一个,这样我就可以在头脑中保留一个数字,而不会忘记它。但是,如果我为此使用raw_input,则每次都必须输入enter,这很烦人。我怎样才能做到这一点,我建立一个计数器,每次按下空格键时就会增加一个变量?如何在python中建立计数器?

这是我的。

x=0 

while x<10000000: 
    press = raw_input() 
    if press == "z": 
     x=x+1 
     print x 
+1

http://stackoverflow.com/questions/575650/how-to-obtain-the-keycodes-in-python – Bemmu 2013-03-13 06:07:39

回答

0

如果您使用的是Linux/Unix,则有curses模块。

import curses 

def check_press(scr): 
    c = None 
    x = 0 
    while c != 120: # exit on x 
     c = scr.getch() 
     if c == 122: # count on 'z' 
      x += 1 
      scr.addstr(0, 0, "%5d" % x) 
      scr.refresh() 

if __name__ == '__main__': 
    curses.wrapper(check_press) 
3

如果您使用Windows,则有msvcrt模块。所以,

import msvcrt 

while x = True: 
    keypress = msvcrt.getch() 
    if keypress == "z": 
     x=x+1 
     print x