【问题标题】:calculating typing speed in characters per millisecond计算每毫秒字符的打字速度
【发布时间】:2016-02-01 10:38:42
【问题描述】:

我试图计算用户键入时每毫秒字符的键入速度。我有两个控件,richtextbox 和 textbox 作为输入区域,当用户在文本区域输入时,字符之间的速度会显示在 Richtextbox 中。

Public Class Form1
Dim swatch As New Stopwatch

Dim finalSpeed As Double
Dim temp As Double


Private Sub typingArea_KeyPressed(sender As Object, e As KeyPressEventArgs) Handles typingArea.KeyPress
    swatch.Start()

    If swatch.IsRunning Then
        temp += swatch.ElapsedMilliseconds
    End If



    If e.KeyChar = ChrW(Keys.Return) Then
        e.KeyChar = Nothing

        swatch.Reset()

        MsgBox(finalSpeed / typingArea.Text.Length)

        typingArea.Text = Nothing
        logArea.Text = Nothing

    End If

    If typingArea.Text <> Nothing Then
        swatch.Start()

    End If


    If swatch.IsRunning = False Then swatch.Start()

    If typingArea.Text.Length > 0 Then

        logArea.AppendText("time between two chars is " + temp.ToString() + Environment.NewLine)
        finalSpeed += temp
        temp = 0
        swatch.Restart()
        'End If
    Else
        swatch.Reset()
    End If

End Sub

Public Function returnAverageSpeed(ByVal length As Integer, ByVal total As Double) As Double
    Return total / length
End Function

End Class

如果文本框中只有一个字符,则无需计算速度,但如果用户键入第二个字符和后续字符,则我们开始计算速度。如果用户按下回车键,我们计算的平均速度将停止该过程,直到用户再次开始在文本框中输入。这里的问题是,程序无法记录第一个字符和第二个字符之间的速度,但其他字符现在似乎工作正常。

【问题讨论】:

  • 您是否意识到,您在输入每个字符时都使用微积分更新标签这一事实会使您的度量的正确性无效?
  • FWIW - 世界上最快的打字员每分钟输入 216 个单词。平均单词是 5 个字符加上一个用于分隔符。这意味着每毫秒的字符数小于整个字符。

标签: .net vb.net visual-studio-2012


【解决方案1】:

说实话,代码有点难读。而且我不确定为什么两个按键之间的第一个间隙不起作用。但是稍微改写了一下,它似乎工作了。

Private Sub typingArea_KeyPressed(sender As Object, e As KeyPressEventArgs) Handles typingArea.KeyPress
    'if the stopwatch is running add elapsed time. Otherwise start the stopwatch
    If swatch.IsRunning Then
        temp = swatch.ElapsedMilliseconds
        finalSpeed += temp
    Else
        swatch.Start()
    End If
    'if user presses return, pop up the message box, reset the swatch, clear the boxes  and exit the sub
    If e.KeyChar = ChrW(Keys.Return) Then
        e.KeyChar = Nothing
        swatch.Reset()
        MsgBox(finalSpeed / typingArea.Text.Length)
        typingArea.Text = Nothing
        logArea.Text = Nothing
        Exit Sub
    ElseIf typingArea.TextLength > 0 Then
        'If the user has pressed anything other than return and there is already text in the typing area then do this
        logArea.AppendText("time between two chars is " + temp.ToString() + Environment.NewLine)
        finalSpeed += temp
        temp = 0
        swatch.Restart()
    ElseIf typingArea.TextLength = 0 Then
        'if there is nothing in the typing area then reset swatch
        swatch.Restart()
    End If

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-21
    • 2012-06-29
    • 1970-01-01
    • 2012-02-17
    • 1970-01-01
    • 2014-12-20
    • 2012-02-19
    相关资源
    最近更新 更多