代码之家  ›  专栏  ›  技术社区  ›  Debajit

可可:测量按键之间的时间?

  •  2
  • Debajit  · 技术社区  · 16 年前

    5 回复  |  直到 16 年前
        1
  •  8
  •   David Weiss    15 年前

    NSDate *start = [NSDate date];
    // do the thing you are timing
    NSDate *stop = [NSDate date];
    
    NSTimeInterval duration = [start timeIntervalSinceDate:stop];
    
        2
  •  5
  •   Mike Abdullah    16 年前

    也许更好的方法是提取与每个按键相关的NSEvent,并比较它们的-timestamp属性的差异。

        3
  •  2
  •   Peter Hosey    16 年前

    获取当前时间,然后减去上一个当前时间。看 -[NSDate timeIntervalSinceDate:] .

        4
  •  0
  •   stefanB    15 年前
    • 您可以设置计时器,在用户停止键入后执行某项操作X的时间,然后在每次用户键入某项操作时重新启动该时间,从而延迟计时器的到期时间。

    • 您可以选择在最后一次按键时启动计时器,持续X时间。到期时,您可以检查上次击键的时间戳,该时间戳是您在上次击键时保存的,然后您可以从(超时-上次击键时间)的时间量开始重新启动计时器。到期后再次检查。然后,在每次按键时,如果计时器正在运行,则仅更新最后一次按键的时间戳。..
        5
  •  0
  •   Amaan Durrani    6 年前

    我认为你应该用线来做这件事。为按键创建线程,在每个线程中,您可以计算按键之间的时间。如需更多解释,请观看我的视频,了解这个确切的解决方案。

    https://www.youtube.com/watch?v=sDGYM8LeZh8

    请参阅下面的代码:

    import keyboard # keyboard library
    import string   # string for capturing keyboard key codes
    import time     # for capturing time
    
    from threading import * # threads for keypresses
    
    # get the keys
    keys = list(string.ascii_lowercase)
    
    # key listener
    def listen(key):
        while True:
            global timeda   # global variable for storing time for 1st keypress
            global newda    # global variable for storing time for next keypress
    
            keyboard.wait(key)  # when key is presses
    
            # check if variables are defined
            try:
                timeda
                newda
    
            # this will run for the first keypress only so assign initial time to variable
    
            except NameError:
                timeda = time.time()
                newda  = time.time()
                print("First key is pressed at "+str(round(newda,2)))
                print('\n==========\n')
    
            # for all keypresses except for the first will record time here
            else:
                newda = time.time()         # assign time for next keypressed
                newtime = newda - timeda    # get difference between two keys presses
    
                # just to test time of first keypress
                print("Previous keypress was at "+str(round(timeda,2)))
    
                # just to test time of next keypress
                print("Current keypress is at "+ str(round(newda,2)))
    
                # convert time into seconds
                newtime = newtime % 60
    
                print("Difference between two keypresses is "+str(round(newtime,2)))
                print('\n==========\n')     # need some space for printing difference 
                timeda = time.time()
    
    # creating threads for keys and assigning event and args 
    threads = [Thread(target=listen, kwargs={'key':key}) for key in keys]
    
    # calling each thread
    for thread in threads:
        thread.start()
    
    
    # thats it