Measuring time between keystrokes in python

前端 未结 3 591
轻奢々
轻奢々 2020-12-19 20:11

Using the following code:

   >>> import time
   >>> start = time.time()
   >>> end = time.time()
   >>> end - start


        
相关标签:
3条回答
  • 2020-12-19 20:28

    A simple way you could do that is:

    from time import time
    begin = time.time()
    raw_input() #this is when you start typing
    #this would be after you hit enter
    end = time.time()
    elapsed = end - begin
    print elapsed
    #it will tell you how long it took you to type your name
    
    0 讨论(0)
  • 2020-12-19 20:28

    On Windows you can you msvcrt.kbhit() to tell when a key has been pressed. See http://effbot.org/librarybook/msvcrt.htm for a worked-out example.

    0 讨论(0)
  • 2020-12-19 20:48

    This will work (on some systems!):

    import termios, sys, time
    def getch(inp=sys.stdin):
        old = termios.tcgetattr(inp)
        new = old[:]
        new[-1] = old[-1][:]
        new[3] &= ~(termios.ECHO | termios.ICANON)
        new[-1][termios.VMIN] = 1
        try:
            termios.tcsetattr(inp, termios.TCSANOW, new)
            return inp.read(1)
        finally:
            termios.tcsetattr(inp, termios.TCSANOW, old)
    
    
    inputstr = ''
    while '\n' not in inputstr:
        c = getch()
        if not inputstr: t = time.time()
        inputstr += c
    elapsed = time.time() - t
    

    See this answer for nonblocking console input on other systems.

    0 讨论(0)
提交回复
热议问题