Results 1 to 12 of 12

Thread: About timer

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2008
    Posts
    34

    About timer

    I'm using VB 2008 and i'm a newby.

    Anyone out there know how to get the time between keystrokes?

    For example, i type 'happy' and i want to calculate the time gap between the key i stroked, h-a, a-p, p-p, p-y .

    is it we need to use timer?how?

    thanks!

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: About timer

    Welcome to the Forums.

    You can use the Stopwatch class to measure elapsed time in miliseconds.

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim st As Stopwatch = Stopwatch.StartNew()
            'Do something
            Threading.Thread.Sleep(500)
            MessageBox.Show(st.ElapsedMilliseconds.ToString)
        End Sub
    
    End Class
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2008
    Posts
    34

    Re: About timer

    thankyou so much!

  4. #4
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: About timer

    I have to ask.. If the stopwatch is on the same thread as your application, and you sleep the thread in your example, won't the value shown be pretty much 0?

  5. #5
    Hyperactive Member gnaver's Avatar
    Join Date
    Jul 2005
    Location
    Denmark/Sweden
    Posts
    289

    Re: About timer

    nope

  6. #6
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: About timer

    Hmm.. that is interesting.. But I suppose if the stopwatch simply stores its first value and then checks the system clock and calculates the difference to get the timeelapsed, it makes sense. heh.

  7. #7
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: About timer

    Quote Originally Posted by MaximilianMayrhofer
    Hmm.. that is interesting.. But I suppose if the stopwatch simply stores its first value and then checks the system clock and calculates the difference to get the timeelapsed, it makes sense. heh.
    Thats exactly what it does
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  8. #8
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: About timer

    I'm still a major newb haha.

  9. #9

    Thread Starter
    Member
    Join Date
    Jun 2008
    Posts
    34

    Re: About timer

    Urmm, the code given is for the stopwatch to run right?

    Anyone know how we can start/stop the stopwatch by typing a single key in somewhere like a textbox?

  10. #10
    Hyperactive Member gnaver's Avatar
    Join Date
    Jul 2005
    Location
    Denmark/Sweden
    Posts
    289

    Re: About timer

    maybe in the, keydown/keypress/keyup or textchange event of the textbox...

    you could declare a list of string and the stopwatch

    vb Code:
    1. Dim test As New Stopwatch
    2.     Dim times As New List(Of String)

    then in the textbox1_textchange event put this

    vb Code:
    1. If times.Count = 0 Then
    2.             test.Start()
    3.             times.Add(test.ElapsedMilliseconds & " (" & TextBox1.Text & ")")
    4.         Else
    5.             times.Add(test.ElapsedMilliseconds & " (" & TextBox1.Text.Substring(TextBox1.Text.Length - 2, 2) & ")")
    6.             test.Reset()
    7.             test.Start()
    8.         End If

    to view the result do something like this..

    vb Code:
    1. For Each item As String In times
    2.             result += item & Environment.NewLine
    3.         Next
    4.         MessageBox.Show(result)

    this is some fast and ugly code, but maybe you get the idea

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: About timer

    @rachelism, Read the documentation on the Stopwatch class.

    It also states that if you use StartNew it initializes it and resets all values whereas .Start will attempt to restart any already initialized instances and if none found it will start it.

    There are alot more features to read about and will help you take advantage of the calss as you need.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12

    Thread Starter
    Member
    Join Date
    Jun 2008
    Posts
    34

    Re: About timer

    thanks for the help..

    so i used key-down and key-up method to determine the time of a key being pressed. it was successful

    now, i wish to determine the time interval between two key press.

    i guess we have to use 'keypress'. i had read the keypress event but still not sure how to do it. anyone have idea how to do it?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width