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

Cocoa 可可:测量按键之间的时间?,cocoa,Cocoa,我正在尝试编写一个应用程序,在这个应用程序中,我需要接受按键,并在用户停止键入时(或在按键之间有指定的延迟时)调用函数 如何测量两次击键之间的时间?获取当前时间,然后减去上一次当前时间。请参阅。可能更好的方法是选择与每个按键相关联的N事件,并比较它们的-timestamp属性的差异。类似以下内容: NSDate *start = [NSDate date]; // do the thing you are timing NSDate *stop = [NSDate date]; NSTimeI

我正在尝试编写一个应用程序,在这个应用程序中,我需要接受按键,并在用户停止键入时(或在按键之间有指定的延迟时)调用函数


如何测量两次击键之间的时间?

获取当前时间,然后减去上一次当前时间。请参阅。

可能更好的方法是选择与每个按键相关联的N事件,并比较它们的-timestamp属性的差异。

类似以下内容:

NSDate *start = [NSDate date];
// do the thing you are timing
NSDate *stop = [NSDate date];

NSTimeInterval duration = [start timeIntervalSinceDate:stop];
  • 您可以将计时器设置为在用户停止键入后的X时间内执行某个内容,然后在每次用户键入某个内容时重新启动该时间,这样会延迟计时器的到期时间
如果很容易重置计时器上的超时,则不确定这在计算上有多密集

  • 或者,您可以在最后一次击键时启动计时器,时间为X。到期时,您可以检查上次击键的时间戳(上次击键时保存的时间戳),然后您可以从(超时-上次击键时间)开始重新启动计时器。到期后再做一次检查。然后,在每次击键时,如果计时器正在运行,则只更新最后一次击键的时间戳

我认为您应该使用线程来实现这一点。为关键点创建线程,在每个线程中可以计算关键点笔划之间的时间。要了解更多的解释,请观看我的视频,了解这个确切的解决方案

请参阅下面的代码:

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

我认为最后一行应该是:NSTimeInterval duration=[stop TimeIntervalsCenceDate:start];这能测量毫秒吗?