Results 1 to 8 of 8

Thread: How to detect if keyboard idle and how long?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    258

    How to detect if keyboard idle and how long?

    I've got an app that allows user to type in text and does an auto-search based on what the user typed. But if we do it after every key stroke, the early keystrokes lag because the search returns a lot.

    So. Is there a way to determine how long ago the user last pressed a key?

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,398

    Re: How to detect if keyboard idle and how long?

    Well if you take two(2) datetimes and add/subtract them, you will get a timespan. I think that's what you may be looking for, something like this:
    Code:
    Private dt As DateTime
        Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
            dt = DateTime.Now
        End Sub
    
        Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
            Dim ts As TimeSpan = DateTime.Now - dt
            MessageBox.Show(ts.ToString, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Sub
    Mind you this would get annoying because for instance, me typing out quick reply, I've hit a key prob 100 times now, that would give me 100 + messagebox's. But it will getcha on the right path.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    258

    Re: How to detect if keyboard idle and how long?

    I know how to jerry-rig a solution, I was just wondering if one was built-in.

    I COULD record the datetime of the last keystroke and the last search, then fire the search if the current datetime (from a timer tick event) is more than a second after the last keystroke AND no intervening search has fired.

    I could do that, but I'm hoping for a simpler way.

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,398

    Re: How to detect if keyboard idle and how long?

    I'm hoping for a simpler way
    3 lines of code isn't simple enough? Any ways... As far as I know there is no built in function. But if you do use the route I suggested, don't use a timer, just compare the timespan. If the timespan's second is > 1(or w/e) then...
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    258

    Re: How to detect if keyboard idle and how long?

    Have to use a timer to kick off the search... since the search is fired based upon INactivity, I have to have a timer fire to check for that inactivity.

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,398

    Re: How to detect if keyboard idle and how long?

    You could use a timer, but it's not the only way. You could compare the timespan as shown above, or compare a variable based on the timer's tick. This is how it would look like if you used a timer:
    Code:
    Private i As Integer = 0
        Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
            Timer1.Stop()
            MessageBox.Show("It has been " & i.ToString & " seconds since the a key was pressed.")
            If i >= 0 Then
                i = 0
                Timer1.Start()
            End If
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            i += 1
    
        End Sub
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  7. #7
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: How to detect if keyboard idle and how long?

    Here's an msdn article that talks about improving LINQ queries: http://msdn.microsoft.com/en-us/magazine/cc721610.aspx.

    Whether you use LINQ or not you can benefit from the section that talks about performing incremental searches.
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: How to detect if keyboard idle and how long?

    You might take a look at this link to give you other ideas about it. It's API calls, but might be more reliable:

    http://dataerror.blogspot.com/2005/0...idle-time.html
    My usual boring signature: Nothing

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