I'm trying to write an app where I need to accept keypresses, and call a function whenever the user has stopped typing (or when there is a specified delay between keystrokes).
How do I measure the time between two keystrokes?
I'm trying to write an app where I need to accept keypresses, and call a function whenever the user has stopped typing (or when there is a specified delay between keystrokes).
How do I measure the time between two keystrokes?
Get the current time, then subtract the previous current time. See -[NSDate timeIntervalSinceDate:].
Probably a better approach is to pick up the NSEvent associated with each keypress and compare the difference in their -timestamp property.
Something like this:
NSDate *start = [NSDate date];
// do the thing you are timing
NSDate *stop = [NSDate date];
NSTimeInterval duration = [start timeIntervalSinceDate:stop];
Not sure how computationally intensive this is, if it is easy to reset timeout on timer.