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?
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.
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.
Re: How to detect if keyboard idle and how long?
Quote:
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...
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.
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
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.
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