Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 以字符/毫秒为单位计算键入速度_.net_Vb.net_Visual Studio 2012 - Fatal编程技术网

.net 以字符/毫秒为单位计算键入速度

.net 以字符/毫秒为单位计算键入速度,.net,vb.net,visual-studio-2012,.net,Vb.net,Visual Studio 2012,我试图计算用户打字时的打字速度,单位为每毫秒字符数。我有两个控件,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 KeyP

我试图计算用户打字时的打字速度,单位为每毫秒字符数。我有两个控件,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
公共类表单1
暗淡的样本作为新的秒表
暗淡的最后速度加倍
双温变暗
私有子类型Area_KeyPressed(发送方作为对象,e作为KeyPressEventArgs)处理类型Area.KeyPress
swatch.Start()
如果斯沃琪正在运行,那么
温度+=样本时间间隔毫秒
如果结束
如果e.KeyChar=ChrW(Keys.Return),则
e、 KeyChar=无
swatch.Reset()
MsgBox(最终速度/键入区域.文本.长度)
typingArea.Text=无
logArea.Text=无
如果结束
如果键入区域,则不发送文本
swatch.Start()
如果结束
如果swatch.IsRunning=False,则swatch.Start()
如果键入rea.Text.Length>0,则
AppendText(“两个字符之间的时间为”+temp.ToString()+Environment.NewLine)
最终速度+=温度
温度=0
swatch.Restart()
"完"
其他的
swatch.Reset()
如果结束
端接头
公共函数返回平均速度(ByVal长度为整数,ByVal总计为双精度)为双精度
返回总长度/长度
端函数
末级

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

老实说,代码有点难读。我不知道为什么两个按键之间的第一个间隙不起作用。但经过一点改写,它似乎奏效了

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

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