I’m building a typing test application in C#. The program calculates typing speed in characters per minute (CPM) by tracking the number of key presses (timesPressed) and dividing it by the total elapsed time in minutes. Below is the relevant portion of the code
// Calculate characters per minute
TimeSpan elapsedTime = stopwatch.Elapsed;
double minutes = elapsedTime.TotalMinutes;
if (minutes > 0)
{
double charactersPerMinute = timesPressed / minutes;
MessageBox.Show($"Correct text. Unique letters: {uniqueLetters}. Characters per minute: {charactersPerMinute}");
}
else
{
MessageBox.Show("Elapsed time is too short to calculate characters per minute.");
}
My logic is as follows:
1.stopwatch.Elapsed captures the elapsed time since the test started.
2.TotalMinutes is used to convert this time into minutes.
3.CPM is calculated by dividing the total number of keystrokes (timesPressed) by the elapsed time in minutes.
Is this a valid approach to measuring typing speed in CPM? Are there any edge cases or inaccuracies in this logic? For example, should I use enterText.Length instead of timesPressed? I would appreciate any suggestions to improve this calculation, although I would like it to count the wrong ones as keystrokes as well.
6